-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.h
More file actions
169 lines (144 loc) · 5.09 KB
/
Parser.h
File metadata and controls
169 lines (144 loc) · 5.09 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef Parser_h
#define Parser_h
#include "Arduino.h"
#include <eeprom.h>
//#define DEBUG_TOKENIZER
//#define DEBUG_VARS
//#define DEBUG_PARSER
//#define DEBUG_ROM_OPS
//#define DEBUG_RUNTIME
enum MemValueMarker {
memv_Integer1Byte = 0,
memv_Integer2Byte = 1,
memv_Integer3Byte = 2,
memv_Integer4Byte = 3,
memv_Integer5Byte = 4,
memv_Integer6Byte = 5,
memv_Integer7Byte = 6,
memv_Integer8Byte = 7,
memv_Double = 8,
memv_VarAddress = 9,
memv_End = 10
};
enum ExpressionReturnType {
Invalid,
Byte,
Integer,
Long,
Double,
Variable,
Void,
TokenierOverflow,
StackOverFlow
};
enum Symbols {
_WS, _EOF, _ERROR, _NUMBER, _ID, _MULTIPLY, _DIVIDE, _ADD, _SUBTRACT, _LEFT_PARS, _RIGHT_PARS,
_EQUAL, _BEGIN, _END, _PROG, _EXEC, _IF, _THEN, _ELSE, _WHILE, _FOR, _DO, _TO, _STEP,
_FX, _COLON, _NOT, _AND, _OR, _XOR, _NAND, _NOR, _XNOR, _EQ, _NEQ, _GT, _GTEQ, _ST, _STEQ, _POW,
_COMMA, _XSymbolsEnd
};
enum OpCodes {
PUSH, PUSHV, POP, POPV,
ADD, SUBTR, MULT, DIV, NEG, NOT, AND, OR, XOR, NAND, NOR, XNOR,
EQ, NEQ, GT, GTEQ, ST, STEQ, STORE, GOSUB, RET, POW, ENDPROG
};
struct tokenDef {
char Def[15];
Symbols Type;
int Col;
};
enum memValueType { memValueLong, memValueDouble };
struct varDef {
char Def[15];
memValueType Type;
byte Value[4];
varDef *next;
};
struct fxDef {
char Def[15];
memValueType Type;
uint8_t numParams;
bool isBuiltin;
void* Fx;
fxDef *next;
};
struct memValue {
memValueType Type;
uint8_t Size;
byte *Value;
};
typedef memValue* (*funcptr_0)();
typedef memValue* (*funcptr_1)(memValue*);
typedef memValue* (*funcptr_2)(memValue*, memValue*);
typedef memValue* (*funcptr_3)(memValue*, memValue*, memValue*);
typedef memValue* (*funcptr_4)(memValue*, memValue*, memValue*, memValue*);
typedef memValue* (*funcptr_5)(memValue*, memValue*, memValue*, memValue*, memValue*);
class Parser {
public:
Parser ();
void SendHeader();
void SendPrompt();
bool parse(const char * code);
bool testTokenizer(const char * code);
uint16_t calculatedStackSize;
uint16_t calculatedHeapSize;
void run(uint16_t address);
fxDef* addBuiltinFunction(const char * name, funcptr_0 pointer);
fxDef* addBuiltinFunction(const char * name, funcptr_1 pointer);
fxDef* addBuiltinFunction(const char * name, funcptr_2 pointer);
fxDef* addBuiltinFunction(const char * name, funcptr_3 pointer);
fxDef* addBuiltinFunction(const char * name, funcptr_4 pointer);
fxDef* addBuiltinFunction(const char * name, funcptr_5 pointer);
double GetDouble(memValue *value);
long GetLong(memValue *value);
void PutLong(memValue *value, long num);
void PutDouble(memValue *value, double num);
bool IsDouble(memValue *value);
bool IsLong(memValue *value);
private:
bool isWhiteSpace(char* token);
bool isIdentifier(char* token);
bool isNumber(char* token);
bool stringIsInList(char* s, int start, const char* list);
bool charIsInList(char c, const char* list);
bool stringIsSymbol(char* s, const char* symbol);
bool beginTokenizerTran();
void commitTokenizerTran();
void rollbackTokenizerTran();
void getNextToken(tokenDef * ret);
void rawGetNextToken(const char * code, tokenDef* ret);
void calculateMemSizes(uint16_t stackSize, uint16_t heapSize);
ExpressionReturnType parseProgram(uint16_t * stackSize);
ExpressionReturnType parseAssignExpression(uint16_t * stackSize);
ExpressionReturnType parseAddExpression(uint16_t * stackSize);
ExpressionReturnType parseMultExpression(uint16_t * stackSize);
ExpressionReturnType parseBaseExpression(uint16_t * stackSize);
ExpressionReturnType parseNextMultExpression(Symbols operationInsert, ExpressionReturnType prevRetType, uint16_t * stackSize);
ExpressionReturnType parseValue(uint16_t * stackSize);
ExpressionReturnType parseParameters(uint16_t * stackSize);
fxDef* addBuiltinFunction(const char * name, void *pointer, uint8_t numParameters);
varDef* defineVariable(char * var, memValueType type);
varDef* getVarAddress(char * var);
fxDef* getFunctionAddress(char * var);
uint16_t evaluateFunction(fxDef* fxPointer, byte *stack, memValue *retValue, uint16_t stackPointer, uint16_t stackSize);
uint16_t writeToRom(long value, uint16_t address);
uint16_t writeToRom(double value, uint16_t address);
uint16_t writePointerToRom(fxDef *pointer, uint16_t address);
uint16_t writePointerToRom(varDef *pointer, uint16_t address);
uint16_t readPointerFromRom(uint16_t address, varDef **pointer);
uint16_t readPointerFromRom(uint16_t address, fxDef **pointer);
uint16_t writeByteToROM(byte b, uint16_t address);
uint16_t readFromROM(uint16_t address, memValue *value);
byte readByteFromROM(uint16_t address);
uint16_t pushValueIntoStack(memValue *value, byte *stack, uint16_t stackPointer, uint16_t stackSize);
uint16_t popFromStack(memValue *value, byte *stack, uint16_t stackPointer, uint16_t stackSize);
const char* buffer;
uint16_t tokenPosition;
uint16_t tranStack[5];
byte tranStackPointer;
uint16_t romPointer;
tokenDef lastReadToken;
varDef * variables;
fxDef * functions;
};
#endif