Nut-language
It is a simple language. It is inspired by Kamin language.
The implementation of compiler and n-code simulator (interpreter) is
taken from base2.c. The aim of this language is for teaching
computer architecture related to language. The language has elegance
and simplicity. It has only one form of syntax
(op arg1...
argn)
where op is the operator and arg1..argn are the arguments to the
operator. An operator can be built-in function or a user-defined
function. You can immediately recognise this as yet another LISP
follower. However, the domain of value is integer, although the
machine code to implement this language is a non-linear code, the
evaluation and memory model is a traditional one (has array and use
stack for evaluation). The non-scalar object is an array, called vector
in Nut-speak. It has built-in function to access and create array,
All variables are local and must be declared inside a function's
scope. An array can be allocated by the function "new".
reserved words
if while do set setv vec new sys
+ - * / = < >
The language is based on expressions. Every expression returns a
value.
Meaning of expressions
control expression
(if e1 e2 e3)
if eval(e1) is true then eval(e2) else eval(e3)
(while e1 e2)
while eval(e1) is true eval(e2) return the last eval(e2)
(do e1 ... en)
eval(e1) then eval(e2) ... eval(en) return eval(en)
(op e1 e2..en)
eval(e1)... eval(en) keep the results in the evaluation stack and then
apply op
value-expression
(set a e)
assign eval(e) to the local variable a, return eval(e)
(setv a e1 e2)
eval(e1) to be an index, eval(e2) to be a value, assign a[index]
= value
where a is a pointer to a vector (allocated by new), return value
(vec a e)
eval(e) to be an index, return a[index]
where a is a pointer to a vector (allocated by new)
(new e)
return pointer to a newly allocated chunk of memory of size eval(e)
Note: set setv vec treat the first variable name in a special
way, the name is not evaluated.
arithmetic
(bop e1 e2)
where bop are + - * / = < >
they have their usual meaning, return eval(e1) bop eval(e2)
other
(sys a e)
system call for i/o functions
a = 1 print integer eval(e)
a = 2 print character eval(e)
return eval(e)
Define function
"def" follow by the name and list of formal argument and list of local
variable and body of function.
(def not (b) () (if b 0 1)) ;;
define a not function, no local variable
(define mod (m n) ()
;; a mod function with two arguments, no local
(- m (* n (/ m n))))
Example
(set a (new 10))
;; allocate a vector of size 10 and assign to a
(vec a 3)
;; get a[3]
(setv a 2 4)
;; a[2] = 4
;; to fill an array a size k, i
is a local
(def array (a k) (i)
(do
(set i 0)
(while (< i
k)
(do
(setv a i (+ i 2)) ;; fill with i + 2
(set i (+ i 1)))))) ;; increment i
;; factorial
(def fac (n) ()
(if (= n 0 )
1
(* n (fac (- n
1)))))
;; greatest common divisor
(def gcd (m n) ()
(if (= n 0)
m
(gcd n (mod m
n))))
Realisation
The present version (0.1) does not implement * / (multiply,
divide). There must be one function, called "main", to be the
first function to be executed. The compiler reads the whole
program once through stdin and produces an object code to be executed
in a N-simulator. Because there is no global variables, large
programs will need different strategy to be implemented. However,
it is not a problem for small programs aimed for illustration purpose
in the class.
23 November 2004
P. Chongstitvatana