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
|
||||
|
||||
// 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
|
||||
number a = 2
|
||||
|
|
@ -13,7 +13,10 @@ boolean flag = true
|
|||
|
||||
// Arithmetic and precedence
|
||||
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
|
||||
number neg = -n
|
||||
|
|
@ -31,10 +34,8 @@ print(true || false) // expect 1 (true)
|
|||
print(n > 5) // expect 1
|
||||
print(s != "") // expect 1
|
||||
|
||||
// Indentation-based nested if blocks (two spaces)
|
||||
if (n >= 10)
|
||||
print(n) // expect 10
|
||||
if (a + b == 5)
|
||||
print(42) // expect 42
|
||||
// Mixed logical/relational
|
||||
print(a < b && n >= 10) // expect 1
|
||||
print((a + b) == 5 && flag) // expect 1
|
||||
|
||||
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 ===")
|
||||
Loading…
Add table
Add a link
Reference in a new issue