]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/parse.h
Removal of PLIB/SG from SimGear
[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 tok {
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     TOK_PLUSEQ, TOK_MINUSEQ, TOK_MULEQ, TOK_DIVEQ, TOK_CATEQ,
19     TOK_FORINDEX
20 };
21
22 // Precedence rules
23 enum { PREC_BINARY=1, PREC_REVERSE, PREC_PREFIX, PREC_SUFFIX };
24
25 struct Token {
26     enum tok type;
27     int line;
28     char* str;
29     int strlen;
30     int rule;
31     double num;
32     struct Token* next;
33     struct Token* prev;
34     struct Token* children;
35     struct Token* lastChild;
36 };
37
38 struct Parser {
39     // Handle to the Nasal interpreter
40     struct Context* context;
41
42     char* err;
43     int errLine;
44     jmp_buf jumpHandle;
45
46     // The parse tree ubernode
47     struct Token tree;
48
49     // The input buffer
50     char* buf;
51     int   len;
52
53     // Input file parameters (for generating pretty stack dumps)
54     naRef srcFile;
55     int firstLine;
56
57     // Chunk allocator.  Throw away after parsing.
58     void** chunks;
59     int* chunkSizes;
60     int nChunks;
61     int leftInChunk;
62
63     // Computed line number table for the lexer
64     int* lines;
65     int  nLines;
66
67     struct CodeGenerator* cg;
68 };
69
70 struct CodeGenerator {
71     int lastLine;
72
73     // Accumulated byte code array
74     unsigned short* byteCode;
75     int codesz;
76     int codeAlloced;
77
78     // Inst. -> line table, stores pairs of {ip, line}
79     unsigned short* lineIps;
80     int nLineIps; // number of pairs
81     int nextLineIp;
82
83     int* argSyms;
84     int* optArgSyms;
85     int* optArgVals;
86     naRef restArgSym;
87
88     // Stack of "loop" frames for break/continue statements
89     struct {
90         int breakIP;
91         int contIP;
92         struct Token* label;
93     } loops[MAX_MARK_DEPTH];
94     int loopTop;
95
96     // Dynamic storage for constants, to be compiled into a static table
97     naRef consts;
98 };
99
100 void naParseError(struct Parser* p, char* msg, int line);
101 void naParseInit(struct Parser* p);
102 void* naParseAlloc(struct Parser* p, int bytes);
103 void naParseDestroy(struct Parser* p);
104 void naLex(struct Parser* p);
105 int naLexUtf8C(char* s, int len, int* used); /* in utf8lib.c */
106 naRef naCodeGen(struct Parser* p, struct Token* block, struct Token* arglist);
107
108 void naParse(struct Parser* p);
109
110
111
112 #endif // _PARSE_H