Files
2024-08-31 12:07:21 +03:00

42 lines
1.3 KiB
Java

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<String, IValue> 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<String, IType> typeCheck(IDictionary<String, IType> typeTable) throws BaseException {
typeTable.add(variableName, variableType);
return typeTable;
}
}