package Models.Statements; import Exceptions.BaseException; import Models.Program.IDictionary; import Models.Program.ProgramState; import Models.Types.IType; import Models.Values.IValue; public class VariableDeclarationStatement implements IStatement { private String variableName; private IType variableType; public VariableDeclarationStatement(String variableName, IType variableType) { this.variableName = variableName; this.variableType = variableType; } public String toString() { return variableType.toString() + " " + variableName; } public IStatement copy() { return new VariableDeclarationStatement(variableName, variableType); } public ProgramState execute(ProgramState state) throws BaseException { IDictionary symbolTable = state.getSymbolTable(); if(!symbolTable.contains(variableName)) { symbolTable.add(variableName, variableType.defaultValue()); } else { throw new BaseException("Variable " + variableName + " is already defined!"); } return null; } public IDictionary typeCheck(IDictionary typeTable) throws BaseException { typeTable.add(variableName, variableType); return typeTable; } }