summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tnslc/'165
-rw-r--r--tnslc/compile/scope.tnsl120
-rw-r--r--tnslc/utils/algo.tnsl2
3 files changed, 120 insertions, 167 deletions
diff --git a/tnslc/' b/tnslc/'
deleted file mode 100644
index 485e5d1..0000000
--- a/tnslc/'
+++ /dev/null
@@ -1,165 +0,0 @@
-
-struct Function {
- ~uint8 name,
- utils.Vector
- inputs,
- outputs,
- ~parse.Node _up,
-}
-
-/; method Function
- /; init (~parse.Node n)
- self.name = utils.strcpy(n`.data)
- self._up = n
- Var v
- self.inputs.init(len v)
- self.outputs.init(len v)
- ;/
-
- /; _resolve_dlist (~Module parent, ~parse.Node dl)
- ~parse.Node tn = NULL
- ~parse.Node n
- /; loop (int i = 0; i < dl`.sub.count) [i++]
- n = dl`.sub.get(i)
- /; if (n`._type == parse.NTYPE_TYPE)
- tn = n
- ;; else if (n`._type == parse.NTYPE_ID)
- /; if (tn == NULL)
- _printf("Identifier declared in parameter list before any type was found!\n\0")
- return
- ;/
- Var p
- p.init(tn, n)
- p._resolve_type(parent)
- self.inputs.push(~p)
- ;/
- ;/
- ;/
-
- /; _resolve_tlist (~Module parent, ~parse.Node tl)
- ~parse.Node n
- parse.Node dummy
- dummy.data = "### OUTPUT ###\0"
- /; loop (int i = 0; i < tl`.sub.count) [i++]
- n = tl`.sub.get(i)
- /; if (n`._type == parse.NTYPE_TYPE)
- Var r
- r.init(n, ~dummy)
- r._resolve_type(parent)
- self.outputs.push(~r)
- ;/
- ;/
- ;/
-
- /; _resolve_type (~Module parent)
- ~parse.Node _up = self._up
- /; if (_up`.sub.count < 1)
- return
- ;/
-
- ~parse.Node lst = _up`.sub.get(0)
- /; if (lst`._type == parse.NTYPE_DLIST)
- self._resolve_dlist(parent, lst)
- /; if (_up`.sub.count > 1)
- lst = _up`.sub.get(1)
- ;/
- ;/
-
- /; if (lst`._type == parse.NTYPE_TLIST)
- self._resolve_tlist(parent, lst)
- ;/
- ;/
-
- /; _fqn (~Module parent) [~uint8]
- utils.Vector out
-
- return out.as_cstr()
- ;/
-
- /; _build_scope(~Module parent, ~CompBuf cb) [Scope]
- ~uint8 fqn = self._fqn(parent)
- Scope out
- out.init(parent, cb, fqn)
- _delete(fqn)
-
- # TODO: Push all saved vars and deal with parameters
- # TODO: Add all params to the scope
-
- return out
- ;/
-
- /; _end_scope(~Scope scope, ~CompBuf cb)
- ;/
-
- /; _compile (~Module parent, ~CompBuf cb)
- # Sanity check
- ~parse.Node _up = self._up
- /; if (_up`.sub.count < 1)
- ~Scope s = self._build_scope(parent, cb)
- self._end_scope(s, cb)
- return
- ;/
-
- # Skip past parameters and outputs
- int i = 0
- ~parse.Node n = _up`.sub.get(i)
- /; if (n`._type == parse.NTYPE_DLIST)
- i++
- /; if (_up`.sub.count > 1)
- n = _up`.sub.get(1)
- ;/
- ;/
-
- /; if (n`._type == parse.NTYPE_TLIST)
- i++
- ;/
-
- # Create scope and start compiling statements from here.
- Scope fscope = self._build_scope(parent, cb)
- fscope._compile_statements(cb, _up, i)
- self._end_scope(~fscope, cb)
- ;/
-
- /; _print (int idt)
- _indent(idt)
- _printf("{ Function : \0")
- _printf(self.name)
- _printf("\n\0")
-
- _indent(idt)
- _printf(" inputs:\n\0")
- ~Var prtv
- /; loop (int i = 0; i < self.inputs.count) [i++]
- prtv = self.inputs.get(i)
- prtv`._print(idt + 1)
- ;/
-
- _indent(idt)
- _printf(" outputs:\n\0")
- /; loop (int i = 0; i < self.outputs.count) [i++]
- prtv = self.outputs.get(i)
- prtv`._print(idt + 1)
- ;/
-
- _indent(idt)
- _printf("}\n\0")
- ;/
-
- /; end
- _delete(self.name)
-
- ~Var v
- /; loop (int i = 0; i < self.inputs.count) [i++]
- v = self.inputs.get(i)
- v`.end()
- ;/
- self.inputs.end()
-
- /; loop (int i = 0; i < self.outputs.count) [i++]
- v = self.outputs.get(i)
- v`.end()
- ;/
- self.outputs.end()
- ;/
-;/
-
diff --git a/tnslc/compile/scope.tnsl b/tnslc/compile/scope.tnsl
index f4eefdd..9cfc2ea 100644
--- a/tnslc/compile/scope.tnsl
+++ b/tnslc/compile/scope.tnsl
@@ -6,14 +6,38 @@ struct Scope {
~Scope parent,
utils.Vector
- vars
+ vars,
+
+ int unique
}
+/; _recursive_mod_name(~Module mod, ~utils.Vector vec)
+ /; if (mod`.parent !== NULL)
+ _recursive_mod_name(mod`.parent, vec)
+ vec`.push_char('.')
+ ;/
+ vec`.push_cstr(mod`.name)
+;/
+
+/; _recursive_scope_name(~Scope s, ~utils.Vector vec)
+ /; if (s`.parent == NULL)
+ /; if (s`.mod !== NULL)
+ _recursive_mod_name(s`.mod, vec)
+ vec`.push_char('.')
+ ;/
+ ;; else
+ _recursive_scope_name(s`.parent, vec)
+ ;/
+ vec`.push_cstr(s`.name)
+;/
+
/; method Scope
/; init (~Module mod, ~CompBuf cb, ~uint8 name)
self.name = utils.strcpy(name)
self.mod = mod
self.cb = cb
+ self.parent = NULL
+ self.unique = 0
Var v
self.vars.init(len v)
@@ -31,5 +55,99 @@ struct Scope {
v`.end()
;/
;/
+
+ #
+ # Sub scope
+ #
+
+ /; mk_sub (~uint8 name) [Scope]
+ Scope out
+ out.init(self.mod, self.cb, name)
+ out.parent = ~self
+ return out
+ ;/
+
+ # Generate a garantueed unique name for the sub scope, using
+ # the provided name as a base
+ /; gen_sub(~uint8 name) [Scope]
+ utils.Vector true_name
+ true_name.init(1)
+
+ # Append a 'unique' number
+ ~uint8 u = utils.int_to_str(self.unique)
+ true_name.push_char('#')
+ true_name.push_cstr(u)
+ true_name.push_char('#')
+ true_name.push_cstr(name)
+ _delete(u)
+
+ Scope out = self.mk_sub(true_name.as_cstr())
+ true_name.end()
+
+ # Inc for subsequent names
+ self.unique++
+
+ return out
+ ;/
+
+ #
+ # Label generation
+ #
+
+
+ /; base_label [utils.Vector]
+ utils.Vector out
+ out.init(1)
+
+ _recursive_mod_name(~self, ~out)
+
+ return out
+ ;/
+
+ /; place_base_label
+ utils.Vector bl = self.base_label()
+ self.cb`.add_c(bl.as_cstr())
+ self.cb`.add_c(":\n\0")
+ bl.end()
+ ;/
+
+ /; start_label [~uint8]
+ utils.Vector v = self.base_label()
+ v.push_cstr("#start\0")
+ return v.as_cstr()
+ ;/
+
+ /; place_start_label
+ ~uint8 sl = self.start_label()
+ self.cb`.add_c(sl)
+ self.cb`.add_c(":\n\0")
+ _delete(sl)
+ ;/
+
+ /; rep_label [~uint8]
+ utils.Vector v = self.base_label()
+ v.push_cstr("#rep\0")
+ return v.as_cstr()
+ ;/
+
+ /; place_rep_label
+ ~uint8 rl = self.rep_label()
+ self.cb`.add_c(rl)
+ self.cb`.add_c(":\n\0")
+ _delete(rl)
+ ;/
+
+ /; end_label [~uint8]
+ utils.Vector v = self.base_label()
+ v.push_cstr("#end\0")
+ return v.as_cstr()
+ ;/
+
+ /; place_end_label
+ ~uint8 el = self.end_label()
+ self.cb`.add_c(el)
+ self.cb`.add_c(":\n\0")
+ _delete(el)
+ ;/
;/
diff --git a/tnslc/utils/algo.tnsl b/tnslc/utils/algo.tnsl
index 35ac35c..49c4636 100644
--- a/tnslc/utils/algo.tnsl
+++ b/tnslc/utils/algo.tnsl
@@ -150,7 +150,7 @@
/; loop (i > 0) [i = i / 16]
int n = i % 16
/; if (n > 9)
- out.push_char('a' + n - 10)
+ out.push_char('A' + n - 10)
;; else
out.push_char('0' + n)
;/