summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Gunger <kgunger12@gmail.com>2024-10-14 14:32:38 -0400
committerKyle Gunger <kgunger12@gmail.com>2024-10-14 14:32:38 -0400
commitb42f6ee60d74f9dc6f82182445fa3d8ae8f90420 (patch)
treedfa1eaf3ff8022ed28b47bca5b0122c7e7e009a2
parent16d26fc9a879f9d5044119d991433d798ed8f3ec (diff)
[tnslc] compile structs
-rw-r--r--tnslc/compile/compile.tnsl1
-rw-r--r--tnslc/compile/generate.tnsl4
-rw-r--r--tnslc/compile/type.tnsl197
-rw-r--r--tnslc/test.tnsl3
-rw-r--r--tnslc/tnslc.tnsl9
5 files changed, 205 insertions, 9 deletions
diff --git a/tnslc/compile/compile.tnsl b/tnslc/compile/compile.tnsl
index cda1f62..3560f48 100644
--- a/tnslc/compile/compile.tnsl
+++ b/tnslc/compile/compile.tnsl
@@ -1,3 +1,4 @@
/; module compile
+ :import "type.tnsl"
:import "generate.tnsl"
;/
diff --git a/tnslc/compile/generate.tnsl b/tnslc/compile/generate.tnsl
index 4cee1e3..ef6a76c 100644
--- a/tnslc/compile/generate.tnsl
+++ b/tnslc/compile/generate.tnsl
@@ -1,2 +1,6 @@
/; generate (~utils.File fin, fout)
+ parse.Node n = parse.generate_ast(fin)
+ n.update_children()
+ parse.print_ast(~n)
+ n.end()
;/
diff --git a/tnslc/compile/type.tnsl b/tnslc/compile/type.tnsl
new file mode 100644
index 0000000..43c1e6b
--- /dev/null
+++ b/tnslc/compile/type.tnsl
@@ -0,0 +1,197 @@
+struct Variable {
+ ~uint8 name,
+ ~Type _type
+}
+
+/; method Variable
+ /; init (~uint8 name)
+ self.name = name
+ ;/
+
+ /; end
+ _delete(self.name)
+ ;/
+;/
+
+struct Type {
+ ~uint8 name,
+ uint size,
+ utils.Vector vars,
+ ~Module methods,
+}
+
+/; method Type
+ /; init(~uint8 name)
+ self.name = name
+ Variable tmp
+ self.vars.init(len tmp)
+ ;/
+
+ /; add_var (~Variable v)
+ self.vars.push(v)
+ ;/
+
+ /; end
+ _delete(self.name)
+ /; loop (int i = 0; i < self.vars.count) [i++]
+ ~Variable v = self.vars.get(i)
+ v`.end()
+ ;/
+ self.vars.end()
+ ;/
+;/
+
+struct Function {
+ ~uint8
+ name,
+ ~parse.Node
+ body,
+ utils.Vector
+ inputs,
+ outputs
+}
+
+/; method Function
+ /; init (~uint8 name)
+ self.name = name
+ Variable vtmp
+ self.inputs.init(len vtmp)
+ self.outputs.init(len vtmp)
+ ;/
+
+ /; add_input (~Variable v)
+ self.inputs.push(v)
+ ;/
+
+ /; add_output (~Variable v)
+ self.outputs.push(v)
+ ;/
+
+ /; end
+ _delete(self.name)
+ ;/
+;/
+
+struct Enum {
+ ~uint8 name,
+ ~Type _type,
+ utils.Vector vals
+}
+
+/; method Enum
+ /; init (~uint8 name)
+ self.name = name
+ Variable vtmp
+ self.vals.init(len vtmp)
+ ;/
+
+ /; end
+ _delete(self.name)
+ /; loop (int i = 0; i < self.vals.count) [i++]
+ ~Variable v = self.vals.get(i)
+ v`.end()
+ ;/
+ self.vals.end()
+ ;/
+;/
+
+struct Module {
+ ~uint8 name,
+ ~Module parent,
+ utils.Vector
+ sub,
+ vars,
+ types,
+ funcs,
+ enums
+
+}
+
+/; method Module
+ /; init (~uint8 name)
+ self.name = name
+ Module mtmp
+ Variable vtmp
+ Type ttmp
+ Function ftmp
+ Enum etmp
+ self.sub.init(len mtmp)
+ self.vars.init(len vtmp)
+ self.types.init(len ttmp)
+ self.funcs.init(len ftmp)
+ self.enums.init(len etmp)
+ ;/
+
+ /; update_children ()
+ /; loop (int i = 0; i < self.sub.count) [i++]
+ ~Module s = self.sub.get(i)
+ s`.parent = ~self
+ ;/
+ ;/
+
+ /; add_sub(~Module m)
+ self.sub.push(m)
+ /; loop (int i = 0; i < self.sub.count) [i++]
+ ~Module s = self.sub.get(i)
+ s`.update_children()
+ ;/
+ ;/
+
+ /; add_var (~Variable v)
+ self.vars.push(v)
+ ;/
+
+ /; add_type (~Type t)
+ self.types.push(t)
+ ;/
+
+ /; add_funcs (~Function f)
+ self.funcs.push(f)
+ ;/
+
+ /; add_enum (~Enum e)
+ self.enums.push(e)
+ ;/
+
+ /; end
+ _delete(self.name)
+
+ /; loop (int i = 0; i < self.sub.count) [i++]
+ ~Module m = self.sub.get(i)
+ m`.end()
+ ;/
+ self.sub.end()
+
+ /; loop (int i = 0; i < self.vars.count) [i++]
+ ~Variable v = self.vars.get(i)
+ v`.end()
+ ;/
+ self.vars.end()
+
+ /; loop (int i = 0; i < self.types.count) [i++]
+ ~Type t = self.types.get(i)
+ t`.end()
+ ;/
+ self.types.end()
+
+ /; loop (int i = 0; i < self.funcs.count) [i++]
+ ~Function f = self.funcs.get(i)
+ f`.end()
+ ;/
+ self.funcs.end()
+
+ /; loop (int i = 0; i < self.enums.count) [i++]
+ ~Enum e = self.enums.get(i)
+ e`.end()
+ ;/
+ self.enums.end()
+ ;/
+;/
+
+{}~uint8 GEN_VAR_NAMES = { "int\0", "int8\0", "int16\0", "int32\0", "int64\0", "uint\0", "uint8\0", "uint16\0", "uint32\0", "uint64\0", "float\0", "float32\0", "float64\0", "vect\0", "bool\0", "void\0" }
+
+{}uint GEN_VAR_SIZES = { 8, 1, 2, 4, 8, 8, 1, 2, 4, 8, 8, 4, 8, 0, 1, 8}
+
+
+/; restructure (~parse.Node) [Module]
+;/
diff --git a/tnslc/test.tnsl b/tnslc/test.tnsl
index 498db26..6b55408 100644
--- a/tnslc/test.tnsl
+++ b/tnslc/test.tnsl
@@ -6,5 +6,6 @@ int i = 1 + 2 * 4 - 3 + 4
/; main ({}{}uint8 args) [int]
~uint8 as = 12
- as = 3 + 4
+ as` = 3 + 4
;/
+
diff --git a/tnslc/tnslc.tnsl b/tnslc/tnslc.tnsl
index 2cb9603..e95a63b 100644
--- a/tnslc/tnslc.tnsl
+++ b/tnslc/tnslc.tnsl
@@ -35,14 +35,7 @@ usage:
fout.init(DEFAULT_FOUT)
;/
- parse.Node n = parse.generate_ast(~fin)
- n.update_children()
- parse.print_ast(~n)
- n.end()
-
- # utils.Vector v = parse.gen_token_list(~fin)
- # parse.print_token_list(~v)
- # parse.end_token_list(~v)
+ compile.generate(~fin, ~fout)
fin.end()
fout.end()