Nut-Compiler completion kit
You must upload the updated "nut31.zip" to be used as the
tool. The Nut-completion kit ("nut2.txt") is a
partially implemented Nut-compiler-in-nut. Your task is to add
some language features to this compiler. You are assumed to be
able to
1) Edit the compiler source
(in nut) using an appropriate editor (should have parenthesis
matching check).
2) Compile the source successfully.
3) Test the modified program, know how to debug it.
All of these have to be done from DOS window.
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, "nut2.txt", to compile the example
"t2.txt" when run it with "nsim31" it outputs "string12".
Try it on two other examples "bubblen.txt", "quick1.txt" which are
sorting programs.
You can compare the object outputted from your compiler "nut2.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").
28 June 2006