Also fix the jump if its the last one
Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
parent
ff308649b9
commit
ae379df9db
7 changed files with 185 additions and 2 deletions
|
|
@ -147,7 +147,7 @@ fn emit_ast_expr(
|
||||||
*jump = new_jump as isize;
|
*jump = new_jump as isize;
|
||||||
} else {
|
} else {
|
||||||
panic!("Got an else without a previous if?");
|
panic!("Got an else without a previous if?");
|
||||||
};
|
}
|
||||||
|
|
||||||
if let Some(expression) = expression {
|
if let Some(expression) = expression {
|
||||||
let emit_slot = machine.reserve_slot();
|
let emit_slot = machine.reserve_slot();
|
||||||
|
|
@ -167,6 +167,15 @@ fn emit_ast_expr(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(previous_jump) = previous_jump.take() {
|
||||||
|
let new_jump = eval.len() - previous_jump - 1;
|
||||||
|
let Instruction::JumpIfNotTrue { jump, .. } = &mut eval[previous_jump] else {
|
||||||
|
panic!("Jump slot had something that is not a jump?!");
|
||||||
|
};
|
||||||
|
|
||||||
|
*jump = new_jump as isize;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(ws) = previous_post_whitespace_content {
|
if let Some(ws) = previous_post_whitespace_content {
|
||||||
eval.push(Instruction::AppendContent {
|
eval.push(Instruction::AppendContent {
|
||||||
content: ws.source().clone(),
|
content: ws.source().clone(),
|
||||||
|
|
|
||||||
36
tests/cases/1-parsed@if_else_if.snap
Normal file
36
tests/cases/1-parsed@if_else_if.snap
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
---
|
||||||
|
source: tests/file_tests.rs
|
||||||
|
expression: parsed
|
||||||
|
input_file: tests/cases/if_else_if.nomo
|
||||||
|
---
|
||||||
|
ParsedTemplate {
|
||||||
|
tokens: [
|
||||||
|
[LeftDelim]"{{" (0..2),
|
||||||
|
[Whitespace]" " (2..3),
|
||||||
|
[ConditionalIf]"if" (3..5),
|
||||||
|
[Whitespace]" " (5..6),
|
||||||
|
[Ident]"test" (6..10),
|
||||||
|
[Whitespace]" " (10..11),
|
||||||
|
[RightDelim]"}}" (11..13),
|
||||||
|
[Whitespace]"\n " (13..18),
|
||||||
|
[Content]"Not Hello World! :C" (18..37),
|
||||||
|
[Whitespace]"\n" (37..38),
|
||||||
|
[LeftDelim]"{{" (38..40),
|
||||||
|
[Whitespace]" " (40..41),
|
||||||
|
[ConditionalElse]"else" (41..45),
|
||||||
|
[Whitespace]" " (45..46),
|
||||||
|
[ConditionalIf]"if" (46..48),
|
||||||
|
[Whitespace]" " (48..49),
|
||||||
|
[Ident]"another_test" (49..61),
|
||||||
|
[Whitespace]" " (61..62),
|
||||||
|
[RightDelim]"}}" (62..64),
|
||||||
|
[Whitespace]"\n " (64..69),
|
||||||
|
[Content]"Hello World!" (69..81),
|
||||||
|
[Whitespace]"\n" (81..82),
|
||||||
|
[LeftDelim]"{{" (82..84),
|
||||||
|
[Whitespace]" " (84..85),
|
||||||
|
[End]"end" (85..88),
|
||||||
|
[Whitespace]" " (88..89),
|
||||||
|
[RightDelim]"}}" (89..91),
|
||||||
|
],
|
||||||
|
}
|
||||||
60
tests/cases/2-ast@if_else_if.snap
Normal file
60
tests/cases/2-ast@if_else_if.snap
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
---
|
||||||
|
source: tests/file_tests.rs
|
||||||
|
expression: ast
|
||||||
|
input_file: tests/cases/if_else_if.nomo
|
||||||
|
---
|
||||||
|
TemplateAst {
|
||||||
|
root: [
|
||||||
|
ConditionalChain {
|
||||||
|
chain: [
|
||||||
|
Block {
|
||||||
|
prev_whitespace_content: None,
|
||||||
|
expression: IfConditional {
|
||||||
|
expression: VariableAccess(
|
||||||
|
[Ident]"test" (6..10),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
post_whitespace_content: Some(
|
||||||
|
[Whitespace]"\n " (13..18),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
ConditionalContent {
|
||||||
|
content: [
|
||||||
|
StaticContent(
|
||||||
|
[Content]"Not Hello World! :C" (18..37),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
Block {
|
||||||
|
prev_whitespace_content: Some(
|
||||||
|
[Whitespace]"\n" (37..38),
|
||||||
|
),
|
||||||
|
expression: ElseConditional {
|
||||||
|
expression: Some(
|
||||||
|
VariableAccess(
|
||||||
|
[Ident]"another_test" (49..61),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
post_whitespace_content: Some(
|
||||||
|
[Whitespace]"\n " (64..69),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
ConditionalContent {
|
||||||
|
content: [
|
||||||
|
StaticContent(
|
||||||
|
[Content]"Hello World!" (69..81),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
Block {
|
||||||
|
prev_whitespace_content: Some(
|
||||||
|
[Whitespace]"\n" (81..82),
|
||||||
|
),
|
||||||
|
expression: EndBlock,
|
||||||
|
post_whitespace_content: None,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
@ -14,7 +14,7 @@ input_file: tests/cases/condition.nomo
|
||||||
emit_slot: VariableSlot {
|
emit_slot: VariableSlot {
|
||||||
index: 0,
|
index: 0,
|
||||||
},
|
},
|
||||||
jump: 9223372036854775807,
|
jump: 5,
|
||||||
},
|
},
|
||||||
AppendContent {
|
AppendContent {
|
||||||
content: "\n " (13..18),
|
content: "\n " (13..18),
|
||||||
|
|
|
||||||
61
tests/cases/3-instructions@if_else_if.snap
Normal file
61
tests/cases/3-instructions@if_else_if.snap
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
---
|
||||||
|
source: tests/file_tests.rs
|
||||||
|
expression: emit
|
||||||
|
input_file: tests/cases/if_else_if.nomo
|
||||||
|
---
|
||||||
|
[
|
||||||
|
LoadFromContextToSlot {
|
||||||
|
name: "test" (6..10),
|
||||||
|
slot: VariableSlot {
|
||||||
|
index: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
JumpIfNotTrue {
|
||||||
|
emit_slot: VariableSlot {
|
||||||
|
index: 0,
|
||||||
|
},
|
||||||
|
jump: 5,
|
||||||
|
},
|
||||||
|
AppendContent {
|
||||||
|
content: "\n " (13..18),
|
||||||
|
},
|
||||||
|
AppendContent {
|
||||||
|
content: "Not Hello World! :C" (18..37),
|
||||||
|
},
|
||||||
|
Jump {
|
||||||
|
jump: 9,
|
||||||
|
},
|
||||||
|
AppendContent {
|
||||||
|
content: "\n " (13..18),
|
||||||
|
},
|
||||||
|
AppendContent {
|
||||||
|
content: "\n" (37..38),
|
||||||
|
},
|
||||||
|
LoadFromContextToSlot {
|
||||||
|
name: "another_test" (49..61),
|
||||||
|
slot: VariableSlot {
|
||||||
|
index: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
JumpIfNotTrue {
|
||||||
|
emit_slot: VariableSlot {
|
||||||
|
index: 1,
|
||||||
|
},
|
||||||
|
jump: 5,
|
||||||
|
},
|
||||||
|
AppendContent {
|
||||||
|
content: "\n " (64..69),
|
||||||
|
},
|
||||||
|
AppendContent {
|
||||||
|
content: "Hello World!" (69..81),
|
||||||
|
},
|
||||||
|
Jump {
|
||||||
|
jump: 2,
|
||||||
|
},
|
||||||
|
AppendContent {
|
||||||
|
content: "\n " (64..69),
|
||||||
|
},
|
||||||
|
AppendContent {
|
||||||
|
content: "\n" (81..82),
|
||||||
|
},
|
||||||
|
]
|
||||||
6
tests/cases/4-output@if_else_if.snap
Normal file
6
tests/cases/4-output@if_else_if.snap
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
source: tests/file_tests.rs
|
||||||
|
expression: output
|
||||||
|
input_file: tests/cases/if_else_if.nomo
|
||||||
|
---
|
||||||
|
"\n\n Hello World!\n"
|
||||||
11
tests/cases/if_else_if.nomo
Normal file
11
tests/cases/if_else_if.nomo
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"test": false,
|
||||||
|
"another_test": true,
|
||||||
|
"stuff": "more"
|
||||||
|
}
|
||||||
|
---
|
||||||
|
{{ if test }}
|
||||||
|
Not Hello World! :C
|
||||||
|
{{ else if another_test }}
|
||||||
|
Hello World!
|
||||||
|
{{ end }}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue