lua-geneator

代码生成

定义关键字及符号映射

src=lua-lexer.js

src=lua-token.js

src=lua-parser.js

src=lua-gen-js.js

drawCode
function drawCode(code, dom, idx) {
  let str = `<textarea id="code-${idx}" class="code" rows=6 style="width:100%" readonly>${code}</textarea>`;
  dom.innerHTML = str;
}

window.drawCode = drawCode;
draw
function draw(code, ast, idx) {
  let e = document.getElementById(`draw-${idx}`);
  e.classList.add("box");
  let c = document.createElement("div");
  c.classList.add("code");
  e.appendChild(c);
  drawCode(code, c, idx);
}
window.draw = draw;
// 生成的js代码
//eval 执行结果
source code 👇👇👇
let idx = arguments[0];
let code = `
local name,age = "jack",18;
local jsCode = document.getElementById("js-eval");
jsCode.textContent = "hello world!!!, name is".." "..name.." age is "..age;
`;
let lexer = new Lexer(code);
let parser = new Parser(lexer);
let ast = parser.parse();
draw(code, ast, idx);
console.log(ast);
let jsCode = gen(ast);
document.getElementById("js-code").textContent = jsCode.split(";").join(";\n");
console.log(jsCode);

eval(jsCode);