Exemplo n.º 1
0
        private static Lexeme EvalLessThan(Lexeme t, Lexeme env)
        {
            var left  = Eval(t.Left, env);
            var right = Eval(t.Right, env);

            if (left.type == "INTEGER" && right.type == "INTEGER")
            {
                if (left.ival < right.ival)
                {
                    return(new Lexeme("INTEGER", 1));
                }
                else
                {
                    return(new Lexeme("INTEGER", 0));
                }
            }

            throw new Exception("Invalid LESSTHAN!" + ExceptionTypesLeftRight(left, right));
        }
Exemplo n.º 2
0
        Lexeme op7()
        {
            Lexeme tree = null;

            if (check("TIMES"))
            {
                tree = match("TIMES");
            }
            else if (check("DIVIDES"))
            {
                tree = match("DIVIDES");
            }
            else if (check("MOD"))
            {
                tree = match("MOD");
            }

            return(tree);
        }
Exemplo n.º 3
0
        private static Lexeme EvalGreaterThanEqualTo(Lexeme t, Lexeme env)
        {
            var left  = Eval(t.Left, env);
            var right = Eval(t.Right, env);

            if (left.type == "INTEGER" && right.type == "INTEGER")
            {
                if (left.ival >= right.ival)
                {
                    return(new Lexeme("INTEGER", 1));
                }
                else
                {
                    return(new Lexeme("INTEGER", 0));
                }
            }

            throw new Exception("Invalid GREATERTHAN_EQUALTO!" + ExceptionTypesLeftRight(left, right));
        }
Exemplo n.º 4
0
        private static Lexeme EvalIf(Lexeme tree, Lexeme env)
        {
            var conditional = tree.Left.Left;
            var ifBlock     = tree.Left.Right.Left;

            var origCond = conditional; //only used for error message

            conditional = Eval(conditional, env);

            if (conditional.type != "INTEGER")
            {
                throw new Exception("Invalid conditional in IF statement! Conditional is type: " + origCond.type);
            }

            if (conditional.ival == 1) //true
            {
                var ifEnv = Environment.Extend(null, null, env);
                return(Eval(ifBlock, ifEnv));
            }
            else //false
            {
                if (tree.Right != null)
                {
                    if (tree.Right.type == "ELIF")
                    {
                        return(EvalIf(tree.Right, env)); //shares same logic as IF so no need to make a new function
                    }
                    else if (tree.Right.type == "ELSE")
                    {
                        return(EvalElse(tree.Right, env));
                    }
                    else
                    {
                        throw new Exception("Invalid link in if statement: " + tree.Right.type);
                    }
                }
                else
                {
                    return(conditional); //if is false and no chain
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Prints arguments to console followed by a newline
        /// </summary>
        private static Lexeme EvalPrintLn(Lexeme tree, Lexeme env)
        {
            var args = tree.Left.Left;

            if (args == null)
            {
                Console.WriteLine();
            }
            else
            {
                string message = Eval(args.Left, env).GetValue();
                while (args.Right != null)
                {
                    message += Eval(args.Right.Left, env).GetValue();
                    args     = args.Right;
                }
                Console.WriteLine(message);
            }
            return(tree);
        }
Exemplo n.º 6
0
        private static Lexeme EvalPlusTo(Lexeme t, Lexeme env)
        {
            var left    = t.Left;
            var right   = Eval(t.Right, env);
            var leftVal = Environment.Lookup(left.sval, env);

            if (leftVal.type == "INTEGER" && right.type == "INTEGER")
            {
                var newVal = (leftVal.ival + right.ival).ToString();
                Environment.Update(t.Left.sval, env, newVal);
            }
            else
            {
                var newVal = (leftVal.GetValue() + right.GetValue()).ToString();
                Environment.Update(t.Left.sval, env, newVal);
            }

            t = t.Left;
            return(t);
        }
Exemplo n.º 7
0
        private static Lexeme EvalTimesTo(Lexeme t, Lexeme env)
        {
            var    left    = t.Left;
            var    right   = Eval(t.Right, env);
            var    leftVal = Environment.Lookup(left.sval, env);
            String newVal  = null;

            if (leftVal.type == "INTEGER" && right.type == "INTEGER")
            {
                newVal = (leftVal.ival * right.ival).ToString();
                Environment.Update(t.Left.sval, env, newVal);
            }
            else
            {
                throw new Exception("Unexpected types in EvalTimesTo!" + ExceptionTypesLeftRight(leftVal, right));
            }

            t = t.Left;
            return(t);
        }
Exemplo n.º 8
0
        private static Lexeme EvalWhile(Lexeme tree, Lexeme env)
        {
            var conditional = Eval(tree.Left, env);
            var whileBlock  = tree.Right.Left;

            if (conditional.type != "INTEGER")
            {
                throw new Exception("Invalid conditional in WHILE statement");
            }

            if (conditional.ival == 1) //execute loop
            {
                var whileEnv = Environment.Extend(null, null, env);
                Eval(whileBlock, whileEnv);
                return(EvalWhile(tree, env));
            }
            else
            {
                return(tree);
            }
        }
Exemplo n.º 9
0
        static public int Main()
        {
            Lexeme env = null;

            Console.WriteLine("Creating a new environment");
            env = Environment.Create();
            Environment.Print(env);

            Console.WriteLine("Adding variable x with value 3");
            Lexeme vari1 = new Lexeme("ID", "x");
            Lexeme val1  = new Lexeme("INTEGER", 3);

            Environment.Insert(vari1, val1, env);
            Environment.Print(env);

            Console.WriteLine("Extending the environment with y:4 and z:\"hello\"");
            var childEnv = Environment.Create();

            Lexeme vari2 = new Lexeme("ID", "y");
            Lexeme val2  = new Lexeme("INTEGER", 4);

            Environment.Insert(vari2, val2, childEnv);

            Lexeme vari3 = new Lexeme("ID", "z");
            Lexeme val3  = new Lexeme("STRING", "hello");

            vari2.Right = vari3;
            val2.Right  = val3;
            Environment.Insert(vari3, val3, childEnv);

            env = Environment.Extend(childEnv.Left.Left, childEnv.Left.Right, env);
            Console.WriteLine("The local environment is:");
            Environment.Print(env);
            Console.WriteLine("The environment is:");
            Environment.Print(Environment.GetParent(env));

            return(0);
        }
Exemplo n.º 10
0
        private static Lexeme EvalMinusTo(Lexeme t, Lexeme env)
        {
            var    left    = t.Left;
            var    right   = Eval(t.Right, env);
            var    leftVal = Environment.Lookup(left.sval, env);
            String newVal  = null;

            if (leftVal.type == "INTEGER" && right.type == "INTEGER")
            {
                newVal = (leftVal.ival - right.ival).ToString();
                Environment.Update(t.Left.sval, env, newVal);
            }
            else
            {
                var baseStr   = Eval(left, env).GetValue();
                var removeStr = Eval(right, env).GetValue();
                newVal = baseStr.Replace(removeStr, "");
                Environment.Update(t.Left.sval, env, newVal);
            }

            t = t.Left;
            return(t);
        }
Exemplo n.º 11
0
        Lexeme op5()
        {
            Lexeme tree = null;

            if (check("LESSTHAN"))
            {
                tree = match("LESSTHAN");
            }
            else if (check("LESSTHAN_EQUALTO"))
            {
                tree = match("LESSTHAN_EQUALTO");
            }
            else if (check("GREATERTHAN"))
            {
                tree = match("GREATERTHAN");
            }
            else if (check("GREATERTHAN_EQUALTO"))
            {
                tree = match("GREATERTHAN_EQUALTO");
            }

            return(tree);
        }
Exemplo n.º 12
0
        /// <summary>
        /// no args returns random int from 1-100 inclusive
        /// one arg returns random int from 1-arg1 inclusive
        /// two args returns random int from arg1-arg2 inclusive
        /// </summary>
        private static Lexeme EvalRandom(Lexeme tree, Lexeme env)
        {
            var args = tree.Left.Left;

            int lowerBound = 1;
            int upperBound = 100;

            if (tree.Left != null)
            {
                upperBound = Convert.ToInt32(Eval(args.Left, env).GetValue());

                if (tree.Left.Right != null)
                {
                    lowerBound = upperBound;
                    upperBound = Convert.ToInt32(Eval(args.Right, env).GetValue());
                }
            }

            var result = new Random().Next(lowerBound, upperBound + 1);

            System.Threading.Thread.Sleep(new Random().Next(15, 20)); //Without sleeping for at least 11-15 milliseconds repeats will be generated. Sleeping a random time between 15-20 for more random? :D

            return(new Lexeme("INTEGER", result));
        }
Exemplo n.º 13
0
        public static Lexeme Lookup(string id, Lexeme env)
        {
            if (id == "this")
            {
                return(env);
            }

            if (env.type == "CLOSURE")
            {
                env = env.Left;
            }

            while (env != null)
            {
                var table = Car(env);
                var vars  = Car(table);
                var vals  = Cdr(table);

                while (vars != null)
                {
                    if (vars.type == "JOIN" && id == Car(vars).sval)
                    {
                        return(Car(vals));
                    }
                    else if (id == vars.sval)
                    {
                        return(vals);
                    }
                    vars = Cdr(vars);
                    vals = Cdr(vals);
                }
                env = Cdr(env);
            }

            throw new Exception(String.Format("variable '{0}' is undefined!", id));
        }
Exemplo n.º 14
0
        public static void Print(Lexeme env)
        {
            if (env != null)
            {
                var table = Car(env);
                var vars  = Car(table);
                var vals  = Cdr(table);

                while (vars != null)
                {
                    if (Car(vals).type == "STRING")
                    {
                        Console.WriteLine(String.Format("\t{0} : '{1}'", Car(vars).sval, Car(vals).GetValue()));
                    }
                    else
                    {
                        Console.WriteLine(String.Format("\t{0} : {1}", Car(vars).sval, Car(vals).GetValue()));
                    }

                    vars = Cdr(vars);
                    vals = Cdr(vals);
                }
            }
        }
Exemplo n.º 15
0
 public static Lexeme GetParent(Lexeme env)
 {
     return(Cdr(env));
 }
Exemplo n.º 16
0
 public static Lexeme Extend(Lexeme vars, Lexeme vals, Lexeme env)
 {
     return(Cons(new Lexeme("ENV"),
                 Cons(new Lexeme("VALUES"), vars, vals),
                 env));
 }
Exemplo n.º 17
0
 public void SetEnvValue(Lexeme val)
 {
     type   = "ENV";
     envVal = val;
 }
Exemplo n.º 18
0
        //operators
        private static Lexeme EvalSimpleOp(Lexeme t, Lexeme env)
        {
            switch (t.type)
            {
            case "PLUS":
                return(EvalPlus(t, env));

            case "PLUS_TO":
                return(EvalPlusTo(t, env));

            case "INCREMENT":
                return(EvalIncrement(t, env));

            case "MINUS":
                return(EvalMinus(t, env));

            case "MINUS_TO":
                return(EvalMinusTo(t, env));

            case "DECREMENT":
                return(EvalDecrement(t, env));

            case "TIMES":
                return(EvalTimes(t, env));

            case "TIMES_TO":
                return(EvalTimesTo(t, env));

            case "DIVIDES":
                return(EvalDivides(t, env));

            case "DIVIDES_TO":
                return(EvalDivideTo(t, env));

            case "MOD":
                return(EvalMod(t, env));

            case "MOD_TO":
                return(EvalModTo(t, env));

            case "EXPN":
                return(EvalExpn(t, env));

            case "EXPN_TO":
                return(EvalExpnTo(t, env));

            case "LESSTHAN":
                return(EvalLessThan(t, env));

            case "LESSTHAN_EQUALTO":
                return(EvalLessThanEqualTo(t, env));

            case "GREATERTHAN":
                return(EvalGreaterThan(t, env));

            case "GREATERTHAN_EQUALTO":
                return(EvalGreaterThanEqualTo(t, env));

            case "EQUAL":
                return(EvalEqual(t, env));

            case "NOT_EQUAL":
                return(EvalNotEqual(t, env));
            }

            throw new Exception("Unrecognized operator!");
        }
Exemplo n.º 19
0
 private static Lexeme GetFuncCallArgs(Lexeme t)
 {
     return(t.Right.Left);
 }
Exemplo n.º 20
0
 private static Lexeme GetFuncCallClosure(Lexeme t, Lexeme env)
 {
     return(Environment.Lookup(t.sval, env));
 }
Exemplo n.º 21
0
 private static Lexeme GetClosureParams(Lexeme closure)
 {
     return(closure.Right.Right.Left);
 }
Exemplo n.º 22
0
 public static void SetCdr(Lexeme cons, Lexeme val)
 {
     cons.Right = val;
 }
Exemplo n.º 23
0
 public static void SetCar(Lexeme cons, Lexeme val)
 {
     cons.Left = val;
 }
Exemplo n.º 24
0
        //<<<<<<<<<<<<<<<<<<<<<<<< Builtins

        private static Lexeme EvalLambdaDef(Lexeme t, Lexeme env)
        {
            var body = t.Right;

            return(Eval(body, env));
        }
Exemplo n.º 25
0
 public static Lexeme Car(Lexeme cons)
 {
     return(cons.Left);
 }
Exemplo n.º 26
0
 private static Lexeme EvalReturn(Lexeme tree, Lexeme env)
 {
     return(Eval(tree.Left, env));
 }
Exemplo n.º 27
0
 public static Lexeme Cdr(Lexeme cons)
 {
     return(cons.Right);
 }
Exemplo n.º 28
0
 private static Lexeme GetFuncDefName(Lexeme t)
 {
     return(new Lexeme("ID", t.sval));
 }
Exemplo n.º 29
0
 public static Lexeme Caddr(Lexeme cons)
 {
     return(cons.Right.Right.Left);
 }
Exemplo n.º 30
0
 private static Lexeme GetClosureEnvironment(Lexeme closure)
 {
     return(closure.Left);
 }