Exemplo n.º 1
0
        public static void Start()
        {
            Object.Environment env      = Object.Environment.NewEnvironment();
            Object.Environment macroEnv = Object.Environment.NewEnvironment();

            for (;;)
            {
                System.Console.Write(PROMPT);
                string line = System.Console.ReadLine();

                lexer.Lexer   l = lexer.Lexer.New(line);
                parser.Parser p = parser.Parser.New(l);

                ast.Program program = p.ParseProgram();
                if (parser.Parser.Errors().Count != 0)
                {
                    printParserErrors(parser.Parser.Errors());
                    continue;
                }

                evaluator.macro_expansion.DefineMacros(program, macroEnv);
                ast.Node expanded = evaluator.macro_expansion.ExpandMacros(program, macroEnv);

                Object.Object evaluated = evaluator.evaluator.Eval(expanded, env);
                if (evaluated != null)
                {
                    System.Console.Write(evaluated.Inspect());
                    System.Console.WriteLine();
                }
            }
        }
Exemplo n.º 2
0
        public static Parser New(lexer.Lexer l)
        {
            Parser p = new Parser();

            Parser.l      = l;
            Parser.errors = new List <string>();

            prefixParseFns = new Dictionary <TokenType, prefixParseFn>();
            p.registerPrefix(token.IDENT, parseIdentifier);
            p.registerPrefix(token.INT, parseIntegerLiteral);
            p.registerPrefix(token.STRING, parseStringLiteral);
            p.registerPrefix(token.BANG, parsePrefixExpression);
            p.registerPrefix(token.MINUS, parsePrefixExpression);
            p.registerPrefix(token.TRUE, parseBoolean);
            p.registerPrefix(token.FALSE, parseBoolean);
            p.registerPrefix(token.LPAREN, parseGroupedExpression);
            p.registerPrefix(token.IF, parseIfExpression);
            p.registerPrefix(token.FUNCTION, parseFunctionLiteral);
            p.registerPrefix(token.LBRACKET, parseArrayLiteral);
            p.registerPrefix(token.LBRACE, parseHashLiteral);
            p.registerPrefix(token.MACRO, parseMacroLiteral);

            infixParseFns = new Dictionary <TokenType, infixParseFn>();
            p.registerInfix(token.PLUS, parseInfixExpression);
            p.registerInfix(token.MINUS, parseInfixExpression);
            p.registerInfix(token.SLASH, parseInfixExpression);
            p.registerInfix(token.ASTERISK, parseInfixExpression);
            p.registerInfix(token.EQ, parseInfixExpression);
            p.registerInfix(token.NOT_EQ, parseInfixExpression);
            p.registerInfix(token.LT, parseInfixExpression);
            p.registerInfix(token.GT, parseInfixExpression);

            p.registerInfix(token.LPAREN, parseCallExpression);
            p.registerInfix(token.LBRACKET, parseIndexExpression);

            // Read two tokens, so curToken and peekToken are both set
            nextToken();
            nextToken();

            return(p);
        }