This is an interpreter for a interpreted language written in JAVA. The interpreted language is called MUA(MakeUp Programming Language). The grammar of this language can be found below.
It support function defining, function calling, loop, recursive procedures, etc.
Try it by yourself! Also, Please do not borrow the code directly for course projects if your mentor does not allow you to do so. Thanks: )
1.Some basic operations
2.Define a function named f
with argument a
. Multi-line definition is support, which means you can
define a function in multiple lines and the interpreter will try to match the brankets.
3.A more complex function. The infix expression is also supported.
4.Argument for functions can be result of an infix expression. Negative numbers supported also.
5.Double numbers and infix expression.
6.Repeat
7.Some other operations.
number, word, list, bool
-
content of a number starts with
[0~9]
or-
-
content of a word starts with
"
. Anything following"
before a space will be considered as part of the word, including"
and[]
-
content of a list is wrapped with
[]
, the elements inside are seperated by space. The elements inside can be any data type and can be different.
format: op_name
arguments
Different arguments are seperated by spaces.
fundamental op:
-
//
:comment -
make <word> <value>
: bind thevalue
to theword
. Theword
is then calledname
and located in thenamespace
. -
thing <word>
:return the value bound withword
-
:<word>
:the same asthing
-
erase <word>
:erase the value bound withword
-
isname <word>
:ifword
is aname
-
print <value>
:print value -
read
:return a word or list read from stdin -
readlinst
:return a line read from stdin and form a list -
operator
-
add
,sub
,mul
,div
,mod
:<operator> <number> <number>
-
eq
,gt
,lt
:<operator> <number|word> <number|word>
-
and
,or
:<operator> <bool> <bool>
-
not
:not <bool>
-
-
random <number>
:return a random number between [0,number> -
sqrt <number>
:return sqrt -
isnumber <value>
:return bool indicating whethervalue
is a number -
isword <value>
:return bool indicating whethervalue
is a word -
islist <value>
:return bool indicating whethervalue
is a list -
isbool <value>
:return bool indicating whethervalue
is a bool -
isempty <word|list>
: return bool indicating whetherword|list
is empty -
test <value>
:test whethervalue
is true or false, no return value -
iftrue <list>
:if thetest
before is true, do the list -
iffalse <list>
:if thetest
before is false, do the list -
word <word> <word|number|bool>
:merge twoword
into one. The second value can beword
,number
, orbool
-
list <list1> <list2>
:mergelist1
andlist2
in to one list, elements inlist2
following those inlist1
-
join <list> <value>
:add the [value] into the [list] as the last element -
first <word|list>
:return the first character/element of word/list -
last <word|list>
:return the last character/element of word/list -
butfirst <word|list>
:return a list/word excepting the last element/character -
butlast <word|list>
:return a list/word excepting the last element/character -
item <number> <word|list>
:return the [number] character/element in a word/list -
repeat <number> <list>
:run the code in [list] for [number] times -
stop
:stop the execution of current code fragment(may be a piece of code in run/repeat/if/function) -
wait <number>
:wait fornumber
ms -
save <word>
:save the current namespace to [word]file -
load <word>
:load contents from [word]file and add to the namespace -
erall
:erase all thename
s in current namespace -
poall
:print all thename
s in current namespace
make <word> [<list1> <list2>]
word is the function name
list1 is the arguments list, list2 is the operation list
<functionName> <arglist>
functionName is the function name defined in make
. "
is not needed.
arglist is the arguments list, values in arglist
are correspondingly bound to the name
in function definition.
-
output <value>
:setvalue
as the return value to the caller. But do not stop execution of function. -
stop
:stop execution -
local <word>
: set theword
as local name. The arguments are also local names.
The language provide some frequent use constants and operations as built-in name. These names can be erased also.
-
pi
:3.14159 -
if <bool> <list1> <list2>
:ifbool
is true, runlist1
, elselist2
. Both lists can be empty -
run <list>
:run the code in thelist