diff --git a/documentation/plthy.pdf b/documentation/plthy.pdf index 2b51666..09e5149 100644 Binary files a/documentation/plthy.pdf and b/documentation/plthy.pdf differ diff --git a/documentation/plthy.tex b/documentation/plthy.tex index 1d59838..8228aff 100644 --- a/documentation/plthy.tex +++ b/documentation/plthy.tex @@ -18,12 +18,12 @@ \textit{statement} & $\rightarrow$ & \texttt{maybe} \textit{statement}\\ \hline \textit{statement} & $\rightarrow$ & \texttt{do} \textit{command}\\ \hline \textit{statement} & $\rightarrow$ & \texttt{[} \textit{statements} \texttt{]}\\ \hline - \textit{statement} & $\rightarrow$ & \textit{statement} \texttt{if} \textit{expression} \\ \hline - \textit{statement} & $\rightarrow$ & \textit{statement} \texttt{because} \textit{expression} \\ \hline - \textit{statement} & $\rightarrow$ & \texttt{until} \textit{expression} \textit{statement} \\ \hline \textit{statement} & $\rightarrow$ & \texttt{set} \textit{expression} \texttt{->} \textbf{id} \\ \hline \textit{statement} & $\rightarrow$ & \texttt{define} \textbf{function} \texttt{<} \textbf{int} \texttt{>} \texttt{as} \textit{statement} \\ \hline \textit{statement} & $\rightarrow$ & \texttt{return} \textit{expression} \\ \hline\hline + \textit{command} & $\rightarrow$ & \textit{statement} \texttt{if} \textit{expression} \\ \hline + \textit{command} & $\rightarrow$ & \textit{statement} \texttt{because} \textit{expression} \\ \hline + \textit{command} & $\rightarrow$ & \textit{statement} \texttt{until} \textit{expression} \\ \hline \textit{command} & $\rightarrow$ & \textbf{builtin} \texttt{<} \textit{expressions} \texttt{>} \\ \hline \textit{command} & $\rightarrow$ & \texttt{"} \textbf{function} \texttt{" <} \textit{expressions} \texttt{>} \\ \hline\hline \textit{expressions} & $\rightarrow$ & \\ \hline diff --git a/plthy_impl/ast_nodes.py b/plthy_impl/ast_nodes.py index 62abba8..c849bab 100644 --- a/plthy_impl/ast_nodes.py +++ b/plthy_impl/ast_nodes.py @@ -301,7 +301,7 @@ class Call(Statement): return vtable, ftable, result -class StatementIf(Statement): +class CommandIf(Command): def __init__(self, statement: Statement, condition): self.statement = statement self.condition = condition @@ -316,13 +316,13 @@ class StatementIf(Statement): else: return vtable, ftable, result -class StatementBecause(Statement): +class CommandBecause(Command): def __init__(self, statement: Statement, condition): self.statement = statement self.condition = condition def __repr__(self) -> str: - return f"if({self.statement}, {self.condition})" + return f"because({self.statement}, {self.condition})" def eval(self, vtable, ftable): vtable, ftable, result = self.condition.eval(vtable, ftable) @@ -332,7 +332,7 @@ class StatementBecause(Statement): print("C001: Because assertion incorrect") exit() -class StatementUntil(Statement): +class CommandUntil(Command): def __init__(self, statement: Statement, condition): self.statement = statement self.condition = condition diff --git a/plthy_impl/lexer.py b/plthy_impl/lexer.py index 16c17b9..25aded3 100644 --- a/plthy_impl/lexer.py +++ b/plthy_impl/lexer.py @@ -36,8 +36,8 @@ SYMBOL_TOKENS = [ ("SYMBOL_AND", r"\/\\"), ("SYMBOL_SET", r"\-\>"), ("SYMBOL_TILDE", r"\~"), -# ("SYMBOL_LPARENS", r"\("), -# ("SYMBOL_RPARENS", r"\)"), + ("SYMBOL_LPARENS", r"\("), + ("SYMBOL_RPARENS", r"\)"), ("SYMBOL_LBRACKET", r"\["), ("SYMBOL_RBRACKET", r"\]"), ("SYMBOL_LCURL", r"\{"), diff --git a/plthy_impl/parser.py b/plthy_impl/parser.py index a5f6cc2..34f49c2 100644 --- a/plthy_impl/parser.py +++ b/plthy_impl/parser.py @@ -8,9 +8,9 @@ class Parser(): self.pg = ParserGenerator( [i[0] for i in ALL_TOKENS], precedence=[ - ('left', ["KEYWORD_MAYBE", "KEYWORD_RETURN"]), - ('left', ["KEYWORD_IF", "KEYWORD_BECAUSE", "KEYWORD_UNTIL", "KEYWORD_DEFINE", "KEYWORD_AS"]), - ('left', ["KEYWORD_DO", "BUILTIN", "SYMBOL_SET"]), + ('left', ["KEYWORD_SET", "SYMBOL_SET", "KEYWORD_IF", "KEYWORD_MAYBE", "KEYWORD_RETURN"]), + ('left', [ "KEYWORD_BECAUSE", "KEYWORD_UNTIL", "KEYWORD_DEFINE", "KEYWORD_AS"]), + ('left', ["KEYWORD_DO", "BUILTIN"]), ('left', ["SYMBOL_EQUALS", "SYMBOL_LT","SYMBOL_GT"]), ('left', ["SYMBOL_PLUS", "SYMBOL_MINUS", "SYMBOL_OR", "SYMBOL_AND"]), ('left', ["SYMBOL_TIMES", "SYMBOL_DIVIDE", "SYMBOL_TILDE"]) @@ -33,7 +33,7 @@ class Parser(): return [tokens[0]] + tokens[2] ## statement ## - @self.pg.production('statement : KEYWORD_SET expression SYMBOL_SET ID', precedence="SYMBOL_SET") + @self.pg.production('statement : KEYWORD_SET expression SYMBOL_SET ID') def statement_set(tokens): return ast_nodes.StatementSet(tokens[1], tokens[3].value) @@ -45,18 +45,6 @@ class Parser(): def statement_maybe(tokens): return ast_nodes.Maybe(tokens[1]) - @self.pg.production('statement : statement KEYWORD_IF expression') - def statement_if(tokens): - return ast_nodes.StatementIf(tokens[0], tokens[2]) - - @self.pg.production('statement : statement KEYWORD_BECAUSE expression') - def statement_because(tokens): - return ast_nodes.StatementBecause(tokens[0], tokens[2]) - - @self.pg.production('statement : KEYWORD_UNTIL expression statement') - def statement_until(tokens): - return ast_nodes.StatementUntil(tokens[2],tokens[1]) - @self.pg.production('statement : KEYWORD_DEFINE ID SYMBOL_LT DATA_INT SYMBOL_GT KEYWORD_AS statement', precedence="KEYWORD_DEFINE") def statement_define(tokens): return ast_nodes.StatementDefine(tokens[1].value, int(tokens[3].value), tokens[6]) @@ -78,6 +66,18 @@ class Parser(): def command_call(tokens): return ast_nodes.Call(tokens[1].value,tokens[4]) + @self.pg.production('command : statement KEYWORD_IF expression') + def command_if(tokens): + return ast_nodes.CommandIf(tokens[0], tokens[2]) + + @self.pg.production('command : statement KEYWORD_BECAUSE expression') + def command_because(tokens): + return ast_nodes.CommandBecause(tokens[0], tokens[2]) + + @self.pg.production('command : statement KEYWORD_UNTIL expression') + def command_until(tokens): + return ast_nodes.CommandUntil(tokens[0],tokens[2]) + ## expressions ## @self.pg.production('expressions : ') def expressions_none(_): @@ -92,6 +92,10 @@ class Parser(): def exp_int(tokens): return ast_nodes.ExpInt(int(tokens[0].value)) + @self.pg.production('expression : SYMBOL_LPARENS expression SYMBOL_RPARENS') + def exp_paren(tokens): + return tokens[1] + @self.pg.production('expression : DATA_FLOAT') def exp_float(tokens): return ast_nodes.ExpInt(float(tokens[0].value)) diff --git a/tests/06_if.plthy b/tests/06_if.plthy index 98bcfae..3670b7e 100644 --- a/tests/06_if.plthy +++ b/tests/06_if.plthy @@ -1,5 +1,5 @@ hello| set 2 -> x| -do print<'a';> if variable x = 1| -do print<'b';> if variable x = 2| +do do print<'a';> if variable x = 1| +do do print<'b';> if variable x = 2| goodbye| \ No newline at end of file diff --git a/tests/08_precedence.expected b/tests/08_precedence.expected index 4e41fda..79c22c4 100644 --- a/tests/08_precedence.expected +++ b/tests/08_precedence.expected @@ -1,4 +1,4 @@ 7 -5 +5 5 5 6 True diff --git a/tests/08_precedence.plthy b/tests/08_precedence.plthy index 559255b..4bc5c86 100644 --- a/tests/08_precedence.plthy +++ b/tests/08_precedence.plthy @@ -1,9 +1,9 @@ hello| set 1 + 2 * 3 -> x| do print| -set 5 -> y if variable x = 7| -do print| -set set 5 -> z + 1 -> a| -do print| +set do set 5 -> y if variable x = 7 -> z| +do print| +set set 5 -> a + 1 -> b| +do print| do print2*2+5;>| goodbye| \ No newline at end of file diff --git a/tests/09_fib.plthy b/tests/09_fib.plthy index 3fd522a..1aa6c78 100644 --- a/tests/09_fib.plthy +++ b/tests/09_fib.plthy @@ -1,7 +1,7 @@ hello| define fib<1> as [ - return 1 if argument #1 = 1| - return 1 if argument #1 = 2| + do return 1 if argument #1 = 1| + do return 1 if argument #1 = 2| return do "fib" + do "fib"| ]| do print;>| diff --git a/tests/13_because.plthy b/tests/13_because.plthy index 1e105c4..1602b2f 100644 --- a/tests/13_because.plthy +++ b/tests/13_because.plthy @@ -1,5 +1,5 @@ hello| set 2 -> x| -set 3 -> x because variable x = 2| +do set 3 -> x because variable x = 2| do print| goodbye| \ No newline at end of file diff --git a/tests/15_C001.plthy b/tests/15_C001.plthy index f5cba89..451a024 100644 --- a/tests/15_C001.plthy +++ b/tests/15_C001.plthy @@ -1,5 +1,5 @@ hello| set 2 -> x| -set 3 -> x because variable x = 3| +do set 3 -> x because variable x = 3| do print| goodbye| \ No newline at end of file