diff options
Diffstat (limited to 'tnslc/compile/codegen.tnsl')
| -rw-r--r-- | tnslc/compile/codegen.tnsl | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/tnslc/compile/codegen.tnsl b/tnslc/compile/codegen.tnsl index 165fe12..ae67442 100644 --- a/tnslc/compile/codegen.tnsl +++ b/tnslc/compile/codegen.tnsl @@ -1,4 +1,81 @@ +/; _indent (int idt) + /; loop (int i = 0; i < idt) [i++] + _printf(" \0") + ;/ +;/ /; generate (~utils.File fin, fout) + # Parse files into AST + parse.Node ast = parse.generate_ast(fin) + ast.update_children() + + parse.print_ast(~ast) + + # Create output buffer + CompBuf buffer + buffer.init() + + # Transform into a module tree + Module mod + mod.init(~ast, ~buffer) + _gen_prims(~mod) + mod.update_children() + mod.collect_methods(~ast) + + # Compile code + mod.compile(~buffer) + + mod.print() + + # Write assembly to output file + fout.create() + buffer.write_to(fout) + fout.close() + + # Free all structs + mod.end() + buffer.end() + ast.end() +;/ + +/; _gen_prim(~Module m, int size, ~uint8 id) + ~Module mds = m`._create_methods(id) + + Var t + + Struct s + s.size = size + s.methods = NULL + s.members.init(len t) + s.name = utils.strcpy(id) + s._up = NULL + + m`.structs.push(~s) +;/ + +/; _gen_prims (~Module m) + + # One byte prims + _gen_prim(m, 1, "bool\0") + _gen_prim(m, 1, "uint8\0") + _gen_prim(m, 1, "int8\0") + + # Two byte prims + _gen_prim(m, 2, "uint16\0") + _gen_prim(m, 2, "int16\0") + + # Four byte prims + _gen_prim(m, 4, "uint32\0") + _gen_prim(m, 4, "int32\0") + _gen_prim(m, 4, "float32\0") + + # Eight byte prims + _gen_prim(m, 8, "uint64\0") + _gen_prim(m, 8, "int64\0") + _gen_prim(m, 8, "float64\0") + _gen_prim(m, 8, "uint\0") + _gen_prim(m, 8, "int\0") + _gen_prim(m, 8, "float\0") + _gen_prim(m, 8, "void\0") ;/ |