Attempt on implementing a secd virtual machine without any knowledge in virtual machines. The machine is adapted from original paper The Mechanical Evaluation of Expressions with slight changes.
The project uses nothing but c++ standard library and is built with cmake. So, simple
$ mkdir build
$ cmake -B build
$ make -C build
will be enough.
There are two targets: secd
- the machine itself, and asm
- the assembler to get machine code to execute.
For the test, a factorial program is present in the repo, it calculates the factorial of 7 recursively.
One can test it with
$ ./build/asm fact.asm fact.func
$ ./build/secd fact.func
There will be some samples of code to compile and test on the secd machine.
The factorial program is present in the repo and duplicated in the README here.
func:
LDE x
LDC 0
EQ
SEL then else
STP
then:
LDC 1
STP
else:
LDE f
LDC 1
LDE x
SUB
LDE f
AP
LDE x
MUL
STP
main:
LDF func (f x)
ST f
LDE f
LDE read
AP
LDE f
AP
LDE print
AP
STP
The machine does not have built-in list instructions, therefore they should be implemented manually
dispatch:
LDE c
LDC 0
EQ
SEL then else
STP
then:
LDE x
STP
else:
LDE y
STP
cons:
LDE x
LDE y
LDF dispatch // closure created
STP
car:
LDC 0
LDE l
AP
STP
cdr:
LDC 1
LDE l
AP
STP