Interpreter for a simple language, build with Alex and Happy.
Use
stack exec pig
to start interpreterstack install
to installpig
executable on your machine
--version
show version of interpreter--help
: show options with short description-l|--load FILE
: start interpreter withFILE
loaded
Command line arguments for scripts are stored in a list of strings named args
. To distinguish script arguments from intepreter's arguments use --
, for example with command pig -l script.pig -- help
string help
will be available in the script as args(0)
.
PiG interpreter can also be used with shebang with #!<path>/<to>/<pig> -l
, see example.
In PiG, everything is an expression:
- literal values:
null
true
/false
- numbers (integral and real)
- characters like
'a'
and strings like"abcd"
- functions (lambdas) in form
(<arg1>,...,<argn>) => <sequence>
. Return value of a function is either the value of its last expression or value explicitly marked with thereturn
keyword. - lists in form
[<expr1>,...,<exprn>]
- dictionaries (maps) in form
[<key1>: <value1>,...,<keyn>: <valuen]
- assignments:
<name> = <expr>
, where name consists of alphanumeric characters,<name>(<number>) = <expr>
to assign value to a specific element of a list, or<name>(<expr>) = <expr>
to add key-value pair to a dictionary. Value of assignment is the value of expression on the right. - sequence of expressions, separated and optionally ended by
;
. When sequence is enclosed by braces ({...}
), it is treated as a single expression (sequences can be nested, like{ <expr1>; { <expr2>; <expr3> }; }
). Value of a sequence is the value of the last expression in it. - while loop:
while <expr1> : <expr2>
. Both<expr1>
and<expr2>
must be a single expressions, but they don't have to be enclosed in braces.<expr2>
will be executed as long as<expr1>
is atrue
, non-zero number, non-empty list or a non-empty string. Value of a while expression is a list of values of<expr2>
(for example value ofx = 3; while x > 0 : x = x - 1
is[2,1,0]
). - if:
if <expr> : <expr> elif <expr> : <expr> ... else : <expr>
(elif
andelse
are optional). Value of "if" is the value of expression after ifrst condition evaluated totrue
, non-zero number, non-empty list or a non-empty string, or value of expresion afterelse
(null
if noelse
is specified). - a variable name, as in assignment. If the variable is bound to a list,
<name>(<num1>,...,<numn>)
syntax can be used to get the value at specified index or a list of them. - function application, in form
<name>(<arg1>,...,<argn>)
. If the<name>
is a list, arguments that can be evaluated to numbers will be rounded to integers and treated as zero-based indices, and the return value will be the value associated with that index or a list of values in case of more than one indices (for example a value ofl = [1,2,3]; l(0)
is1
, value ofl = [1,2,3]; l(0,2)
is[1,3]
). If the<name>
is a dictionary, the return value will be the value associated with the argument or a list of values in case of more than one argument (for example a value ofm = ["a":1,"b":3]; m("a")
is1
, value ofm = ["a":1,"b":3]; m("a","b")
is[1,3]
). - expressions with build-in keywords and operators
Build-in functions provided:
print(arg1,...,argn)
prints all its arguments to stdoutread()
reads string from stdinopen(<file path>[, <mode string>])
opens file and returns its handleclose(<handle>)
closes filereadFile(<handle>)
reads line form filewriteFile(<hande>,arg1,...,argn)
writes all its arguments except handle to filestrToNum(arg)
parses number from stringstrToList(arg)
turns string to a list of charslistToStr(arg)
turns a list of chars to stringlength(arg)
returns the length of a list, map or stringisNum(arg)
checks ifarg
is a numberisBool(arg)
checks ifarg
is a booleanisList(arg)
checks ifarg
is a listisStr(arg)
checks ifarg
is a stringisFun(arg)
checks ifarg
is a functionexit()
closes interpreter
Keyword load "<file name>"
executes code from other files.
Build-in operators are:
+
,-
,*
,/
,^
and%
(modulo) for numbers-
,||
,&&
for booleans<>
,-<
,>-
for lists (>-
removes and returns first element,-<
the last one,<>
concatenates second argument to the end of the first one, works with any type but results always in a list)><
for strings (attaches second argument at the end of the firs one, works with any type but results always in a string)
Interpreter also provides four directives:
:exit | :e
(or Ctrl+d) to leave the interpreter:rm <variable name>
to remove binding of a variable:clear | :c
to remove all bindings:help | :h
to display information about directives
See examples for more info.