forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
151 lines (133 loc) · 4.61 KB
/
parser.cpp
File metadata and controls
151 lines (133 loc) · 4.61 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
// Copyright 2014 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.
#include "parser.h"
namespace cashew {
// common strings
IString TOPLEVEL("toplevel"),
DEFUN("defun"),
BLOCK("block"),
STAT("stat"),
ASSIGN("assign"),
NAME("name"),
VAR("var"),
CONST("const"),
CONDITIONAL("conditional"),
BINARY("binary"),
RETURN("return"),
IF("if"),
ELSE("else"),
WHILE("while"),
DO("do"),
FOR("for"),
SEQ("seq"),
SUB("sub"),
CALL("call"),
NUM("num"),
LABEL("label"),
BREAK("break"),
CONTINUE("continue"),
SWITCH("switch"),
STRING("string"),
INF("inf"),
NaN("nan"),
TEMP_RET0("tempRet0"),
UNARY_PREFIX("unary-prefix"),
MATH_FROUND("Math_fround"),
SIMD_FLOAT32X4("SIMD_Float32x4"),
SIMD_FLOAT64X2("SIMD_Float64x2"),
SIMD_INT8X16("SIMD_Int8x16"),
SIMD_INT16X8("SIMD_Int16x8"),
SIMD_INT32X4("SIMD_Int32x4"),
SIMD_BOOL8X16("SIMD_Bool8x16"),
SIMD_BOOL16X8("SIMD_Bool16x8"),
SIMD_BOOL32X4("SIMD_Bool32x4"),
SIMD_BOOL64X2("SIMD_Bool64x2"),
PLUS("+"),
MINUS("-"),
OR("|"),
AND("&"),
XOR("^"),
L_NOT("!"),
B_NOT("~"),
LT("<"),
GE(">="),
LE("<="),
GT(">"),
EQ("=="),
NE("!="),
DIV("/"),
MOD("%"),
MUL("*"),
RSHIFT(">>"),
LSHIFT("<<"),
TRSHIFT(">>>"),
TEMP_DOUBLE_PTR("tempDoublePtr"),
HEAP8("HEAP8"),
HEAP16("HEAP16"),
HEAP32("HEAP32"),
HEAPF32("HEAPF32"),
HEAPU8("HEAPU8"),
HEAPU16("HEAPU16"),
HEAPU32("HEAPU32"),
HEAPF64("HEAPF64"),
F0("f0"),
EMPTY(""),
FUNCTION("function"),
OPEN_PAREN("("),
OPEN_BRACE("["),
OPEN_CURLY("{"),
CLOSE_CURLY("}"),
COMMA(","),
QUESTION("?"),
COLON(":"),
CASE("case"),
DEFAULT("default"),
DOT("dot"),
PERIOD("."),
NEW("new"),
ARRAY("array"),
OBJECT("object"),
THROW("throw"),
SET("=");
IStringSet keywords("var const function if else do while for break continue return switch case default throw try catch finally true false null new");
const char *OPERATOR_INITS = "+-*/%<>&^|~=!,?:.",
*SEPARATORS = "([;{}";
int MAX_OPERATOR_SIZE = 3;
std::vector<OperatorClass> operatorClasses;
static std::vector<std::unordered_map<IString, int>> precedences; // op, type => prec
struct Init {
Init() {
// operators, rtl, type
operatorClasses.push_back(OperatorClass(".", false, OperatorClass::Binary));
operatorClasses.push_back(OperatorClass("! ~ + -", true, OperatorClass::Prefix));
operatorClasses.push_back(OperatorClass("* / %", false, OperatorClass::Binary));
operatorClasses.push_back(OperatorClass("+ -", false, OperatorClass::Binary));
operatorClasses.push_back(OperatorClass("<< >> >>>", false, OperatorClass::Binary));
operatorClasses.push_back(OperatorClass("< <= > >=", false, OperatorClass::Binary));
operatorClasses.push_back(OperatorClass("== !=", false, OperatorClass::Binary));
operatorClasses.push_back(OperatorClass("&", false, OperatorClass::Binary));
operatorClasses.push_back(OperatorClass("^", false, OperatorClass::Binary));
operatorClasses.push_back(OperatorClass("|", false, OperatorClass::Binary));
operatorClasses.push_back(OperatorClass("? :", true, OperatorClass::Tertiary));
operatorClasses.push_back(OperatorClass("=", true, OperatorClass::Binary));
operatorClasses.push_back(OperatorClass(",", true, OperatorClass::Binary));
precedences.resize(OperatorClass::Tertiary + 1);
for (size_t prec = 0; prec < operatorClasses.size(); prec++) {
for (auto curr : operatorClasses[prec].ops) {
precedences[operatorClasses[prec].type][curr] = prec;
}
}
}
};
Init init;
int OperatorClass::getPrecedence(Type type, IString op) {
return precedences[type][op];
}
bool OperatorClass::getRtl(int prec) {
return operatorClasses[prec].rtl;
}
bool isIdentInit(char x) { return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z') || x == '_' || x == '$'; }
bool isIdentPart(char x) { return isIdentInit(x) || (x >= '0' && x <= '9'); }
} // namespace cashew