From 3b5de0beed36c10798f96972ec9c9eeb142561c2 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Tue, 25 Jun 2024 00:19:17 -0400 Subject: Start work on ast gen --- tnslc/README.md | 4 +-- tnslc/compile/ast.tnsl | 86 ++++++++++++++++++++++++++++++++++++++++++++ tnslc/compile/generator.tnsl | 22 +++--------- tnslc/test.tnsl | 9 ++++- tnslc/tnslc.tnsl | 3 +- tnslc/utils/iterator.tnsl | 12 +++++++ 6 files changed, 113 insertions(+), 23 deletions(-) diff --git a/tnslc/README.md b/tnslc/README.md index 0eb3ddc..9a69f6b 100644 --- a/tnslc/README.md +++ b/tnslc/README.md @@ -4,12 +4,12 @@ The reference compiler for the TNSL programming language. The compiler is writt ## Usage: -Place the [bootstrap compiler](https://git.cshift.net/CircleShift/ctc) `ctc` in the folder and execute `run.sh` +Place the [bootstrap compiler](https://git.cshift.net/CircleShift/ctc) `ctc` in this folder and execute `build.sh` The compiler outputs x86 NASM compatible assembly. Examples: - `./ctc dummy.tnsl dummy.asm` - Run the bootstrap compiler on the dummy file, output to dummy.asm -- `./run.sh` - Build the compiler +- `./build.sh` - Build the compiler ## License diff --git a/tnslc/compile/ast.tnsl b/tnslc/compile/ast.tnsl index 139597f..ebe44cb 100644 --- a/tnslc/compile/ast.tnsl +++ b/tnslc/compile/ast.tnsl @@ -1,2 +1,88 @@ +int NT_MODULE = 0 +int NT_BLOCK = 1 +int NT_FUNC = 2 +int NT_PARAM = 3 +int NT_RESULT = 4 +int NT_DATA = 5 +int NT_TYPE = 6 +int NT_STRUCT = 7 + +struct Node { + int _type, + ~uint8 data, + utils.Vector sub +} + +/; method Node + /; init (int typ, ~uint8 dat) + self._type = typ + self.data = dat + Node sub + self.sub.init(len sub) + ;/ + + /; end + _delete(self.data) + + ~Node n + /; loop (int i = 0; i < self.sub.count) [i++] + n = self.sub.get(i) + n`.end() + ;/ + self.sub.end() + ;/ + +;/ + +/; build_struct (utils.Iterator it) +;/ + +/; build_module () +;/ + +/; build_block (utils.Iterator it) +;/ + +/; build_preproc (utils.Iterator it) +;/ + +/; build_vardef (utils.Iterator it) +;/ + +/; at_defn (utils.Iterator it) [bool] + return false +;/ + +~uint8 TOKEN_COUNT = "Token count: %d\n\0" + +/; build_file (~utils.File fin, ~Node mod) + utils.Vector tokens = tokenize(fin) + _print_num(TOKEN_COUNT, tokens.count) + + utils.Iterator tokit + tokit.init(~tokens) + + /; loop (tokit.at_end() == false) + ~Token t = tokit.get() + /; if (utils.strcmp(t`.data, "/;\0") == true) + _printf("Block detected!\n\0") + build_block(tokit) + ;; else if (utils.strcmp(t`.data, "struct\0") == true) + _printf("Struct detected!\n\0") + build_struct(tokit) + ;; else if (utils.strcmp(t`.data, ":\0") == true) + _printf("Preproc detected!\n\0") + build_preproc(tokit) + ;; else if (at_defn(tokit) == true) + _printf("Defn detected!\n\0") + build_vardef(tokit) + ;; else + # _printf("Error detected!\n\0") + # TODO: ERROR + ;/ + + tokit.next() + ;/ +;/ diff --git a/tnslc/compile/generator.tnsl b/tnslc/compile/generator.tnsl index eedc552..10c75f8 100644 --- a/tnslc/compile/generator.tnsl +++ b/tnslc/compile/generator.tnsl @@ -1,25 +1,11 @@ -~uint8 TOKEN_COUNT = "Token count: %d\n\0" /; generate (~utils.File fin, fout) - utils.Vector tokens = tokenize(fin) - - _print_num(TOKEN_COUNT, tokens.count) - - fout`.create() - - /; loop (int i = 0; i < tokens.count) [i++] - ~Token tok = tokens.get(i) - ~uint8 buf = tok`.sprint() - fout`.write_cstr(buf) - fout`.write('\n') - _delete(buf) - ;/ - - fout`.close() - - free_token_list(~tokens) + Node root + root.init(NT_MODULE, NULL) + build_file(fin, ~root) + root.end() ;/ diff --git a/tnslc/test.tnsl b/tnslc/test.tnsl index 7572f1f..28f0917 100644 --- a/tnslc/test.tnsl +++ b/tnslc/test.tnsl @@ -1,5 +1,12 @@ # should not be included + +/; whatev (~uint8 a) [uint8] + return a{0} +;/ + /; main [int] - + ~uint8 a = "asdf\0" + whatev(a) + whatev("asdf\0") return 0 ;/ diff --git a/tnslc/tnslc.tnsl b/tnslc/tnslc.tnsl index ce68133..bc3fbb9 100644 --- a/tnslc/tnslc.tnsl +++ b/tnslc/tnslc.tnsl @@ -4,7 +4,7 @@ ~uint8 DEFAULT_FOUT = "out.asm\0" ~uint8 USAGE = " -TNSLC v0.5.0 (C) 2024 CircleShift +TNSLC v0.6.0 (C) 2024 CircleShift Softworks usage: tnslc (file in) [file out] @@ -25,7 +25,6 @@ usage: return 1 ;/ - utils.File fin, fout fin.init(argv{1}) diff --git a/tnslc/utils/iterator.tnsl b/tnslc/utils/iterator.tnsl index b38d71f..c797017 100644 --- a/tnslc/utils/iterator.tnsl +++ b/tnslc/utils/iterator.tnsl @@ -19,10 +19,22 @@ struct Iterator { ;/ ;/ + /; at_end [bool] + return (self.pos + 1) !< (self.v`.count) + ;/ + /; prev /; if (self.pos > 0) self.pos-- ;/ ;/ + + /; get_pos [int] + return self.pos + ;/ + + /; set_pos (int p) + self.pos = p + ;/ ;/ -- cgit v1.2.3