]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/parse.h
Clamp pitch values rather than just dumping an error message.
[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
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 char* byteCode;
70     int nBytes;
71     int codeAlloced;
72
73     // Stack of "loop" frames for break/continue statements
74     struct {
75         int breakIP;
76         int contIP;
77         struct Token* label;
78     } loops[MAX_MARK_DEPTH];
79     int loopTop;
80
81     // Dynamic storage for constants, to be compiled into a static table
82     naRef consts;   // index -> naRef
83     naRef interned; // naRef -> index (scalars only!)
84     int nConsts;
85 };
86
87 void naParseError(struct Parser* p, char* msg, int line);
88 void naParseInit(struct Parser* p);
89 void* naParseAlloc(struct Parser* p, int bytes);
90 void naParseDestroy(struct Parser* p);
91 void naLex(struct Parser* p);
92 naRef naCodeGen(struct Parser* p, struct Token* tok);
93
94 void naParse(struct Parser* p);
95
96
97
98 #endif // _PARSE_H