/*
* Copyright (c) 2010 Mathew Hall, University of Sheffield.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* Neither the name of the University of Sheffield nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package gpinterpreter;
import gpinterpreter.vector.VecInstruction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static gpinterpreter.vector.VecSymbol.*;
/**
* Parse a String program into a list of Instructions.
* This class is only useful for deserialisation.
* @author mat
*
*/
public abstract class GPParser {
public static List<VecInstruction> parse(String program) throws ParseErrorException{
String[] lst = program.split("[ \t\n\r]+");
ArrayList<VecInstruction> p = new ArrayList<VecInstruction>();
do{
Tuple<VecInstruction,String[]> res = parseExpr(lst);
p.add(res.fst());
lst = res.snd();
}while(lst.length != 0);
return p;
}
private static Tuple<VecInstruction,String[]> parseExpr(String[] tokens) throws ParseErrorException{
if(tokens[0].equals("NOP") || tokens[0].equals(""))
return new Tuple<VecInstruction,String[]>(new VecInstruction(NOP),Arrays.copyOfRange(tokens,1, tokens.length));
if(tokens[0].equals("SPLIT")){
int operand1;
try{
operand1 = Integer.parseInt(tokens[1]);
}catch(Exception e){
throw new ParseErrorException("parse failed on operands of SPLIT " + tokens[1]);
}
return new Tuple<VecInstruction,String[]>(new VecInstruction(SPLIT,operand1),Arrays.copyOfRange(tokens,2, tokens.length));
}
if(tokens[0].equals("MERGE")){
//tokens = Arrays.copyOfRange(tokens,1, tokens.length);
int operand1, operand2;
try{
operand1 = Integer.parseInt(tokens[1]);
operand2 = Integer.parseInt(tokens[2]);
}catch(Exception e){
throw new ParseErrorException("parse failed on operands of MERGE " + tokens[1] + " " + tokens[2]);
}
return new Tuple<VecInstruction,String[]>(new VecInstruction(MERGE,operand1,operand2),Arrays.copyOfRange(tokens,3, tokens.length));
}
if(tokens[0].equals("MOVE")){
int operand1, operand2;
try{
operand1 = Integer.parseInt(tokens[1]);
operand2 = Integer.parseInt(tokens[2]);
}catch(Exception e){
String toks = "";
for(String s : tokens) toks += ","+s;
throw new ParseErrorException("parse failed on operands of MOVE " + tokens.length + "[" + toks +"]" );
}
return new Tuple<VecInstruction,String[]>(new VecInstruction(MOVE,operand1,operand2),Arrays.copyOfRange(tokens,3, tokens.length));
}
throw new ParseErrorException("Unexpected symbol <" + tokens[0] + ">");
}
}