public Type Check() { var typeCheckingEnvironment = new TypeCheckingEnvironment(); _functionEnvironment.Check(typeCheckingEnvironment, _functionEnvironment); return(_expression.Check(typeCheckingEnvironment, _functionEnvironment)); }
public void Check(TypeCheckingEnvironment typeCheckingEnvironment, FunctionEnvironment functionEnvironment) { foreach (var f in _functions.Values) { f.Check(typeCheckingEnvironment, functionEnvironment); } }
public override Type Check(TypeCheckingEnvironment env, FunctionEnvironment fEnv) { Type t1 = e1.Check(env, fEnv); env.DeclareLocal(name, t1); Type t2 = e2.Check(env, fEnv); env.Pop(); return(t2); }
public override Type Check(TypeCheckingEnvironment typeCheckingEnvironment, FunctionEnvironment functionEnvironment) { var argumentType = _arg.Check(typeCheckingEnvironment, functionEnvironment); var function = functionEnvironment.GetFunction(_name); if (function.CheckArgType(argumentType)) { return function.ReturnType; } throw new InvalidOperationException(string.Format("Type error in call of function {0}", _name)); }
public void Check(TypeCheckingEnvironment typeCheckingEnvironment, FunctionEnvironment functionEnvironment) { typeCheckingEnvironment.DeclareLocal(_argument.Item1, _argument.Item2); var t = _body.Check(typeCheckingEnvironment, functionEnvironment); typeCheckingEnvironment.Pop(); if (t != _returnType) { throw new InvalidOperationException(string.Format("Body of {0} returns {1}, {2} expected", _name, t, _returnType)); } }
public override Type Check(TypeCheckingEnvironment typeCheckingEnvironment, FunctionEnvironment functionEnvironment) { var t1 = _e1.Check(typeCheckingEnvironment, functionEnvironment); var t2 = _e2.Check(typeCheckingEnvironment, functionEnvironment); switch (_op) { case Operator.Add: case Operator.Div: case Operator.Mul: case Operator.Sub: if (t1 == Type.IntegerType && t2 == Type.IntegerType) { return(Type.IntegerType); } throw new InvalidOperationException("Arguments to + - * / must be int"); case Operator.Mod: //Balazs if (t1 == Type.IntegerType && t2 == Type.IntegerType) { return(Type.IntegerType); } else { throw new InvalidOperationException("Arguments to + - * / % must be int"); } case Operator.Eq: case Operator.Ge: case Operator.Gt: case Operator.Le: case Operator.Lt: case Operator.Ne: if (t1 == Type.IntegerType && t2 == Type.IntegerType) { return(Type.BooleanType); } throw new InvalidOperationException("Arguments to == >= > <= < != must be int"); case Operator.Or: case Operator.And: if (t1 == Type.BooleanType && t2 == Type.BooleanType) { return(Type.BooleanType); } throw new InvalidOperationException("Arguments to & must be bool"); default: throw new InvalidOperationException(string.Format("Unknown binary operator: {0}", _op)); } }
public override Type Check(TypeCheckingEnvironment env, FunctionEnvironment fEnv) { Type t1 = e1.Check(env, fEnv); Type t2 = e2.Check(env, fEnv); Type t3 = e3.Check(env, fEnv); if (t1 != Type.BooleanType) { throw new InvalidOperationException("condition must be boolean"); } if (t2 != t3) { throw new InvalidOperationException("the two types must be the same"); } return(t2); }
public void Check(TypeCheckingEnvironment env, FunctionEnvironment fEnv) { foreach (var arg in args) { env.DeclareLocal(arg.Item1, arg.Item2); } Type t = body.Check(env, fEnv); foreach (var arg in args) { env.Pop(); } if (t != returnType) { throw new InvalidOperationException("Body of " + fName + " returns " + t + ", " + returnType + " expected"); } }
public override Type Check(TypeCheckingEnvironment typeCheckingEnvironment, FunctionEnvironment functionEnvironment) { var t1 = _expression.Check(typeCheckingEnvironment, functionEnvironment); switch (_op) { case Operator.Neg: if (t1 == Type.IntegerType) { return Type.IntegerType; } throw new InvalidOperationException("Argument to - must be int"); case Operator.Not: if (t1 == Type.BooleanType) { return Type.BooleanType; } throw new InvalidOperationException("Argument to ! must be bool"); default: throw new InvalidOperationException(string.Format("Unknown unary operator: {0}", _op)); } }
public override Type Check(TypeCheckingEnvironment env, FunctionEnvironment fenv) { Type[] parameterTypes = new Type[_args.Count]; int index = 0; FunctionDefinition fDef = fenv.GetFunction(fName); foreach (Expression expression in _args) { parameterTypes[index++] = expression.Check(env, fenv); } if (fDef.CheckArgType(parameterTypes)) { return(fDef.returnType); } else { throw new InvalidOperationException("Type error in call of function " + fName); } }
public override Type Check(TypeCheckingEnvironment typeCheckingEnvironment, FunctionEnvironment functionEnvironment) { var t1 = _e1.Check(typeCheckingEnvironment, functionEnvironment); var t2 = _e2.Check(typeCheckingEnvironment, functionEnvironment); switch (_op) { case Operator.Add: case Operator.Div: case Operator.Mul: case Operator.Sub: if (t1 == Type.IntegerType && t2 == Type.IntegerType) { return Type.IntegerType; } throw new InvalidOperationException("Arguments to + - * / must be int"); case Operator.Eq: case Operator.Ge: case Operator.Gt: case Operator.Le: case Operator.Lt: case Operator.Ne: if (t1 == Type.IntegerType && t2 == Type.IntegerType) { return Type.BooleanType; } throw new InvalidOperationException("Arguments to == >= > <= < != must be int"); case Operator.Or: case Operator.And: if (t1 == Type.BooleanType && t2 == Type.BooleanType) { return Type.BooleanType; } throw new InvalidOperationException("Arguments to & must be bool"); default: throw new InvalidOperationException(string.Format("Unknown binary operator: {0}", _op)); } }
public override Type Check(TypeCheckingEnvironment typeCheckingEnvironment, FunctionEnvironment functionEnvironment) { var t1 = _expression.Check(typeCheckingEnvironment, functionEnvironment); switch (_op) { case Operator.Neg: if (t1 == Type.IntegerType) { return(Type.IntegerType); } throw new InvalidOperationException("Argument to - must be int"); case Operator.Not: if (t1 == Type.BooleanType) { return(Type.BooleanType); } throw new InvalidOperationException("Argument to ! must be bool"); default: throw new InvalidOperationException(string.Format("Unknown unary operator: {0}", _op)); } }
public Type Check() { var typeCheckingEnvironment = new TypeCheckingEnvironment(); _functionEnvironment.Check(typeCheckingEnvironment, _functionEnvironment); return _expression.Check(typeCheckingEnvironment, _functionEnvironment); }