diff --git a/lang/compile.py b/lang/compile.py index f8142c8..d729d66 100755 --- a/lang/compile.py +++ b/lang/compile.py @@ -106,13 +106,16 @@ return '\n\tASTStatement(value=%s)' % (self.value) class ASTFunction(): - def __init__(self, name, args, statements): + def __init__(self, public, class_, name, args, statements): + self.public = public + self.class_ = class_ self.name = name self.args = args self.statements = statements def __repr__(self): - return 'ASTFunction(name="%s", args="%s", statements=%s)' % (self.name, self.args, self.statements) + return 'ASTFunction(public=%s, class="%s", name="%s", args="%s", statements=%s)' % \ + (self.public, self.class_, self.name, self.args, self.statements) ## Parser @@ -259,16 +262,25 @@ def parse_function(lines): line = lines[0] lines = lines[1:] - if line[0] != "Function": + public = True + if len(line) < 5 or line[1] != "Class" or line[3] != "Function": print("not a function? line: %s" % (' '.join(line))) return (None, []) - name = line[1] + if line[0] == "Public": + public = True + elif line[0] == "Private": + public = False + else: + print("not a valid privacy? line: %s" % (' '.join(line))) + return (None, []) + class_ = line[2] + name = line[4] statements = [] - if len(line) != 2: # we have args - if len(line) < 4 or line[2] != "Args": + if len(line) > 5: # we have args + args = line[6:] + if line[5] != "Args" or len(args) == 0: print("no function args? line: %s" % (' '.join(line))) return (None, []) - args = line[3:] else: args = [] while len(lines) != 0: @@ -281,7 +293,7 @@ return (None, []) else: statements.append(statement) - func = ASTFunction(name, args, statements) + func = ASTFunction(public, class_, name, args, statements) return (func, lines) class ASTMetadata(): @@ -419,13 +431,16 @@ return 'IRReturn()' class IRFunction(): - def __init__(self, name, args, statements): + def __init__(self, public, class_, name, args, statements): + self.public = public + self.class_ = class_ self.name = name self.args = args self.statements = statements def __repr__(self): - return 'IRFunction(name="%s", args=%s, statements=%s)' % (self.name, self.args, self.statements) + return 'ASTFunction(public=%s, class="%s", name="%s", args="%s", statements=%s)' % \ + (self.public, self.class_, self.name, self.args, self.statements) class IRDepthCheck(): def __init__(self, depth): @@ -582,6 +597,8 @@ return sub_ir def generate_ir_function(ast): + public = ast.public + class_ = ast.class_ name = ast.name ir = [] statement_id = 1 @@ -594,7 +611,7 @@ return None ir = ir + sub_ir statement_id += 1 - return IRFunction(name, ast.args, ir) + return IRFunction(public, class_, name, ast.args, ir) def generate_ir_metadata(ast): return IRMetadata(ast.id, ast.name, ast.uses) @@ -692,9 +709,11 @@ if registers is None: return None new_ir = replace_variables(ir, registers) + public = ir.public + class_ = ir.class_ name = ir.name args = ir.args - return IRFunction(name, args, new_ir) + return IRFunction(public, class_, name, args, new_ir) def registers_allocate(module_args, ir): new_ir = [] diff --git a/lang/modules/B.txt b/lang/modules/B.txt index 0b599b9..1ef4f15 100644 --- a/lang/modules/B.txt +++ b/lang/modules/B.txt @@ -2,6 +2,6 @@ Use C Use D -Function Test +Public Class B Function Test Return True EndFunction diff --git a/lang/modules/C.txt b/lang/modules/C.txt index 9b217e5..963ddf1 100644 --- a/lang/modules/C.txt +++ b/lang/modules/C.txt @@ -1,6 +1,6 @@ Module C Use D -Function Test +Public Class C Function Test Return True EndFunction diff --git a/lang/modules/D.txt b/lang/modules/D.txt index b4a2502..5ff4d08 100644 --- a/lang/modules/D.txt +++ b/lang/modules/D.txt @@ -1,5 +1,5 @@ Module D -Function Test +Public Class D Function Test Args A Return True EndFunction diff --git a/lang/modules/another.txt b/lang/modules/another.txt index e69c3e7..23b087d 100644 --- a/lang/modules/another.txt +++ b/lang/modules/another.txt @@ -1,7 +1,7 @@ Module Another Use B -Function GetHalfish Args Num +Public Class Another Function GetHalfish Args Num Set FinalNum To Num Subtract 2 Return FinalNum EndFunction diff --git a/lang/modules/main.txt b/lang/modules/main.txt index a07a3cf..4229361 100644 --- a/lang/modules/main.txt +++ b/lang/modules/main.txt @@ -1,18 +1,18 @@ Module Main Use Another -Function BoolTest +Public Class Main Function BoolTest Set A To True Set B To A Invert Return B EndFunction -Function NoneTest +Public Class Main Function NoneTest Set A To None Return A EndFunction -Function LoopThirds Args Num +Public Class Main Function LoopThirds Args Num If Num Equals 0 Then Return True Set TenThirds To 1 Divide 30 @@ -21,11 +21,11 @@ Jump Self LoopThirds NewNum EndFunction -Function RatioTest +Public Class Main Function RatioTest Jump Self LoopThirds 10 EndFunction -Function IfTest +Public Class Main Function IfTest If False Then Return False @@ -36,18 +36,18 @@ Else Return False EndFunction -Function CountDown Args NumFrom +Public Class Main Function CountDown Args NumFrom If NumFrom Equals 0 Then Return True Set Next To NumFrom Subtract 1 Jump Self CountDown Next EndFunction -Function LoopTest +Public Class Main Function LoopTest Jump Self CountDown 10000 EndFunction -Function MakeNumber +Public Class Main Function MakeNumber Set Halfish To Another GetHalfish 620 Set Double To Halfish Add Halfish Set Final To Double Subtract 2