Skip to content

Commit 0178add

Browse files
Add support for other address modes and test
1 parent 7728976 commit 0178add

File tree

6 files changed

+79
-93
lines changed

6 files changed

+79
-93
lines changed

src/data/grammar.txt

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/data/redcode.pest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Instruction = _{ Operation ~ Field ~ ("," ~ Field)? }
1010

1111
Operation = ${ Opcode }
1212

13-
Field = ${ AddressMode? ~ Expr }
13+
Field = !{ AddressMode? ~ Expr }
1414

1515
Opcode = {
1616
^"DAT" | ^"MOV" | ^"ADD" | ^"SUB" | ^"MUL" | ^"DIV" | ^"MOD" |
@@ -22,6 +22,6 @@ Modifier = { ^"A" | ^"B" | ^"AB" | ^"BA" | ^"F" | ^"X" | ^"I" }
2222

2323
AddressMode = { "#" | "$" | "*" | "@" | "{" | "<" | "}" | ">" }
2424

25-
Expr = !{ Number }
25+
Expr = { Number }
2626

2727
Number = @{ ASCII_DIGIT+ }

src/load_file.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ enum_string!(pub Opcode, {
2020
Slt => "SLT",
2121
Spl => "SPL",
2222
Nop => "NOP",
23-
Org => "ORG",
24-
Equ => "EQU",
25-
End => "END",
2623
});
2724

2825
impl Default for Opcode {
@@ -31,9 +28,21 @@ impl Default for Opcode {
3128
}
3229
}
3330

31+
enum_string!(pub PseudoOpcode, {
32+
Org => "ORG",
33+
Equ => "EQU",
34+
End => "END",
35+
});
36+
3437
enum_string!(pub AddressMode, {
3538
Immediate => "#",
36-
Direct => "",
39+
Direct => "$",
40+
IndirectA => "*",
41+
IndirectB => "@",
42+
PreDecIndirectA => "{",
43+
PreDecIndirectB => "<",
44+
PostIncIndirectA => "}",
45+
PostIncIndirectB => ">",
3746
});
3847

3948
impl Default for AddressMode {

src/parser.rs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,47 @@ mod tests {
155155

156156
#[test]
157157
fn parse_field_with_mode() {
158-
parses_to! {
159-
parser: RedcodeParser,
160-
input: "#123",
161-
rule: Rule::Field,
162-
tokens: [
163-
Field(0, 4, [
164-
AddressMode(0, 1),
165-
Expr(1, 4, [
166-
Number(1, 4)
158+
for test_input in [
159+
"#123", "$123", "*123", "@123", "{123", "<123", "}123", ">123",
160+
]
161+
.iter()
162+
{
163+
parses_to! {
164+
parser: RedcodeParser,
165+
input: test_input,
166+
rule: Rule::Field,
167+
tokens: [
168+
Field(0, 4, [
169+
AddressMode(0, 1),
170+
Expr(1, 4, [
171+
Number(1, 4)
172+
]),
173+
])
174+
]
175+
};
176+
}
177+
}
178+
179+
#[test]
180+
fn parse_opcode_modifier() {
181+
for test_input in [
182+
"mov.a", "mov.b", "mov.ab", "mov.ba", "mov.f", "mov.x", "mov.i",
183+
]
184+
.iter()
185+
{
186+
dbg!(test_input);
187+
parses_to! {
188+
parser: RedcodeParser,
189+
input: test_input,
190+
rule: Rule::Operation,
191+
tokens: [
192+
Operation(0, test_input.len(), [
193+
Opcode(0, 3),
194+
Modifier(4, test_input.len()),
167195
]),
168-
])
169-
]
170-
};
196+
]
197+
}
198+
}
171199
}
172200

173201
#[allow(clippy::cyclomatic_complexity)]

tests/expected_loadfile.red

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
MOV 0, 1
2-
DAT 1, 2
3-
MOV 1, 0
4-
JMP 123, 0
5-
ADD 1, 2
6-
SUB 3, 4
7-
MUL 5, 6
8-
DIV 7, 8
9-
MOD 9, 10
10-
JMZ 0, 0
11-
JMN 0, 0
12-
DJN 0, 0
13-
CMP 0, 0
14-
SEQ 0, 0
15-
SNE 0, 0
16-
SLT 0, 0
17-
SPL 0, 0
18-
NOP 0, 0
19-
ORG 0, 0
20-
EQU 0, 0
21-
END 0, 0
1+
MOV $0, $1
2+
DAT $1, $2
3+
MOV $1, $0
4+
JMP $123, $0
5+
ADD #1, @2
6+
SUB $3, $4
7+
MUL $5, $6
8+
DIV $7, $8
9+
MOD $9, $10
10+
JMZ $0, $0
11+
JMN $0, $0
12+
DJN $0, $0
13+
CMP $0, $0
14+
SEQ $0, $0
15+
SNE $0, $0
16+
SLT $0, $0
17+
SPL $0, $0
18+
NOP $0, $0

tests/test_file.red

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mov 1, 0
44

55
jmp 123
66

7-
add 1, 2
7+
add # 1, @ 2
88
sub 3, 4
99
mul 5, 6
1010
div 7, 8
@@ -19,6 +19,8 @@ sne 0
1919
slt 0
2020
spl 0
2121
nop 0
22-
org 0
23-
equ 0
24-
end 0 ; TODO optional operands for END or possibly some other opcodes?
22+
23+
; TODO rewrite grammar to handle org, equ, end
24+
; org 0
25+
; equ 0
26+
; end 0

0 commit comments

Comments
 (0)