Skip to content

Commit

Permalink
parser little done and fix more bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
RezSat committed May 11, 2024
1 parent ea85d84 commit 978dbf9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
12 changes: 5 additions & 7 deletions src/nodes/binary_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
class BinaryOperation(Node):
def __init__(self, left, op, right):
self.left = left
self.op = self.op
self.right = self.right
self.op = op
self.right = right

def __repr__(self):
return {
return str({
"BinaryOperation": self.op,
"Left": self.left,
"Right": self.right
}
})

def __str__(self):
return str(self.__repr__())

class Add(BinaryOperation):
def __init__(self, left, right):
super().__init__(left, "+", right)
super().__init__(left=left, op="+", right=right)

class Subtract(BinaryOperation):
def __init__(self, left, right):
Expand Down
3 changes: 2 additions & 1 deletion src/nodes/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ def to_latex(self):
def __float__(self):
return float(self.value)


def __repr__(self):
return str({"Number": self.value})
63 changes: 61 additions & 2 deletions src/parser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,67 @@
from nodes import *

"""
TODO:
basic arithmetic
parenthesis
one-line function definition
function calls
coefficiet-symbol
parenthesis means multiplication ( well sometimes )
statement separation
variable assignments
equation
inequality
data-strctures:
matrices
hash-maps/dicts
sets
more later
"""
class Parser:
def __init__(self, tokens: list):
self.tokens = tokens
self.tokens = iter(tokens)
self.current_token = next(self.tokens)

def advance(self):
try:
self.current_token = next(self.tokens)
except StopIteration:
self.current_token = None

def parse(self):
pass
node = self.parse_expression()
return node

def parse_expression(self):
node = self.parse_term()

while self.current_token != None and self.current_token.value in ("+", "-"):
operator_ = globals()[self.current_token.name]
self.advance()
right = self.parse_term()
node = operator_(left=node, right=right) # Add(left, right) or Subtract(left, right)
return node

def parse_term(self):
node = self.parse_factor()

while self.current_token != None and self.current_token.value in ("*", "/"):
operator_ = globals()[self.current_token.name]
self.advance()
right = self.parse_factor()
node = operator_(left=node, right=node)

return node

def parse_factor(self):
node = None

if self.current_token.name == "Number":
node = Number(self.current_token.value)
self.advance()
return node

0 comments on commit 978dbf9

Please sign in to comment.