Skip to content

Commit 1690d47

Browse files
committed
vm backend stable
1 parent b7f0b0c commit 1690d47

37 files changed

+2747
-525
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
a.out
1+
compile-exe
2+
execute-exe
23
*.bin
3-
execute
4-
compile

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
all: compile-exe execute-exe
2+
3+
compile-exe: compile.c compile-exe.c num.c vm_backend.c
4+
gcc -Wall -fsanitize=address -g compile.c compile-exe.c num.c vm_backend.c -o compile-exe
5+
6+
execute-exe: execute.c execute-exe.c num.c vm_engine.c
7+
gcc -m32 -Wall -fsanitize=address -g execute.c execute-exe.c num.c vm_engine.c -o execute-exe
8+
9+
test-simple: all
10+
find forth_programs/simple -maxdepth 1 -type f | xargs -I{} ./compile-and-run.sh {}
11+
12+
clean:
13+
rm -f compile-exe execute-exe *.bin

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
`gcc-multilib` needs to be installed to compile `execute-exe` as 32 bit.
2+
3+
```sh
4+
make
5+
make test-simple
6+
```
7+
8+
How to run `life.fs` and the hash_xors
9+
10+
```sh
11+
./compile-and-run.sh forth_programs/life/life.fs < forth_programs/life/starting_board.txt
12+
./compile-and-run.sh forth_programs/hash_xor/hash_xor.fs < forth_programs/hash_xor/hash_input.txt
13+
```

compile-and-run.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
BASENAME=$(basename $1 .fs)
4+
BINNAME="${BASENAME}.bin"
5+
echo $1
6+
./compile-exe vm $1 $BINNAME
7+
./execute-exe vm $BINNAME
8+
echo

compile-exe.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <fcntl.h>
2+
#include <unistd.h>
3+
#include "mcp_forth.h"
4+
5+
int main(int argc, char ** argv)
6+
{
7+
assert(argc == 4);
8+
9+
const mcp_forth_backend_t * backend;
10+
if(0 == strcmp("vm", argv[1])) {
11+
backend = &compact_bytecode_vm_backend;
12+
} else {
13+
assert(0);
14+
}
15+
16+
int fd;
17+
int res;
18+
19+
fd = open(argv[2], O_RDONLY);
20+
assert(fd != -1);
21+
int source_len = lseek(fd, 0, SEEK_END);
22+
assert(source_len != -1);
23+
res = lseek(fd, 0, SEEK_SET);
24+
assert(res != -1);
25+
26+
char * source = malloc(source_len);
27+
assert(source);
28+
29+
res = read(fd, source, source_len);
30+
assert(res == source_len);
31+
32+
res = close(fd);
33+
assert(res != -1);
34+
35+
uint8_t * bin;
36+
int error_near = -1;
37+
int bin_len = mcp_forth_compile(source, source_len, &bin, backend, &error_near);
38+
free(source);
39+
if(bin_len < 0) {
40+
printf("error %d near %d\n", bin_len, error_near);
41+
return 1;
42+
}
43+
44+
fd = open(argv[3], O_WRONLY | O_CREAT | O_TRUNC, 0664);
45+
assert(fd != -1);
46+
res = write(fd, bin, bin_len);
47+
assert(res == bin_len);
48+
free(bin);
49+
50+
res = close(fd);
51+
assert(res != -1);
52+
}

0 commit comments

Comments
 (0)