Example #1
0
 public void should_build_tree_from_enum()
 {
     TreeBuilder builder = new TreeBuilder();
     string rootPath = "C:\\dummy";
     var testData = CreateTestData(rootPath,"In Flames", 2, 4);
     var result = BuildTestResultTree("In Flames", 2,4);
     builder.Build(rootPath.Length, testData);
 }
Example #2
0
        public void ShouldBuildTree()
        {
            var b = new TreeBuilder();
            var apiPermissions = new []
            {
                new ApiPermission(){Group = "A"},
                new ApiPermission(){Group = "B"},
                new ApiPermission(){Group = "C"},
                new ApiPermission(){Group = "C.D"},
                new ApiPermission(){Group = "C.D.E"},
                new ApiPermission(){Group = "F.G"}
            };
            var permissions = b.Build(apiPermissions.Select(p => p.Group));
            Assert.That(permissions, Is.Not.Null);

            var expectedPermissions = new[]
            {
                new SimpleTreeNode()
                {
                    Name= "A", Children = new SimpleTreeNode[0]
                },
                new SimpleTreeNode()
                {
                    Name= "B", Children = new SimpleTreeNode[0]
                },
                new SimpleTreeNode()
                {
                    Name = "C",
                    Children = new[]
                    {
                        new SimpleTreeNode()
                        {
                            Name = "D",
                            Children = new []
                            {
                                new SimpleTreeNode()
                                {
                                    Name="E", Children = new SimpleTreeNode[0]
                                }
                            }
                        }
                    }
                },
                new SimpleTreeNode()
                {
                    Name = "F",
                    Children = new []
                    {
                        new SimpleTreeNode()
                        {
                            Name= "G", Children = new SimpleTreeNode[0]
                        }
                    }
                }
            };

            Assert.That(permissions, new MemberwiseEqualConstraint(expectedPermissions, MemberwiseEqualityComparer.ComparisonMode.CompareEverything, null));
        }
Example #3
0
        private static void TreeOperations()
        {
            Tree.TreeBuilder treeBuilder = new Tree.TreeBuilder();
            //treeBuilder.run();

            int[] array = new int[] { 5, 2, 15, 3, 9, 1, 0, 20 };
            //int[] newarray= treeBuilder.MinHeap(array);

            Tree.ElementNode <int> node = treeBuilder.BinaryTree(array, 0);
            //Tree.ElementNode<int> node = treeBuilder.createCompleteBinaryTreeThroughArrayIteratively(array);

            treeBuilder.inorderTraversal(node);

            //Console.WriteLine(String.Join(" ", newarray));

            treeBuilder.RepresentTreeinDoublyLL(node);

            while (node.right != null)
            {
                Console.WriteLine(node);
            }
        }
Example #4
0
        public static int Main(string[] args)
        {
            // Create scanner that reads from standard input
            Scanner scanner = new Scanner(Console.In);

            if (args.Length > 1 ||
                (args.Length == 1 && ! args[0].Equals("-d")))
            {
                Console.Error.WriteLine("Usage: mono SPP [-d]");
                return 1;
            }

            // If command line option -d is provided, debug the scanner.
            if (args.Length == 1 && args[0].Equals("-d"))
            {
                // Console.Write("Scheme 4101> ");
                Token tok = scanner.getNextToken();
                while (tok != null)
                {
                    TokenType tt = tok.getType();

                    Console.Write(tt);
                    if (tt == TokenType.INT)
                        Console.WriteLine(", intVal = " + tok.getIntVal());
                    else if (tt == TokenType.STRING)
                        Console.WriteLine(", stringVal = " + tok.getStringVal());
                    else if (tt == TokenType.IDENT)
                        Console.WriteLine(", name = " + tok.getName());
                    else
                        Console.WriteLine();

                    // Console.Write("Scheme 4101> ");
                    tok = scanner.getNextToken();
                }
                return 0;
            }

            // Create parser
            TreeBuilder builder = new TreeBuilder();
            Parser parser = new Parser(scanner, builder);
            Node root;

            // TODO: Create and populate the built-in environment and
            // create the top-level environment
            // Read-eval-print loop
            Tree.Environment env = new Tree.Environment();
            Node id = new Ident("car");
            env.define(id, new BuiltIn(id));
            id = new Ident("cdr");
            env.define(id, new BuiltIn(id));
            id = new Ident("cons");
            env.define(id, new BuiltIn(id));
            id = new Ident("set-car!");
            env.define(id, new BuiltIn(id));
            id = new Ident("set-cdr!");
            env.define(id, new BuiltIn(id));
            id = new Ident("null?");
            env.define(id, new BuiltIn(id));
            id = new Ident("pair?");
            env.define(id, new BuiltIn(id));
            id = new Ident("eq?");
            env.define(id, new BuiltIn(id));
            id = new Ident("symbol?");
            env.define(id, new BuiltIn(id));
            id = new Ident("number?");
            env.define(id, new BuiltIn(id));
            id = new Ident("b+");
            env.define(id, new BuiltIn(id));
            id = new Ident("b-");
            env.define(id, new BuiltIn(id));
            id = new Ident("b*");
            env.define(id, new BuiltIn(id));
            id = new Ident("b/");
            env.define(id, new BuiltIn(id));
            id = new Ident("b=");
            env.define(id, new BuiltIn(id));
            id = new Ident("b<");
            env.define(id, new BuiltIn(id));
            id = new Ident("procedure?");
            env.define(id, new BuiltIn(id));
            id = new Ident("read");
            env.define(id, new BuiltIn(id));
            id = new Ident("write");
            env.define(id, new BuiltIn(id));
            id = new Ident("display");
            env.define(id, new BuiltIn(id));
            id = new Ident("newline");
            env.define(id, new BuiltIn(id));
            id = new Ident("eval");
            env.define(id, new BuiltIn(id));
            id = new Ident("apply");
            env.define(id, new BuiltIn(id));
            id = new Ident("interaction-environment");
            env.define(id, new BuiltIn(id));
            env = new Tree.Environment(env);

            // TODO: print prompt and evaluate the expression
            root = (Node) parser.parseExp();
            while (root != null)
            {
                root.eval(env).print(0);
                root = (Node) parser.parseExp();
            }
            return 0;
        }
Example #5
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)
 {
     if (symbol.getName().Equals("car"))
     {
         if (args.getCdr() == Nil.getInstance()) { return args.getCar().getCar(); }
         else { throw new InvalidOperationException(); }
     }
     else if (symbol.getName().Equals("cdr"))
     {
         if (args.getCdr() == Nil.getInstance()) { return args.getCar().getCdr(); }
         else { throw new InvalidOperationException(); }
     }
     else if (symbol.getName().Equals("cons"))
     {
         return new Cons(args.getCar(), args.getCdr());
     }
     else if (symbol.getName().Equals("set-car!"))
     {
         if (args.getCar().GetType() != typeof(Cons))
         {
             Console.Error.WriteLine("Error: invalid arguments");
             throw new InvalidOperationException();
         }
         args.getCar().setCar(args.getCdr().getCar());
         return args.getCar();
     }
     else if (symbol.getName().Equals("set-cdr!"))
     {
         if (args.getCar().GetType() != typeof(Cons))
         {
             Console.Error.WriteLine("Error: invalid arguments");
             throw new InvalidOperationException();
         }
         args.getCar().setCdr(args.getCdr().getCar());
         return args.getCar();
     }
     else if (symbol.getName().Equals("null?"))
     {
         if (args.getCar() == Nil.getInstance())
             return BoolLit.getInstance(true);
         else return BoolLit.getInstance(false);
     }
     else if (symbol.getName().Equals("pair?"))
     {
         if (args.getCar().isPair())
             return BoolLit.getInstance(true);
         else return BoolLit.getInstance(false);
     }
     else if (symbol.getName().Equals("eq?"))
     {
         if (args.getCar() is Ident && args.getCdr().getCar() is Ident)
         {
             if (((Ident)args.getCar()).getName().Equals(((Ident)args.getCdr().getCar()).getName()))
             {
                 return BoolLit.getInstance(true);
             }
             else { return BoolLit.getInstance(false); }
         }
         else
         {
             if (args.getCar() == args.getCdr().getCar()) { return BoolLit.getInstance(true); }
             else { return BoolLit.getInstance(false); }
         }
     }
     else if (symbol.getName().Equals("symbol?"))
     {
         if (args.getCdr() == Nil.getInstance())
         {
             if (args.getCar().isSymbol()) { return BoolLit.getInstance(true); }
             else { return BoolLit.getInstance(false); }
         }
         else { throw new InvalidOperationException(); }
     }
     else if (symbol.getName().Equals("number?"))
     {
         if (args.getCdr() == Nil.getInstance())
         {
             if (args.getCar().GetType() == typeof(IntLit)) { return BoolLit.getInstance(true); }
             else { return BoolLit.getInstance(false); }
         }
         else { throw new InvalidOperationException(); }
     }
     else if (symbol.getName().Equals("b+"))
     {
         if (args.getCdr().getCdr() == Nil.getInstance())
         {
             if (args.getCar() is IntLit && args.getCdr().getCar() is IntLit)
             {
                 return new IntLit(((IntLit)args.getCar()).intVal + ((IntLit)args.getCdr().GetCar()).intVal);
             }
             else throw new InvalidOperationException();
         }
         else { throw new InvalidOperationException(); }
     }
     else if (symbol.getName().Equals("b-"))
     {
         if (args.getCdr().getCdr() == Nil.getInstance())
         {
             if (args.getCar() is IntLit && args.getCdr().getCar() is IntLit)
             {
                 return new IntLit(((IntLit)args.getCar()).intVal - ((IntLit)args.getCdr().GetCar()).intVal);
             }
             else throw new InvalidOperationException();
         }
         else throw new InvalidOperationException();
     }
     else if (symbol.getName().Equals("b*"))
     {
         if (args.getCdr().getCdr() == Nil.getInstance())
         {
             if (args.getCar() is IntLit && args.getCdr().getCar() is IntLit)
             {
                 return new IntLit(((IntLit)args.getCar()).intVal * ((IntLit)args.getCdr().GetCar()).intVal);
             }
             else throw new InvalidOperationException();
         }
         else throw new InvalidOperationException();
     }
     else if (symbol.getName().Equals("b/"))
     {
         if (args.getCdr().getCdr() == Nil.getInstance())
         {
             if (args.getCar() is IntLit && args.getCdr().getCar() is IntLit)
             {
                 return new IntLit(((IntLit)args.getCar()).intVal / ((IntLit)args.getCdr().GetCar()).intVal);
             }
             else throw new InvalidOperationException();
         }
         else throw new InvalidOperationException();
     }
     else if (symbol.getName().Equals("b="))
     {
         if (args.getCdr().getCdr() == Nil.getInstance())
         {
             if (args.getCar() is IntLit && args.getCdr().getCar() is IntLit)
             {
                 return BoolLit.getInstance(((IntLit)args.getCar()).intVal == ((IntLit)args.getCdr().GetCar()).intVal);
             }
             else throw new InvalidOperationException();
         }
         else throw new InvalidOperationException();
     }
     else if (symbol.getName().Equals("b<"))
     {
         if (args.getCdr().getCdr() == Nil.getInstance())
         {
             if (args.getCar() is IntLit && args.getCdr().getCar() is IntLit)
             {
                 return BoolLit.getInstance(((IntLit)args.getCar()).intVal < ((IntLit)args.getCdr().GetCar()).intVal);
             }
             else throw new InvalidOperationException();
         }
         else throw new InvalidOperationException();
     }
     else if (symbol.getName().Equals("procedure?"))
     {
         if (args.getCdr() == Nil.getInstance())
         {
             if (args.getCar().isProcedure()) { return BoolLit.getInstance(true); }
             else { return BoolLit.getInstance(false); }
         }
         else { throw new InvalidOperationException(); }
     }
     else if (symbol.getName().Equals("read"))
     {
         //not sure if works
         Scanner scanner = new Scanner(Console.In);
         TreeBuilder builder = new TreeBuilder();
         Parser parser = new Parser(scanner, builder);
         return (Node)parser.parseExp();
     }
     else if (symbol.getName().Equals("write"))
     {
         ((StringLit)args.getCar()).printQuote(true);
         return Nil.getInstance();
     }
     else if (symbol.getName().Equals("display"))
     {
         ((StringLit)args.getCar()).printQuote(false);
         return Nil.getInstance();
     }
     else if (symbol.getName().Equals("newline"))
     {
         Console.WriteLine("");
         return Nil.getInstance();
     }
     else if (symbol.getName().Equals("eval"))
     {
         //not sure if this works
         return args.getCar().eval((Tree.Environment)args.getCdr().getCar());
     }
     else if (symbol.getName().Equals("apply"))
     {
         //not sure if this works
         return (Closure)args.getCar().apply(args.getCdr().getCar());
     }
     else if (symbol.getName().Equals("interaction-environment"))
     {
         //Not yet implemented
         return Nil.getInstance();
     }
     else
     {
         return Nil.getInstance();
     }
 }
Example #6
0
 //if arg's length = 1
 private Node apply1(Node arg1)
 {
     string name = this.symbol.getName();
     bool flag = name.Equals("car");
     Node result;
     if (flag)
     {
         result = arg1.getCar();
     }//end if
     else
     {
         bool flag2 = name.Equals("cdr");
         if (flag2)
         {
             result = arg1.getCdr();
         }//end if
         else
         {
             bool flag3 = name.Equals("number?");
             if (flag3)
             {
                 result = BoolLit.getInstance(arg1.isNumber());
             }//end if
             else
             {
                 bool flag4 = name.Equals("symbol?");
                 if (flag4)
                 {
                     result = BoolLit.getInstance(arg1.isSymbol());
                 }//end if
                 else
                 {
                     bool flag5 = name.Equals("null?");
                     if (flag5)
                     {
                         result = BoolLit.getInstance(arg1.isNull());
                     }//end if
                     else
                     {
                         bool flag6 = name.Equals("pair?");
                         if (flag6)
                         {
                             result = BoolLit.getInstance(arg1.isPair());
                         }//end if
                         else
                         {
                             bool flag7 = name.Equals("procedure?");
                             if (flag7)
                             {
                                 result = BoolLit.getInstance(arg1.isProcedure());
                             }//end if
                             else
                             {
                                 bool flag8 = name.Equals("write");// i am not sure if im handling this correctly
                                 if (flag8)
                                 {
                                     arg1.print(-1);
                                     result = Unspecific.getInstance();
                                 }//end if
                                 else
                                 {
                                     bool flag9 = name.Equals("display"); // i am not sure if im handling this correctly
                                     if (flag9)
                                     {
                                         StringLit.printDoubleQuotes = false;
                                         arg1.print(-1);
                                         StringLit.printDoubleQuotes = true;
                                         result = Unspecific.getInstance();
                                     }//end if
                                     else
                                     {
                                         bool flag10 = name.Equals("load");
                                         if (flag10)
                                         {
                                             bool flag11 = !arg1.isString();
                                             if (flag11)
                                             {
                                                 Console.Error.WriteLine("Error: wrong type of argument");
                                                 result = Nil.getInstance();
                                             }
                                             else
                                             {
                                                 string stringVal = arg1.getStringVal();
                                                 try
                                                 {
                                                     //not sure why it has to be Parse.Scanner to call this
                                                     Parse.Scanner s = new Parse.Scanner(File.OpenText(stringVal));
                                                     TreeBuilder b = new TreeBuilder();
                                                     Parse.Parser parser = new Parse.Parser(s, b);
                                                     for (Node node = (Node)parser.parseExp(); node != null; node = (Node)parser.parseExp())
                                                     {
                                                         node.eval(Scheme4101.env);
                                                     }
                                                 }
                                                 catch (SystemException)
                                                 {
                                                     Console.Error.WriteLine("Could not find file " + stringVal);
                                                 }
                                                 result = Unspecific.getInstance();
                                             }
                                         }//end if
                                         else
                                         {
                                             Console.Error.WriteLine("Error: wrong number of arguments");
                                             result = Nil.getInstance();
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }//end inner sub sub sub else
             }//end inner sub sub else
         }//end inner sub else
     }//end outer else
     return result;
 }
Example #7
0
        public override Node apply(Node args)
        {
            if (args == null)
            {
                return(null);
            }

            String symbolType = symbol.getName();
            Node   arg1       = args.getCar();
            Node   arg2       = args.getCdr();

            if (arg1.isNull())
            {
                arg1 = Nil.getInstance();
            }
            if (arg2.isNull())
            {
                arg2 = Nil.getInstance();
            }
            else
            {
                arg2 = args.getCdr().getCar();
            }

            if (symbolType.Equals("b+"))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(new IntLit(arg1.getValue() + arg2.getValue()));
                }
                else
                {
                    return(new StringLit("Error: Both arguments must be numbers for b+"));
                }
            }
            if (symbolType.Equals("b-"))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(new IntLit(arg1.getValue() - arg2.getValue()));
                }
                else
                {
                    return(new StringLit("Error: Both arguments must be numbers for b-"));
                }
            }
            if (symbolType.Equals("b*"))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(new IntLit(arg1.getValue() * arg2.getValue()));
                }
                else
                {
                    return(new StringLit("Error: Both arguments must be numbers for b*"));
                }
            }
            if (symbolType.Equals("b/"))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(new IntLit(arg1.getValue() / arg2.getValue()));
                }
                else
                {
                    return(new StringLit("Error: Both arguments must be numbers for b/"));
                }
            }
            if (symbolType.Equals("b="))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(new BoolLit(arg1.getValue() == arg2.getValue()));
                }
                else
                {
                    return(new StringLit("Error: Both arguments must be numbers for b="));
                }
            }
            if (symbolType.Equals("b<"))
            {
                if (arg1.isNumber() & arg2.isNumber())
                {
                    return(new BoolLit(arg1.getValue() < arg2.getValue()));
                }
                else
                {
                    return(new StringLit("Error: Both arguments must be numbers for b<"));
                }
            }

            if (symbolType.Equals("null?"))
            {
                return(new BoolLit(arg1.isNull()));
            }
            if (symbolType.Equals("number?"))
            {
                return(new BoolLit(arg1.isNumber()));
            }
            if (symbolType.Equals("pair?"))
            {
                return(new BoolLit(arg1.isPair()));
            }
            if (symbolType.Equals("symbol?"))
            {
                return(new BoolLit(arg1.isSymbol()));
            }
            if (symbolType.Equals("procedure?"))
            {
                return(new BoolLit(arg1.isProcedure()));
            }

            if (symbolType.Equals("car"))
            {
                if (arg1.isNull())
                {
                    return(arg1);
                }
                return(arg1.getCar());
            }
            if (symbolType.Equals("cdr"))
            {
                if (arg1.isNull())
                {
                    return(arg1);
                }
                return(arg1.getCdr());
            }
            if (symbolType.Equals("set-car!"))
            {
                arg1.setCar(arg2);
                return(arg1);
            }
            if (symbolType.Equals("set-cdr!"))
            {
                arg1.setCdr(arg2);
                return(arg1);
            }

            if (symbolType.Equals("cons"))
            {
                return(new Cons(arg1, arg2));
            }

            if (symbolType.Equals("eq?"))
            {
                return(new BoolLit(arg1 == arg2));
            }

            if (symbolType.Equals("display"))
            {
                return(arg1);
            }

            if (symbolType.Equals("write"))
            {
                //write calls your pretting printer
                arg1.print(0);
                return(new StringLit(""));
            }

            if (symbolType.Equals("eval"))
            {
                //eval calls your C# eval() function
                return(eval(arg1, env));
            }

            if (symbolType.Equals("apply"))
            {
                //apply calls your C# apply function
                return(arg1.apply(arg2));
            }

            if (symbolType.Equals("newline"))
            {
                return(new StringLit(""));
            }

            if (symbolType.Equals("read"))
            {
                Scanner     io      = new Scanner(Console.In);
                TreeBuilder tree    = new TreeBuilder();
                Parser      parse   = new Parser(io, tree);
                Node        theNode = (Node)parse.parseExp();
                return(theNode);
            }

            if (symbolType.Equals("interaction-environment"))
            {
                env.print(0);
            }
            else
            {
                arg1.print(0);
                return(Nil.getInstance());
            }

            return(Nil.getInstance());
        }
Example #8
0
        public override Node apply(Node args)
        {
            //set initial values
            string argsName   = symbol.getName();
            int    argsAmount = 0;

            //initialize
            Node firstArgs  = null;
            bool b1         = false;
            Node secondArgs = null;
            bool b2         = false;

            //setup initial Args
            while (!args.isNull())
            {
                argsAmount = argsAmount + 1;
                if (argsAmount == 1)
                {
                    firstArgs = args.getCar();
                    b1        = true;
                }
                if (argsAmount == 2)
                {
                    secondArgs = args.getCar();
                    b2         = true;
                }
                args = args.getCdr();
            }

            //start apply based on amount or arguements and type of arguement
            //switch statement possibly
            if (argsAmount == 0)
            {
                // read
                if (argsName.Equals("read"))
                {
                    Scanner     s  = new Scanner(Console.In);
                    TreeBuilder tb = new TreeBuilder();
                    Parser      p  = new Parser(s, tb);
                    Node        n  = (Node)p.parseExp();
                    if (n != null)
                    {
                        return(n);
                    }
                }
                //newline
                else if (argsName.Equals("newline"))
                {
                    Console.WriteLine();
                    //check value upspecified
                    return(new StringLit(" "));
                }
                //i-e
                else if (argsName.Equals("interaction-environment"))
                {
                    return(Scheme4101.env);
                }
                //invalid argsName given
                else
                {
                    Console.Error.WriteLine("invalid arguemtent name given");
                    return(Nil.getInstance());
                }
            }
            else if (argsAmount == 1)
            {
                //symbol?
                if (argsName.Equals("symbol?"))
                {
                    bool b = firstArgs.isSymbol();
                    return(BoolLit.getInstance(b));
                }
                //number?
                else if (argsName.Equals("number?"))
                {
                    bool b = firstArgs.isNumber();
                    return(BoolLit.getInstance(b));
                }
                //car
                else if (argsName.Equals("car"))
                {
                    return(firstArgs.getCar());
                }
                //cdr
                else if (argsName.Equals("cdr"))
                {
                    return(firstArgs.getCdr());
                }
                //null?
                //b1?
                else if (argsName.Equals("null?") && b1)
                {
                    bool b = firstArgs.isNull();
                    return(BoolLit.getInstance(b));
                }
                //pair
                else if (argsName.Equals("pair?"))
                {
                    bool b = firstArgs.isPair();
                    return(BoolLit.getInstance(b));
                }
                //precedure
                else if (argsName.Equals("procedure?"))
                {
                    bool b = firstArgs.isProcedure();
                    return(BoolLit.getInstance(b));
                }
                //write
                else if (argsName.Equals("write"))
                {
                    firstArgs.print(0);
                    //return value unspecified
                    return(new StringLit(" "));
                }
                //display
                else if (argsName.Equals("display"))
                {
                    //temp change value
                    StringLit.quoted = false;
                    firstArgs.print(0);
                    //reset changed value
                    StringLit.quoted = true;
                    //return value unspecified
                    return(new StringLit(" "));
                }
            }
            else if (argsAmount == 2)
            {
                //eq?
                if (argsName.Equals("eq?"))
                {
                    bool bt1 = firstArgs.isSymbol();
                    bool bt2 = secondArgs.isSymbol();
                    if (bt1 && bt2)
                    {
                        bool bt3 = firstArgs.getName().Equals(secondArgs.getName());
                        return(BoolLit.getInstance(bt3));
                    }
                    else
                    {
                        bool bt4 = firstArgs == secondArgs;
                        return(BoolLit.getInstance(bt4));
                    }
                }
                //cons
                else if (argsName.Equals("cons"))
                {
                    return(new Cons(firstArgs, secondArgs));
                }
                //set-car!
                else if (argsName.Equals("set-car!"))
                {
                    firstArgs.setCar(secondArgs);
                    //return value unspecified
                    return(new StringLit(" "));
                }
                //set-cdr!
                else if (argsName.Equals("set-cdr!"))
                {
                    firstArgs.setCdr(secondArgs);
                    //return value unspecified
                    return(new StringLit(" "));
                }
                //eval
                else if (argsName.Equals("eval"))
                {
                    return(firstArgs.eval((Environment)secondArgs));
                }
                //apply
                else if (argsName.Equals("apply"))
                {
                    return(firstArgs.apply(secondArgs));
                }
                else
                {
                    return(Nil.getInstance());
                }

                //binary arithematic opecations tmp vars
                int bo1 = 0;
                int bo2 = 0;
                if (firstArgs.isNumber())
                {
                    bo1 = firstArgs.getValue();
                }
                else if (secondArgs.isNumber())
                {
                    bo2 = secondArgs.getValue();
                }
                else
                {
                    Console.Error.WriteLine("invalid arguemtent name given");
                    return(Nil.getInstance());
                }
                //b+
                if (argsName.Equals("b+"))
                {
                    return(new IntLit(bo1 + bo2));
                }
                //b-
                else if (argsName.Equals("b-"))
                {
                    return(new IntLit(bo1 - bo2));
                }
                //b*
                else if (argsName.Equals("b*"))
                {
                    return(new IntLit(bo1 * bo2));
                }
                //b/
                else if (argsName.Equals("b/"))
                {
                    return(new IntLit(bo1 / bo2));
                }
                //b=
                else if (argsName.Equals("b="))
                {
                    bool b = bo1 == bo2;
                    return(BoolLit.getInstance(b));
                }
                //b<
                else if (argsName.Equals("b<"))
                {
                    bool b = bo1 < bo2;
                    return(BoolLit.getInstance(b));
                }
                else
                {
                    Console.Error.WriteLine("Aritheatic Operation Error");
                    return(Nil.getInstance());
                }
            }
            //invalid argsName given
            else
            {
                Console.Error.WriteLine("invalid arguemtent name given");
                return(Nil.getInstance());
            }

            return(Nil.getInstance());
        }
Example #9
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());
        }