Ejemplo 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();
                }
            }
        }
Ejemplo n.º 2
0
        public ast.Program ParseProgram()
        {
            ast.Program program = new ast.Program {
            };
            program.Statements = new List <ast.Statement>();

            while (!curTokenIs(token.EOF))
            {
                ast.Statement stmt = parseStatement();
                if (stmt != null)
                {
                    program.Statements.Add(stmt);
                }
                nextToken();
            }

            return(program);
        }
Ejemplo n.º 3
0
        static Object.Object evalProgram(ast.Program program, Object.Environment env)
        {
            Object.Object result = null;

            foreach (ast.Statement statement in program.Statements)
            {
                result = Eval(statement, env);

                if (result is Object.ReturnValue)
                {
                    return(((Object.ReturnValue)result).Value);
                }

                if (result is Object.Error)
                {
                    return(result);
                }
            }

            return(result);
        }