-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.js
143 lines (134 loc) · 3.36 KB
/
parser.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
function Ast(type, head, args) {
this.type = type;
this.head = head;
this.args = args || [];
}
Ast.prototype.toString = function() {
var s = this.type + '|' + this.head;
if (this.args.length > 0)
s += '<' + this.args + '>';
return s;
}
// Tokens[][] -> AST[]
function parse(tokens) {
var i, cmd,
commandAsts = [];
for (i=0; i<tokens.length; i++) {
cmd = tokens[i];
//print('parsing ' + cmd);
// Directive
if (cmd[0].type === '.') {
commandAsts.push(parseDirective(cmd));
}
// Op Code
else if (cmd[0].type === OPCODE) {
commandAsts.push(parseOp(cmd));
}
// Decl, with name
else if (cmd[0].type === SIZE_DECL) {
commandAsts.push(parseDeclNoName(cmd));
}
// Decl, no name
else if (cmd.length>1 && cmd[1].type === SIZE_DECL) {
commandAsts.push(parseDecl(cmd));
}
// Constant
else if (cmd.length>2 && cmd[1].type === CONST_DEF) {
commandAsts.push(parseConstant(cmd));
}
// Label
else if (cmd.length>1 && cmd[1].type===':') {
commandAsts.push(new Ast(':', cmd[0].token));
}
else {
//print('***UNHANDLED: ' + cmd);
}
}
return commandAsts;
}
function parseDirective(tokens) {
var dir, i, ast,
args = [];
dir = tokens[0].token;
for (i=1; i<tokens.length; i++) {
[j,ast] = parseArg(tokens,i);
args.push(ast);
i=j;
}
return new Ast(tokens[0].type, dir, args);
}
function parseOp(tokens) {
var op, i, ast,
args = [];
op = tokens[0].token;
// FIXME: Assuming every other token is a comma
for (i=1; i<tokens.length; i+=2) {
[j,ast] = parseArg(tokens,i);
args.push(ast);
i=j;
}
return new Ast(tokens[0].type, op, args);
}
function parseArg(tokens, i) {
var j = i,
ast,
argAst;
switch (tokens[i].type) {
case '-':
[j,argAst] = parseArg(tokens,i+1);
ast = new Ast('-', argAst);
break;
case '[':
[j,ast] = parseMem(tokens,i);
break;
/* FIXME: Need to fixe parseMem first, but need this as well.
case '(':
[j,ast] = parseParens(tokens,i);
break;
*/
case SIZE:
if (tokens.length > i+2) {
// Ignoring 'ptr'
[j,argAst] = parseArg(tokens,i+2);
ast = new Ast(tokens[i].type, tokens[i].token, [argAst]);
} else {
ast = new Ast(tokens[i].type, tokens[i].token);
}
break;
case ',':
//throw new Error('Comma passed to parseArg');
return parseArg(tokens,i+1);
default:
ast = new Ast(tokens[i].type, tokens[i].token);
}
return [j,ast];
}
function parseMem(tokens, i) {
var j, ast, lhs, rhs;
// FIXME: Need to handle more complex cases
if (tokens[i+2].type === ']') {
[j,ast] = parseArg(tokens, i+1);
j++;
}
else {
[j,lhs] = parseArg(tokens, i+1);
[j,rhs] = parseArg(tokens, i+3);
ast = new Ast(tokens[i+2].type, lhs, [rhs]);
j++;
}
return [j, new Ast('[', ast)];
}
function parseDecl(tokens) {
//FIXME: Need to handle 'dup'
var [j,ast] = parseArg(tokens, 2);
return new Ast(tokens[1].type, tokens[1].token, [tokens[0].token, ast]);
}
function parseDeclNoName(tokens) {
//FIXME: Need to handle 'dup'
var [j,ast] = parseArg(tokens, 1);
return new Ast(tokens[0].type, tokens[0].token, [ast]);
}
function parseConstant(tokens) {
var [j,argAst] = parseArg(tokens, 2);
return new Ast(tokens[1].type, tokens[0].token, [argAst]);
}