mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 20:05:31 +00:00
Added samples for Culebra toy language.
This commit is contained in:
parent
341659f699
commit
d93682dc56
17
language/samples/closure.cul
Normal file
17
language/samples/closure.cul
Normal file
@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Closure test
|
||||
*/
|
||||
|
||||
make_func = fn (mut x) {
|
||||
mut n = 100
|
||||
fn () {
|
||||
n = n + 1
|
||||
x = x + 1 + n
|
||||
}
|
||||
}
|
||||
|
||||
f = make_func(10)
|
||||
|
||||
pp("1: { f() }")
|
||||
pp("2: { f() }")
|
||||
pp("3: { f() }")
|
17
language/samples/fib.cul
Normal file
17
language/samples/fib.cul
Normal file
@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Fibonacci
|
||||
*/
|
||||
|
||||
fib = fn (x) {
|
||||
if x < 2 {
|
||||
x
|
||||
} else {
|
||||
self(x - 2) + self(x -1)
|
||||
}
|
||||
}
|
||||
|
||||
mut i = 0
|
||||
while i < 30 {
|
||||
pp("{i}: {fib(i)}")
|
||||
i = i + 1
|
||||
}
|
17
language/samples/fizzbuzz.cul
Normal file
17
language/samples/fizzbuzz.cul
Normal file
@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Fizz Buzz
|
||||
*/
|
||||
|
||||
mut i = 1
|
||||
while i < 24 {
|
||||
if i % 15 == 0 {
|
||||
pp('FizzBuzz')
|
||||
} else if i % 5 == 0 {
|
||||
pp('Buzz')
|
||||
} else if i % 3 == 0 {
|
||||
pp('Fizz')
|
||||
} else {
|
||||
pp(i)
|
||||
}
|
||||
i = i + 1
|
||||
}
|
Loading…
Reference in New Issue
Block a user