package com.example.Models.Statements;

import com.example.Exceptions.BaseException;
import com.example.Models.Program.IDictionary;
import com.example.Models.Program.ProgramState;
import com.example.Models.Types.IType;
import com.example.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;
    }
}
