Skip to content

Commit c6f0a41

Browse files
committed
Add support for parsing draw instruction
1 parent fb7748c commit c6f0a41

File tree

6 files changed

+332
-1
lines changed

6 files changed

+332
-1
lines changed

Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ velcro = "0.5.4"
2424

2525
[build-dependencies]
2626
lalrpop = "0.22.2"
27+
28+
[dev-dependencies]
29+
pretty_assertions = "1.4.1"

src/logic/ast.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ pub enum Instruction {
2929
Print {
3030
value: Value,
3131
},
32+
Draw {
33+
op: DrawOp,
34+
x: Value,
35+
y: Value,
36+
p1: Value,
37+
p2: Value,
38+
p3: Value,
39+
p4: Value,
40+
},
3241
SetRate {
3342
value: Value,
3443
},
@@ -47,6 +56,26 @@ pub enum ConditionOp {
4756
Always,
4857
}
4958

59+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
60+
pub enum DrawOp {
61+
Clear,
62+
Color,
63+
Col,
64+
Stroke,
65+
Line,
66+
Rect,
67+
LineRect,
68+
Poly,
69+
LinePoly,
70+
Triangle,
71+
Image,
72+
Print,
73+
Translate,
74+
Scale,
75+
Rotate,
76+
Reset,
77+
}
78+
5079
#[derive(Debug, Clone, PartialEq)]
5180
pub enum Value {
5281
Variable(String),
@@ -127,3 +156,14 @@ where
127156
Err(_) => Value::Variable(n.into()),
128157
}
129158
}
159+
160+
macro_rules! optional_args {
161+
($($typ:ident)::+ { $($name:ident$(: $value:expr)?),+ ; $($extra:ident),+ $(,)? }) => {
162+
$($typ)::+ {
163+
$($name$(: $value)?),+ ,
164+
$($extra: Value::None),+
165+
}
166+
};
167+
}
168+
169+
pub(super) use optional_args;

src/logic/grammar.lalrpop

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ match {
2020
"jump",
2121
"set",
2222
"print",
23+
"draw",
2324
"setrate",
2425

2526
"equal",
@@ -31,6 +32,23 @@ match {
3132
"strictEqual",
3233
"always",
3334

35+
"clear",
36+
"color",
37+
"col",
38+
"stroke",
39+
"line",
40+
"rect",
41+
"lineRect",
42+
"poly",
43+
"linePoly",
44+
"triangle",
45+
"image",
46+
// print
47+
"translate",
48+
"scale",
49+
"rotate",
50+
"reset",
51+
3452
// https://github.com/Anuken/Arc/blob/071fdffaf220cd57cf971a0ee58db2f321f92ee1/arc-core/src/arc/util/Strings.java#L495
3553
// for example, -+1.--0. is a valid number literal
3654
r"(?x)
@@ -84,7 +102,7 @@ Instruction: Instruction = {
84102
Instruction::Stop,
85103

86104
"jump" <target:JumpTarget> <op:ConditionOp0> =>
87-
Instruction::Jump { <>, x: Value::None, y: Value::None },
105+
optional_args!(Instruction::Jump { <>; x, y }),
88106

89107
"jump" <target:JumpTarget> <op:ConditionOp2> <x:Value> <y:Value> =>
90108
Instruction::Jump {<>},
@@ -95,6 +113,27 @@ Instruction: Instruction = {
95113
"print" <value:Value> =>
96114
Instruction::Print {<>},
97115

116+
"draw" <op:DrawOp0> =>
117+
optional_args!(Instruction::Draw { <>; x, y, p1, p2, p3, p4 }),
118+
119+
"draw" <op:DrawOp1> <x:Value> =>
120+
optional_args!(Instruction::Draw { <>; y, p1, p2, p3, p4 }),
121+
122+
"draw" <op:DrawOp2> <x:Value> <y:Value> =>
123+
optional_args!(Instruction::Draw { <>; p1, p2, p3, p4 }),
124+
125+
"draw" <op:DrawOp3> <x:Value> <y:Value> <p1:Value> =>
126+
optional_args!(Instruction::Draw { <>; p2, p3, p4 }),
127+
128+
"draw" <op:DrawOp4> <x:Value> <y:Value> <p1:Value> <p2:Value> =>
129+
optional_args!(Instruction::Draw { <>; p3, p4 }),
130+
131+
"draw" <op:DrawOp5> <x:Value> <y:Value> <p1:Value> <p2:Value> <p3:Value> =>
132+
optional_args!(Instruction::Draw { <>; p4 }),
133+
134+
"draw" <op:DrawOp6> <x:Value> <y:Value> <p1:Value> <p2:Value> <p3:Value> <p4:Value> =>
135+
Instruction::Draw {<>},
136+
98137
"setrate" <value:Value> =>
99138
Instruction::SetRate {<>},
100139

@@ -116,6 +155,43 @@ ConditionOp2: ConditionOp = {
116155
"strictEqual" => ConditionOp::StrictEqual,
117156
};
118157

158+
DrawOp0: DrawOp = {
159+
"reset" => DrawOp::Reset,
160+
};
161+
162+
DrawOp1: DrawOp = {
163+
"col" => DrawOp::Col,
164+
"stroke" => DrawOp::Stroke,
165+
"rotate" => DrawOp::Rotate,
166+
};
167+
168+
DrawOp2: DrawOp = {
169+
"translate" => DrawOp::Translate,
170+
"scale" => DrawOp::Scale,
171+
};
172+
173+
DrawOp3: DrawOp = {
174+
"clear" => DrawOp::Clear,
175+
"print" => DrawOp::Print,
176+
};
177+
178+
DrawOp4: DrawOp = {
179+
"color" => DrawOp::Color,
180+
"line" => DrawOp::Line,
181+
"rect" => DrawOp::Rect,
182+
"lineRect" => DrawOp::LineRect,
183+
};
184+
185+
DrawOp5: DrawOp = {
186+
"poly" => DrawOp::Poly,
187+
"linePoly" => DrawOp::LinePoly,
188+
"image" => DrawOp::Image,
189+
};
190+
191+
DrawOp6: DrawOp = {
192+
"triangle" => DrawOp::Triangle,
193+
};
194+
119195
Symbol = {
120196
SYMBOL,
121197
LABEL,
@@ -126,6 +202,7 @@ Symbol = {
126202
"jump",
127203
"set",
128204
"print",
205+
"draw",
129206
"setrate",
130207

131208
"equal",
@@ -136,6 +213,22 @@ Symbol = {
136213
"greaterThanEq",
137214
"strictEqual",
138215
"always",
216+
217+
"clear",
218+
"color",
219+
"col",
220+
"stroke",
221+
"line",
222+
"rect",
223+
"lineRect",
224+
"poly",
225+
"linePoly",
226+
"triangle",
227+
"image",
228+
"translate",
229+
"scale",
230+
"rotate",
231+
"reset",
139232
};
140233

141234
Value: Value = {

0 commit comments

Comments
 (0)