summaryrefslogtreecommitdiff
path: root/tnslc
diff options
context:
space:
mode:
authorKyle Gunger <kgunger12@gmail.com>2023-09-18 22:57:27 -0400
committerKyle Gunger <kgunger12@gmail.com>2023-09-18 22:57:27 -0400
commit2570eded660b769b0f49edb673346b28c6ca7c9b (patch)
treec88c156cd8702597f4115da8f000095ff096608f /tnslc
parent8b9b469242cb628fe469edaa4cc55bf952de178b (diff)
stuff
Diffstat (limited to 'tnslc')
-rw-r--r--tnslc/compiler.tnsl77
-rw-r--r--tnslc/compiler_structs.tnsl47
-rw-r--r--tnslc/tnslc_wrapped.tnsl6
-rw-r--r--tnslc/utils.tnsl26
4 files changed, 115 insertions, 41 deletions
diff --git a/tnslc/compiler.tnsl b/tnslc/compiler.tnsl
index efa74a7..fd5cc15 100644
--- a/tnslc/compiler.tnsl
+++ b/tnslc/compiler.tnsl
@@ -2,15 +2,30 @@
# TEXT UTILS #
##############
+{}uint8 e_tc_nl = "\n\0"
+{}uint8 e_noquit = "[TNSLC] [UB] PRE-ALPHA VERSION OF COMPILER UNABLE TO EXIT! UNDEFINED BEHAVIOUR AHEAD\n\0"
+
+{}uint8 e_unknown_meta = "[TNSLC] [WARNING] Unknown meta character '%c'!\n\0"
/; parse_meta (~uint8 str, int idx) [uint8]
+ uint8 c = str{idx}
- /; if (str{idx} == 'n')
+ /; if (c == 'n')
return '\n'
- ;; else if (str{idx} == '0')
+ ;; else if (c == 'r')
+ return '\r'
+ ;; else if (c == '\\')
+ return '\\'
+ ;; else if (c == '"')
+ return '"'
+ ;; else if (c == '\'')
+ return '\''
+ ;; else if (c == '0')
return 0
;/
-
- return str{idx}
+
+ _print_num(~e_unknown_meta{0}, c)
+ _printf(~e_noquit{0})
+ return c
;/
/; unquote_char (~uint8 str) [uint8]
@@ -35,6 +50,7 @@
/; loop (int i = 1; i < l - 1) [i++]
/; if (str{i} == '\\')
out{ln} = parse_meta(str, i + 1)
+ i++
;; else
out{ln} = str{i}
;/
@@ -67,9 +83,8 @@
return out
;/
-{}uint8 w_method_guard = "_#"
-{}uint8 w_enum_guard = "__#"
-
+{}uint8 w_method_guard = "_#\0"
+{}uint8 w_enum_guard = "__#\0"
####################
# FIND/PARSE UTILS #
@@ -80,13 +95,14 @@
/; matching_delim (Vector v, int c) [int]
~Token cur
cur = v.get(c)
- uint8 op
+ uint8 op, first
+ first = cur`.data{0}
- /; if (cur`.data{0} == '(')
+ /; if (first == '(')
op = ')'
- ;; else if (cur`.data{0} == '[')
+ ;; else if (first == '[')
op = ']'
- ;; else if (cur`.data{0} == '{')
+ ;; else if (first == '{')
op = '}'
;; else
op = ';'
@@ -104,7 +120,7 @@
continue
;/
- uint8 first = cur`.data{0}
+ first = cur`.data{0}
# Increments
/; if (first == '(')
p++
@@ -135,7 +151,8 @@
;; else if (first == '}')
s--
;; else if (first == ';')
- /; if (cur`.data{1} == '/')
+ first = cur`.data{1}
+ /; if (first == '/')
f--
;/
;/
@@ -159,8 +176,6 @@
{}uint8 e_circular = "[TNSLC] [ERROR] Circular struct definition detected in structs:\n\0"
-{}uint8 e_tc_nl = "\n\0"
-{}uint8 e_noquit = "[TNSLC] [UB] PRE-ALPHA VERSION OF COMPILER UNABLE TO EXIT! UNDEFINED BEHAVIOUR AHEAD\n\0"
# Structure sizing for the first round
/; size_struct (~Type t, ~Module m)
/; if (t`.s !== 0)
@@ -224,33 +239,20 @@
;/
;/
-# Parses a struct (and skips that many tokens)
-/; create_struct (Vector v, ~int c) [Type]
-;/
-
-/; create_module (~uint8 name, bool e, bool m) [Module]
- Module out
- out.start()
- out.name
-;/
-{}uint8 r1_export = "export\0"
-{}uint8 r1_module = "module\0"
-{}uint8 r1_struct = "struct\0"
-{}uint8 r1_method = "method\0"
-/; round_one (Path in, ~Module root)
+/; round_one_file (Path in, ~Module root)
~uint8 pth = in.full_path()
Vector v = tokenize_file(pth)
_delete(pth)
- ~Token cur
+ # round_one_tokens(in, v, root, 0, v.num_el)
+
+ # Clean up tokens
+ ~Token t
/; loop (int i = 0; i < v.num_el) [i++]
- cur = v.get(i)
- /; if(cstr_eq(cur`.data, ~r1_struct{0}))
- ;/
+ t = v.get(i)
+ _delete(t`.data)
;/
-
- flush_structs(root)
;/
@@ -259,8 +261,9 @@
root.start()
root.exp = true
- round_one(in, ~root)
- CompData dat = round_two(in, ~root)
+# round_one_file(in, ~root)
+# flush_structs(root)
+# CompData dat = round_two(in, ~root)
~void fd = out.open_write()
dat.write_file(fd)
diff --git a/tnslc/compiler_structs.tnsl b/tnslc/compiler_structs.tnsl
index cb7f807..03ee9ce 100644
--- a/tnslc/compiler_structs.tnsl
+++ b/tnslc/compiler_structs.tnsl
@@ -13,6 +13,9 @@ struct CompData {
csec
}
+{}uint8 w_data_sec = "\n\nsection .data\n\n\0"
+{}uint8 w_text_sec = "\n\nsection .text\n\n\0"
+
/; method CompData
/; start
self.hsec.start(1)
@@ -36,11 +39,15 @@ struct CompData {
/; loop (int i = 0; i < self.hsec.num_el) [i++]
_write_byte(fd, self.hsec.get(i))
;/
+
+ write_to_file(fd, ~w_data_sec{0})
/; loop (int i = 0; i < self.dsec.num_el) [i++]
_write_byte(fd, self.dsec.get(i))
;/
+ write_to_file(fd, ~w_text_sec{0})
+
/; loop (int i = 0; i < self.csec.num_el) [i++]
_write_byte(fd, self.csec.get(i))
;/
@@ -198,7 +205,7 @@ struct Type {
/; copy [Type]
Type out
- out.name = self.name
+ out.name = cstr_make_copy(self.name)
out.mod = self.mod
out.s = self.s
@@ -217,9 +224,34 @@ struct Type {
return out
;/
+ /; push_ptr (uint8 ptype)
+ self.ptr_chain.push(~ptype)
+ ;/
+
+ /; pop_ptr [uint8]
+ int l = self.ptr_chain.num_el
+ uint8 out
+ out = self.ptr_chain.get(l)
+ self.ptr_chain.pop()
+ return out
+ ;/
+
+ /; push_member(Variable member)
+ Variable to_push = member.copy()
+ self.members.push(~to_push)
+ ;/
+
/; _del
self.ptr_chain._del()
+
+ ~Variable v
+ /; loop (int i = 0; i < self.members.num_el) [i++]
+ v = self.members.get(i)
+ v`._del()
+ ;/
self.members._del()
+
+ _delete(self.name)
;/
;/
@@ -274,6 +306,7 @@ struct Variable {
/; copy [Variable]
Variable out
+ out.name = cstr_make_copy(self.name)
out.data_type = self.data_type.copy()
out.location = self.location
out.loc_type = self.loc_type
@@ -339,7 +372,7 @@ struct Module {
typ, # Types
fnc, # Functions
def, # Variable definitions (lables)
- sub # Sub modules (pointers)
+ sub # Sub modules
}
/; method Module
@@ -352,6 +385,14 @@ struct Module {
self.exp = false
;/
+ /; push_struct(Type t)
+ self.typ.push(~t)
+ ;/
+
+ /; push_sub(Module s)
+ self.sub.push(~s)
+ ;/
+
/; _find_type(Vector a, int depth) [~Type]
~Type none = 0
~Module p = self.parent
@@ -430,6 +471,8 @@ struct Module {
m`._del()
;/
self.sub._del()
+
+ _delete(self.name)
;/
;/
diff --git a/tnslc/tnslc_wrapped.tnsl b/tnslc/tnslc_wrapped.tnsl
index 2d5df69..3e73778 100644
--- a/tnslc/tnslc_wrapped.tnsl
+++ b/tnslc/tnslc_wrapped.tnsl
@@ -2,9 +2,9 @@
:include "logging.tnsl"
:include "utils.tnsl"
:include "vector.tnsl"
-# :include "tokenizer.tnsl"
+:include "tokenizer.tnsl"
:include "compiler_structs.tnsl"
-# :include "compiler.tnsl"
+:include "compiler.tnsl"
{}uint8 w_usage = "Usage: tnslc [file to compile] [file to write]"
@@ -21,6 +21,8 @@
in.start(argv{1})
out.start(argv{2})
+ compile(in, out)
+
in._del()
out._del()
diff --git a/tnslc/utils.tnsl b/tnslc/utils.tnsl
index 91e8d2e..a1c5884 100644
--- a/tnslc/utils.tnsl
+++ b/tnslc/utils.tnsl
@@ -69,6 +69,14 @@
;/
;/
+/; cstr_make_copy(~uint8 to_copy) [~uint8]
+ int l = cstr_len(to_copy)
+ ~uint8 out = _alloc(l + 1)
+ out{l} = 0
+ cstr_copy(to_copy, out)
+ return out
+;/
+
/; contains_char ({}uint8 arr, uint8 c) [bool]
/; loop (int i = 0; i < len arr) [i++]
/; if (arr{i} == c)
@@ -94,3 +102,21 @@
_write_byte(file, string + i)
;/
;/
+
+/; cstr_add(~uint8 a, b) [~uint8]
+ int al = cstr_len(a)
+ int bl = cstr_len(b)
+ ~uint8 out = _alloc(al + bl + 1)
+
+ /; loop (int i = 0; i < al) [i++]
+ out{i} = a{i}
+ ;/
+
+ /; loop (int i = 0, i < bl) [i++]
+ out{al + i} = b{i}
+ ;/
+
+ out{al + bl} = 0
+ return out
+;/
+