Skip to content

Commit 69539ac

Browse files
committed
Added string parsing to parser and code generator
1 parent 5dcf01e commit 69539ac

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

code_generator.py

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def generate_footer(self):
5252
def generate_NumberNode(self, node):
5353
return str(node.value)
5454

55+
def generate_StringNode(self, node):
56+
return str(node.value)
57+
5558
def generate_ExpressionNode(self, node):
5659
left_code = self.generate(node.left, is_root=False)
5760
right_code = self.generate(node.right, is_root=False)

custom_parser.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from dictionary import Dictionary
3-
from parser_nodes import AssignmentNode, ExpressionNode, NumberNode, IfNode, CellReferenceNode
3+
from parser_nodes import AssignmentNode, ExpressionNode, NumberNode, StringNode, IfNode, CellReferenceNode
44

55
logger = logging.getLogger(__name__)
66

@@ -71,6 +71,8 @@ def parse_primary(self, tokens):
7171
token = tokens[0]
7272
if token.type == Dictionary.INTEGER:
7373
return NumberNode(token.value), 1
74+
elif token.type == Dictionary.STRING:
75+
return StringNode(token.value), 1
7476
elif token.type == Dictionary.CELL:
7577
return self.parse_cell_reference(token), 1
7678
elif token.type == Dictionary.LEFT_PARENTHESES:
@@ -146,11 +148,13 @@ def parse_assignment(self, line):
146148
raise ValueError("Invalid assignment statement")
147149
variable = tokens[0]
148150
# if the variable is a cell reference, parse it
151+
print(variable.type)
149152
if variable.type == Dictionary.CELL:
150153
variable = self.parse_cell_reference(variable)
151154
# everything after the assignment token is the expression
152155
variable_value = tokens[i+1:]
153156
# if we only have 1 token, and it's an identifier, we can return the variable
157+
print(variable_value[0].type)
154158
if len(variable_value) == 1 and variable_value[0].type == Dictionary.IDENTIFIER:
155159
return AssignmentNode(variable, variable_value[0])
156160
# otherwise, parse the expression
@@ -169,7 +173,7 @@ def parse_cell_reference(self, token):
169173
raise ValueError("Invalid cell reference")
170174
if not any(char in token.value for char in Dictionary.ALPHABETIC_CHARACTERS):
171175
raise ValueError("Invalid cell reference")
172-
if any(char not in Dictionary.NUMERIC_CHARACTERS + Dictionary.ALPHABETIC_CHARACTERS for char in token.value):
176+
if any(char not in Dictionary.NUMERIC_CHARACTERS | Dictionary.ALPHABETIC_CHARACTERS for char in token.value):
173177
raise ValueError("Invalid cell reference")
174178
# there can be no numeric characters before the alphabetic characters. SO go through the string until we find a numeric character, and then check if the rest of the string is numeric
175179
i = 0

parser_nodes.py

+11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ def __repr__(self):
3333
def __str__(self):
3434
return self.__repr__()
3535

36+
class StringNode(Node):
37+
"""Class for nodes that represent strings (str)."""
38+
def __init__(self, value):
39+
self.value = value
40+
41+
def __repr__(self):
42+
return f"StringNode({self.value})"
43+
44+
def __str__(self):
45+
return self.__repr__()
46+
3647
class IfNode(Node):
3748
"""Class for nodes that represent if statements."""
3849
def __init__(self, condition, body):

text_input.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ if 2 + 40 is equal to 42
22
x is "Skynet status: online"
33
y is 'Oh shit!'
44

5-
cell R2D2 is 1337
5+
cell a2 is 1337

0 commit comments

Comments
 (0)