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
|
package bjc.dicelang;
import static bjc.dicelang.Errors.ErrorKey.*;
import static bjc.dicelang.Node.Type.*;
import static bjc.dicelang.Token.Type.*;
import java.util.Deque;
import java.util.LinkedList;
import bjc.utils.data.ITree;
import bjc.utils.data.Tree;
import bjc.utils.funcdata.IList;
public class Parser {
public Parser() {
}
public boolean parseTokens(IList<Token> tokens, IList<ITree<Node>> results) {
Deque<ITree<Node>> working = new LinkedList<>();
for (Token tk : tokens) {
switch (tk.type) {
case OBRACKET:
case OBRACE:
working.push(new Tree<>(new Node(OGROUP, tk)));
break;
case CBRACKET:
case CBRACE:
boolean sc = parseClosingGrouper(working, tk);
if (!sc)
return false;
break;
case MULTIPLY:
case DIVIDE:
case IDIVIDE:
case DICEGROUP:
case DICECONCAT:
case DICELIST:
case STRCAT:
case STRREP:
case LET:
case BIND:
if (working.size() < 2) {
Errors.inst.printError(EK_PARSE_BINARY);
return false;
} else {
ITree<Node> right = working.pop();
ITree<Node> left = working.pop();
ITree<Node> opNode = new Tree<>(new Node(BINOP, tk.type));
opNode.addChild(left);
opNode.addChild(right);
working.push(opNode);
}
break;
case ADD:
case SUBTRACT:
if (working.size() == 0) {
Errors.inst.printError(EK_PARSE_UNOPERAND, tk.toString());
return false;
} else if (working.size() == 1) {
ITree<Node> operand = working.pop();
ITree<Node> opNode = new Tree<>(new Node(UNARYOP, tk.type));
opNode.addChild(operand);
working.push(opNode);
} else {
ITree<Node> right = working.pop();
ITree<Node> left = working.pop();
ITree<Node> opNode = new Tree<>(new Node(BINOP, tk.type));
opNode.addChild(left);
opNode.addChild(right);
working.push(opNode);
}
break;
case COERCE:
case DICESCALAR:
case DICEFUDGE:
if (working.size() == 0) {
Errors.inst.printError(EK_PARSE_UNOPERAND, tk.toString());
} else {
ITree<Node> operand = working.pop();
ITree<Node> opNode = new Tree<>(new Node(UNARYOP, tk.type));
opNode.addChild(operand);
working.push(opNode);
}
break;
case INT_LIT:
case FLOAT_LIT:
case STRING_LIT:
case VREF:
case DICE_LIT:
working.push(new Tree<>(new Node(TOKREF, tk)));
break;
default:
Errors.inst.printError(EK_PARSE_INVTOKEN, tk.type.toString());
return false;
}
}
for (ITree<Node> ast : working) {
results.add(ast);
}
return true;
}
private boolean parseClosingGrouper(Deque<ITree<Node>> working, Token tk) {
if (working.size() == 0) {
Errors.inst.printError(EK_PARSE_NOCLOSE);
return false;
}
ITree<Node> groupNode = null;
switch (tk.type) {
case CBRACE:
groupNode = new Tree<>(new Node(GROUP, Node.GroupType.CODE));
break;
case CBRACKET:
groupNode = new Tree<>(new Node(GROUP, Node.GroupType.ARRAY));
break;
default:
break;
}
Token matching = null;
if (tk.type == CBRACKET) {
matching = new Token(Token.Type.OBRACKET, tk.intValue);
} else if (tk.type == CBRACE) {
matching = new Token(Token.Type.OBRACE, tk.intValue);
}
ITree<Node> matchNode = new Tree<>(new Node(OGROUP, matching));
if (!working.contains(matchNode)) {
Errors.inst.printError(EK_PARSE_UNCLOSE, tk.toString(), matchNode.toString());
System.out.println("\tCurrent forest is: ");
int treeNo = 1;
for (ITree<Node> ast : working) {
System.out.println("Tree " + treeNo++ + ": " + ast.toString());
}
return false;
} else {
Deque<ITree<Node>> childs = new LinkedList<>();
while (!working.peek().equals(matchNode)) {
childs.push(working.pop());
}
// Discard opener
working.pop();
for (ITree<Node> child : childs) {
groupNode.addChild(child);
}
working.push(groupNode);
}
return true;
}
}
|