Package scriptingLanguage.frames

Source Code of scriptingLanguage.frames.ObjectFrame

package scriptingLanguage.frames;

import lipstone.joshua.customStructures.tuples.Pair;
import scriptingLanguage.Interpreter;
import scriptingLanguage.Token;
import scriptingLanguage.errors.InterpreterException;
import scriptingLanguage.errors.InvalidAssignmentException;
import scriptingLanguage.errors.VariableNotFoundException;
import scriptingLanguage.variables.AbstractClass;
import scriptingLanguage.variables.Variable;

public class ObjectFrame extends Frame {
  private final AbstractFrame staticFrame;
 
  public ObjectFrame(AbstractFrame previous, Interpreter interpreter, AbstractFrame staticFrame) {
    super(previous, interpreter);
    this.staticFrame = staticFrame;
  }
 
  @Override
  public boolean writeVariable(String variable, Token value, boolean isNew) throws InterpreterException {
    if (isNew) {
      if (variables.containsKey(variable))
        throw new InvalidAssignmentException("The a variable called " + variable + " already exists in this frame.");
      variables.put(variable, new Pair<AbstractClass<?>, Token>(((Variable<?>) value.getCar()).getType(), value));
      return true;
    }
    try {
      return super.writeVariable(variable, value, false);
    }
    catch (VariableNotFoundException e) {
      //else if (previous != null && !(previous instanceof RootFrame))
      //return previous.writeVariable(variable, value, false); //Cannot be a first assignment
      //else
      return staticFrame.writeVariable(variable, value, false); //Cannot be a first assignment
    }
  }
 
  @Override
  public Token readVariable(String variable) throws InterpreterException {
    try {
      return super.readVariable(variable);
    }
    catch (VariableNotFoundException e) {
      return staticFrame.readVariable(variable);
    }
  }
}
TOP

Related Classes of scriptingLanguage.frames.ObjectFrame

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.