Example #1
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
        var env = new Tree.Environment(); // create built in environment for scheme functions
        var id = new Ident("car");
        // TODO: create lines for definitions of built in functions from first page of docs
        id = new Ident("b+");
        env.define(id, new BuiltIn(id)); // populates environment--puts car into built in environment--define manages tree for you
        env = new Tree.Environment(env); //
        root = (Node) parser.parseExp();
        while (root != null)
        {
            root.print(0);
            root = (Node) parser.parseExp();
        }

        return 0;
    }
Example #2
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

        // TODO: print prompt and evaluate the expression
        root = (Node) parser.parseExp();
        while (root != null)
        {
            root.print(0);
            root = (Node) parser.parseExp();
        }

        return 0;
    }
Example #3
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
        Parser parser = new Parser(scanner);
        Node root;

        // Parse and pretty-print each input expression
        root = parser.parseExp();
        while (root != null)
        {
            root.print(0);
            Console.WriteLine();
            root = parser.parseExp();
        }

        return 0;
    }
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();
     }
 }