1 | package compiler.lang.pabloS; |
---|
2 | |
---|
3 | import pabloS.ast.*; |
---|
4 | import compiler.pabloS.ast.helpers.Accessors; |
---|
5 | |
---|
6 | public class BuiltinsUtil { |
---|
7 | |
---|
8 | public static String BUILTIN_PACKAGE_NAME = "pablo"; |
---|
9 | |
---|
10 | public static boolean isAdvance(ASTNode node) { |
---|
11 | if(node == null) { |
---|
12 | return false; |
---|
13 | } |
---|
14 | |
---|
15 | if (!(node instanceof FuncCallNode)) { |
---|
16 | return false; |
---|
17 | } |
---|
18 | |
---|
19 | FuncCallNode fNode = (FuncCallNode)node; |
---|
20 | ASTNode nameNode = Accessors.funcCallIdentifier(fNode); |
---|
21 | ASTNode ArgsListNode = (ASTNode)Accessors.funcCallArgsListNode(fNode); |
---|
22 | |
---|
23 | // Advance(X) |
---|
24 | if (isQualifiedName(nameNode, BuiltinsUtil.BUILTIN_PACKAGE_NAME, Builtins.ADVANCE.pabloName()) |
---|
25 | && ArgsListNode.nChildren() == 1) { |
---|
26 | return true; |
---|
27 | } |
---|
28 | |
---|
29 | // Advance(X,n) |
---|
30 | if (isQualifiedName(nameNode, BuiltinsUtil.BUILTIN_PACKAGE_NAME, Builtins.ADVANCE.pabloName()) |
---|
31 | && ArgsListNode.nChildren() == 2) { |
---|
32 | return true; |
---|
33 | } |
---|
34 | |
---|
35 | return false; |
---|
36 | } |
---|
37 | |
---|
38 | public static boolean isBuiltinOperations(ASTNode node) { |
---|
39 | |
---|
40 | for (Builtins builtin : Builtins.values()) { |
---|
41 | if (isQualifiedName(node, BuiltinsUtil.BUILTIN_PACKAGE_NAME, builtin.pabloName())) { |
---|
42 | return true; |
---|
43 | } |
---|
44 | } |
---|
45 | return false; |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | public static boolean isCarryOne(ASTNode node) { |
---|
50 | |
---|
51 | for (Builtins carryOneOperation : Builtins.carryOneOperations()) { |
---|
52 | |
---|
53 | ASTNode identifier = Accessors.funcCallIdentifier((FuncCallNode) node); |
---|
54 | int argCount = Accessors.funcCallArgCount((FuncCallNode)node); |
---|
55 | |
---|
56 | if (isQualifiedName(identifier, BuiltinsUtil.BUILTIN_PACKAGE_NAME, carryOneOperation.pabloName()) |
---|
57 | && argCount == carryOneOperation.argCount()) { |
---|
58 | return true; |
---|
59 | } |
---|
60 | } |
---|
61 | return false; |
---|
62 | } |
---|
63 | |
---|
64 | public static boolean isCarryN(ASTNode node) { |
---|
65 | |
---|
66 | for (Builtins carryNOperation : Builtins.carryNOperations()) { |
---|
67 | ASTNode identifier = Accessors.funcCallIdentifier((FuncCallNode) node); |
---|
68 | int argCount = Accessors.funcCallArgCount((FuncCallNode) node); |
---|
69 | |
---|
70 | if (isQualifiedName(identifier, BuiltinsUtil.BUILTIN_PACKAGE_NAME, carryNOperation.pabloName()) |
---|
71 | && argCount == carryNOperation.argCount()) { |
---|
72 | return true; |
---|
73 | } |
---|
74 | } |
---|
75 | return false; |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | /** Returns true if the node represents a call to the |
---|
81 | * given built-in Func. |
---|
82 | * |
---|
83 | * @param node node to be checked |
---|
84 | * @return whether the node is a call of the Func |
---|
85 | */ |
---|
86 | public static boolean isBuiltInCallNoArgCount(ASTNode node) { |
---|
87 | if(!(node instanceof FuncCallNode)) { |
---|
88 | return false; |
---|
89 | } |
---|
90 | |
---|
91 | FuncCallNode fNode = (FuncCallNode)node; |
---|
92 | ASTNode nameNode = Accessors.funcCallIdentifier(fNode); |
---|
93 | |
---|
94 | for (Builtins builtin : Builtins.values()) { |
---|
95 | if (isQualifiedName(nameNode, BuiltinsUtil.BUILTIN_PACKAGE_NAME, builtin.pabloName())) { |
---|
96 | return true; |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | return false; |
---|
101 | } |
---|
102 | |
---|
103 | /** Returns true if the node represents a call to the |
---|
104 | * given built-in Func with the given number of Args. |
---|
105 | * |
---|
106 | * @param node node to be checked |
---|
107 | * @return whether the node is a call of the Func with argNumToMatch Args |
---|
108 | */ |
---|
109 | public static boolean isBuiltInCall(ASTNode node) { |
---|
110 | |
---|
111 | for (Builtins builtin : Builtins.values()) { |
---|
112 | if (isCall(node, BuiltinsUtil.BUILTIN_PACKAGE_NAME, builtin.pabloName(), builtin.argCount())) { |
---|
113 | return true; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | return false; |
---|
118 | } |
---|
119 | |
---|
120 | /** Returns true if the node represents a call to the |
---|
121 | * given Func with the given number of Args. |
---|
122 | * If node does not have a packageName attached, it is assumed to match the given packageName. |
---|
123 | * |
---|
124 | * @param node node to be checked |
---|
125 | * @param packageName package name that must be matched. |
---|
126 | * @param FuncName name of Func to be matched |
---|
127 | * @param numArgs number of Args to be matched |
---|
128 | * @return whether the node is a call of the Func with argNumToMatch Args |
---|
129 | */ |
---|
130 | public static boolean isCall(ASTNode node, String packageName, String FuncName, int numArgs) { |
---|
131 | if(!(node instanceof FuncCallNode)) { |
---|
132 | return false; |
---|
133 | } |
---|
134 | |
---|
135 | FuncCallNode fNode = (FuncCallNode)node; |
---|
136 | ASTNode nameNode = Accessors.funcCallIdentifier(fNode); |
---|
137 | |
---|
138 | boolean nameMatches = isQualifiedName(nameNode, packageName, FuncName); |
---|
139 | boolean numArgsMatches = Accessors.funcCallArgsListNode(fNode).nChildren() == numArgs; |
---|
140 | |
---|
141 | return nameMatches && numArgsMatches; |
---|
142 | } |
---|
143 | |
---|
144 | /** Determines if node represents the name of the Func described by packageName, FuncName. |
---|
145 | * In particular, if node represents a simple (non-qualified) name, then it is compared with FuncName. |
---|
146 | * (Essentially, this assumes that the packageName is correct.) |
---|
147 | * If node represents a compound (qualified) name, then it is compared with 'packageName.FuncName'. |
---|
148 | * |
---|
149 | * @param node |
---|
150 | * @param packageName |
---|
151 | * @param FuncName |
---|
152 | * @return True if the node matches packageName.FuncName. |
---|
153 | */ |
---|
154 | public static boolean isQualifiedName(ASTNode node, String packageName, String FuncName) { |
---|
155 | if(node instanceof IdentifierNode) { |
---|
156 | return Accessors.name(node) == FuncName; |
---|
157 | } |
---|
158 | else if (node instanceof CompoundIdentifierNode) { |
---|
159 | ASTNode pckage = node.child(0); |
---|
160 | ASTNode member = node.child(1); |
---|
161 | |
---|
162 | return (Accessors.name(pckage) == packageName) && |
---|
163 | (Accessors.name(member) == FuncName); |
---|
164 | } |
---|
165 | return false; |
---|
166 | } |
---|
167 | |
---|
168 | } |
---|