Lot of example directory structure refactoring. (0.39.9).
This commit is contained in:
parent
564999709c
commit
e240768c26
13 changed files with 20 additions and 21 deletions
22
examples/basics/boolean_decl.fun
Normal file
22
examples/basics/boolean_decl.fun
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
boolean b = false
|
||||
print("b => " + to_string(b))
|
||||
/* Try typeof via built-in function if available */
|
||||
print("typeof(b) => " + typeof(b))
|
||||
|
||||
/* Expected output:
|
||||
b => false
|
||||
typeof(b) => Boolean
|
||||
*/
|
||||
75
examples/basics/booleans.fun
Normal file
75
examples/basics/booleans.fun
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
// Boolean datatype usage examples (first-class booleans)
|
||||
// Run like:
|
||||
// FUN_LIB_DIR="$(pwd)/lib" ./build/fun examples/basics/booleans.fun
|
||||
|
||||
// Literals
|
||||
boolean t = true
|
||||
print("typeof(t) => " + typeof(t))
|
||||
boolean f = false
|
||||
print("typeof(f) => " + typeof(f))
|
||||
|
||||
print("t literal => " + to_string(t)) // "true"
|
||||
print("f literal => " + to_string(f)) // "false"
|
||||
|
||||
// Logical operators (short-circuit semantics)
|
||||
print("true && true => " + to_string(true && true))
|
||||
print("true && false => " + to_string(true && false))
|
||||
print("false || true => " + to_string(false || true))
|
||||
print("false || false => " + to_string(false || false))
|
||||
print("!true => " + to_string(!true))
|
||||
print("!false => " + to_string(!false))
|
||||
|
||||
// Equality and inequality
|
||||
print("true == true => " + to_string(true == true))
|
||||
print("true != false => " + to_string(true != false))
|
||||
|
||||
// Interoperability with 0/1 (still supported)
|
||||
print("true == 1 => " + to_string(true == 1))
|
||||
print("false == 0 => " + to_string(false == 0))
|
||||
print("1 == true => " + to_string(1 == true))
|
||||
print("0 != true => " + to_string(0 != true))
|
||||
|
||||
// Use in conditionals
|
||||
if t
|
||||
print("if t: branch taken")
|
||||
else
|
||||
print("if t: branch NOT taken")
|
||||
|
||||
if f
|
||||
print("if f: branch taken (unexpected)")
|
||||
else
|
||||
print("if f: else branch taken")
|
||||
|
||||
/* Expected output:
|
||||
typeof(t) => Boolean
|
||||
typeof(f) => Boolean
|
||||
t literal => true
|
||||
f literal => false
|
||||
true && true => true
|
||||
true && false => false
|
||||
false || true => 1
|
||||
false || false => 0
|
||||
!true => false
|
||||
!false => true
|
||||
true == true => true
|
||||
true != false => true
|
||||
true == 1 => true
|
||||
false == 0 => true
|
||||
1 == true => true
|
||||
0 != true => true
|
||||
if t: branch taken
|
||||
if f: else branch taken
|
||||
*/
|
||||
56
examples/basics/builtins_conversions.fun
Normal file
56
examples/basics/builtins_conversions.fun
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*/
|
||||
|
||||
/*
|
||||
* Built-in Type Conversions
|
||||
*
|
||||
* Shows how FunScript handles:
|
||||
* - Implicit type conversions (numeric, string)
|
||||
* - Explicit casting operations
|
||||
* - Conversion functions and operators
|
||||
* - Handling of special cases in conversions
|
||||
*/
|
||||
|
||||
// Conversions and length built-ins demo
|
||||
|
||||
// len on array and string
|
||||
print(len([1, 2, 3])) // -> 3
|
||||
print(len("hello")) // -> 5
|
||||
|
||||
// to_number with various string inputs
|
||||
print(to_number("42")) // -> 42
|
||||
print(to_number(" -7 ")) // -> -7
|
||||
print(to_number("12a")) // non-numeric suffix -> 0
|
||||
print(to_number("")) // empty -> 0
|
||||
print(to_number(" ")) // spaces -> 0
|
||||
|
||||
// to_string on numbers and arrays
|
||||
print(to_string(42)) // -> "42" (printed as 42)
|
||||
|
||||
arr = [1, 2]
|
||||
s = to_string(arr) // -> "[array n=2]"
|
||||
print(s)
|
||||
|
||||
// combine to_string with concatenation
|
||||
print("val=" + to_string(99)) // -> "val=99"
|
||||
|
||||
/* Expected output:
|
||||
3
|
||||
5
|
||||
42
|
||||
-7
|
||||
0
|
||||
0
|
||||
0
|
||||
42
|
||||
[array n=2]
|
||||
val=99
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue