-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_const.js
190 lines (165 loc) · 4.81 KB
/
parse_const.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var nearley_1 = __importDefault(require("nearley"));
var grammar = require("./const");
;
;
;
function test_expression(input) {
try {
// Make a parser and feed the input
var parser = new nearley_1.default.Parser(grammar.ParserRules, grammar.ParserStart);
var ans = parser.feed(input);
// Check if there are any results
if (ans.results.length !== 0) {
var result = ans.results[0];
return result;
}
else {
// This means the input is incomplete.
console.log("Error: incomplete input, parse failed.");
return undefined;
}
}
catch (e) {
// Panic in style, by graphically pointing out the error location.
console.log("error at " + e.offset);
return undefined;
}
}
if (process.argv.length !== 3) {
console.log("specify arg");
process.exit(0);
}
var input = process.argv[2];
console.log(input);
var parsed = test_expression(input);
if (parsed === undefined) {
throw "err";
}
console.log(JSON.stringify(parsed));
/*
function isterminal(node: Node): boolean
{
return node.type === "number";
}
function visittree(node: Node, arg?: number): Instruction[]
{
let out: Instruction[] = [];
if(node.type === "root")
{
out = visittree(node.arg);
// TODO optimize last push
}
else if(node.type === "parens")
{
out = visittree(node.arg);
// TODO optimize nested parens
}
else if(node.type === "^")
{
out = [];
//visittree(node.base, 1) + visittree(node.exponent, 2) + "fexp\n";
}
else if(node.type === "*")
{
let arg1part = visittree(node.arg1, 1);
let arg2part = visittree(node.arg2, 2);
out = [...arg1part, ...arg2part];
if(!isterminal(node.arg1)) out = [ ...out, { type: "POPFAC" }];
if(!isterminal(node.arg2)) out = [ ...out, { type: "POPAFAC" }];
out = [ ...out, { type: "FMUL" }];
out = [ ...out, { type: "FPUSH" }];
}
else if(node.type === "/")
{
let arg1part = visittree(node.arg1, 1);
let arg2part = visittree(node.arg2, 2);
out = [...arg1part, ...arg2part];
if(!isterminal(node.arg1)) out = [ ...out, { type: "POPFAC" }];
if(!isterminal(node.arg2)) out = [ ...out, { type: "POPAFAC" }];
out = [ ...out, { type: "FDIV" }];
out = [ ...out, { type: "FPUSH" }];
}
else if(node.type === "+")
{
let arg1part = visittree(node.arg1, 1);
let arg2part = visittree(node.arg2, 2);
out = [...arg1part, ...arg2part];
if(!isterminal(node.arg1)) out = [ ...out, { type: "POPFAC" }];
if(!isterminal(node.arg2)) out = [ ...out, { type: "POPAFAC" }];
out = [ ...out, { type: "FADD" }];
out = [ ...out, { type: "FPUSH" }];
}
else if(node.type === "-")
{
let arg1part = visittree(node.arg1, 1);
let arg2part = visittree(node.arg2, 2);
out = [...arg1part, ...arg2part];
if(!isterminal(node.arg1)) out = [ ...out, { type: "POPFAC" }];
if(!isterminal(node.arg2)) out = [ ...out, { type: "POPAFAC" }];
out = [ ...out, { type: "FSUB" }];
out = [ ...out, { type: "FPUSH" }];
}
else if(node.type === "sin")
{
/*
let arg1part = visittree(node.arg, 1);
let poppart = "";
if(!isterminal(node.arg)) poppart += "fpop FAC\n";
out = arg1part + poppart + "fsin\nfpush\n";
* /
let arg1part = visittree(node.arg, 1);
out = [...arg1part];
if(!isterminal(node.arg)) out = [ ...out, { type: "POPFAC" }];
out = [ ...out, { type: "FSIN" }];
out = [ ...out, { type: "FPUSH" }];
}
else if(node.type === "pi")
{
if(arg === 1) out = [{ type: "FLDFAC", arg: "PI" }];
else out = [{ type: "FLDAFAC", arg: "PI" }];
}
else if(node.type === "number")
{
if(arg === 1) out = [{ type: "FLDFAC", arg: node.num }];
else out = [{ type: "FLDAFAC", arg: node.num }];
}
else throw `unrecognized node ${(node as any).type}`;
return out;
}
/*
3*3
3 * exp
exp * 3 => no fpop fac
exp * exp => no fpop fac, only fop afac
(3+4*2)*(2+1)
fld fac, 4
fld fac1, 2
fmul
fpush fac
fld fac, 3
fpop fac1
fsum
fpush fac
fld fac, 2
fld fac1, 1
fsum
fpush fac
fpop fac1
fpop fac
fmul
* /
function printInstruction(code: Instruction[]) {
code.forEach(e => {
if(e.type === "FLDFAC" || e.type === "FLDAFAC") console.log(`${e.type} ${e.arg}`);
else console.log(`${e.type}`);
});
}
// console.log(parsed);
let output = visittree(parsed);
// printInstruction(output);
*/