public void Check(TypeCheckingEnvironment typeCheckingEnvironment, FunctionEnvironment functionEnvironment)
 {
     foreach (var f in _functions.Values)
     {
         f.Check(typeCheckingEnvironment, functionEnvironment);
     }
 }
 public void Check(TypeCheckingEnvironment typeCheckingEnvironment, FunctionEnvironment functionEnvironment)
 {
     foreach (var f in _functions.Values)
     {
         f.Check(typeCheckingEnvironment, functionEnvironment);
     }
 }
示例#3
0
 public int Eval(RuntimeEnvironment runtimeEnvironment, FunctionEnvironment functionEnvironment, int argument)
 {
     runtimeEnvironment.AllocateLocal(_argument.Item1);
     runtimeEnvironment.GetVariable(_argument.Item1).Value = argument;
     var v = _body.Eval(runtimeEnvironment, functionEnvironment);
     runtimeEnvironment.Pop();
     return v;
 }
示例#4
0
        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);
        }
示例#5
0
 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));
 }
示例#6
0
 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));
     }
 }
示例#7
0
 public override int Eval(RuntimeEnvironment env, FunctionEnvironment fEnv)
 {
     if (e1.Eval(env, fEnv) == 1)
     {
         return(e2.Eval(env, fEnv));
     }
     else
     {
         return(e3.Eval(env, fEnv));
     }
 }
示例#8
0
 public override int Eval(RuntimeEnvironment runtimeEnvironment, FunctionEnvironment functionEnvironment)
 {
     var value = _expression.Eval(runtimeEnvironment, functionEnvironment);
     switch (_op)
     {
         case Operator.Not:
             return value == 0 ? 1 : 0;
         case Operator.Neg:
             return -value;
         default:
             throw new InvalidOperationException(string.Format("Unknown unary operator: {0}", _op));
     }
 }
        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 int Eval(RuntimeEnvironment runtimeEnvironment, FunctionEnvironment functionEnvironment)
        {
            var v1 = _e1.Eval(runtimeEnvironment, functionEnvironment);
            var v2 = _e2.Eval(runtimeEnvironment, functionEnvironment);

            switch (_op)
            {
            case Operator.Add:
                return(v1 + v2);

            case Operator.Div:
                return(v1 / v2);

            case Operator.Mul:
                return(v1 * v2);

            case Operator.Sub:
                return(v1 - v2);

            case Operator.Eq:
                return(v1 == v2 ? 1 : 0);

            case Operator.Ne:
                return(v1 != v2 ? 1 : 0);

            case Operator.Lt:
                return(v1 < v2 ? 1 : 0);

            case Operator.Le:
                return(v1 <= v2 ? 1 : 0);

            case Operator.Gt:
                return(v1 > v2 ? 1 : 0);

            case Operator.Ge:
                return(v1 >= v2 ? 1 : 0);

            case Operator.And:
                return(v1 == 0 ? 0 : v2);

            case Operator.Or:
                return(v1 == 0 ? v2 : 1);

            case Operator.Mod:          //Balazs
                return(v1 % v2);

            default:
                throw new InvalidOperationException(string.Format("Unknown binary operator: {0}", _op));
            }
        }
示例#11
0
        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);
        }
示例#12
0
        public override int Eval(RuntimeEnvironment env, FunctionEnvironment fEnv)
        {
            int value = e1.Eval(env, fEnv);

            env.AllocateLocal(name);                    // It pushes a new Pair<String, Storage>(name, new Storage()) to the stack called locals. The Storage value is 0 or null the String name is the type the storage.value is the value.

            Storage storage = env.GetVariable(name);    // this method returns a Storage object. The storage variable that we create here, is pointing to the same object as the other Storage variable in the Stack<Pair<String, Storage> locals.

            storage.Value = value;                      // The Storage has only a value field. If we modify the value of this Storage variable, the value of the object will be modofied.

            int result = e2.Eval(env, fEnv);

            env.Pop();

            return(result);
        }
示例#13
0
        public override int Eval(RuntimeEnvironment runtimeEnvironment, FunctionEnvironment functionEnvironment)
        {
            var value = _expression.Eval(runtimeEnvironment, functionEnvironment);

            switch (_op)
            {
            case Operator.Not:
                return(value == 0 ? 1 : 0);

            case Operator.Neg:
                return(-value);

            default:
                throw new InvalidOperationException(string.Format("Unknown unary operator: {0}", _op));
            }
        }
        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");
            }
        }
示例#15
0
        public override int Eval(RuntimeEnvironment env, FunctionEnvironment fenv)
        {
            int[] values;
            if (_args.Count > 0)
            {
                values = new int[_args.Count];
                int index = 0;
                foreach (Expression e in _args)
                {
                    values[index++] = e.Eval(env, fenv);
                }
            }
            else
            {
                values = new int[0];
            }
            FunctionDefinition fDef = fenv.GetFunction(fName);
            int result = fDef.Eval(env, fenv, values);

            return(result);
        }
示例#16
0
        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);
            }
        }
示例#17
0
 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));
     }
 }
示例#18
0
        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 int Eval(RuntimeEnvironment env, FunctionEnvironment fenv, int[] values)
 {
     if (args.Count > 0)
     {
         int index = 0;
         foreach (Tuple <string, Type> tuple in args)
         {
             env.AllocateLocal(tuple.Item1);
             env.GetVariable(tuple.Item1).Value = values[index++];
         }
         int v = body.Eval(env, fenv);
         foreach (Tuple <string, Type> tuple in args)
         {
             env.Pop();
         }
         return(v);
     }
     else
     {
         int v = body.Eval(env, fenv);
         return(v);
     }
 }
示例#20
0
        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));
            }
        }
示例#21
0
        public override int Eval(RuntimeEnvironment runtimeEnvironment, FunctionEnvironment functionEnvironment)
        {
            var v1 = _e1.Eval(runtimeEnvironment, functionEnvironment);
            var v2 = _e2.Eval(runtimeEnvironment, functionEnvironment);

            switch (_op)
            {
                case Operator.Add:
                    return v1 + v2;
                case Operator.Div:
                    return v1 / v2;
                case Operator.Mul:
                    return v1 * v2;
                case Operator.Sub:
                    return v1 - v2;
                case Operator.Eq:
                    return v1 == v2 ? 1 : 0;
                case Operator.Ne:
                    return v1 != v2 ? 1 : 0;
                case Operator.Lt:
                    return v1 < v2 ? 1 : 0;
                case Operator.Le:
                    return v1 <= v2 ? 1 : 0;
                case Operator.Gt:
                    return v1 > v2 ? 1 : 0;
                case Operator.Ge:
                    return v1 >= v2 ? 1 : 0;
                case Operator.And:
                    return v1 == 0 ? 0 : v2;
                case Operator.Or:
                    return v1 == 0 ? v2 : 1;
                default:
                    throw new InvalidOperationException(string.Format("Unknown binary operator: {0}", _op));
            }
        }
示例#22
0
 public Program(IDictionary <string, FunctionDefinition> functions, Expression expression)
 {
     _functionEnvironment = new FunctionEnvironment(functions);
     _expression          = expression;
 }
示例#23
0
 public override int Eval(RuntimeEnvironment runtimeEnvironment, FunctionEnvironment functionEnvironment)
 {
     var argumentValue = _arg.Eval(runtimeEnvironment, functionEnvironment);
     var function = functionEnvironment.GetFunction(_name);
     return function.Eval(runtimeEnvironment, functionEnvironment, argumentValue);
 }
示例#24
0
 public Program(IDictionary<string, FunctionDefinition> functions, Expression expression)
 {
     _functionEnvironment = new FunctionEnvironment(functions);
     _expression = expression;
 }