aboutsummaryrefslogtreecommitdiffstats
path: root/forthmachine.h
blob: 814e957a30b2bbee323fecd501143fa593896694 (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
#ifndef OPTABLE_H
#define OPTABLE_H
#include <stdio.h>
#include <stdbool.h>
#include "stack.h"
#include "errorhandler.h"


#define OPTABLE_MAX_SIZE 1024
#define DEFINED_FUNC_MAX_LENGTH 1024
#define WORD_LEN_LIMIT 255

typedef struct optable optable;
typedef struct wordop wordop;
typedef struct forthmachine forthmachine;

typedef void (*stackop)(forthmachine* fm);
typedef void (*directiveop)(forthmachine* fm, int len, char* line, int* i);

typedef enum {
    compileditem_stackop = 0,
    compileditem_literal = 1,
    compileditem_ifcontrol = 2,
    compileditem_doloopcontrol = 3,
} compileditem_type;

typedef struct {
    compileditem_type type;
    union {
        wordop* wordop;
        stackitem literal;
        struct {
            int jumpto;
        };
        int loopbackto;
    };
} compileditem;

typedef enum {
    optype_builtin = 0,
    optype_compiled = 1,
} optype;

struct wordop {
    char* word;
    optype optype;
    union {
        stackop op;
        struct {
            compileditem* oplist;
            int oplistlen;
        };
    };
};

struct optable {
    int len;
    wordop* optable;
    errorhandler errorhandler;
};

/**
 * getop returns the first wordop in the optable which is called by the word given as a parameter
 *      if none exist, returns 0
 */
wordop* optable_getop(optable* optable, char *word);

void optable_defineop(optable* optable, char *input, int* starti);

optable* optable_new(errorhandler errorhandler);

struct forthmachine {
    optable* ot;
    stack* s;
    char* outputbuffer;
    errorhandler errorhandler;
    stack* lcs;
};

#define MAX_OUTPUT_BUFFER_SIZE 1024

forthmachine* forthmachine_new(errorhandler errorhandler);
void forthmachine_eval(forthmachine* fm, int len, char* line);

#endif //OPTABLE_H