summaryrefslogtreecommitdiff
path: root/nandgame/assembler/experiments/parser3.py
blob: f62f2e7e38c97e76d7f5f73e5f24bfcaf375e073 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# parser.py
import ply.yacc as yacc
from lexer import tokens

# --- AST node types ---


class Label:
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return f"Label({self.name!r})"


class Instr:
    def __init__(self, op, args):
        self.op = op  # "mov", "add", "jmp"
        self.args = args  # list of operands

    def __repr__(self):
        return f"Instr({self.op!r}, {self.args!r})"


# --- Grammar ---


def p_program(p):
    """program : lines"""
    p[0] = p[1]


def p_lines_multi(p):
    """lines : lines line"""
    # p[1] is the list so far, p[2] is a single line (Label or Instr)
    p[0] = p[1]
    if p[2] is not None:
        p[0].append(p[2])


def p_lines_single(p):
    """lines : line"""
    # start list with the single line (unless it's None; but we never produce None here)
    p[0] = [p[1]] if p[1] is not None else []


def p_line(p):
    """line : label
    | instruction"""
    p[0] = p[1]


def p_label(p):
    """label : IDENT COLON"""
    p[0] = Label(p[1])


def p_instruction_mov(p):
    """instruction : MOV REGISTER COMMA NUMBER"""
    # mov A, #123
    p[0] = Instr("mov", [p[2], p[4]])


def p_instruction_add(p):
    """instruction : ADD REGISTER COMMA REGISTER"""
    # add B, A
    p[0] = Instr("add", [p[2], p[4]])


def p_instruction_jmp(p):
    """instruction : JMP IDENT"""
    # jmp label
    p[0] = Instr("jmp", [p[2]])


def p_error(p):
    if p is None:
        raise SyntaxError("Syntax error at EOF")
    else:
        raise SyntaxError(f"Syntax error at {p.value!r} (type {p.type})")

parser = yacc.yacc()