Skip to content

🚀 A powerful tool for creating programming languages, interpreters/compilers.

Notifications You must be signed in to change notification settings

llvm-js/llvm-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

author
E5war5IT
Apr 11, 2024
c70da7b · Apr 11, 2024

History

20 Commits
Nov 13, 2023
Apr 10, 2024
Apr 11, 2024
Apr 11, 2024
Nov 13, 2023
Nov 13, 2023

Repository files navigation

LLVM.JS (llvm.js)

Install

npm

npm i llvm.js

Note

URL npm

git

git clone https://github.com/llvm-js/llvm-project.git

Require

const llvm = require('llvm.js/llvm');
const exceptions = require('llvm.js/exceptions');
const expression = require('llvm.js/expression');
const codeGen = require('llvm.js/codegen');

Quick start

const llvm = require('llvm.js/llvm');
const language = new llvm.llvm();

// config (Optional)

let file_c = fs.readFileSync(src).toString('utf8').split('\n');
const lexer = new llvm.Lexer();
let ast = lexer.lexer(file_c);
// console.log(ast); // example

language.run(); // view the characteristics of the programming language and the project  

REPL

language.repl((data) => {
    console.log(data);
});

Example JavaScript compile

Windows

cd ./tests
node language.js

Input file:

// JavaScript --> AsmX MachineCode
let text = "Hello MachineCode AsmX!";
const example = 'this constant section';

// optimized
console.log('call 1');
console.log('call 2');

Output file:

segment variable:
    text: "Hello MachineCode AsmX!"

segment const:
    example: 'this constant section'

print:
   push $1
   call 0x04

main code:
    mov $1 'call 1'
    func print
    mov $1 'call 2'
    func print

An example of using llvm.js in RunKit + npm

JavaScript code:

const fs = require('fs');
const Expression = require('llvm.js/expression');
const codeGen = require('llvm.js/codegen');

let expression = new Expression('2 + 4');
console.log(expression.answer());

codeGen.genSLFunction('print');
codeGen.callFunction('print', '1');
codeGen.codegen('output');

fs.readFileSync('output.bytex').toString('utf8');

Output:

6

print:
   push $1
   call 0x04

main code:
    mov $1 1
    func print

Documentation

  • Config
    • set(object: IConfig)
    • setCommentLine(string: string)
    • forbiddenSymbol(char: string)
    • setCommentBlock(string: string)
    • setCommentBlock(string: string[])
    • clearCommentLine( )
    • clearCommentBlock( )
    • setSupportNumberSnake(bool: boolean)
    • clearSupportNumberSnake( )
  • Grammar
    • verifyGrammar(current: number, ast: Array, grammar: Array, strict: boolean): object
    • verifyGrammarNoStrict(current: number, ast: Array, grammar: Array): object

Releases

No releases published

Packages

No packages published