language

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 4de17896fe14e4f8303dec7ccb33a42258f58479
parent 8749c60f93b58f010decbd126686d4b09f9f93d2
Author: Paul Longtine <paul@nanner.co>
Date:   Sun, 24 Sep 2017 23:03:29 -0400

Compiler now fails when statement is not matched, along with a tiny error where tabs were not a seperator character!

Diffstat:
Msrc/lc/interpreter.py | 29+++++++++++++++--------------
Msrc/lc/main.py | 31++++++++++++++++++++-----------
Msrc/lc/parser.py | 9+++++++++
Msrc/lc/test_files/class.ti | 15+++------------
4 files changed, 47 insertions(+), 37 deletions(-)

diff --git a/src/lc/interpreter.py b/src/lc/interpreter.py @@ -39,18 +39,14 @@ class Label(AbstractToken): self.name = names[1] - print("RESOLVING {}".format(self.name)) t = self.i.ns.resolve_with_obj(self.parent, self.name) - print("RESOLVED {}: {}".format(self.name, t)) self.expr = t[0] else: self.name = names[0] - print("RESOLVING {}".format(self.name)) t = self.i.ns.resolve(self.name) - print("RESOLVED {}: {}".format(self.name, t)) self.scope = t[0] self.expr = t[1] @@ -240,22 +236,27 @@ class Interpreter(): self.p = Parser(filename) self.ns = Namespace() + self.success = False self.program = self.p.get_statements() - self.line = (None, None) + if self.program == False: + print("Terminating parsing...") + else: + self.line = (None, None) - self.ln = 0 + self.ln = 0 - self.cur_directives = [] + self.cur_directives = [] + + self.directives = [self.cur_directives] - self.directives = [self.cur_directives] + for self.ln, self.line in enumerate(self.program): + t = self.line[0].action(self) + for i in t: + if i != False: + self.line[2].append(i) - #initalizes values n' stuff - for self.ln, self.line in enumerate(self.program): - t = self.line[0].action(self) - for i in t: - if i != False: - self.line[2].append(i) + self.success = True def nxt(self, n): if len(self.program) <= self.ln + n: diff --git a/src/lc/main.py b/src/lc/main.py @@ -22,28 +22,24 @@ def printb(l): else: print(" "+hex(l), end="") -if __name__ == "__main__": - import sys - - if len(sys.argv) != 3: - print("useage: {} input_file output_file".format(sys.argv[0])) - sys.exit(1) - - itr = Interpreter(sys.argv[1]) - - out = open(sys.argv[2], "wb") +def write_out(output_file): + out = open(output_file, "wb") print("\nTO BYTES\n") + rv = [] + for n, l in enumerate(itr.program): print("{}: {} <= ".format(str(n).rjust(4), l[0].name.rjust(15), l[1]), - end="") + end="") + for e in l[2]: t = e.action() printb(t) rv.append(t) + print() program = bytearray() @@ -51,3 +47,16 @@ if __name__ == "__main__": out.write(program) out.close() + +if __name__ == "__main__": + import sys + + if len(sys.argv) != 3: + print("useage: {} input_file output_file".format(sys.argv[0])) + sys.exit(1) + + itr = Interpreter(sys.argv[1]) + + if itr.success: + write_out(sys.argv[2]) + diff --git a/src/lc/parser.py b/src/lc/parser.py @@ -18,6 +18,7 @@ class Parser(): "\-", "\*", "\/", + "\t", " " ] self.end_statements = [ @@ -415,10 +416,12 @@ class Parser(): # # NOTE: The order of active_tokens is of most-probable to match # to least-probable to match + fail = True for a in self.active_tokens: r = a.match(l) # If the line matches the token, if r: + fail = False # If the token is an "incude" token, include the file # specified by the "include" directive if a.name == "include": @@ -435,5 +438,11 @@ class Parser(): a.name.rjust(15), r)) break + if fail: + print("Error, Line #{0}".format(num)) + print("{}".format(l)) + rv = False; + break; + return rv diff --git a/src/lc/test_files/class.ti b/src/lc/test_files/class.ti @@ -19,22 +19,13 @@ class ObjectFibb: { int a = 0; int b = 1; - int c = 0; Counter acc = new Counter(); func next -> void: { - if c == 0: - { - b = a + b; - c = 1; - } else - if c == 1: - { - a = a + b; - c = 0; - } + b = a + b; + a = a + b; } func display -> void: @@ -60,5 +51,5 @@ test.run(15); print "ALL DONE!"; print test.a; print test.b; -print test.c; +print test.acc.data;