-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.pl
209 lines (177 loc) · 5.24 KB
/
parsing.pl
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
% parsing.pl
:- module(parsing, [optional_whitespace//0,
required_whitespace//0,
label//1,
instruction//1]).
:- use_module(library(dcg/basics)).
:- use_module(opcodes).
/*
Current idea:
- Read code in actual assembler syntax (as a list of strings)
- For each line:
- remove comments
- convert to lowercase
- strip leading/trailing whitespace
- single-space the resulting string
- if empty, remove from list
- parse the resulting string, i.e. convert it to the appropriate value in
"Prolog assembler", like rts/implied, or label(here), etc.
- these values can then be passed to the assembler, etc
While the DCGs can in theory be made bidirectional, generating output with them
seems to be exceedingly slow, so I am using them for parsing only.
*/
% Check if Value is between Low and High (inclusive). Succeeds if this is so,
% fails with an error otherwise.
check_value(Value, Low, High) :-
Value >= Low,
Value =< High, !.
check_value(Value, Low, High) :-
must_be(between(Low, High), Value).
is_word(Value) :-
check_value(Value, 0, 0xFFFF).
is_byte(Value) :-
check_value(Value, 0, 255).
% Source: https://www.metalevel.at/prolog/dcg
% SWIPL doesn't seem to have this built in. No matter:
list([]) --> [].
list([L|Ls]) --> [L], list(Ls).
% Match 0 or 1 whitespace characters.
optional_whitespace --> required_whitespace, !.
optional_whitespace --> [].
% Match exactly one whitespace character.
required_whitespace --> [W], { code_type(W, space) }.
% Match any of the values in Codes.
any_of(Codes, Result) -->
[Result],
{ member(Result, Codes), ! }.
% Parse hex numbers, either of two or four bytes long. Other lengths are
% (currently) not allowed; neither are decimal numbers. Opcode DCG rules
% specify which one they want, so there's no ambiguity between e.g. `ADC $02`
% (zeropage) and `ADC $0002` (absolute).
asm_byte(N) -->
`$`,
xdigit(B1),
xdigit(B2),
{ N is B1 * 16 + B2 }.
asm_word(N) -->
`$`,
xdigit(B1),
xdigit(B2),
xdigit(B3),
xdigit(B4),
{ N is B4 + B3 << 4 + B2 << 8 + B1 << 12 }.
asm_opcode(Opcode/Mode) -->
nonblanks(Chars),
{ Chars = [_,_,_], % opcodes are always exactly 3 characters
% convert to lowercase
atom_codes(OpcodeA, Chars),
string_lower(OpcodeA, OpcodeS),
atom_string(Opcode, OpcodeS),
opcode(Opcode, Mode, _) }.
label(Name) -->
list(NameChars), % should be nonblanks(NameChars) but that doesn't work
`:`, eos, !,
{ atom_codes(Name, NameChars) }.
% instruction(-Instruction)
% NOTE: The order of these matters. E.g. adc/absolute will partially match
% "ADC $D000,X" (the matching part is "ADC $D000"), but then the eventual call
% to phrase/2 will fail because there are still characters left in the input
% string.
instruction(label(Name)) -->
label(Name).
instruction(Opcode/implied) -->
asm_opcode(Opcode/implied),
eos, !.
instruction(Head/absolute_x) -->
asm_opcode(Opcode/absolute_x),
required_whitespace,
asm_word(Address),
{ Head =.. [Opcode, Address] },
optional_whitespace,
`,`,
optional_whitespace,
any_of(`xX`, _),
eos, !.
instruction(Head/absolute_y) -->
asm_opcode(Opcode/absolute_y),
required_whitespace,
asm_word(Address),
{ Head =.. [Opcode, Address] },
optional_whitespace,
`,`,
optional_whitespace,
any_of(`yY`, _),
eos, !.
instruction(Head/absolute) -->
asm_opcode(Opcode/absolute),
required_whitespace,
asm_word(Address), eos, !,
{ Head =.. [Opcode, Address],
is_word(Address) }.
instruction(Head/relative) -->
asm_opcode(Opcode/relative),
required_whitespace,
asm_word(Address), eos, !,
{ Head =.. [Opcode, Address],
is_word(Address) }.
instruction(Opcode/accumulator) -->
asm_opcode(Opcode/accumulator),
required_whitespace,
any_of(`aA`, _),
eos, !.
instruction(Head/immediate) -->
asm_opcode(Opcode/immediate),
required_whitespace,
`#`,
asm_byte(Value),
eos, !,
{ Head =.. [Opcode, Value] }.
instruction(Head/indirect) -->
asm_opcode(Opcode/indirect),
required_whitespace,
`(`,
optional_whitespace,
asm_word(Address),
optional_whitespace,
`)`,
eos, !,
{ Head =.. [Opcode, Address] }.
instruction(Head/indirect_x) -->
asm_opcode(Opcode/indirect_x),
required_whitespace,
`(`,
optional_whitespace,
asm_byte(Address),
optional_whitespace,
`,`,
optional_whitespace,
any_of(`xX`, _),
optional_whitespace,
`)`,
eos, !,
{ Head =.. [Opcode, Address] }.
instruction(Head/indirect_y) -->
asm_opcode(Opcode/indirect_y),
required_whitespace,
`(`,
optional_whitespace,
asm_byte(Address),
optional_whitespace,
`)`,
optional_whitespace,
`,`,
optional_whitespace,
any_of(`yY`, _),
eos, !,
{ Head =.. [Opcode, Address] }.
instruction(Head/zeropage) -->
asm_opcode(Opcode/zeropage),
required_whitespace,
asm_byte(Address), eos, !,
{ Head =.. [Opcode, Address],
is_byte(Address) }.
% TODO:
% - whichever opcodes are not covered yet :)
%
% parse_line(Line, Instruction) (bidirectional?)
% parse_lines(Lines, Instructions) (bidirectional?)