Skip to content

Commit

Permalink
Basics have been implemented!
Browse files Browse the repository at this point in the history
This commit marks the end of the start; all basic functionalities have been implemented. I haven't, however, spent much time making sure I was programming neatly and according to the guidelines set by the course I'm following.
Furthermore, there are quite a lot of partially right or misbehaving functions that require some polishing. Anyway, I'm very happy I managed to accomplish this in this relatively small amount of time. I wanted to rush this first part
since I don't really like this course and just want to be over with it. But now that I have accomplished this, I am starting to like it more!
  • Loading branch information
JVKran committed May 2, 2021
1 parent 430ec7a commit dda7faf
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
8 changes: 8 additions & 0 deletions interpret/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,11 @@ def visitListNode(node, context):
elements.append(visit(element_node, context))

return elements

def visitReturnNode(node, context):
if node.nodeToReturn:
value = visit(node.nodeToReturn, context)
return value
else:
return None

2 changes: 1 addition & 1 deletion lex/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def createWordList(line : str) -> List[str]:
return [""]
head, *tail = line
currentWordList = createWordList(tail)
if head in " \t\n\r":
if head in " \t\r":
currentWordList = [""] + currentWordList
else:
newWord = head + currentWordList[0]
Expand Down
8 changes: 6 additions & 2 deletions main.srpl
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
variable result is 6 plus 5 multiplied_by 2
variable otherResult is result multiplied_by 4
variable result is 6 plus 5 multiplied_by 2 ;
variable otherResult is result multiplied_by 4 ;
task add lhs , rhs consists_of ;
variable res is lhs plus rhs ;
flush res ;
variable sum is execute add ( result , otherResult )
8 changes: 6 additions & 2 deletions parse/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ def __init__(self, name : str, arguments : List[token.Token], codeSequence):
self.arguments = arguments
self.codeSequence = codeSequence

class CallNode:
class CallNode():
def __init__(self, node_to_call, arg_nodes):
self.node_to_call = node_to_call
self.arg_nodes = arg_nodes

class ReturnNode():
def __init__(self, nodeToReturn):
self.nodeToReturn = nodeToReturn

class ListNode():
def __init__(self, element_nodes):
self.element_nodes = element_nodes
Expand Down Expand Up @@ -50,4 +54,4 @@ def __init__(self, condition, codeSequence):
self.condition = condition
self.codeSequence = codeSequence

Node = Union[FunctionNode, CallNode, NumberNode, OperatorNode, VariableNode, IfNode, WhileNode, ListNode]
Node = Union[FunctionNode, CallNode, NumberNode, OperatorNode, VariableNode, IfNode, WhileNode, ListNode, ReturnNode]
18 changes: 14 additions & 4 deletions parse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ def statements(tokenList : List[token.Token], tokenIndex : int):
while type(tokenList[tokenIndex]) == token.NewlineToken:
tokenIndex = incrementTokenIndex(tokenIndex, len(tokenList))

tokenIndex, statement = expression(tokenList, tokenIndex)
statements.append(statement)
tokenIndex, stateMent = statement(tokenList, tokenIndex)
statements.append(stateMent)

more_statements = True

Expand All @@ -160,12 +160,22 @@ def statements(tokenList : List[token.Token], tokenIndex : int):

if not more_statements: break
lastTokenIndex = tokenIndex
tokenIndex, statement = expression(tokenList, tokenIndex)
tokenIndex, stateMent = statement(tokenList, tokenIndex)
more_statements = not (lastTokenIndex == tokenIndex)
statements.append(statement)
statements.append(stateMent)

return tokenIndex, ListNode(statements)

def statement(tokenList, tokenIndex):
if type(tokenList[tokenIndex]) == token.ReturnToken:
tokenIndex = incrementTokenIndex(tokenIndex, len(tokenList))
tokenIndex, expr = expression(tokenList, tokenIndex)
return tokenIndex, ReturnNode(expr)
else:
tokenIndex, expr = expression(tokenList, tokenIndex)
return tokenIndex, expr


def binaryOperator(tokenList : List[token.Token], f : Callable[[A, B], C], operations : List[token.Token], tokenIndex : int) -> Tuple[int, Node]:
tokenIndex, left = f(tokenList, tokenIndex)

Expand Down
2 changes: 2 additions & 0 deletions srpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

def file():
tokens = lexer.lex(None, sys.argv[1])
print(tokens)
ast = parser.parse(tokens)
context = Context("<main>")
result = visit(ast, context)
Expand All @@ -22,6 +23,7 @@ def shell():

tokens = lexer.lex([text], None)
ast = parser.parse(tokens)
print(ast)
result = visit(ast, context)

if len(result) > 1: print("\t" + str(result))
Expand Down

0 comments on commit dda7faf

Please sign in to comment.