Ejemplo n.º 1
0
        public override Node eval(Node t, Environment env)
        {
            Node var = t.getCdr().getCar();
            Node val = t.getCdr().getCdr().getCar();

            env.define(var, val);
            return(Void.getInstance());
        }
Ejemplo n.º 2
0
        public override Node eval(Node t, Environment env)
        {
            Node car1 = t.getCdr().getCar();
            Node car2 = t.getCdr().getCdr().getCar();

            if (car1.isSymbol())
            {
                // if first arg is not a list, define variable
                env.define(car1, car2);
            }
            else
            {
                // if first arg is list, define function and function body
                Cons funcbody   = new Cons(car1.getCdr(), t.getCdr().getCdr());
                Node closureFun = new Cons(new Ident("lambda"), funcbody).eval(env);
                env.define(car1.getCar(), closureFun);
            }
            return(Void.getInstance());
        }
Ejemplo n.º 3
0
        public override Node eval(Node t, Environment env)
        {
            while (!(t.getCdr() is Nil))
            {
                t = t.getCdr();
                if (!(t.getCar() is Cons))
                {
                    Console.Error.WriteLine("Error: unexpected argument");
                }
                Node n = t.getCar().getCar().eval(env);
                if (t.getCar().getCar().eval(env).Equals(BoolLit.getInstance(true)))
                {
                    t = t.getCar();

                    return(t.getCdr().getCar().eval(env));
                }
            }
            // get else to work...?
            return(Void.getInstance());
        }
Ejemplo n.º 4
0
        // sets first as function, args as function arguments, and performs apply() on first
        public override Node eval(Node t, Environment env)
        {
            Node first = t.getCar();
            Node args  = t.getCdr();

            if (!(args is Nil))
            {
                if (t.getCdr().getCar().isSymbol())
                {
                    args = getArgs(t.getCdr(), env);
                }
                if (t.getCdr().getCar().isPair() && !t.getCdr().getCar().getCar().isNumber())
                {
                    args = new Cons(t.getCdr().getCar().eval(env), Nil.getInstance());
                }
                if (env.lookup(first).isProcedure())
                {
                    first = env.lookup(first);
                    Node temp = first.apply(args);
                    return(temp);
                }
                else
                {
                    Console.WriteLine("Error: Procedure not specified.");
                }
            }
            else
            {
                if (env.lookup(first).isProcedure())
                {
                    first = env.lookup(first);
                    Node temp = first.apply(Nil.getInstance());
                    return(temp);
                }
            }
            return(Void.getInstance());
        }
Ejemplo n.º 5
0
        public override Node eval(Node exp, Environment env)
        {
            int num = Node.length(exp);

            if (num <= 2)
            {
                Console.Error.WriteLine("Error: invalid define expression");
                return(Nil.getInstance());
            }
            Node car1 = exp.getCdr().getCar();

            if (car1.isSymbol() && num == 3)
            {
                Node car2 = exp.getCdr().getCdr().getCar();
                env.define(car1, car2.eval(env));
                return(Void.getInstance());
            }
            if (!car1.isPair())
            {
                Console.Error.WriteLine("Error: invalid expression");
                return((Node)Nil.getInstance());
            }
            Node car3 = car1.getCar();
            Node cdr1 = car1.getCdr();
            Node cdr2 = exp.getCdr().getCdr();

            if (!car3.isSymbol() || !isValid(cdr1))
            {
                Console.Error.WriteLine("Error: ill-formed definition");
                return((Node)Nil.getInstance());
            }
            Node node = (Node) new Cons((Node) new Ident("lambda"), (Node) new Cons(cdr1, cdr2));

            env.define(car3, node.eval(env));
            return((Node)Void.getInstance());
        }
Ejemplo n.º 6
0
        // TODO: The method apply() should be defined in class Node
        // to report an error.  It should be overridden only in classes
        // BuiltIn and Closure.
        public override Node apply(Node args)
        {
            // return new StringLit("Error: BuiltIn.apply not yet implemented");
            Node   arg1    = Nil.getInstance();
            String symName = symbol.getName();

            if (symName.Equals("interaction-environment"))
            {
                return(global);
            }
            if (symName.Equals("newline"))
            {
                Console.WriteLine();
                return(Void.getInstance());
            }
            if (!(args.getCar() is Nil))
            {
                arg1 = args.getCar();
            }
            Node arg2 = Nil.getInstance();

            if (!args.getCdr().isNull())
            {
                arg2 = args.getCdr().getCar();
            }
            if (symName.Equals("symbol?"))
            {
                return(BoolLit.getInstance(arg1.isSymbol()));
            }
            if (symName.Equals("number?"))
            {
                return(BoolLit.getInstance(arg1.isNumber()));
            }
            if (symName.Equals("b+"))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(new IntLit(arg1.getVal() + arg2.getVal()));
                }
            }
            if (symName.Equals("b-"))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(new IntLit(arg1.getVal() - arg2.getVal()));
                }
            }
            if (symName.Equals("b*"))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(new IntLit(arg1.getVal() * arg2.getVal()));
                }
            }
            if (symName.Equals("b/"))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(new IntLit(arg1.getVal() / arg2.getVal()));
                }
            }
            if (symName.Equals("b="))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(BoolLit.getInstance(arg1.getVal() == arg2.getVal()));
                }
            }
            if (symName.Equals("b<"))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(BoolLit.getInstance(arg1.getVal() < arg2.getVal()));
                }
            }
            if (symName.Equals("b>"))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(BoolLit.getInstance(arg1.getVal() > arg2.getVal()));
                }
            }
            if (symName.Equals("b>="))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(BoolLit.getInstance(arg1.getVal() >= arg2.getVal()));
                }
            }
            if (symName.Equals("b<="))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(BoolLit.getInstance(arg1.getVal() <= arg2.getVal()));
                }
            }
            if (symName.Equals("car"))
            {
                return(arg1.getCar());
            }
            if (symName.Equals("cdr"))
            {
                return(arg1.getCdr());
            }
            if (symName.Equals("set-car!"))
            {
                arg1.setCar(arg2);
                return(Void.getInstance());
            }
            if (symName.Equals("set-cdr!"))
            {
                arg1.setCdr(arg2);
                return(Void.getInstance());
            }
            if (symName.Equals("null?"))
            {
                return(BoolLit.getInstance(arg1.isNull()));
            }
            if (symName.Equals("pair?"))
            {
                return(BoolLit.getInstance(arg1.isPair()));
            }
            if (symName.Equals("eq?"))
            {
                if (arg1.isSymbol() && arg2.isSymbol())
                {
                    return(BoolLit.getInstance(arg1.getName().Equals(arg2.getName())));
                }
                return(BoolLit.getInstance(arg1 == arg2));
            }
            if (symName.Equals("procedure?"))
            {
                return(BoolLit.getInstance(arg1.isProcedure()));
            }
            if (symName.Equals("read"))
            {
                Scanner     scanner = new Scanner(Console.In);
                TreeBuilder builder = new TreeBuilder();
                Parser      parser  = new Parser(scanner, builder);
                Node        root    = (Node)parser.parseExp();
                return(Void.getInstance());
            }
            if (symName.Equals("write"))
            {
                arg1.print(0);
                return(Void.getInstance());
            }
            if (symName.Equals("display"))
            {
                ((StringLit)arg1).displayPrint();
                return(Void.getInstance());
            }
            if (symName.Equals("eval"))
            {
                return(arg1.eval((Environment)arg2));
            }
            if (symName.Equals("apply"))
            {
                return(arg1.apply(arg2));
            }
            return(Void.getInstance());
        }