Exemplo n.º 1
0
 public Scanner(SourceReader source, ErrorHandler errorHandler)
 {
     Source = source;
     ErrorHandler = errorHandler;
     SymbolTokens.Add("(", Token.Types.LParen);
     SymbolTokens.Add(")", Token.Types.RParen);
     SymbolTokens.Add("+", Token.Types.OpPlus);
     SymbolTokens.Add("-", Token.Types.OpMinus);
     SymbolTokens.Add("/", Token.Types.OpDivide);
     SymbolTokens.Add("*", Token.Types.OpMultiply);
     SymbolTokens.Add("<", Token.Types.OpLess);
     SymbolTokens.Add("=", Token.Types.OpEquals);
     SymbolTokens.Add("&", Token.Types.OpAnd);
     SymbolTokens.Add("!", Token.Types.OpNot);
     SymbolTokens.Add(";", Token.Types.LineTerm);
     SymbolTokens.Add(":", Token.Types.Colon);
     SymbolTokens.Add(":=", Token.Types.OpAssignment);
     SymbolTokens.Add("..", Token.Types.OpRange);
     KeywordTokens.Add("var", Token.Types.KwVar);
     KeywordTokens.Add("int", Token.Types.KwInt);
     KeywordTokens.Add("string", Token.Types.KwString);
     KeywordTokens.Add("bool", Token.Types.KwBool);
     KeywordTokens.Add("for", Token.Types.KwFor);
     KeywordTokens.Add("end", Token.Types.KwEnd);
     KeywordTokens.Add("in", Token.Types.KwIn);
     KeywordTokens.Add("do", Token.Types.KwDo);
     KeywordTokens.Add("read", Token.Types.KwRead);
     KeywordTokens.Add("print", Token.Types.KwPrint);
     KeywordTokens.Add("assert", Token.Types.KwAssert);
     EscapeCharacters.Add('"', '"');
     EscapeCharacters.Add('\'', '\'');
     EscapeCharacters.Add('n', '\n');
     EscapeCharacters.Add('t', '\t');
     EscapeCharacters.Add('\\', '\\');
 }
 public TypeCheckerVisitor(ErrorHandler errors, StmtList statements)
 {
     Errors = errors;
     SymbolTable = new SymbolTable();
     Checker = new TypeChecker(errors);
     RootStmtList = statements;
 }
Exemplo n.º 3
0
 public TypeChecker(ErrorHandler errors)
 {
     Errors = errors;
     intTypeBindings = new Dictionary<Operator, ExprType>()
     {
         { Operator.Plus, ExprType.IntType },
         { Operator.Minus, ExprType.IntType },
         { Operator.Divide, ExprType.IntType },
         { Operator.Times, ExprType.IntType },
         { Operator.Equals, ExprType.BoolType },
         { Operator.Less, ExprType.BoolType }
     };
     boolTypeBindings = new Dictionary<Operator, ExprType>()
     {
         { Operator.Equals, ExprType.BoolType },
         { Operator.And, ExprType.BoolType },
         { Operator.Not, ExprType.BoolType },
         { Operator.Less, ExprType.BoolType }
     };
     stringTypeBindings = new Dictionary<Operator, ExprType>()
     {
         { Operator.Plus, ExprType.StringType },
         { Operator.Equals, ExprType.BoolType },
         { Operator.Less, ExprType.BoolType }
     };
     typeBindings = new Dictionary<ExprType, Dictionary<Operator, ExprType>>()
     {
         { ExprType.IntType, intTypeBindings },
         { ExprType.BoolType, boolTypeBindings },
         { ExprType.StringType, stringTypeBindings }
     };
 }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Usage: interpreter filename [-p]\n\n-p: Print the parsed abstract syntax tree.");
                return;
            }
            string filename = args[0];
            StreamReader sourceFile;
            try
            {
                sourceFile = new StreamReader(filename);
            }
            catch (Exception)
            {
                Console.WriteLine("File " + filename + " not found.");
                return;
            }

            SourceReader source = new SourceReader(sourceFile);
            ErrorHandler errors = new ErrorHandler();
            Scanner lexer = new Scanner(source, errors);
            Parser parser = new Parser(lexer, errors);
            StmtList ast = parser.Parse();
            TypeCheckerVisitor checker = new TypeCheckerVisitor(errors, ast);
            SymbolTable SymbolTable = checker.TypeCheck();
            if (errors.HasErrors)
            {
                Console.WriteLine("Given program contains following errors:");
                foreach (Error error in errors.GetErrors())
                {
                    Console.WriteLine(error);
                }
                return;
            }
            if (args.Length > 1 && args[1] == "-p")
            {
                Console.WriteLine("Parsed abstact syntax tree:");
                AstPrinterVisitor printer = new AstPrinterVisitor(ast);
                printer.Print();
                Console.WriteLine();
                Console.WriteLine("Program output:");
            }
            ExecutorVisitor executor = new ExecutorVisitor(SymbolTable, ast);
            try
            {
                executor.Execute();
            }
            catch (RuntimeException e)
            {
                Console.WriteLine("Runtime exception: " + e.Message);
            }
        }
Exemplo n.º 5
0
 public Parser(Scanner scanner, ErrorHandler errors)
 {
     Scanner = scanner;
     Errors = errors;
     CurrentToken = Scanner.GetNextToken();
 }
Exemplo n.º 6
0
 public void Initialize()
 {
     errors = new ErrorHandler();
 }