summaryrefslogtreecommitdiff
path: root/nandgame/assembler/parser.py
blob: 0b34ebebda13d294c8980a994f335e494dc8de44 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3

import sys
from typing import Any, MutableSequence, Tuple

import ply.yacc as yacc
from ply.lex import LexToken

# Get the token map from the lexer.  This is required.
from .lexer import tokens

from . import parser_types as pt


P = MutableSequence[Any]


def p_program(p: P) -> None:
    """program : instruction_list
               | empty
    """
    p[0] = p[1]


def p_empty(p: P) -> None:
    """empty :"""
    pass


def p_instructions(p: P) -> None:
    """instruction_list : instruction_list line
                        | line
    """
    if len(p) == 2:
        p[0] = [p[1]]
    else:
        p[0] = p[1] + [p[2]]
    pass


# # try right-recursive?
# def p_instructions2(p: P) -> None:
#     """instruction_list2 : line instruction_list2
#                          | line
#     """
#     if len(p) == 2:
#         p[0] = [p[1]]
#     else:
#         p[0] = [p[1]] + p[2]

#     print(f"INSes2: {len(p)}")
#     print(f"    {p[1]}")
#     if len(p) > 2:
#         print(f"    {p[2]}")
#     pass


def p_line(p: P) -> None:
    """line : instruction NL
            | jumpdest NL
            | NL
    """
    # | instruction error NL
    # | jumpdest error NL
    if len(p) == 2:
        pass
    elif len(p) == 3:
        p[0] = p[1]
        print(f"Item: {p[0]}")
    # if error handling
    else:
        p[0] = p[1]
        assert(False)


def p_instruction(p: P) -> None:
    """instruction : valid_instruction
                   | one_arg_invalid
    """
    p[0] = p[1]


def p_valid_instruction(p: P) -> None:
    """valid_instruction : no_args
                         | two_arg
                         | three_arg
    """
    tp: Tuple[Any, Any, Any, Any, Any] = p[1]
    p[0] = pt.Instruction(p.lineno(1), *tp)


def p_jumpdest(p: P) -> None:
    """jumpdest : symbol COLON"""
    p[0] = pt.JumpTarget(lineno=p.lineno(1), label=p[1])


def p_no_arg(p: P) -> None:
    """no_args : opcode"""
    p[0] = (*p[1], None, None, None)
    pass


def p_onearg_invalid(p: P) -> None:
    """one_arg_invalid : opcode argument"""
    op: Tuple[str,str] = p[1]
    p[0] = pt.ErrorInstruction(p.lineno(1), op[1], "No opcode only supports one argument.")
    pass


def p_two_arg(p: P) -> None:
    """two_arg : opcode register COMMA argument"""
    p[0] = (*p[1], p[2], p[4], None)
    pass


def p_three_arg(p: P) -> None:
    """three_arg : opcode register COMMA argument COMMA argument"""
    p[0] = (*p[1], p[2], p[4], p[6])
    pass


# checks which combinations are allowed is done one level up
def p_argument(p: P) -> None:
    """argument : number
                | register
                | symbol
    """
    p[0] = p[1]


def p_symbol(p: P) -> None:
    """symbol : SYMBOL"""
    p[0] = pt.Symbol(p[1])


def p_register(p: P) -> None:
    """register : REG"""
    p[0] = pt.Register(p[1])


def p_opcode(p: P) -> None:
    """opcode : opcode_jmp
              | opcode_njmp
    """
    p[0] = p[1]


def p_opcode_jmp(p: P) -> None:
    """opcode_jmp : OP DOT JUMP
    """
    p[0] = (p[1], p[3])


def p_opcode_njmp(p: P) -> None:
    """opcode_njmp : OP
    """
    p[0] = (p[1], None)


def p_number(p: P) -> None:
    """number : NUMBER
              | HEXNUMBER
    """
    p[0] = pt.Immediate(p[1])


def p_error(p: LexToken) -> LexToken:
    if p:
        print(f"WARNING: Unexpected {repr(p.value)} on line {p.lineno}", file=sys.stderr)
    else:
        print("WARNING: Unexpected end of file.", file=sys.stderr)
        return

    while True:
        tok = parser.token()
        if not tok or tok.type == 'NL':
            break
    parser.errok()
    return tok


parser = yacc.yacc()