Add evaluating of simple maths
Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
parent
05c095ccfe
commit
d222573a3a
8 changed files with 543 additions and 4 deletions
56
tests/cases/1-parsed@maths.snap
Normal file
56
tests/cases/1-parsed@maths.snap
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: parsed
|
||||
info:
|
||||
input: "{{= 5 * 3 }}\n{{= 2 * 3 + 4 * 3 }}\n{{= 3 / 3 + 3 }}"
|
||||
context: {}
|
||||
input_file: tests/cases/maths.nomo
|
||||
---
|
||||
ParsedTemplate {
|
||||
tokens: [
|
||||
[LeftDelim]"{{" (0..2),
|
||||
[WantsOutput]"=" (2..3),
|
||||
[Whitespace]" " (3..4),
|
||||
[Literal(Integer(5))]"5" (4..5),
|
||||
[Whitespace]" " (5..6),
|
||||
[Operator(Times)]"*" (6..7),
|
||||
[Whitespace]" " (7..8),
|
||||
[Literal(Integer(3))]"3" (8..9),
|
||||
[Whitespace]" " (9..10),
|
||||
[RightDelim]"}}" (10..12),
|
||||
[Whitespace]"\n" (12..13),
|
||||
[LeftDelim]"{{" (13..15),
|
||||
[WantsOutput]"=" (15..16),
|
||||
[Whitespace]" " (16..17),
|
||||
[Literal(Integer(2))]"2" (17..18),
|
||||
[Whitespace]" " (18..19),
|
||||
[Operator(Times)]"*" (19..20),
|
||||
[Whitespace]" " (20..21),
|
||||
[Literal(Integer(3))]"3" (21..22),
|
||||
[Whitespace]" " (22..23),
|
||||
[Operator(Plus)]"+" (23..24),
|
||||
[Whitespace]" " (24..25),
|
||||
[Literal(Integer(4))]"4" (25..26),
|
||||
[Whitespace]" " (26..27),
|
||||
[Operator(Times)]"*" (27..28),
|
||||
[Whitespace]" " (28..29),
|
||||
[Literal(Integer(3))]"3" (29..30),
|
||||
[Whitespace]" " (30..31),
|
||||
[RightDelim]"}}" (31..33),
|
||||
[Whitespace]"\n" (33..34),
|
||||
[LeftDelim]"{{" (34..36),
|
||||
[WantsOutput]"=" (36..37),
|
||||
[Whitespace]" " (37..38),
|
||||
[Literal(Integer(3))]"3" (38..39),
|
||||
[Whitespace]" " (39..40),
|
||||
[Operator(Divide)]"/" (40..41),
|
||||
[Whitespace]" " (41..42),
|
||||
[Literal(Integer(3))]"3" (42..43),
|
||||
[Whitespace]" " (43..44),
|
||||
[Operator(Plus)]"+" (44..45),
|
||||
[Whitespace]" " (45..46),
|
||||
[Literal(Integer(3))]"3" (46..47),
|
||||
[Whitespace]" " (47..48),
|
||||
[RightDelim]"}}" (48..50),
|
||||
],
|
||||
}
|
||||
100
tests/cases/2-ast@maths.snap
Normal file
100
tests/cases/2-ast@maths.snap
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: ast
|
||||
info:
|
||||
input: "{{= 5 * 3 }}\n{{= 2 * 3 + 4 * 3 }}\n{{= 3 / 3 + 3 }}"
|
||||
context: {}
|
||||
input_file: tests/cases/maths.nomo
|
||||
---
|
||||
TemplateAst {
|
||||
root: [
|
||||
Interpolation {
|
||||
prev_whitespace_content: None,
|
||||
expression: Operation {
|
||||
op: Times,
|
||||
lhs: Literal {
|
||||
source: [Literal(Integer(5))]"5" (4..5),
|
||||
value: Integer {
|
||||
value: 5,
|
||||
},
|
||||
},
|
||||
rhs: Literal {
|
||||
source: [Literal(Integer(3))]"3" (8..9),
|
||||
value: Integer {
|
||||
value: 3,
|
||||
},
|
||||
},
|
||||
},
|
||||
post_whitespace_content: Some(
|
||||
[Whitespace]"\n" (12..13),
|
||||
),
|
||||
},
|
||||
Interpolation {
|
||||
prev_whitespace_content: None,
|
||||
expression: Operation {
|
||||
op: Plus,
|
||||
lhs: Operation {
|
||||
op: Times,
|
||||
lhs: Literal {
|
||||
source: [Literal(Integer(2))]"2" (17..18),
|
||||
value: Integer {
|
||||
value: 2,
|
||||
},
|
||||
},
|
||||
rhs: Literal {
|
||||
source: [Literal(Integer(3))]"3" (21..22),
|
||||
value: Integer {
|
||||
value: 3,
|
||||
},
|
||||
},
|
||||
},
|
||||
rhs: Operation {
|
||||
op: Times,
|
||||
lhs: Literal {
|
||||
source: [Literal(Integer(4))]"4" (25..26),
|
||||
value: Integer {
|
||||
value: 4,
|
||||
},
|
||||
},
|
||||
rhs: Literal {
|
||||
source: [Literal(Integer(3))]"3" (29..30),
|
||||
value: Integer {
|
||||
value: 3,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
post_whitespace_content: Some(
|
||||
[Whitespace]"\n" (33..34),
|
||||
),
|
||||
},
|
||||
Interpolation {
|
||||
prev_whitespace_content: None,
|
||||
expression: Operation {
|
||||
op: Plus,
|
||||
lhs: Operation {
|
||||
op: Divide,
|
||||
lhs: Literal {
|
||||
source: [Literal(Integer(3))]"3" (38..39),
|
||||
value: Integer {
|
||||
value: 3,
|
||||
},
|
||||
},
|
||||
rhs: Literal {
|
||||
source: [Literal(Integer(3))]"3" (42..43),
|
||||
value: Integer {
|
||||
value: 3,
|
||||
},
|
||||
},
|
||||
},
|
||||
rhs: Literal {
|
||||
source: [Literal(Integer(3))]"3" (46..47),
|
||||
value: Integer {
|
||||
value: 3,
|
||||
},
|
||||
},
|
||||
},
|
||||
post_whitespace_content: None,
|
||||
},
|
||||
],
|
||||
}
|
||||
187
tests/cases/3-instructions@maths.snap
Normal file
187
tests/cases/3-instructions@maths.snap
Normal file
|
|
@ -0,0 +1,187 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: emit
|
||||
info:
|
||||
input: "{{= 5 * 3 }}\n{{= 2 * 3 + 4 * 3 }}\n{{= 3 / 3 + 3 }}"
|
||||
context: {}
|
||||
input_file: tests/cases/maths.nomo
|
||||
---
|
||||
VMInstructions {
|
||||
labels: {},
|
||||
instructions: [
|
||||
LoadLiteralToSlot {
|
||||
source: [Literal(Integer(5))]"5" (4..5),
|
||||
value: Integer {
|
||||
value: 5,
|
||||
},
|
||||
slot: VariableSlot {
|
||||
index: 1,
|
||||
},
|
||||
},
|
||||
LoadLiteralToSlot {
|
||||
source: [Literal(Integer(3))]"3" (8..9),
|
||||
value: Integer {
|
||||
value: 3,
|
||||
},
|
||||
slot: VariableSlot {
|
||||
index: 2,
|
||||
},
|
||||
},
|
||||
MathOperate {
|
||||
op: Times,
|
||||
left_slot: VariableSlot {
|
||||
index: 1,
|
||||
},
|
||||
right_slot: VariableSlot {
|
||||
index: 2,
|
||||
},
|
||||
result_slot: VariableSlot {
|
||||
index: 0,
|
||||
},
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
index: 0,
|
||||
},
|
||||
},
|
||||
AppendContent {
|
||||
content: "\n" (12..13),
|
||||
},
|
||||
LoadLiteralToSlot {
|
||||
source: [Literal(Integer(2))]"2" (17..18),
|
||||
value: Integer {
|
||||
value: 2,
|
||||
},
|
||||
slot: VariableSlot {
|
||||
index: 5,
|
||||
},
|
||||
},
|
||||
LoadLiteralToSlot {
|
||||
source: [Literal(Integer(3))]"3" (21..22),
|
||||
value: Integer {
|
||||
value: 3,
|
||||
},
|
||||
slot: VariableSlot {
|
||||
index: 6,
|
||||
},
|
||||
},
|
||||
MathOperate {
|
||||
op: Times,
|
||||
left_slot: VariableSlot {
|
||||
index: 5,
|
||||
},
|
||||
right_slot: VariableSlot {
|
||||
index: 6,
|
||||
},
|
||||
result_slot: VariableSlot {
|
||||
index: 4,
|
||||
},
|
||||
},
|
||||
LoadLiteralToSlot {
|
||||
source: [Literal(Integer(4))]"4" (25..26),
|
||||
value: Integer {
|
||||
value: 4,
|
||||
},
|
||||
slot: VariableSlot {
|
||||
index: 8,
|
||||
},
|
||||
},
|
||||
LoadLiteralToSlot {
|
||||
source: [Literal(Integer(3))]"3" (29..30),
|
||||
value: Integer {
|
||||
value: 3,
|
||||
},
|
||||
slot: VariableSlot {
|
||||
index: 9,
|
||||
},
|
||||
},
|
||||
MathOperate {
|
||||
op: Times,
|
||||
left_slot: VariableSlot {
|
||||
index: 8,
|
||||
},
|
||||
right_slot: VariableSlot {
|
||||
index: 9,
|
||||
},
|
||||
result_slot: VariableSlot {
|
||||
index: 7,
|
||||
},
|
||||
},
|
||||
MathOperate {
|
||||
op: Plus,
|
||||
left_slot: VariableSlot {
|
||||
index: 4,
|
||||
},
|
||||
right_slot: VariableSlot {
|
||||
index: 7,
|
||||
},
|
||||
result_slot: VariableSlot {
|
||||
index: 3,
|
||||
},
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
index: 3,
|
||||
},
|
||||
},
|
||||
AppendContent {
|
||||
content: "\n" (33..34),
|
||||
},
|
||||
LoadLiteralToSlot {
|
||||
source: [Literal(Integer(3))]"3" (38..39),
|
||||
value: Integer {
|
||||
value: 3,
|
||||
},
|
||||
slot: VariableSlot {
|
||||
index: 12,
|
||||
},
|
||||
},
|
||||
LoadLiteralToSlot {
|
||||
source: [Literal(Integer(3))]"3" (42..43),
|
||||
value: Integer {
|
||||
value: 3,
|
||||
},
|
||||
slot: VariableSlot {
|
||||
index: 13,
|
||||
},
|
||||
},
|
||||
MathOperate {
|
||||
op: Divide,
|
||||
left_slot: VariableSlot {
|
||||
index: 12,
|
||||
},
|
||||
right_slot: VariableSlot {
|
||||
index: 13,
|
||||
},
|
||||
result_slot: VariableSlot {
|
||||
index: 11,
|
||||
},
|
||||
},
|
||||
LoadLiteralToSlot {
|
||||
source: [Literal(Integer(3))]"3" (46..47),
|
||||
value: Integer {
|
||||
value: 3,
|
||||
},
|
||||
slot: VariableSlot {
|
||||
index: 14,
|
||||
},
|
||||
},
|
||||
MathOperate {
|
||||
op: Plus,
|
||||
left_slot: VariableSlot {
|
||||
index: 11,
|
||||
},
|
||||
right_slot: VariableSlot {
|
||||
index: 14,
|
||||
},
|
||||
result_slot: VariableSlot {
|
||||
index: 10,
|
||||
},
|
||||
},
|
||||
EmitFromSlot {
|
||||
slot: VariableSlot {
|
||||
index: 10,
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
9
tests/cases/4-output@maths.snap
Normal file
9
tests/cases/4-output@maths.snap
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: output
|
||||
info:
|
||||
input: "{{= 5 * 3 }}\n{{= 2 * 3 + 4 * 3 }}\n{{= 3 / 3 + 3 }}"
|
||||
context: {}
|
||||
input_file: tests/cases/maths.nomo
|
||||
---
|
||||
"15\n18\n4"
|
||||
6
tests/cases/maths.nomo
Normal file
6
tests/cases/maths.nomo
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
}
|
||||
---
|
||||
{{= 5 * 3 }}
|
||||
{{= 2 * 3 + 4 * 3 }}
|
||||
{{= 3 / 3 + 3 }}
|
||||
Loading…
Add table
Add a link
Reference in a new issue