Skip to content

Reference Manual

Jakub T. Jankiewicz edited this page May 25, 2022 · 46 revisions

Gaiman Programming Language Reference Manual

Commands

  • echo - print argument on a terminal
  • ask - print prefix and get input from the user
  • get - send GET HTTP request and return JSON, HTML, XML or Text depending on content-type of the response
  • post - send POST HTTP request similar to get
  • config - configure the terminal
  • prompt - change the prompt (prefix that can be changed by using backspace)
  • input - enter text into a command like if user type it
  • sleep - pause the interpreter with given milliseconds

Extra Commands

  • sleep* - pause the interpreter but don't hide the cursor
  • input* - type into the command line with animation
  • ask* - get input from a user, the prefix is printed with typing animation
  • echo* - prints anything on the terminal with typing animation
  • prompt* - change the prompt with typing animation

Operator precedence

Operator Description
[] array and object access
() parentheses grouping
* / % Multiplication, division, modulo
+ - Addition (or concatenation), subtraction
< > <= >= boolean compare
== != equal, not equal
and Boolean and
or Boolean or
not Boolean not

Functions

# named function
def square(x)
   return x * x
end

# anonymous function expression
let square = lambda(x)
   return x * x
end

Functions supports splat operator:

def sum(*args)
   let result = 0
   for i in args do
       result += i
   end
   return result
end

echo sum(1,2,3,4,5)
echo sum(*[1,2,3,4])

Variables

let x = 10
let y = "hello, world"

Numbers

Gaiman supports standard form of numbers:

  • integers
  • hexadecimal 0x100
  • octal 0777
  • binary 0b11000111010101
  • floating-point numbers 0.1e-2 or 0.1e10

Arrays

let x = [1,2,3,4]

it supports methods like in JavaScript map/reduce/filter/forEach/some/every/find/sort.

let x = [1,2,3,4]

while x.length do
    echo x.pop()
end

Dictionaries

let x = {"foo" => "Hello", "bar" => "world"}

Strings

Strings allow for variable interpolation. Strings only support double-quotes.

let name = "John"
echo "Hello $name"

string methods have methods like in JavaScript e.g. replace/search/toLowerCase/toUpperCase

here docs

let x = <<<MSG
Hello
this
is
string
MSG

Regular expressions

let x = /foo|bar/g

if statements

Parentheses around conditional are optional:

let name = ask "? "
if name == "foo" then
  echo "this is FOO"
end
let name = ask "? "
if name == "foo" then
   echo "is foo"
else if name == bar then
   echo "is bar"
else
   echo "is something else"
end

if name == "foo" then
  echo "this is FOO"
end

Loops

for loop

Over array:

for i in [1, 2, 3, 4] do
   echo i
end

Over key, value pair (array with index or dictionary)

let array = ["hello", "world"]
for key, value in array do
   echo "$index => $value"
end

let dict = {"foo" => "hello", "bar" => "world"}
for key, value in dict do
   echo "$key => $value"
end

while loop

Parentheses around conditional are optional:

while true do
  let cmd = ask ">>> "
  echo cmd
end

If you don't put async command like ask inside while true loop, you may create infinite loop. But if something like this happen the application will detect that and stop the program.

You can use config command to change the guard infinite loop. To turn of the infinite loop protection use:

config {"loop_threshold" => infinity, "loop_timeout" => infinity}

You can also turn off echo newline at the end of the text, and make it explicit:

config {"newline" => false}
echo "this"
echo " is"
echo " the"
echo " text\n"

This will print "this is the text" in a single line.

regex match

let cmd = ask "? "
if cmd =~ /echo (.+)/ then
    echo $1
end

Each group in regex has $ variable defined.

Clone this wiki locally