Skip to content

Commit 983b153

Browse files
committed
Fix checking functions too early during compile
Caused a compile error when a function did not exist but was inside a proper function_exists() or extension_exists() code block.
1 parent 507e0f0 commit 983b153

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

src/main/java/com/laytonsmith/core/functions/Compiler.java

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ public static ParseTree rewrite(List<ParseTree> list, boolean returnSConcat,
220220
// add all preceding symbols
221221
while(list.size() > index + 1 && (list.get(index).getData() instanceof CSymbol
222222
|| (list.get(index).getData() instanceof CFunction cf
223-
&& cf.hasFunction() && cf.getFunction() != null
224-
&& cf.getFunction().getName().equals(Compiler.p.NAME)
223+
&& cf.val().equals(Compiler.p.NAME)
225224
&& list.get(index).numberOfChildren() == 1
226225
&& (list.get(index).getChildAt(0).getData() instanceof CClassType
227226
|| __type_ref__.createFromBareStringOrConcats(
@@ -359,8 +358,8 @@ public static ParseTree rewrite(List<ParseTree> list, boolean returnSConcat,
359358
// Rewrite cast operator.
360359
for(int i = list.size() - 2; i >= 0; i--) {
361360
ParseTree node = list.get(i);
362-
if(node.getData() instanceof CFunction cf && cf.hasFunction() && cf.getFunction() != null
363-
&& cf.getFunction().getName().equals(Compiler.p.NAME) && node.numberOfChildren() == 1) {
361+
if(node.getData() instanceof CFunction cf && cf.val().equals(Compiler.p.NAME)
362+
&& node.numberOfChildren() == 1) {
364363

365364
// Convert bare string or concat() to type reference if needed.
366365
ParseTree typeNode = node.getChildAt(0);
@@ -649,36 +648,28 @@ private static void rewriteParenthesis(List<ParseTree> list) throws ConfigCompil
649648
Stack<ParseTree> executes = new Stack<>();
650649
while(listInd > 0) {
651650
ParseTree node = list.get(listInd);
652-
try {
653-
if(node.getData() instanceof CFunction cf
654-
&& cf.hasFunction()
655-
&& cf.getFunction() != null
656-
&& cf.getFunction().getName().equals(Compiler.p.NAME)) {
657-
ParseTree prevNode = list.get(listInd - 1);
658-
Mixed prevNodeVal = prevNode.getData();
659-
660-
// Do not rewrite parenthesis like "@a = (1);" or "key: (value)" to execute().
661-
if(prevNodeVal instanceof CSymbol
662-
|| prevNodeVal instanceof CLabel || prevNodeVal instanceof CString) {
663-
break;
664-
}
651+
if(!(node.getData() instanceof CFunction cf && cf.val().equals(Compiler.p.NAME))) {
652+
break;
653+
}
654+
ParseTree prevNode = list.get(listInd - 1);
655+
Mixed prevNodeVal = prevNode.getData();
665656

666-
// Do not rewrite casts to execute() if the callable is the cast (i.e. "(type) (val)").
667-
if(prevNodeVal instanceof CFunction cfunc && cfunc.hasFunction() && cfunc.getFunction() != null
668-
&& cfunc.getFunction().getName().equals(Compiler.p.NAME) && prevNode.numberOfChildren() == 1
669-
&& (prevNode.getChildAt(0).getData().isInstanceOf(CClassType.TYPE)
670-
|| __type_ref__.createFromBareStringOrConcats(prevNode.getChildAt(0)) != null)) {
671-
break;
672-
}
657+
// Do not rewrite parenthesis like "@a = (1);" or "key: (value)" to execute().
658+
if(prevNodeVal instanceof CSymbol
659+
|| prevNodeVal instanceof CLabel || prevNodeVal instanceof CString) {
660+
break;
661+
}
673662

674-
executes.push(node);
675-
list.remove(listInd--);
676-
} else {
677-
break;
678-
}
679-
} catch (ConfigCompileException e) {
680-
break; // The function does not exist. Ignore and handle as "not a p()".
663+
// Do not rewrite casts to execute() if the callable is the cast (i.e. "(type) (val)").
664+
if(prevNodeVal instanceof CFunction cfunc && cfunc.val().equals(Compiler.p.NAME)
665+
&& prevNode.numberOfChildren() == 1
666+
&& (prevNode.getChildAt(0).getData().isInstanceOf(CClassType.TYPE)
667+
|| __type_ref__.createFromBareStringOrConcats(prevNode.getChildAt(0)) != null)) {
668+
break;
681669
}
670+
671+
executes.push(node);
672+
list.remove(listInd--);
682673
}
683674
if(!executes.isEmpty()) {
684675
if(listInd >= 0) {

0 commit comments

Comments
 (0)