language

Some fools attempt at an interpreted language
Log | Files | Refs

commit 646c1b1a5bb99d9c8fd25fbc24f85222a151d95d
parent 363896004c2e541fecb8d0cf60b1f5408720a9fe
Author: Paul Longtine <paullongtine@gmail.com>
Date:   Wed Apr 27 19:27:33 2016

Added frontend stuff for objects

Diffstat:
 src/lc/bytecode.py                              | 36 ++++++++++++++++++--
 src/lc/interpreter.py                           | 13 +++++++-
 src/lc/main.py                                  | 15 ++++++--
 src/lc/memonic.py                               | 10 +++---
 src/lc/parser.py                                | 47 ++++++++++++++++++++++++--
 src/lc/test_files/class.ti                      | 12 +++++++-
 src/vm/tests/cases/program_fibb/expected_output |  2 +-
 src/vm/tests/cases/program_fibb/fibb            | Bin 94 -> 0 bytes
 8 files changed, 123 insertions(+), 12 deletions(-)

diff --git a/src/lc/bytecode.py b/src/lc/bytecode.py @@ -38,6 +38,38 @@ class VariableGet(): self.label.action() ]) +class ClassDef(): + def __init__(self, label, args): + self.label = label + self.args = args + + def action(self): + tmp = self.args.action() if self.args != None else 0x0 + return([ + OP_DECLASS, + self.label.action(), + tmp + ]) + +class NewClass(): + def __init__(self, toset, label, args): + self.toset = toset + self.label = label + self.args = args + + def action(self): + return([ + self.args.action(), + OP_NEW, + self.label.action(), + OP_DEC, + self.toset.action(s=True), + self.toset.action(), + OP_STV, + self.toset.action(s=True), + self.toset.action() + ]) + class FunctionDef(): def __init__(self, label, args, typed): self.label = label @@ -88,7 +120,7 @@ class StringConstant(SerializeableType): OP_CTS, int_to_bytes(len(self.value) + 1), 0x00, - 0x09, + 0x0A, self.value ]) @@ -103,7 +135,7 @@ class IntegerConstant(SerializeableType): OP_CTS, int_to_bytes(len(self.value) + 1), 0x00, - 0x06, + 0x07, self.value ]) diff --git a/src/lc/interpreter.py b/src/lc/interpreter.py @@ -212,6 +212,7 @@ class Interpreter(): self.ln = 0 + self.scopestack = [] self.names = [[]] self.scope = 0 @@ -245,6 +246,18 @@ class Interpreter(): self.names[self.scope].append(t) + def push_scope(self): + self.scopestack.append([self.names, self.scope]) + self.names = [[]] + self.scope = 0 + return False + + def pop_scope(self): + t = self.scopestack.pop() + self.names = t[0] + self.scope = t[1] + return False + def inc_scope(self): self.names.append([]) self.scope += 1 diff --git a/src/lc/main.py b/src/lc/main.py @@ -15,6 +15,12 @@ def tobytearray(l, n, ba): return(ba) +def printb(l): + if type(l) is list: + for i in l: + printb(i) + else: + print(" "+hex(l), end="") if __name__ == "__main__": import sys @@ -27,10 +33,15 @@ if __name__ == "__main__": out = open(sys.argv[2], "wb") + print("\nTO BYTES\n") rv = [] - for l in itr.program: + for n, l in enumerate(itr.program): + print("{}: {} <= ".format(str(n).rjust(4),l[0].name.rjust(15), l[1]), end="") for e in l[2]: - rv.append(e.action()) + t = e.action() + printb(t) + rv.append(t) + print() program = bytearray() program = tobytearray(rv, 0, program) diff --git a/src/lc/memonic.py b/src/lc/memonic.py @@ -57,13 +57,13 @@ OP_ELSE = 0x73 OP_DONE = 0x7E OP_CALL = 0x7F -OP_PUSH = 0x80 -OP_DEL = 0x81 -OP_GET = 0x82 -OP_GETP = 0x83 -OP_CALLM = 0x84 +OP_GETN = 0x80 +OP_CALLM = 0x81 +OP_INDEXO = 0x82 +OP_MODO = 0x83 OP_RETURN = 0xF0 OP_NEW = 0xF1 +OP_ENDCLASS = 0xF2 OP_DECLASS = 0xFE OP_DEFUN = 0xFF diff --git a/src/lc/parser.py b/src/lc/parser.py @@ -26,7 +26,7 @@ class Parser(): "{", "}" ] - + self.known_tokens = [ "return", "print", @@ -34,9 +34,10 @@ class Parser(): "else", "for", "while", - "func" + "func", + "class" ] - + self.defined_types = [ "void", "_addr", @@ -264,6 +265,43 @@ class Parser(): ]) ) + self.statement_class = Statement( + "class", + expression=[ + AtomicSymbol("class"), + self.label_def, + self.paramlist_def, + AtomicSymbol(":") + ], + init=(lambda x: [ + x.new_name(1), + ClassDef(x.eval_label(1), + x.eval_param(2)), + x.add_directive(lambda x: [x.pop_scope(), + x.op(OP_ENDCLASS)]), + x.push_scope() + ]) + ) + + self.statement_new = Statement( + "new", + expression=[ + self.defined_types[6], + self.label_def, + AtomicSymbol("="), + AtomicSymbol("new"), + self.label_def, + self.paramlist_def, + AtomicSymbol(";") + ], + init=(lambda x: [ + x.new_name(1), + NewClass(x.eval_label(1), + x.eval_label(5), + x.eval_args(6)) + ]) + ) + self.statement_inst = Statement( "instantiation", expression=[ @@ -318,6 +356,8 @@ class Parser(): self.statement_while, self.statement_func, self.statement_proc, + self.statement_class, + self.statement_new, self.statement_inst, self.statement_assign, self.statement_expression @@ -344,6 +384,7 @@ class Parser(): rv.extend(l) else: rv.append([a,r,[]]) + print("{}: {}\t{}".format(str(num).rjust(4), a.name.rjust(15),r)) break return rv diff --git a/src/lc/test_files/class.ti b/src/lc/test_files/class.ti @@ -0,0 +1,12 @@ + +class Wow(int cool): +{ + func test(int wow) -> void: + { + print cool * wow; + } + + test(4); +} + +object wee = new Wow(3); diff --git a/src/vm/tests/cases/program_fibb/expected_output b/src/vm/tests/cases/program_fibb/expected_output @@ -14,3 +14,5 @@ 377 610 987 +1597 +2584 diff --git a/src/vm/tests/cases/program_fibb/fibb b/src/vm/tests/cases/program_fibb/fibb Binary files differ