示例#1
0
        public override Type Check(TEnv env, FEnv fEnv)
        {
            Type t1 = e1.Check(env, fEnv);
            Type t2 = e2.Check(env, fEnv);

            switch (op)
            {
            case Operator.Add:
            case Operator.Div:
            case Operator.Mul:
            case Operator.Sub:
                if (t1 == Type.intType && t2 == Type.intType)
                {
                    return(Type.intType);
                }
                else
                {
                    throw new TypeException("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.intType && t2 == Type.intType)
                {
                    return(Type.boolType);
                }
                else
                {
                    throw new TypeException("Arguments to == >= > <= < != must be int");
                }

            case Operator.Or:
            case Operator.And:
                if (t1 == Type.boolType && t2 == Type.boolType)
                {
                    return(Type.boolType);
                }
                else
                {
                    throw new TypeException("Arguments to & must be bool");
                }

            case Operator.Mod:
                if (t1 == Type.intType && t2 == Type.intType)
                {
                    return(Type.intType);
                }
                else
                {
                    throw new TypeException("Arguments to % must be int");
                }

            default:
                throw new Exception("Unknown binary operator: " + op);
            }
        }
        public override Type Check(TEnv env, FEnv fEnv)
        {
            Type t1 = e1.Check(env, fEnv);

            env.DeclareLocal(this.name, t1);
            Type t2 = e2.Check(env, fEnv);

            env.PopEnv();
            return(t2);
        }
        public override int Eval(REnv env, FEnv fEnv)
        {
            int x = e1.Eval(env, fEnv);

            env.AllocateLocal(this.name);
            env.GetVariable(this.name).value = x;
            int y = e2.Eval(env, fEnv);

            env.PopEnv();
            return(y);
        }
示例#4
0
        public override int Eval(REnv env, FEnv fEnv)
        {
            int res1 = e1.Eval(env, fEnv);

            env.AllocateLocal(varName);
            env.GetVariable(varName).value = res1;

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

            env.PopEnv();
            return(res2);
        }
示例#5
0
 public override Type Check(TEnv env, FEnv fenv)
 {
     foreach (var expression in expressions)
     {
         Type    argType = expression.Check(env, fenv);
         FuncDef fDef    = fenv.getFunction(fName);
         if (!fDef.CheckArgType(argType))
         {
             throw new TypeException("Type error in call of function " + fName);
         }
     }
     return(Type.intType);
 }
示例#6
0
        public override int Eval(REnv env, FEnv fEnv)
        {
            int v1 = e1.Eval(env, fEnv);
            int v2 = e2.Eval(env, fEnv);

            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:
                return(v1 % v2);

            default:
                throw new Exception("Unknown binary operator: " + op);
            }
        }
        public override int Eval(REnv env, FEnv fEnv)
        {
            int result;
            int v1 = e1.Eval(env, fEnv);

            if (v1 == 1)
            {
                result = e2.Eval(env, fEnv);
            }
            else
            {
                result = e3.Eval(env, fEnv);
            }
            return(result);
        }
示例#8
0
        public override int Eval(REnv env, FEnv fenv)
        {
            int        value  = 0;
            List <int> values = new List <int>();

            foreach (var expression in expressions)
            {
                int argValue = expression.Eval(env, fenv);
                values.Add(argValue);
            }
            FuncDef fDef = fenv.getFunction(fName);

            value = fDef.Eval(env, fenv, values);
            return(value);
        }
示例#9
0
        public override Type Check(TEnv env, FEnv fEnv)
        {
            Type t1 = cond.Check(env, fEnv);
            Type t2 = e2.Check(env, fEnv);
            Type t3 = e3.Check(env, fEnv);

            if (t1 == Type.boolType && t2 == Type.intType && t3 == Type.intType)
            {
                return(Type.intType);
            }
            else
            {
                throw new TypeException("Condition must return bool and expressions must return int.");
            }
        }
示例#10
0
        public override int Eval(REnv env, FEnv fEnv)
        {
            int conditionRes = cond.Eval(env, fEnv);

            switch (conditionRes)
            {
            case 1:
                return(e2.Eval(env, fEnv));

            case 0:
                return(e3.Eval(env, fEnv));

            default:
                throw new Exception("Arguments after 'if' must return a bool: " + conditionRes);
            }
        }
示例#11
0
        public override int Eval(REnv env, FEnv fEnv)
        {
            int v1 = e1.Eval(env, fEnv);

            switch (op)
            {
            case Operator.Not:
                return(v1 == 0 ? 1 : 0);

            case Operator.Neg:
                return(-v1);

            default:
                throw new Exception("Unknown unary operator: " + op);
            }
        }
示例#12
0
        public void Check(TEnv env, FEnv fEnv)
        {
            foreach (var formArg in formArgs)
            {
                env.DeclareLocal(formArg.Fst, formArg.Snd);
            }
            Type t = body.Check(env, fEnv);

            foreach (var formArg in formArgs)
            {
                env.PopEnv();
            }
            if (t != returnType)
            {
                throw new TypeException("Body of " + fName + " returns " + t + ", " + returnType + " expected");
            }
        }
示例#13
0
        public int Eval(REnv env, FEnv fenv, List <int> args)
        {
            int i = 0;

            foreach (var formArg in formArgs)
            {
                env.AllocateLocal(formArg.Fst);
                env.GetVariable(formArg.Fst).value = args[i];
                i++;
            }
            int v = body.Eval(env, fenv);

            foreach (var formArg in formArgs)
            {
                env.PopEnv();
            }
            return(v);
        }
示例#14
0
        public override Type Check(TEnv env, FEnv fEnv)
        {
            Type t1 = e1.Check(env, fEnv);

            env.DeclareLocal(varName, t1);

            Type t2 = e2.Check(env, fEnv);

            env.PopEnv();

            if (t2 == Type.intType)
            {
                return(Type.intType);
            }
            else
            {
                throw new TypeException("Expressions must return int.");
            }
        }
        public override Type Check(TEnv env, FEnv fenv)
        {
            Type[] types = new Type[args.Count];
            int    index = 0;

            foreach (Expression arg in args)
            {
                types[index++] = arg.Check(env, fenv);
            }
            FuncDef fDef = fenv.getFunction(fName);

            if (fDef.CheckArgTypes(types))
            {
                return(fDef.returnType);
            }
            else
            {
                throw new TypeException("Type error in call of function " + fName);
            }
        }
        public override Type Check(TEnv env, FEnv fEnv)
        {
            Type t1 = e1.Check(env, fEnv);

            if (t1 == Type.boolType)
            {
                Type t2 = e2.Check(env, fEnv);
                Type t3 = e3.Check(env, fEnv);
                if (t2 != t3)
                {
                    throw new TypeException("Arguments t2 and t3 must be the same type");
                }

                return(t2);
            }
            else
            {
                throw new TypeException("Argument t1 has to be of type boolean");
            }
        }
        public override int Eval(REnv env, FEnv fenv)
        {
            int[] argValues;

            if (args.Count > 0)
            {
                int index = 0;
                argValues = new int[args.Count];
                foreach (Expression arg in args)
                {
                    argValues[index++] = arg.Eval(env, fenv);
                }
            }
            else
            {
                argValues = new int[0];
            }
            FuncDef fDef = fenv.getFunction(fName);

            return(fDef.Eval(env, fenv, argValues));
        }
        public int Eval(REnv env, FEnv fenv, int[] values)
        {
            int v;

            if (formArgs.Count > 0)
            {
                int index = 0;
                foreach (Pair <String, Type> argPair in this.formArgs)
                {
                    env.AllocateLocal(argPair.Fst);
                    env.GetVariable(argPair.Fst).value = values[index++];
                }
                v = body.Eval(env, fenv);
                foreach (Pair <String, Type> argPair in this.formArgs)
                {
                    env.PopEnv();
                }
            }
            else
            {
                v = body.Eval(env, fenv);
            }
            return(v);
        }
示例#19
0
 public override int Eval(REnv env, FEnv fEnv)
 {
     return(value);
 }
示例#20
0
 abstract public int Eval(REnv env, FEnv fEnv);
示例#21
0
 abstract public Type Check(TEnv env, FEnv fEnv);
示例#22
0
 public override Type Check(TEnv env, FEnv fEnv)
 {
     return(type);
 }
示例#23
0
 public override int Eval(REnv env, FEnv fEnv)
 {
     return(env.GetVariable(name).value);
 }
示例#24
0
 public Program(Dictionary <String, FuncDef> functions, Expression e)
 {
     fenv   = new FEnv(functions);
     this.e = e;
 }
示例#25
0
 public override Type Check(TEnv env, FEnv fEnv)
 {
     return(env.GetVariable(name));
 }