1 /++ 2 Provides an "interface" to all the compiler modules. 3 +/ 4 module qscript.compiler.compiler; 5 6 import qscript.compiler.misc; 7 import qscript.compiler.tokengen; 8 import qscript.compiler.ast; 9 import qscript.compiler.astgen; 10 import qscript.compiler.astcheck; 11 import qscript.compiler.codegen; 12 13 import std.json; 14 import qscript.compiler.astreadable; 15 16 import utils.misc; 17 18 import navm.navm : NaFunction; 19 20 /// compiles a script from string[] to bytecode (in NaFunction[]). 21 /// 22 /// `script` is the script to compile 23 /// `functions` is an array containing data about a function in Function[] 24 /// `errors` is the array to which errors will be appended 25 /// 26 /// Returns: ByteCode class with the compiled byte code. or null if errors.length > 0 27 public string[] compileScript(string[] script, Function[] functions, ref CompileError[] errors, 28 ref Function[] functionMap){ 29 TokenList tokens = toTokens(script, errors); 30 if (errors.length > 0) 31 return null; 32 ASTGen astMake; 33 ScriptNode scrNode = astMake.generateScriptAST(tokens, errors); 34 if (errors.length > 0) 35 return null; 36 ASTCheck check = new ASTCheck(functions); 37 errors = check.checkAST(scrNode); 38 if (errors.length > 0) 39 return null; 40 .destroy(check); 41 // time for the final thing, to byte code 42 CodeGen bCodeGen = new CodeGen(); 43 bCodeGen.generateByteCode(scrNode); 44 string[] byteCode = bCodeGen.getByteCode(); 45 functionMap = bCodeGen.getFunctionMap; 46 return byteCode; 47 } 48 49 /// Generates an AST for a script, uses ASTCheck.checkAST on it. 50 /// 51 /// Returns: the final AST in readable JSON. 52 public string compileScriptAST(string[] script, Function[] functions, ref CompileError[] errors){ 53 TokenList tokens = toTokens(script, errors); 54 if (errors.length == 0){ 55 ASTGen astMake; 56 ScriptNode scrNode = astMake.generateScriptAST(tokens, errors); 57 if (errors.length == 0){ 58 ASTCheck check = new ASTCheck(functions); 59 errors = check.checkAST(scrNode); 60 .destroy(check); 61 JSONValue jsonAST = scrNode.toJSON; 62 return jsonAST.toPrettyString(); 63 } 64 } 65 return ""; 66 }