예제 #1
0
 public AST(MiniPLGrammar grammar, SyntaxTree tree)
 {
     errors           = new List <String>();
     currentConstants = new HashSet <String>();
     vars             = new Dictionary <String, Variable>();
     this.grammar     = grammar;
     root             = parse(tree.root);
     if (errors.Count > 0)
     {
         throw new SemanticError(errors);
     }
 }
예제 #2
0
        static void interpret(String program)
        {
            System.Console.WriteLine("Parsing");
            MiniPLGrammar g = new MiniPLGrammar();
            AST           ast;

            try
            {
                ast = new AST(g, g.parse(program));
            } //these exceptions are the user's fault
            catch (SemanticError e)
            {
                System.Console.WriteLine("Error parsing program\n" + e.Message);
                return;
            }
            catch (ParseError e)
            {
                System.Console.WriteLine("Error parsing program\n" + e.Message);
                return;
            }
            catch (ScannerException e)
            {
                System.Console.WriteLine("Error parsing program\n" + e.Message);
                return;
            }
            //every other exception is a bug in this program
            System.Console.WriteLine("Interpreting");
            Interpreter inter = new Interpreter();

            try
            {
                inter.run(ast);
            }
            catch (InterpreterException e) //user's fault
            {
                System.Console.WriteLine("Error interpreting program: " + e.Message);
                return;
            }
            //everything else is a bug
            System.Console.WriteLine("");
            System.Console.WriteLine("Done interpreting");
        }