summaryrefslogtreecommitdiff
path: root/nandgame/assembler
diff options
context:
space:
mode:
Diffstat (limited to 'nandgame/assembler')
-rw-r--r--nandgame/assembler/lexer.py6
-rw-r--r--nandgame/assembler/parser.py21
2 files changed, 19 insertions, 8 deletions
diff --git a/nandgame/assembler/lexer.py b/nandgame/assembler/lexer.py
index eb035f2..aab2549 100644
--- a/nandgame/assembler/lexer.py
+++ b/nandgame/assembler/lexer.py
@@ -11,7 +11,8 @@ tokens = (
'NUMBER',
'HEXNUMBER',
'DOT',
- 'REG'
+ 'REG',
+ 'NL'
)
# Regular expression rules for simple tokens
@@ -46,9 +47,10 @@ def t_SYMBOL(t):
return t
# Define a rule so we can track line numbers
-def t_newline(t):
+def t_NL(t):
r'\n+'
t.lexer.lineno += len(t.value)
+ return t
# A string containing ignored characters (spaces and tabs)
t_ignore = ' \t'
diff --git a/nandgame/assembler/parser.py b/nandgame/assembler/parser.py
index b2cc3f9..373f4bd 100644
--- a/nandgame/assembler/parser.py
+++ b/nandgame/assembler/parser.py
@@ -10,7 +10,7 @@ from lexer import tokens
def p_error(p: LexToken):
if p:
- print(f"Unexpected {p.value} on line {p.lineno}")
+ print(f"Unexpected {repr(p.value)} on line {p.lineno}")
else:
print("Unexpected end of file.")
@@ -24,8 +24,8 @@ def p_empty(p):
pass
def p_inss(p):
- '''instruction_list : instruction_list instruction
- | instruction
+ '''instruction_list : instruction_list line
+ | line
'''
if len(p) == 2:
p[0] = [p[1]]
@@ -39,8 +39,8 @@ def p_inss(p):
pass
def p_inss2(p):
- '''instruction_list2 : instruction instruction_list2
- | instruction
+ '''instruction_list2 : line instruction_list2
+ | line
'''
if len(p) == 2:
p[0] = [p[1]]
@@ -53,13 +53,22 @@ def p_inss2(p):
print(f" {p[2]}")
pass
+def p_line(p):
+ '''line : instruction NL
+ | NL
+ '''
+ if len(p) == 2:
+ pass
+ else:
+ p[0] = p[1]
+
def p_instruction(p):
'''instruction : noarg
| onearg
| twoarg
| jumpdest
+ | invalid_arg
'''
- # | invalid_arg
print(f"INS: {p[1]}")
p[0] = p[1]
pass