1 | /* |
---|
2 | * |
---|
3 | * Static factories for various node types. |
---|
4 | * |
---|
5 | * Add factories as needed. |
---|
6 | * |
---|
7 | * @author <ksherdy at sfu dot ca> |
---|
8 | */ |
---|
9 | |
---|
10 | package toolchain.s2k.ast; |
---|
11 | |
---|
12 | import s2k.ast.*; |
---|
13 | |
---|
14 | import s2k.inputHandler.Locator; |
---|
15 | import s2k.lexicalAnalyzer.*; |
---|
16 | import s2k.tokens.*; |
---|
17 | import toolchain.s2k.lang.*; |
---|
18 | |
---|
19 | public class Generators { |
---|
20 | |
---|
21 | // TS: should change grammar and conventions so that every FuncDefNode has a ParameterListNode child |
---|
22 | // but the PLN child has no children if there are no args. This would eliminate the conditional |
---|
23 | // in this method, and probably make things easier in other methods, too. |
---|
24 | public static void appendParameter(FilterDefNode funcDefNode, FilterParameterNode parameterNode) { |
---|
25 | FilterParameterListNode parameterListNode = null; |
---|
26 | parameterListNode = Accessors.parameterListNode(funcDefNode); |
---|
27 | parameterListNode.appendChild(parameterNode); |
---|
28 | } |
---|
29 | |
---|
30 | public static VarDeclNode makeVarDeclNode(Locator locator, ASTNode type, ASTNode lhs) { |
---|
31 | Token assignToken = Generators.makeLextantToken(locator, Lextant.ASSIGN); |
---|
32 | VarDeclNode localVarDecl = new VarDeclNode(assignToken); |
---|
33 | localVarDecl.appendChild(type); |
---|
34 | localVarDecl.appendChild(lhs); |
---|
35 | return localVarDecl; |
---|
36 | } |
---|
37 | |
---|
38 | public static VarDeclNode makeVarDeclNode(Locator locator, ASTNode type, ASTNode lhs, ASTNode rhs) { |
---|
39 | VarDeclNode localVarDecl = makeVarDeclNode(locator,type,lhs); |
---|
40 | localVarDecl.appendChild(rhs); |
---|
41 | return localVarDecl; |
---|
42 | } |
---|
43 | |
---|
44 | public static AssignNode makeAssignNode(Locator locator, ASTNode lhs, ASTNode rhs) { |
---|
45 | Token assignToken = Generators.makeLextantToken(locator, Lextant.ASSIGN); |
---|
46 | AssignNode assign = new AssignNode(assignToken); |
---|
47 | assign.appendChild(lhs); |
---|
48 | assign.appendChild(rhs); |
---|
49 | return assign; |
---|
50 | } |
---|
51 | |
---|
52 | /////////////////////////////////////////////////////////////////// |
---|
53 | // Function call variants |
---|
54 | public static FuncCallNode makeFuncCallNode(Locator locator, String identifier) { |
---|
55 | IdentifierNode identifierNode = Generators.makeIdentifierNode(locator, identifier); |
---|
56 | return Generators.makeFuncCallNode(locator, identifierNode); |
---|
57 | } |
---|
58 | |
---|
59 | public static FuncCallNode makeFuncCallNode(Locator locator, String identifier, ASTNode ...args) { |
---|
60 | FuncCallNode node = Generators.makeFuncCallNode(locator, identifier); |
---|
61 | Generators.addFuncCallArgs(node, args); |
---|
62 | return node; |
---|
63 | } |
---|
64 | |
---|
65 | public static FuncCallNode makeFuncCallNode(Locator locator, String pckage, String name) { |
---|
66 | CompoundIdentifierNode compoundIdentifierNode = Generators.makeCompoundIdentifierNode(locator, pckage, name); |
---|
67 | return Generators.makeFuncCallNode(locator, compoundIdentifierNode); |
---|
68 | } |
---|
69 | |
---|
70 | public static FuncCallNode makeFuncCallNode(Locator locator, String pckage, String name, ASTNode ...args) { |
---|
71 | CompoundIdentifierNode compoundIdentifierNode = Generators.makeCompoundIdentifierNode(locator, pckage, name); |
---|
72 | FuncCallNode node = Generators.makeFuncCallNode(locator, compoundIdentifierNode); |
---|
73 | Generators.addFuncCallArgs(node, args); |
---|
74 | return node; |
---|
75 | } |
---|
76 | |
---|
77 | private static FuncCallNode makeFuncCallNode(Locator locator, ASTNode identifier) { |
---|
78 | assert ((identifier instanceof IdentifierNode) || (identifier instanceof CompoundIdentifierNode)); |
---|
79 | |
---|
80 | LextantToken token = Generators.makeLextantToken(locator, Lextant.LROUND); |
---|
81 | FuncCallNode FuncCallNode = new FuncCallNode(token); |
---|
82 | FuncCallNode.appendChild(identifier); |
---|
83 | FuncCallNode.appendChild(new FuncCallArgListNode(token)); |
---|
84 | |
---|
85 | return FuncCallNode; |
---|
86 | } |
---|
87 | |
---|
88 | private static void addFuncCallArgs(FuncCallNode node, ASTNode ... args) { |
---|
89 | for(ASTNode child: args) { |
---|
90 | Accessors.argsListNode(node).appendChild(child.deepCopy()); |
---|
91 | } |
---|
92 | Accessors.argsListNode(node).setToken(args[0].getToken()); |
---|
93 | } |
---|
94 | |
---|
95 | public static CompoundIdentifierNode makeCompoundIdentifierNode(Locator locator, String pckage, String name) { |
---|
96 | return makeCompoundIdentifierNode(locator, new String [] {pckage, name}); |
---|
97 | } |
---|
98 | |
---|
99 | /** Makes a new CompoundIdentifierNode with an array of identifier strings and and the TextLocation taken from locator. |
---|
100 | * Currently, no special consideration with only one identifier in the array. |
---|
101 | * (Maybe we can return a IdentifierNode directly in later versions.) |
---|
102 | * @param locator a token containing the TextLocation for this new node. |
---|
103 | * @param identifiers all identifiers |
---|
104 | * @return the new CompoundIdentifierNode |
---|
105 | */ |
---|
106 | public static CompoundIdentifierNode makeCompoundIdentifierNode(Locator locator, String ... identifiers) { |
---|
107 | Token dotToken = Generators.makeLextantToken(locator, Lextant.DOT); |
---|
108 | CompoundIdentifierNode CompoundIdentifierNode = new CompoundIdentifierNode(dotToken); |
---|
109 | for (String identifier : identifiers) { |
---|
110 | IdentifierNode identifierNode = Generators.makeIdentifierNode(locator, identifier); |
---|
111 | CompoundIdentifierNode.appendChild(identifierNode); |
---|
112 | } |
---|
113 | return CompoundIdentifierNode; |
---|
114 | } |
---|
115 | |
---|
116 | /** Makes a new IdentifierNode with a identifier string and the TextLocation taken from locator. |
---|
117 | * @param locator a token containing the TextLocation for this new node. |
---|
118 | * @param identifier |
---|
119 | * @return the new IdentifierNode |
---|
120 | */ |
---|
121 | public static IdentifierNode makeIdentifierNode(Locator locator, String identifier) { |
---|
122 | IdentifierToken newToken = Generators.makeIdentifierToken(locator, identifier); |
---|
123 | return new IdentifierNode(newToken); |
---|
124 | } |
---|
125 | |
---|
126 | /** Makes a new LextantToken with the given lextant and location taken from the given locator. |
---|
127 | * @param locator |
---|
128 | * @param lextant |
---|
129 | * @return the new LextantToken |
---|
130 | */ |
---|
131 | public static LextantToken makeLextantToken(Locator locator, Lextant lextant) { |
---|
132 | return LextantToken.make(locator.getLocation(), lextant.getPrimaryLexeme(), lextant); |
---|
133 | } |
---|
134 | |
---|
135 | /** Makes a new StringConstantToken with the given string and location taken from the given locator. |
---|
136 | * @param locator |
---|
137 | * @param string |
---|
138 | * @return the new StringConstantToken |
---|
139 | */ |
---|
140 | public static StringConstantToken makeStringConstantToken(Locator locator, String string) { |
---|
141 | return StringConstantToken.make(LexicalType.STRING, locator.getLocation(), string); |
---|
142 | } |
---|
143 | |
---|
144 | /** Makes a new IdentifierToken with the given identifier and location taken from the given locator. |
---|
145 | * @param locator |
---|
146 | * @param identifier |
---|
147 | * @return the new IdentifierToken |
---|
148 | */ |
---|
149 | public static IdentifierToken makeIdentifierToken(Locator locator, String identifier) { |
---|
150 | return IdentifierToken.make(LexicalType.IDENTIFIER, locator.getLocation(), identifier); |
---|
151 | } |
---|
152 | |
---|
153 | /** Makes a new StringConstantNode with the given string and the TextLocation taken from locator. |
---|
154 | * @param locator a locator containing the TextLocation for this new node. |
---|
155 | * @param string string value/lexeme of new node. |
---|
156 | * @return the new StringConstantNode |
---|
157 | */ |
---|
158 | public static StringConstantNode makeStringConstantNode(Locator locator, String string) { |
---|
159 | StringConstantToken newToken = makeStringConstantToken(locator, string); |
---|
160 | return new StringConstantNode(newToken); |
---|
161 | } |
---|
162 | |
---|
163 | /** Makes a new IntegerConstantNode with a value of n and the TextLocation taken from locator. |
---|
164 | * @param locator a token containing the TextLocation for this new node. |
---|
165 | * @param n integer value of new node. |
---|
166 | * @return the new IntegerConstantNode |
---|
167 | */ |
---|
168 | public static IntegerConstantNode makeIntegerConstantNode(Locator locator, int n) { |
---|
169 | Token newToken = IntConstantToken.makeArtifical(LexicalType.INTEGER_CONST, locator.getLocation(), n); |
---|
170 | IntegerConstantNode integerConstantNode = new IntegerConstantNode(newToken); |
---|
171 | integerConstantNode.setValue(n); |
---|
172 | return integerConstantNode; |
---|
173 | } |
---|
174 | |
---|
175 | public static IntConstantToken makeIntegerConstantToken(Locator locator, int n) { |
---|
176 | return (IntConstantToken) IntConstantToken.makeArtifical(LexicalType.INTEGER_CONST, locator.getLocation(), n); |
---|
177 | } |
---|
178 | |
---|
179 | public static StreamTypeNode makeStreamType(Locator locator) { |
---|
180 | Token streamToken = Generators.makeLextantToken(locator, Lextant.STREAM); |
---|
181 | return new StreamTypeNode(streamToken); |
---|
182 | } |
---|
183 | |
---|
184 | public static BinaryOperatorNode makeBinaryOperatorNode(ASTNode lhs, ASTNode rhs, Token token) { |
---|
185 | BinaryOperatorNode node = new BinaryOperatorNode(token); |
---|
186 | node.appendChild(lhs.deepCopy()); |
---|
187 | node.appendChild(rhs.deepCopy()); |
---|
188 | return node; |
---|
189 | } |
---|
190 | |
---|
191 | public static BlockStmtNode makeBlockStmtNode(Locator locator, ASTNode ...children) { |
---|
192 | LextantToken token = Generators.makeLextantToken(locator, Lextant.LCURLY); |
---|
193 | |
---|
194 | BlockStmtNode result = new BlockStmtNode(token); |
---|
195 | for(ASTNode child: children) { |
---|
196 | result.appendChild(child); |
---|
197 | } |
---|
198 | return result; |
---|
199 | } |
---|
200 | |
---|
201 | public static IfStmtNode makeIfStmtNode(Locator locator, ASTNode expression, BlockStmtNode ifBlockStmt, BlockStmtNode elseBlockStmt) { |
---|
202 | IfStmtNode ifStmtNode = makeIfStmtNode(locator, expression, ifBlockStmt); |
---|
203 | ifStmtNode.appendChild(elseBlockStmt); |
---|
204 | return ifStmtNode; |
---|
205 | } |
---|
206 | |
---|
207 | public static IfStmtNode makeIfStmtNode(Locator locator, ASTNode expression, BlockStmtNode blockStmt) { |
---|
208 | LextantToken token = Generators.makeLextantToken(locator, Lextant.LCURLY); |
---|
209 | IfStmtNode ifStmtNode = new IfStmtNode(token); |
---|
210 | ifStmtNode.appendChild(expression); |
---|
211 | ifStmtNode.appendChild((ASTNode)blockStmt); |
---|
212 | return ifStmtNode; |
---|
213 | } |
---|
214 | |
---|
215 | /////////////////////////////////////////////////////////////////// |
---|
216 | // Mask calls |
---|
217 | public static FuncCallNode makeMaskFuncCall(Locator locator, int fieldWidth, int value) { |
---|
218 | IntegerConstantNode zeroConstantNode = Generators.makeIntegerConstantNode(locator, value); |
---|
219 | IntegerConstantNode fieldWidthNode = Generators.makeIntegerConstantNode(locator, fieldWidth); |
---|
220 | |
---|
221 | FuncCallNode mask = Generators.makeFuncCallNode(locator, BuiltinCallUtil.BUILTIN_PACKAGE_NAME, |
---|
222 | S2KBuiltin.MASK.name(), |
---|
223 | arguments(fieldWidthNode, zeroConstantNode)); |
---|
224 | return mask; |
---|
225 | } |
---|
226 | |
---|
227 | /////////////////////////////////////////////////////////////////// |
---|
228 | // Helpers |
---|
229 | public static ASTNode[] arguments(ASTNode...astNodes) { |
---|
230 | return astNodes; |
---|
231 | } |
---|
232 | |
---|
233 | public static String capitalize(String str) { |
---|
234 | return str.substring(0, 1).toUpperCase() + str.substring(1); |
---|
235 | } |
---|
236 | |
---|
237 | public static String unCapitalize(String str) { |
---|
238 | return str.substring(0, 1).toLowerCase() + str.substring(1); |
---|
239 | } |
---|
240 | |
---|
241 | } |
---|