001 /**
002 * @author paulk, Jul 20, 2002
003 */
004 package toolbus.atom;
005
006 import java.util.Stack;
007
008 import toolbus.Functions;
009 import toolbus.State;
010 import toolbus.TBTermFactory;
011 import toolbus.TBTermVar;
012 import toolbus.environment.Environment;
013 import toolbus.exceptions.ToolBusException;
014 import toolbus.exceptions.ToolBusExecutionException;
015 import toolbus.parsercup.PositionInformation;
016 import toolbus.process.ProcessExpression;
017 import toolbus.process.ProcessInstance;
018 import aterm.ATerm;
019
020 public class Assign extends Atom{
021 private final Ref var;
022 private final Ref exp;
023
024 public Assign(ATerm v, ATerm e, TBTermFactory tbfactory, PositionInformation posInfo){
025 super(tbfactory, posInfo);
026
027 var = new Ref(v);
028 exp = new Ref(e);
029 setAtomArgs(new Ref[]{var, exp});
030 }
031
032 public ProcessExpression copy(){
033 Atom a = new Assign(var.value, exp.value, tbfactory, getPosInfo());
034 a.copyAtomAttributes(this);
035 return a;
036 }
037
038 public void replaceFormals(Environment env) throws ToolBusException{
039 // env = env.copy();
040 setEnv(env);
041 // System.err.println("Assign.replaceformals: " + var.value + "; " + exp.value);
042 // System.err.println("env = " + env);
043 env.setAssignable((TBTermVar) var.value);
044
045 // System.err.println("Assign.replaceformals: " + var.value);
046 var.value = tbfactory.replaceAssignableVar((TBTermVar) var.value, env);
047
048 exp.value = tbfactory.replaceFormals(exp.value, env);
049 // System.err.println("Assign.replaceformals: => " + var.value + "; " + exp.value);
050 // System.err.println("Assign.replaceformals: env = "+env);;
051 }
052
053 public void compile(ProcessInstance P, Stack<String> calls, State follow) throws ToolBusException{
054 super.compile(P, calls, follow);
055 // System.err.println("Assign.compile: " + this + " env = " + getEnv());
056 if(!tbfactory.isAnyVar(var.value)) throw new ToolBusExecutionException("Left-hand side of := should be a variable.", getPosInfo());
057 ATerm vartype = ((TBTermVar) var.value).getVarType();
058
059 // System.err.println(this + "; var = " + var +"; vartype = " + vartype);
060
061 // System.err.println("Assign: " + env);
062 ATerm exptype = Functions.checkType(exp.value, getEnv(), false);
063 // System.err.println(this + "; exp = " + exp.value + "; exptype = " + exptype);
064
065 if(!Functions.compatibleTypes(vartype, exptype)){// lhs = term!
066 throw new ToolBusExecutionException("Wrong types in assignment "+this+"; "+vartype+" := "+exptype, getPosInfo());
067 }
068 }
069
070 public boolean execute() throws ToolBusException{
071 if(!isEnabled()) return false;
072 ProcessInstance p = getProcess();
073 Environment env = getEnv();
074 // System.err.println("Atom.execute: "+ this);
075 // System.err.println("Assign: " + env);
076
077 ATerm newval = Functions.eval(exp.value, p, env);
078
079 // System.err.println("Assign.execute: " + exp.value + "; " + newval);
080 env.assignVar((TBTermVar) var.value, newval);
081 // System.err.println("Assign: " + env);
082 // System.err.println("Assign: " + getFollow());
083 return true;
084 }
085 }