Skip to content

Commit

Permalink
added strings
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbeckon committed Nov 27, 2024
1 parent 75a3809 commit 82a7477
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 8 deletions.
Binary file modified __pycache__/interpreter.cpython-312.pyc
Binary file not shown.
Binary file modified __pycache__/lexer.cpython-312.pyc
Binary file not shown.
Binary file modified __pycache__/parser.cpython-312.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def interpret(self, ast):
self.interpret(node.body)

def evaluate(self, node):
if isinstance(node, StringNode):
return node.value
if isinstance(node, NumberNode):
return node.value
elif isinstance(node, BinOpNode):
Expand Down
1 change: 1 addition & 0 deletions lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

TOKEN_TYPES = [
("NUMBER", r"\d+"),
("STRING", r'"([^"\\]|\\.)*"|\'([^\'\\]|\\.)*\''),
("PLUS", r"\+"),
("MINUS", r"-"),
("TIMES", r"\*"),
Expand Down
5 changes: 2 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import parser
# Create a lexer with the source code
source_code = """
x : 1
x : "test"
for 5
x : x + 1;
say(x)
say(x);
"""

# Step 1: Tokenize the source code
Expand Down
11 changes: 8 additions & 3 deletions notes.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Syntax:
variable assignment statement is like this
x : 3
y: 5
z:2
y : "String"



Expressions
2 + 3: 5
Expand All @@ -28,5 +29,9 @@ Syntax:
#none of this will be interpreted
this will


Structures
for x (for loop with no var)
for i +:: x (for loop with var)
; (break / end loops)


16 changes: 14 additions & 2 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ def __init__(self, loopvar, iterable, body):
self.iterable = iterable
self.body = body


class NumberNode(ASTNode):
def __init__(self, value):
self.value = int(value)

class StringNode(ASTNode):
def __init__(self, value):
self.value = str(value)

class BinOpNode(ASTNode):
def __init__(self, left, op, right):
self.left = left
Expand Down Expand Up @@ -114,6 +117,12 @@ def parse_print(self):
def parse_parentheses(self):
left = None
while self.current_token[0] != "RIGHTPAREN":
if self.current_token[0] == "STRING":
if left == None:
left = StringNode(self.current_token[1])
self.advance()
else:
raise SystemError(f"Unexpected token in parentheses: {self.current_token[0]}")
if self.current_token[0] == "IDENTIFIER":
if left == None:
left = VariableNode(self.current_token[1])
Expand All @@ -136,14 +145,17 @@ def parse_parentheses(self):
print(f"parentheses expr: {left}")
return left

def parse_expression(self) -> BinOpNode | VariableNode | NumberNode | None:
def parse_expression(self) -> BinOpNode | VariableNode | NumberNode | StringNode | None:
left = None
if self.current_token[0] == "IDENTIFIER":
left = VariableNode(self.current_token[1])
self.advance()
elif self.current_token[0] == "NUMBER":
left = NumberNode(self.current_token[1])
self.advance()
elif self.current_token[0] == "STRING":
left = StringNode(self.current_token[1])
self.advance
else:
raise SyntaxError(f"Unexpected token in say function: {self.current_token[0]}")

Expand Down

0 comments on commit 82a7477

Please sign in to comment.