aboutsummaryrefslogtreecommitdiffstats
path: root/forth.c
blob: 9f4e34f2d074fdce06983db0016ebb815315a3a0 (plain)
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
#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';
}

void eval(optable* ot, stack* s, int len, char* line);

void op_exec(wordop* op, optable* ot, stack *s, char *word, int len, char* line, int* i) {
    switch (op->optype) {
        case script:
            eval(ot, s, op->scriptlen, op->script);
            break;
        case builtin:
            op->op(s);
            break;
        case directive:
            op->directive(s, len, line, i, ot);
            break;
        case compiled:
            for (int j = 0; j < op->oplistlen; j++) {
                if (op->oplist[j].isliteral) {
                    stack_push(s, op->oplist[j].literal);
                } else {
                    op_exec(op->oplist[j].wordop, ot, s, word, len, line, i);
                }
            }
    }
}

void exec(optable* ot, stack *s, char *word, int len, char* line, int* i) {
    wordop* op = optable_getop(ot, word);
    if (op) {
        op_exec(op, ot, s, word, len, line, i);
    } else if (isnumber(word)) {
        stack_push(s, atoi(word));
    }
}

void eval(optable* ot, stack* s, 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(ot, s, word, len, line, &i);
            }
            // start new word
            wordi = 0;
        }
        // end of input string
        if (line[i] == '\0') {
            return;
        }
    }
}


#ifndef __EMSCRIPTEN__

int main(int argc, char** argv) {
    optable* ot = optable_new();
    stack* s = stack_new();
    char *line = NULL;
    size_t len = 0;
    ssize_t read;
    while ((read = getline(&line, &len, stdin)) != -1) {
        eval(ot, s, len, line);
    }
}

#else

#include <emscripten/emscripten.h>
int initialised = false;
optable* ot;
stack* s;
int lastline = 0;

#define EXTERN

EXTERN EMSCRIPTEN_KEEPALIVE char* extern_eval(int len, char* line) {
    if (!initialised) {
        ot = optable_new();
        s = stack_new();
        initialised = true;
        outputline = malloc(sizeof(int));
        *outputline = 0;
        // outputtobuffer = true;
    }
    eval(ot, s, len, line);
    if (*outputline != lastline) {
        lastline = *outputline;
        return outputbuffer;
    } else {
        return "";
    }

}
#endif