2021-06-03 04:44:01 +08:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
# This is a comment.
|
|
|
|
|
|
|
|
x = 0 # Creating a variable.
|
|
|
|
|
|
|
|
# In pocketlang statements should end with a new line
|
2021-06-15 15:37:49 +08:00
|
|
|
# or a semicolon. White space characters except for new
|
2021-06-03 04:44:01 +08:00
|
|
|
# lines are ignored in pocketlang.
|
|
|
|
a = 1; b = 2;
|
|
|
|
|
|
|
|
# Data types.
|
|
|
|
# -----------
|
|
|
|
|
|
|
|
null # A null type.
|
|
|
|
true; false # Booleans.
|
|
|
|
42; 3.14 # Numbers.
|
|
|
|
0..10; 10..0 # Range (0..10 = 0 <= r < 10).
|
|
|
|
"hello"; 'world' # Strings (support multiline).
|
|
|
|
[42, 'foo', null] # Lists.
|
|
|
|
{ 'Key':'value' } # Maps.
|
|
|
|
func(x) return x*x end # Lambda/literal functions.
|
|
|
|
|
|
|
|
# Control flow.
|
|
|
|
# -------------
|
|
|
|
|
|
|
|
# If condition.
|
|
|
|
if x == 'foo'
|
2022-04-04 11:17:15 +08:00
|
|
|
print('bar')
|
2021-06-16 16:14:35 +08:00
|
|
|
elsif x == 'bar'
|
2022-04-04 11:17:15 +08:00
|
|
|
print('baz')
|
2021-06-03 04:44:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# In a single line (should add 'then' keyword).
|
|
|
|
if x == 'foo' then print('bar') end
|
|
|
|
|
|
|
|
# For loops, here 'do' keyword is optional if we have a
|
|
|
|
# newline after the sequence (like 'then' in if statements).
|
|
|
|
for i in 0..10 do
|
2022-04-04 11:17:15 +08:00
|
|
|
print(i)
|
2021-06-03 04:44:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# While statement.
|
|
|
|
while x > 0 do print(x -= 1) end
|
|
|
|
|
|
|
|
# In pocketlang variable's lifetime are scope based.
|
|
|
|
if true then
|
2022-04-04 11:17:15 +08:00
|
|
|
local = null
|
2021-06-03 04:44:01 +08:00
|
|
|
end
|
|
|
|
#print(local) # Error: Name 'local' is not defined.
|
|
|
|
|
|
|
|
# Functions.
|
|
|
|
#-----------
|
|
|
|
|
|
|
|
def add(a, b)
|
2022-04-04 11:17:15 +08:00
|
|
|
return a + b
|
2021-06-03 04:44:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Functions can be assigned to a variable.
|
|
|
|
fn = func(x) return x*x end
|
|
|
|
|
|
|
|
# Functions can be passed as an argument and can be returned.
|
|
|
|
def call(fn, x)
|
2022-04-04 11:17:15 +08:00
|
|
|
fn(x)
|
|
|
|
return func print('foo') end
|
2021-06-03 04:44:01 +08:00
|
|
|
end
|
|
|
|
|
2022-04-04 11:17:15 +08:00
|
|
|
# Classes
|
|
|
|
#--------
|
2021-06-03 04:44:01 +08:00
|
|
|
|
2021-06-23 13:21:18 +08:00
|
|
|
class _Vector
|
|
|
|
x = 0; y = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def Vector(x, y)
|
|
|
|
ret = _Vector()
|
|
|
|
ret.x = x; ret.y = y
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
def vecAdd(v1, v2)
|
|
|
|
return Vector(v1.x + v2.x,
|
|
|
|
v1.y + v2.y)
|
|
|
|
end
|
2021-06-03 04:44:01 +08:00
|
|
|
|
2021-06-23 13:21:18 +08:00
|
|
|
v1 = Vector(1, 2)
|
|
|
|
v2 = Vector(3, 4)
|
|
|
|
v3 = vecAdd(v1, v2)
|
|
|
|
print(v3) # [_Vector: x=4, y=6]
|
2021-06-03 04:44:01 +08:00
|
|
|
|
2021-06-06 22:18:47 +08:00
|
|
|
# Fibers & Coroutine
|
|
|
|
#-------------------
|
|
|
|
|
2021-06-23 12:18:15 +08:00
|
|
|
import Fiber
|
|
|
|
|
2021-06-06 22:18:47 +08:00
|
|
|
def fn(p1, p2)
|
2022-04-04 11:17:15 +08:00
|
|
|
print(yield(42)) # Prints 3.14
|
2021-06-06 22:18:47 +08:00
|
|
|
end
|
|
|
|
|
2021-06-23 12:18:15 +08:00
|
|
|
fb = Fiber.new(fn)
|
|
|
|
val = Fiber.run(fb, 1, 2)
|
2021-06-06 22:18:47 +08:00
|
|
|
print(val) ## Prints 42
|
2021-06-23 12:18:15 +08:00
|
|
|
Fiber.resume(fb, 3.14)
|
2021-06-06 22:18:47 +08:00
|
|
|
```
|