@@ -1912,7 +1912,8 @@ public static ParseTree compile(TokenStream stream, Environment environment,
1912
1912
Stack <List <Procedure >> procs = new Stack <>();
1913
1913
procs .add (new ArrayList <>());
1914
1914
processKeywords (tree , environment , compilerErrors );
1915
- addSelfStatements (tree , environment , envs , compilerErrors );
1915
+ addSelfStatements (rootNode , environment , envs , compilerErrors ); // Pass rootNode since this might rewrite 'tree'.
1916
+ tree = rootNode .getChildAt (0 );
1916
1917
rewriteAutoconcats (tree , environment , envs , compilerErrors , true );
1917
1918
processLateKeywords (tree , environment , compilerErrors );
1918
1919
checkLinearComponents (tree , environment , compilerErrors );
@@ -2075,10 +2076,19 @@ private static void moveNodeModifiersOffSyntheticNodes(ParseTree node) {
2075
2076
}
2076
2077
}
2077
2078
2078
- private static void addSelfStatements (ParseTree root , Environment env ,
2079
+ /**
2080
+ * Adds semicolon AST nodes after self statements within children of the given AST node (following
2081
+ * {@link Function#isSelfStatement(Target, Environment, List, Set)}).
2082
+ * When the self statement is not yet within an {@link __autoconcat__}, it is wrapped into one.
2083
+ * @param ast - The abstract syntax tree.
2084
+ * @param env - The environment.
2085
+ * @param envs - The set of expected environment classes at runtime.
2086
+ * @param compilerErrors - A set to put compile errors in.
2087
+ */
2088
+ private static void addSelfStatements (ParseTree ast , Environment env ,
2079
2089
Set <Class <? extends Environment .EnvironmentImpl >> envs , Set <ConfigCompileException > compilerErrors ) {
2080
- for (int i = 0 ; i < root .numberOfChildren (); i ++) {
2081
- ParseTree node = root .getChildAt (i );
2090
+ for (int i = 0 ; i < ast .numberOfChildren (); i ++) {
2091
+ ParseTree node = ast .getChildAt (i );
2082
2092
boolean isSelfStatement ;
2083
2093
try {
2084
2094
isSelfStatement = node .getData () instanceof CFunction cf
@@ -2090,16 +2100,21 @@ private static void addSelfStatements(ParseTree root, Environment env,
2090
2100
return ;
2091
2101
}
2092
2102
if (isSelfStatement ) {
2093
- int offset = i + 1 ;
2094
- if (!(root .getData () instanceof CFunction cf && cf .val ().equals (Compiler .__autoconcat__ .NAME ))) {
2095
- // We need to create an autoconcat node first, and put this and the semicolon in that
2096
- ParseTree newNode = new ParseTree (new CFunction (Compiler .__autoconcat__ .NAME , Target .UNKNOWN ), root .getFileOptions (), true );
2097
- newNode .addChild (root );
2098
- root = newNode ;
2099
- offset = 1 ;
2100
- }
2101
- root .getChildren ().add (offset , new ParseTree (new CSemicolon (Target .UNKNOWN ),
2102
- node .getFileOptions (), true ));
2103
+ if (ast .getData () instanceof CFunction cf && cf .val ().equals (Compiler .__autoconcat__ .NAME )) {
2104
+
2105
+ // Add semicolon to existing autoconcat.
2106
+ ast .getChildren ().add (i + 1 , new ParseTree (
2107
+ new CSemicolon (Target .UNKNOWN ), node .getFileOptions (), true ));
2108
+ i ++; // Skip added semicolon.
2109
+ } else {
2110
+
2111
+ // Replace node with an autoconcat that contains the node and a semicolon.
2112
+ ParseTree newNode = new ParseTree (new CFunction (
2113
+ Compiler .__autoconcat__ .NAME , Target .UNKNOWN ), ast .getFileOptions (), true );
2114
+ newNode .addChild (node );
2115
+ newNode .addChild (new ParseTree (new CSemicolon (Target .UNKNOWN ), node .getFileOptions (), true ));
2116
+ ast .getChildren ().set (i , newNode );
2117
+ }
2103
2118
}
2104
2119
addSelfStatements (node , env , envs , compilerErrors );
2105
2120
}
0 commit comments