示例#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;
    }
示例#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;
    }
示例#3
0
        public Node parseExp()
        {
            Token curToken = scanner.getNextToken();

            // TODO: write code for parsing an exp
            // Began the tree stucture and I believe most of it should be work fine

            //#t grammar
            if (curToken == null)
            {
                return(null);
            }
            else if (curToken.getType() == new Token(TokenType.TRUE).getType())
            {
                return(t);
            }
            //#f grammar
            else if (curToken.getType() == new Token(TokenType.FALSE).getType())
            {
                return(f);
            }

            // ( rest grammar

            else if (curToken.getType() == new Token(TokenType.LPAREN).getType())
            {
                //return new Cons(parseRest(), parseRest());
                return(parseRest());
            }

            // Identifier grammar
            else if (curToken.getType() == new Token(TokenType.IDENT).getType())
            {
                return(new Ident(curToken.getName()));
            }

            // Int_constant grammar
            else if (curToken.getType() == new Token(TokenType.INT).getType())
            {
                return(new IntLit(curToken.getIntVal()));
            }

            // String_constant grammar
            else if (curToken.getType() == new Token(TokenType.STRING).getType())
            {
                return(new StringLit(curToken.getStringVal()));
            }

            // ' exp grammar
            else if (curToken.getType() == new Token(TokenType.QUOTE).getType())
            {
                return(new Cons(new Ident("quote"), new Cons(parseExp(), nil)));
            }

            return(null);
        }
示例#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
        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;
    }
        //parses grammar for exp ->
        public Node parseExp()
        {
            Token tok;

            tok = scanner.getNextToken();
            //checks for end of file
            if (tok == null)
            {
                Nil retNil = nilPoint;
                return(retNil);
            }
            //checks for case of left parenthesis
            if (tok.getType() == TokenType.LPAREN)
            {
                return(parseRest(true));
            }
            //checks for case of false constant
            else if (tok.getType() == TokenType.FALSE)
            {
                BoolLit ret = fBool;
                return(ret);
            }
            //checks for case of true constant
            else if (tok.getType() == TokenType.TRUE)
            {
                BoolLit ret = tBool;
                return(ret);
            }
            //checks for case of QUOTE symbol
            else if (tok.getType() == TokenType.QUOTE)
            {
                return(new Cons(new Ident("quote"), new Cons(parseExp(), new Nil()), true));
            }
            //checks for case int constant
            else if (tok.getType() == TokenType.INT)
            {
                return(new IntLit(tok.getIntVal()));
            }
            //checks for case of string constant
            else if (tok.getType() == TokenType.STRING)
            {
                return(new StringLit(tok.getStringVal()));
            }
            //checks for case of identifier
            else if (tok.getType() == TokenType.IDENT)
            {
                return(new Ident(tok.getName()));
            }

            return(null);
        }
示例#6
0
 public Node parseExp()
 {
     scannerToken = scanner.getNextToken();
     //Console.WriteLine(scannerToken + "ExpACL");
     if (scannerToken == null)
     {
         // Console.WriteLine("NullParseExp()   ");
         return(null);
     }
     else
     {
         return(parseExp(scannerToken));
     }
 }
        public Node parseExp()
        {
            // TODO: write code for parsing an exp
            Token t = scanner.getNextToken();

            if (t == null)
            {
                return(null);
            }

            try {
                switch (t.getType())
                {
                case TokenType.LPAREN:
                    return(parseRest());

                case TokenType.FALSE:
                    return(new BoolLit(false));

                case TokenType.TRUE:
                    return(new BoolLit(true));

                case TokenType.QUOTE:
                    return(new Cons(new Ident("\'"), parseExp()));

                case TokenType.DOT:
                    return(new Cons(new StringLit("."), parseExp()));

                case TokenType.INT:
                    return(new IntLit(t.getIntVal()));

                case TokenType.STRING:
                    return(new StringLit(t.getStringVal()));

                case TokenType.IDENT:
                    return(new Ident(t.getName()));
                }
            } catch (IOException e) {
                Console.Error.WriteLine("IOException: " + e.Message);

                return(new Nil());
            }
            return(null);
        }
示例#8
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;
        }
示例#9
0
 public Node parseExp()
 {
     // TODO: write code for parsing an exp
     return(parseExp(scanner.getNextToken()));
 }
示例#10
0
 public Node parseExp()
 {
     return(parseExp(scanner.getNextToken()));
 }