public override Type Typecheck(TypecheckingState state) { Type = Expression.Typecheck(state); state.EnterScope(); state.BindVariable(Name, Type); var type = Recipient.Typecheck(state); state.ExitScope(); return(type); }
// public string Name; public override Type Typecheck(TypecheckingState state) { try { var type = state.LookupVariable(Name); return(type); } catch (TypechekingStateException e) { TypeError(e.Message); } throw new ImpossibleException(); }
// public string Name; // public Expression Argument; public override Type Typecheck(TypecheckingState state) { try { var fun = state.LookupFunction(Name); var argumentType = Argument.Typecheck(state); if (argumentType != fun.ArgumentType) { TypeError($"Argument type mismatch in call to {Name}"); } return(fun.ReturnType); } catch (TypechekingStateException e) { TypeError(e.Message); } throw new ImpossibleException(); }
public override Type Typecheck(TypecheckingState state) { var type1 = Expression1.Typecheck(state); var type2 = Expression2.Typecheck(state); OperandType = type1; switch (Operator) { case BinaryOperator.Add: if (type1 != Type.NumType && type2 != Type.NumType) { TypeError("+ expects number operands"); } return(Type.NumType); case BinaryOperator.Sub: if (type1 != Type.NumType && type2 != Type.NumType) { TypeError("- expects number operands"); } return(Type.NumType); case BinaryOperator.Eq: if (type1 != type2) { TypeError("== expects operands of the same type"); } return(Type.BoolType); case BinaryOperator.Lt: if (type1 != Type.NumType && type2 != Type.NumType) { TypeError("- expects number operands"); } return(Type.BoolType); } throw new ImpossibleException(); }
// public string Name; // public string ArgumentName; // public Type ArgumentType; // public Type ReturnType; // public Expression Body; // public Expression Recipient; public override Type Typecheck(TypecheckingState state) { state.EnterScope(); state.BindFunction(Name, this); // type check the body once state.EnterScope(); state.BindVariable(ArgumentName, ArgumentType); var returnType = Body.Typecheck(state); if (returnType != ReturnType) { TypeError("Return type mismatch"); } state.ExitScope(); var type = Recipient.Typecheck(state); state.ExitScope(); return(type); }
public abstract Type Typecheck(TypecheckingState state);
// public bool Value; public override Type Typecheck(TypecheckingState state) { return(Type.BoolType); }
// public Expression Expression1; // public Expression Expression2; public override Type Typecheck(TypecheckingState state) { Expression1.Typecheck(state); return(Expression2.Typecheck(state)); }
public override Type Typecheck(TypecheckingState state) { throw new NotImplementedException(); }