npm i llvm.js
Note
git clone https://github.com/llvm-js/llvm-project.git
const llvm = require('llvm.js/llvm');
const exceptions = require('llvm.js/exceptions');
const expression = require('llvm.js/expression');
const codeGen = require('llvm.js/codegen');
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
language.repl((data) => {
console.log(data);
});
cd ./tests
node language.js
// JavaScript --> AsmX MachineCode
let text = "Hello MachineCode AsmX!";
const example = 'this constant section';
// optimized
console.log('call 1');
console.log('call 2');
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
- 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