-
Notifications
You must be signed in to change notification settings - Fork 0
/
fleaux_parser.py
156 lines (123 loc) · 3.3 KB
/
fleaux_parser.py
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
151
152
153
154
155
156
from parglare import Parser, Grammar
from parglare.actions import pass_nochange, pass_single, pass_inner
# sys.setrecursionlimit(2000)
# grammar = Grammar.from_file("fleaux_grammar.pg")
grammar = Grammar.from_string('''
program:
statement_list?;
statement_list:
statement+;
statement:
(let_statement | expression_statement) ';';
let_statement:
LET identifier '(' parameter_decl_list ')' ':' type LET_EXPR_SEPARATOR expression;
expression_statement:
expression;
parameter_decl_list:
parameter+[COMMA];
parameter:
identifier ':' type;
tuple_type:
type_list;
type_list:
type+[COMMA];
type:
NUMBER |
STRING |
BOOL |
NULL |
ANY |
identifier |
qualified_id |
'(' type_list ')';
expression:
term |
term PIPELINE_OPERATOR expression {left, 10} |
term PLUS_OPERATOR expression {left, 5} |
term MINUS_OPERATOR expression {left, 5} |
term DIVIDE_OPERATOR expression {left, 6} |
term MULTIPLY_OPERATOR expression {left, 6} |
term MODULUS_OPERATOR expression {left, 6} |
term EXPONENT_OPERATOR expression {right, 7} |
term EQ_OPERATOR expression {left, 4} |
term NE_OPERATOR expression {left, 4} |
term LT_OPERATOR expression {left, 4} |
term GT_OPERATOR expression {left, 4} |
term GE_OPERATOR expression {left, 4} |
term LE_OPERATOR expression {left, 4} |
term OR_OPERATOR expression {left, 1} |
term AND_OPERATOR expression {left, 2} |
term COMMA expression {left, 1} |
NOT_OPERATOR expression {right, 3} |
PLUS_OPERATOR expression {right, 1} |
MINUS_OPERATOR expression {right, 1};
term:
constant |
qualified_id |
identifier |
parenthesized_expression;
parenthesized_expression: '(' expression ')';
qualified_id:
identifier PERIOD identifier;
identifier:
ID;
constant:
NUMERICAL_CONSTANT | STRING_LITERAL | TRUE | FALSE | NULL;
LAYOUT: LayoutItem | LAYOUT LayoutItem | EMPTY;
LayoutItem: WS | Comment;
terminals
NUMERICAL_CONSTANT:
/-?\d+(\.\d+)?(e|E[-+]?\d+)?/;
STRING_LITERAL: /"((\\")|[^"])*"/;
ID: /[a-zA-Z_][a-zA-Z_0-9]*/;
PIPELINE_OPERATOR: "->";
LE_OPERATOR: "<=";
GE_OPERATOR: ">=";
EQ_OPERATOR: "==";
NE_OPERATOR: "!=";
LT_OPERATOR: "<";
GT_OPERATOR: ">";
AND_OPERATOR: "&&";
OR_OPERATOR: "||";
PLUS_OPERATOR: "+";
MINUS_OPERATOR: "-";
DIVIDE_OPERATOR: "/";
MULTIPLY_OPERATOR: "*";
MODULUS_OPERATOR: "%";
EXPONENT_OPERATOR: "^";
NOT_OPERATOR: "!";
LET_EXPR_SEPARATOR: "::";
LET: "let";
NUMBER: "Number";
STRING: "String";
BOOL: "Bool";
NULL: "Null";
TRUE: "True";
FALSE: "False";
ANY: "Any";
COMMA: ",";
PERIOD: ".";
WS: /\s+/;
Comment: /\/\/.*/;
''')
actions = {
"parenthesized_expression": [pass_inner]
}
parser = Parser(grammar, build_tree=True, actions=actions)
result = parser.parse('''
// let Multiply2(x : Number) : Number :: (x) -> Std.Multiply -> Std.Println;
// let AddPrint(x : Number, y: Number) : Number :: (x, y) -> Std.Add -> Std.Println;
//
// y = 4x^7 - x^5
// let Polynomial(x : Number) : Number :: ((((4, x) -> Std.Multiply, 7) -> Std.Pow), ((x, 5) -> Std.Pow)) -> Std.Subtract;
// let Polynomial2(x : Number) : Number :: ((4*x)^7) - (x^5);
// (1, 2) -> AddPrint;
// ("Hello, World!") -> Println;
2 -> ((((4, x) -> Std.Multiply, 7) -> Std.Pow), ((x, 5) -> Std.Pow)) -> Std.Subtract;
''')
print (type(result))
for stmt in result:
print(stmt)
# parglare.visitor(result, )
# for statement in result:
# print(statement)