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