]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/parse.h
1e5e2d90cca097c28a786755b64b1456a3d27db8
[simgear.git] / simgear / nasal / parse.h
1 #ifndef _PARSE_H
2 #define _PARSE_H
3
4 #include <setjmp.h>
5
6 #include "nasal.h"
7 #include "data.h"
8 #include "code.h"
9
10 enum {
11     TOK_TOP=1, TOK_AND, TOK_OR, TOK_NOT, TOK_LPAR, TOK_RPAR, TOK_LBRA,
12     TOK_RBRA, TOK_LCURL, TOK_RCURL, TOK_MUL, TOK_PLUS, TOK_MINUS, TOK_NEG,
13     TOK_DIV, TOK_CAT, TOK_COLON, TOK_DOT, TOK_COMMA, TOK_SEMI,
14     TOK_ASSIGN, TOK_LT, TOK_LTE, TOK_EQ, TOK_NEQ, TOK_GT, TOK_GTE,
15     TOK_IF, TOK_ELSIF, TOK_ELSE, TOK_FOR, TOK_FOREACH, TOK_WHILE,
16     TOK_RETURN, TOK_BREAK, TOK_CONTINUE, TOK_FUNC, TOK_SYMBOL,
17     TOK_LITERAL, TOK_EMPTY, TOK_NIL, TOK_ELLIPSIS, TOK_QUESTION, TOK_VAR
18 };
19
20 struct Token {
21     int type;
22     int line;
23     char* str;
24     int strlen;
25     double num;
26     struct Token* parent;
27     struct Token* next;
28     struct Token* prev;
29     struct Token* children;
30     struct Token* lastChild;
31 };
32
33 struct Parser {
34     // Handle to the Nasal interpreter
35     struct Context* context;
36
37     char* err;
38     int errLine;
39     jmp_buf jumpHandle;
40
41     // The parse tree ubernode
42     struct Token tree;
43
44     // The input buffer
45     char* buf;
46     int   len;
47
48     // Input file parameters (for generating pretty stack dumps)
49     naRef srcFile;
50     int firstLine;
51
52     // Chunk allocator.  Throw away after parsing.
53     void** chunks;
54     int* chunkSizes;
55     int nChunks;
56     int leftInChunk;
57
58     // Computed line number table for the lexer
59     int* lines;
60     int  nLines;
61     
62     struct CodeGenerator* cg;
63 };
64
65 struct CodeGenerator {
66     int lastLine;
67
68     // Accumulated byte code array
69     unsigned short* byteCode;
70     int codesz;
71     int codeAlloced;
72
73     // Inst. -> line table, stores pairs of {ip, line}
74     unsigned short* lineIps;
75     int nLineIps; // number of pairs
76     int nextLineIp;
77
78     // Stack of "loop" frames for break/continue statements
79     struct {
80         int breakIP;
81         int contIP;
82         struct Token* label;
83     } loops[MAX_MARK_DEPTH];
84     int loopTop;
85
86     // Dynamic storage for constants, to be compiled into a static table
87     naRef consts;
88 };
89
90 void naParseError(struct Parser* p, char* msg, int line);
91 void naParseInit(struct Parser* p);
92 void* naParseAlloc(struct Parser* p, int bytes);
93 void naParseDestroy(struct Parser* p);
94 void naLex(struct Parser* p);
95 naRef naCodeGen(struct Parser* p, struct Token* block, struct Token* arglist);
96
97 void naParse(struct Parser* p);
98
99
100
101 #endif // _PARSE_H