Nut-Compiler completion kit
You must upload the updated "nut31.zip" for the new source "nut.txt"
and the updated "nut31" (fixed bug in char token[80]).
To add "let" "enum" and "string" you must define the following
functions: doGvar, doEnum, doString and parseEnum. (I have
defined parseLet for your example)
in parseName
doGvar
op arg ; is similar to doVar
; the mapping is
op = SET
makeAtom ST.arg
op = SETV makeAtom
STY.arg
op = VEC
makeAtom LDY.arg
return atom
doEnum n
return makeAtom LIT.n
doString s
return makeAtom STR mkSTR
s
mkSTR allocate string space from DS (mem[]) and copy string s to
there. What tok looks like? It is a string prefixed with
". That is how tokeniser scan a token. Therefore doString
is passed with (+ tok 1) which mean it skips the first ".
parseEnum
tokenise
; to get the starting number
k = atoi tok
tokenise ; scan each token
until ")"
while tok string-not-eq
")"
update symbol
table with tok, tyEnum, val k
k = k + 1
tokenise
to update symbol table
you must install tok to get index (handle)
(set idx (install tok))
The user code segment (the n-code that being generated) resides at
"start" (which is instantiated by "sys 9" asking the underlying virtual
machine where "heapFree" is). At the end, to output object, you have to
relocate code segment to start at 2. To relocate code segment two
functions are defined:
1) reloc, to rename every call to the new address.
2) shift, to relocate dot-pair to the new address.
This is done in "outobj2". It also output the data segment. The
object will be accept by "nsim31" properly. Read "outobj" and
"outobj2" to see what has been changed.
Try to use this nut-compiler, "nut.txt", to compile the example
"t2.txt" when run output "string12".
Try it on two other examples "bubblen.txt", "quick1.txt" which are
sorting programs. Redirect the output to x.obj then run it by
editing the header and copy it to a.obj and use "nsim31" to execute it.
You can compare the object outputted from your "nut.txt" with the
correct one outputted from "nut31", "t2.obj". It must be the same,
except the location in symbol table (which is not used in running the
program by "nsim31").
End
4 July 2006