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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdbool.h>
#ifndef STACK_H
#include "stack.h"
#endif
#ifndef OPTABLE_H
#include "optable.h"
#endif
bool isnumber(char *text) {
for(int j = strlen(text); j > 0; j--) {
if(text[j] < '0' && text[j] > '9') {
return false;
}
}
return true;
}
bool notdelim(char c) {
return c != ' ' && c != '\n' && c != '\t' && c != '\0';
}
typedef struct {
optable* ot;
stack* s;
} forthmachine;
forthmachine* forthmachine_new() {
forthmachine* fm = malloc(sizeof(forthmachine));
fm->ot = optable_new();
fm->s = stack_new();
return fm;
}
void eval(forthmachine* fm, int len, char* line);
void op_exec(wordop* op, forthmachine* fm, char *word, int len, char* line, int* i) {
switch (op->optype) {
case script:
eval(fm, op->scriptlen, op->script);
break;
case builtin:
op->op(fm->s);
break;
case directive:
op->directive(fm->s, len, line, i, fm->ot);
break;
case compiled:
for (int j = 0; j < op->oplistlen; j++) {
if (op->oplist[j].isliteral) {
stack_push(fm->s, op->oplist[j].literal);
} else {
op_exec(op->oplist[j].wordop, fm, word, len, line, i);
}
}
}
}
void exec(forthmachine* fm, char *word, int len, char* line, int* i) {
wordop* op = optable_getop(fm->ot, word);
if (op) {
op_exec(op, fm, word, len, line, i);
} else if (isnumber(word)) {
stack_push(fm->s, atoi(word));
}
}
void eval(forthmachine* fm, int len, char* line) {
char word[WORD_LEN_LIMIT];
int wordi = 0;
for (int i = 0; i < len; i++) {
if (notdelim(line[i]) && wordi < WORD_LEN_LIMIT - 1) {
word[wordi++] = line[i];
} else { // end of word
if (wordi > 0) { // don't exec an empty string
word[wordi] = '\0';
exec(fm, word, len, line, &i);
}
// start new word
wordi = 0;
}
// end of input string
if (line[i] == '\0') {
return;
}
}
}
int initialised = false;
forthmachine* fm;
int lastline = 0;
char* buffer_eval(int len, char* line) {
if (!initialised) {
fm = forthmachine_new();
initialised = true;
outputline = 0;
outputbuffer = malloc(sizeof(char) * 1024);
}
strcpy(outputbuffer, "");
eval(fm, len, line);
if (outputline) {
outputline = 0;
return outputbuffer;
} else {
return "";
}
}
void stdin_eval() {
forthmachine* fm = forthmachine_new();
char *line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, stdin)) != -1) {
eval(fm, len, line);
}
}
#ifndef __EMSCRIPTEN__
int main(int argc, char** argv) {
if (argc>1) printf("%s", buffer_eval(strlen(argv[1])+1, argv[1]));
else stdin_eval();
}
#else
#include <emscripten/emscripten.h>
#define EXTERN
EXTERN EMSCRIPTEN_KEEPALIVE char* extern_eval(char* line) {
return buffer_eval(strlen(line)+1, line);
}
#endif
|