Skip to content
Snippets Groups Projects
Commit 19157b15 authored by dgelessus's avatar dgelessus
Browse files

Determine first/last popped nodes statically in parser new methods

This saves having to update the firstPopped/lastPopped instance
variables at runtime on every pop call.
parent be8ff931
No related branches found
No related tags found
No related merge requests found
......@@ -30,6 +30,8 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter
private LinkedList listSimpleTermTransform;
private Map<Node, String> simpleTermOrsimpleListTermTypes;
private int popCount;
GenerateAlternativeCodeForParser(File pkgDir, String aParsedAltName,
String raParsedAltName,
BufferedWriter file,
......@@ -52,6 +54,13 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter
this.macros = macros;
this.listSimpleTermTransform = listSimpleTermTransform;
this.simpleTermOrsimpleListTermTypes = simpleTermOrsimpleListTermTypes;
this.popCount = 0;
}
@Override
public void caseAElem(final AElem node) {
popCount++;
}
@Override
......@@ -134,6 +143,10 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter
macros.apply(file, "ParserNewBodyListAdd", new String[] {type_name, "" + position});
}
if (popCount > 0) {
// The nodeArrayList variables are numbered starting at 1, so the first popped variable has the number popCount and not popCount-1.
macros.apply(file, "ParserNewCheck", new String[] {String.valueOf(popCount)});
}
macros.apply(file, "ParserNewTail");
}
catch(IOException e)
......
This diff is collapsed.
......@@ -39,9 +39,6 @@ public class Parser implements IParser
private final static int ACCEPT = 2;
private final static int ERROR = 3;
private List<Object> firstPopped = null;
private List<Object> lastPopped = null;
public Parser(Lexer lexer)
{
this.lexer = lexer;
......@@ -252,13 +249,7 @@ Macro:ParserCommon
private List<Object> pop()
{
List<Object> list = this.stack.previous().nodes;
if (this.firstPopped == null) {
this.firstPopped = list;
} else {
this.lastPopped = list;
}
return list;
return this.stack.previous().nodes;
}
private int index(Switchable token)
......@@ -375,8 +366,6 @@ Macro:ParserNewHeader
private List<Object> new$0$() /* reduce $1$ */
{
this.firstPopped = null;
this.lastPopped = null;
final boolean addElementsToNewList = addElementsFromListToNewList("$1$");
List<Object> nodeList = new ArrayList<>();
......@@ -470,8 +459,12 @@ Macro:ParserNewBodyListAdd
$
Macro:ParserNewCheck
checkResult(nodeList.get(0), nodeArrayList1, nodeArrayList$0$);
$
Macro:ParserNewTail
checkResult(nodeList.get(0), this.lastPopped, this.firstPopped);
return nodeList;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment