forked from kisonecat/web2js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile.js
171 lines (146 loc) · 6.08 KB
/
compile.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
var Lexer = require('flex-js');
var Binaryen = require('binaryen');
var lexer = new Lexer();
var last_token;
// definitions
lexer.addDefinition('DIGIT', /[0-9]/);
lexer.addDefinition('ALPHA', /[a-zA-Z]/);
lexer.addDefinition('ALPHANUM', /([0-9]|[a-zA-z]|_)/);
lexer.addDefinition('IDENTIFIER', /[a-zA-Z]([0-9]|[a-zA-Z]|_)*/ );
lexer.addDefinition('NUMBER', /[0-9]+/);
lexer.addDefinition('SIGN', /(\+|-)/);
lexer.addDefinition('SIGNED', /(\+|-)?[0-9]+/);
lexer.addDefinition('REAL', /([0-9]+\.[0-9]+|[0-9]+\.[0-9]+e(\+|-)?[0-9]+|[0-9]+e(\+|-)?[0-9]+)/);
lexer.addDefinition('COMMENT', /(({[^}]*})|(\(\*([^*]|\*[^)])*\*\)))/);
lexer.addDefinition('W', /([ \n\t]+)+/);
lexer.addRule('{', function (lexer) {
while (lexer.input() != '}')
;
});
lexer.addRule(/{W}/);
lexer.addRule("packed" , function(lexer) { return 'packed'; } );
lexer.addRule("forward" , function(lexer) { return 'forward'; } );
lexer.addRule("and" , function(lexer) { return 'and'; } );
lexer.addRule("array" , function(lexer) { return 'array'; } );
lexer.addRule("begin" , function(lexer) { return 'begin'; } );
lexer.addRule("case" , function(lexer) { return 'case'; } );
lexer.addRule("const" , function(lexer) { return 'const'; } );
lexer.addRule("div" , function(lexer) { return 'div'; } );
lexer.addRule("do" , function(lexer) { return 'do'; } );
lexer.addRule("downto" , function(lexer) { return 'downto'; } );
lexer.addRule("else" , function(lexer) { return 'else'; } );
lexer.addRule("end" , function(lexer) { return 'end'; } );
lexer.addRule("file" , function(lexer) { return 'file'; } );
lexer.addRule("for" , function(lexer) { return 'for'; } );
lexer.addRule("function" , function(lexer) { return 'function'; } );
lexer.addRule("goto" , function(lexer) { return 'goto'; } );
lexer.addRule("if" , function(lexer) { return 'if'; } );
lexer.addRule("label" , function(lexer) { return 'label'; } );
lexer.addRule("mod" , function(lexer) { return 'mod'; } );
lexer.addRule("not" , function(lexer) { return 'not'; } );
lexer.addRule("of" , function(lexer) { return 'of'; } );
lexer.addRule("or" , function(lexer) { return 'or'; } );
lexer.addRule("procedure" , function(lexer) { return 'procedure'; } );
lexer.addRule("program" , function(lexer) { return 'program'; } );
lexer.addRule("record" , function(lexer) { return 'record'; } );
lexer.addRule("repeat" , function(lexer) { return 'repeat'; } );
lexer.addRule("then" , function(lexer) { return 'then'; } );
lexer.addRule("to" , function(lexer) { return 'to'; } );
lexer.addRule("type" , function(lexer) { return 'type'; } );
lexer.addRule("until" , function(lexer) { return 'until'; } );
lexer.addRule("var" , function(lexer) { return 'var'; } );
lexer.addRule("while" , function(lexer) { return 'while'; } );
lexer.addRule("others" , function(lexer) { return 'others'; } );
lexer.addRule("true" , function(lexer) { return 'true'; } );
lexer.addRule("false" , function(lexer) { return 'false'; } );
lexer.addRule(/'([^']|'')'/ , function(lexer) {
return 'single_char';
});
lexer.addRule(/'([^']|'')+'/ , function(lexer) {
if ((lexer.text == "''''") || (lexer.text.length == 3)) {
lexer.reject();
} else
return 'string_literal';
});
lexer.addRule('+' , function(lexer) {
if ((last_token == 'identifier') ||
(last_token == 'i_num') ||
(last_token == 'r_num') ||
(last_token == ')') ||
(last_token == ']'))
return '+';
else
return 'unary_plus';
});
lexer.addRule('-' , function(lexer) {
if ((last_token == 'identifier') ||
(last_token == 'i_num') ||
(last_token == 'r_num') ||
(last_token == ')') ||
(last_token == ']'))
return '-';
else {
var c;
while ( ((c=lexer.input()) == ' ') || (c == "\t") ) {
}
lexer.unput(c);
if (parseInt(c).toString() != c) {
return 'unary_minus';
}
lexer.reject();
}
});
lexer.addRule(/-?{REAL}/ , function(lexer) { return 'r_num'; } );
lexer.addRule(/-?{NUMBER}/ , function(lexer) {
if ((last_token == 'identifier') ||
(last_token == 'i_num') ||
(last_token == 'r_num') ||
(last_token == ')') ||
(last_token == ']'))
lexer.reject();
return 'i_num';
} );
lexer.addRule("*" , function(lexer) { return '*'; } );
lexer.addRule("/" , function(lexer) { return '/'; } );
lexer.addRule("=" , function(lexer) { return '='; } );
lexer.addRule("<>" , function(lexer) { return '<>'; } );
lexer.addRule("<" , function(lexer) { return '<'; } );
lexer.addRule(">" , function(lexer) { return '>'; } );
lexer.addRule("<=" , function(lexer) { return '<='; } );
lexer.addRule(">=" , function(lexer) { return '>='; } );
lexer.addRule("(" , function(lexer) { return '('; } );
lexer.addRule(")" , function(lexer) { return ')'; } );
lexer.addRule("[" , function(lexer) { return '['; } );
lexer.addRule("]" , function(lexer) { return ']'; } );
lexer.addRule(":=" , function(lexer) { return 'assign'; } );
lexer.addRule(".." , function(lexer) { return '..';} );
lexer.addRule("." , function(lexer) { return '.'; } );
lexer.addRule("," , function(lexer) { return ','; } );
lexer.addRule(";" , function(lexer) { return ';'; } );
lexer.addRule(":" , function(lexer) { return ':'; } );
lexer.addRule("^" , function(lexer) { return '^'; } );
lexer.addRule(/{IDENTIFIER}/ , function(lexer) {
return 'identifier';
} );
lexer.addRule(/./ , function(lexer) { return '..'; } );
var fs = require('fs');
var filename = process.argv[2];
var code = fs.readFileSync(process.argv[2]).toString();
lexer.setSource(code);
var parser = require('./parser').parser;
parser.lexer = {
lex: function () {
var token = lexer.lex();
last_token = token;
this.yytext = lexer.text;
//console.log(lexer.text);
return token;
},
setInput: function (str) {
}
};
var program = parser.parse();
var module = program.generate();
module.runPasses(["remove-unused-brs","pick-load-signs","precompute","precompute-propagate","code-pushing","duplicate-function-elimination","inlining-optimizing","dae-optimizing","generate-stack-ir","optimize-stack-ir"]);
fs.writeFileSync( process.argv[3], module.emitBinary() );
console.log("Using ", program.memory.memorySize, "bytes" );