else and else-if support.
This commit is contained in:
parent
7c735cfdac
commit
3f21434cd1
7 changed files with 132 additions and 45 deletions
19
examples/expression_test.fun → examples/expressions_test.fun
Normal file → Executable file
19
examples/expression_test.fun → examples/expressions_test.fun
Normal file → Executable file
|
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/env fun
|
#!/usr/bin/env fun
|
||||||
|
|
||||||
// Feature test for current Fun parser/VM.
|
// Expressions test: arithmetic, precedence, comparisons, logical ops, unary, parentheses.
|
||||||
|
|
||||||
print("=== Feature Test Start ===")
|
print("=== Expressions test start ===")
|
||||||
|
|
||||||
// Typed declarations
|
// Typed declarations
|
||||||
number a = 2
|
number a = 2
|
||||||
|
|
@ -13,7 +13,10 @@ boolean flag = true
|
||||||
|
|
||||||
// Arithmetic and precedence
|
// Arithmetic and precedence
|
||||||
number sum = a + b * 5 - 1
|
number sum = a + b * 5 - 1
|
||||||
print(sum) // expect 2 + 3 * 5 - 1 = 16
|
print(sum) // expect 2 + 3*5 - 1 = 16
|
||||||
|
|
||||||
|
number calc = (a + b) * (b - a + 1)
|
||||||
|
print(calc) // expect (2+3)*(3-2+1) = 5*2 = 10
|
||||||
|
|
||||||
// Unary operators
|
// Unary operators
|
||||||
number neg = -n
|
number neg = -n
|
||||||
|
|
@ -31,10 +34,8 @@ print(true || false) // expect 1 (true)
|
||||||
print(n > 5) // expect 1
|
print(n > 5) // expect 1
|
||||||
print(s != "") // expect 1
|
print(s != "") // expect 1
|
||||||
|
|
||||||
// Indentation-based nested if blocks (two spaces)
|
// Mixed logical/relational
|
||||||
if (n >= 10)
|
print(a < b && n >= 10) // expect 1
|
||||||
print(n) // expect 10
|
print((a + b) == 5 && flag) // expect 1
|
||||||
if (a + b == 5)
|
|
||||||
print(42) // expect 42
|
|
||||||
|
|
||||||
print("=== Feature Test End ===")
|
print("=== Expressions test end ===")
|
||||||
7
examples/have_fun.fun
Executable file
7
examples/have_fun.fun
Executable file
|
|
@ -0,0 +1,7 @@
|
||||||
|
#!/usr/bin/env fun
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Have fun! in Fun
|
||||||
|
*/
|
||||||
|
|
||||||
|
print("Hello, fun!")
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
#!/usr/bin/env fun
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Hello world in Fun
|
|
||||||
*/
|
|
||||||
|
|
||||||
print("Hello, world!")
|
|
||||||
24
examples/if_else_test.fun
Executable file
24
examples/if_else_test.fun
Executable file
|
|
@ -0,0 +1,24 @@
|
||||||
|
#!/usr/bin/env fun
|
||||||
|
|
||||||
|
// Test else / else if chains and nested if-blocks (two-space indentation)
|
||||||
|
|
||||||
|
print("=== if/else-if/else test ===")
|
||||||
|
|
||||||
|
number n = 42
|
||||||
|
|
||||||
|
if (n < 0)
|
||||||
|
print("neg")
|
||||||
|
else if (n == 0)
|
||||||
|
print("zero")
|
||||||
|
else if (n < 10)
|
||||||
|
print("small")
|
||||||
|
else
|
||||||
|
print("big") // expect: big
|
||||||
|
|
||||||
|
// Nested if inside a true branch
|
||||||
|
if (n >= 10)
|
||||||
|
print(n) // expect: 42
|
||||||
|
if (n == 42)
|
||||||
|
print("answer") // expect: answer
|
||||||
|
|
||||||
|
print("=== if/else-if/else done ===")
|
||||||
|
|
@ -1 +1 @@
|
||||||
print("Hello, world!")
|
print("Have, fun!")
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
#!/usr/bin/env fun
|
#!/usr/bin/env fun
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Hello, world!
|
* Have, fun!
|
||||||
*/
|
*/
|
||||||
|
|
||||||
print("Hello, world!")
|
print("Have, fun!")
|
||||||
|
|
||||||
// Bye, world!
|
// Bye, no fun!
|
||||||
|
|
|
||||||
114
src/parser.c
114
src/parser.c
|
|
@ -629,36 +629,98 @@ static void parse_block(Bytecode *bc, const char *src, size_t len, size_t *pos,
|
||||||
|
|
||||||
/* at same indent -> parse statement */
|
/* at same indent -> parse statement */
|
||||||
if (starts_with(src, len, *pos, "if")) {
|
if (starts_with(src, len, *pos, "if")) {
|
||||||
*pos += 2;
|
int end_jumps[64];
|
||||||
/* require at least one space before condition if present */
|
int end_count = 0;
|
||||||
skip_spaces(src, len, pos);
|
|
||||||
/* condition: a single expression (booleans/ident/number/string) */
|
|
||||||
if (!emit_expression(bc, src, len, pos)) {
|
|
||||||
/* no condition -> treat as false */
|
|
||||||
int ci = bytecode_add_constant(bc, make_int(0));
|
|
||||||
bytecode_add_instruction(bc, OP_LOAD_CONST, ci);
|
|
||||||
}
|
|
||||||
/* end of condition: ignore any trailing until EOL */
|
|
||||||
skip_to_eol(src, len, pos);
|
|
||||||
|
|
||||||
/* conditional jump over the block */
|
for (;;) {
|
||||||
int jmp_false = bytecode_add_instruction(bc, OP_JUMP_IF_FALSE, 0);
|
/* consume 'if' or 'else if' condition */
|
||||||
|
if (starts_with(src, len, *pos, "if")) {
|
||||||
/* parse nested block if next line is indented */
|
*pos += 2;
|
||||||
size_t after_if = *pos;
|
|
||||||
int next_indent = 0;
|
|
||||||
if (read_line_start(src, len, pos, &next_indent)) {
|
|
||||||
if (next_indent > current_indent) {
|
|
||||||
/* parse body at increased indent */
|
|
||||||
parse_block(bc, src, len, pos, next_indent);
|
|
||||||
} else {
|
} else {
|
||||||
/* empty if-body; keep *pos at start of that line (already set) */
|
/* for 'else if' we arrive here with *pos already after 'if' */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* require at least one space before condition if present */
|
||||||
|
skip_spaces(src, len, pos);
|
||||||
|
if (!emit_expression(bc, src, len, pos)) {
|
||||||
|
/* no condition -> treat as false */
|
||||||
|
int ci = bytecode_add_constant(bc, make_int(0));
|
||||||
|
bytecode_add_instruction(bc, OP_LOAD_CONST, ci);
|
||||||
|
}
|
||||||
|
/* end of condition: ignore any trailing until EOL */
|
||||||
|
skip_to_eol(src, len, pos);
|
||||||
|
|
||||||
|
/* conditional jump over this clause's block */
|
||||||
|
int jmp_false = bytecode_add_instruction(bc, OP_JUMP_IF_FALSE, 0);
|
||||||
|
|
||||||
|
/* parse nested block if next line is indented */
|
||||||
|
int next_indent = 0;
|
||||||
|
size_t after_hdr = *pos;
|
||||||
|
if (read_line_start(src, len, pos, &next_indent)) {
|
||||||
|
if (next_indent > current_indent) {
|
||||||
|
/* parse body at increased indent */
|
||||||
|
parse_block(bc, src, len, pos, next_indent);
|
||||||
|
} else {
|
||||||
|
/* empty body; keep *pos at start of that line */
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* EOF -> empty body */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* after body, unconditionally jump to end of the whole chain */
|
||||||
|
int jmp_end = bytecode_add_instruction(bc, OP_JUMP, 0);
|
||||||
|
if (end_count < (int)(sizeof(end_jumps) / sizeof(end_jumps[0]))) {
|
||||||
|
end_jumps[end_count++] = jmp_end;
|
||||||
|
} else {
|
||||||
|
parser_fail(*pos, "Too many chained else/if clauses");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* patch false-jump target to start of next clause (or fallthrough) */
|
||||||
|
bytecode_set_operand(bc, jmp_false, bc->instr_count);
|
||||||
|
|
||||||
|
/* look for else or else if at the same indentation */
|
||||||
|
size_t look = *pos;
|
||||||
|
int look_indent = 0;
|
||||||
|
if (!read_line_start(src, len, &look, &look_indent)) {
|
||||||
|
/* EOF: break and patch end jumps */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (look_indent != current_indent) {
|
||||||
|
/* dedent or deeper indent means no 'else' clause here */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (starts_with(src, len, look, "else")) {
|
||||||
|
/* consume 'else' */
|
||||||
|
*pos = look + 4;
|
||||||
|
skip_spaces(src, len, pos);
|
||||||
|
|
||||||
|
if (starts_with(src, len, *pos, "if")) {
|
||||||
|
/* else if -> consume 'if' token and continue loop to parse condition */
|
||||||
|
*pos += 2;
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
/* plain else: parse its block and finish the chain */
|
||||||
|
skip_to_eol(src, len, pos);
|
||||||
|
int else_indent = 0;
|
||||||
|
if (read_line_start(src, len, pos, &else_indent) && else_indent > current_indent) {
|
||||||
|
parse_block(bc, src, len, pos, else_indent);
|
||||||
|
} else {
|
||||||
|
/* empty else-body */
|
||||||
|
}
|
||||||
|
/* end of chain after else */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* next line is not an else/else if */
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
/* EOF -> empty body */
|
|
||||||
}
|
}
|
||||||
/* patch jump to here */
|
|
||||||
bytecode_set_operand(bc, jmp_false, bc->instr_count);
|
/* patch all end-of-clause jumps to the end of the chain */
|
||||||
|
for (int i = 0; i < end_count; ++i) {
|
||||||
|
bytecode_set_operand(bc, end_jumps[i], bc->instr_count);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue