Utility to write bytecode for TurtleScript parser, bytecode compiler,
startup code and standard library, as a Lua file.
Run it under node
with the CLI in bin/write-lua-bytecode.js
define(['./parse', './bcompile', './bytecode-table', './top-level', './str-escape', './tests', './stdlib', './extensions'], function(parse, bcompile, bytecode_table, top_level, str_escape, tests, stdlib) {
var fake_require =
"var __modules__ = {};\n"+
"define = function _define(name, deps, init_func) {\n"+
" var d = deps.map(function(m) { return __modules__[m]; });\n"+
" __modules__[name] = init_func.apply(this, d);\n"+
"};\n";
var make_compile_from_source = function(parse, bcompile, TOP_LEVEL) {
var compile_from_source = function (source, as_object) {
source = source || '{ return 1+2; }';
var tree = parse(source, TOP_LEVEL);
var bc = bcompile(tree, true);
var result = as_object ? bc : bc.encode();
return result;
};
compile_from_source.make_repl = function() {
var state = null;
return function(source) {
var rv = parse.repl(state, source, TOP_LEVEL);
state = rv.state;
return bcompile(rv.tree).encode();
};
};
return compile_from_source;
};
var cfs_source = make_compile_from_source.toSource ?
make_compile_from_source.toSource() :
make_compile_from_source.toString();
cfs_source = 'define("compile_from_source", ["parse","bcompile","top-level"], '+
cfs_source + ');';
var top_level_source = 'define("top-level", [], function() { return ' +
str_escape(top_level) + '; });';
var source = '{\n'+
stdlib.source()+'\n'+
fake_require +
tests.lookup("tokenize")+"\n"+
tests.lookup("parse")+"\n"+
tests.lookup("bytecode-table")+"\n"+
tests.lookup("bcompile")+"\n"+
top_level_source+"\n"+
cfs_source + '\n' +
"return __modules__['compile_from_source']; }\n";
var compile_from_source = make_compile_from_source(parse, bcompile, top_level);
var bc = compile_from_source(source, true);
var lua_esc = function(str) {