-
Notifications
You must be signed in to change notification settings - Fork 2
/
8inf.c
117 lines (95 loc) · 2.96 KB
/
8inf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lexer_preprocessor.h"
#include "handlers.h"
#include "rts.h"
char **program;
rts_t *stack;
// helper function that prints out the stack
void stackdump(rts_t *st) {
for (int i = st->top - 1; i >= 0; i--) {
word_t wd = st->buf[i];
if (wd.type==TYPE_STR) printf("%i: %s\n", i, wd.strval);
else printf("%i: %i\n", i, wd.intval);
}
}
// helper function that prints out the program
void programdump(char **prog) {
for (char **pc = prog; *pc != NULL; pc++) {
printf("program[%i]: %s\n", (int) (pc - program), *pc);
}
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("no program supplied\n");
return 0;
}
// program is an array of strings; the end of the program is signified by a
// NULL pointer.
program = load_program(argv[1]);
// prints out the words in the program array; uncomment to get an idea of how
// the program is stored in memory.
// programdump(program);
stack = stack_init();
char **pc = program;
for (char **pc = program; *pc != NULL; pc++) {
// word is a string:
if (*pc[0] == '~') {
size_t len = strlen(*pc);
word_t tmp = {.type=TYPE_STR, .strval=calloc(len, sizeof(char))};
memcpy(tmp.strval, *pc + 1, len-2);
push(tmp, stack);
// word is an operation:
} else if (*pc[0] == '.') {
// i/o ops:
if (strcmp(*pc, ".print")==0)
print(stack);
else if (strcmp(*pc, ".newline")==0)
newline(stack);
// integer ops:
else if (strcmp(*pc, ".+")==0)
integer_operator(stack, ADD);
else if (strcmp(*pc, ".-")==0)
integer_operator(stack, SUB);
else if (strcmp(*pc, ".*")==0)
integer_operator(stack, MUL);
else if (strcmp(*pc, "./")==0)
integer_operator(stack, DIV);
else if (strcmp(*pc, ".mod")==0)
integer_operator(stack, MOD);
else if (strcmp(*pc, ".=?")==0)
integer_operator(stack, CMP);
else if (strcmp(*pc, ".>?")==0)
integer_operator(stack, GTH);
// stack ops
else if (strcmp(*pc, ".dup")==0)
duplicate(stack);
else if (strcmp(*pc, ".swap")==0)
swap(stack);
// control flow ops must be handled where we have access to the program
// counter
else if (strcmp(*pc, ".cjump")==0) {
word_t jumpto = pop(stack);
word_t check = pop(stack);
// -1 since we know the pc will increment at end of this for loop
if (check.intval) pc = pc + jumpto.intval - 1;
}
// op not supported:
else {
printf("unrecognizable word: %s\n", *pc); return -1;
}
// word is an int or unrecognized:
} else {
char *endptr;
int z = strtol(*pc, &endptr, 10);
if (*endptr != '\0') {
printf("unrecognizable word: %s\n", *pc); return -1;
} else {
word_t tmp = {.type=TYPE_INT, .intval=z };
push(tmp, stack);
}
}
}
return 0;
}