Skip to content

Commit a77a221

Browse files
committed
We can now also do cell assignment operations
1 parent d4ac2b7 commit a77a221

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

code_generator.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22

3+
from Token import Token
34
from dictionary import Dictionary
45

56
logger = logging.getLogger(__name__)
@@ -80,12 +81,15 @@ def generate_IfNode(self, node):
8081
return f'if {condition_code}:\n{body_code}'
8182

8283
def generate_AssignmentNode(self, node):
83-
code = f"{node.identifier.value} = "
84-
if node.value.type == Dictionary.IDENTIFIER:
85-
code += f"{node.value.value}"
84+
if not isinstance(node.identifier, Token):
85+
identifier_code = self.generate(node.identifier, is_root=False)
8686
else:
87-
code += f"{self.generate(node.value, is_root=False)}"
88-
return code
87+
identifier_code = node.identifier.value
88+
if not isinstance(node.value, Token):
89+
value_code = self.generate(node.value, is_root=False)
90+
else:
91+
value_code = node.value.value
92+
return f"{identifier_code} = {value_code}"
8993

9094
def generate_IdentifierNode(self, node):
9195
return node.value

custom_parser.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ def _split_into_lines(self, tokens):
2525

2626
def parse_line(self, line):
2727
"""Parses a single line of tokens into a node."""
28-
#print(f"Parsing line: {line}")
28+
print(f"Parsing line: {line}")
2929
if not line:
3030
return None # handle empty lines
3131
first_token = line[0]
3232
if first_token.type == Dictionary.KEYWORD and first_token.value == 'if':
3333
return self.parse_if_statement(line)
34-
elif first_token.type == Dictionary.IDENTIFIER:
34+
elif first_token.type == Dictionary.IDENTIFIER or first_token.type == Dictionary.CELL:
3535
return self.parse_assignment(line)
3636
else:
3737
return self.parse_expression(line)
@@ -134,7 +134,6 @@ def parse_if_statement(self, line):
134134

135135
def parse_assignment(self, line):
136136
"""Parse an assignment statement."""
137-
# Example parsing assuming format `variable = expression`
138137
# remove whitespace tokens
139138
tokens = [token for token in line if token.type != Dictionary.WHITE_SPACE]
140139
# find the assignment token
@@ -146,6 +145,9 @@ def parse_assignment(self, line):
146145
if i != 1:
147146
raise ValueError("Invalid assignment statement")
148147
variable = tokens[0]
148+
# if the variable is a cell reference, parse it
149+
if variable.type == Dictionary.CELL:
150+
variable = self.parse_cell_reference(variable)
149151
# everything after the assignment token is the expression
150152
variable_value = tokens[i+1:]
151153
# if we only have 1 token, and it's an identifier, we can return the variable

text_input.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
if cell a1 is not equal to 1
44
hello is yes
55

6-
variable is lol
6+
cell a1 is cell b3

0 commit comments

Comments
 (0)