Ejemplo n.º 1
0
 public void Send(Message message)
 {
     foreach(var obj in observers)
     {
         obj.AcceptMessage(message);
     }
 }
Ejemplo n.º 2
0
 public static void AbortTranslation(ErrorCode err, message.MessageProducer mp)
 {
     string fatal = "FATAL ERROR: " + err.Message;
        var args = Tuple.Create(0, 0, "", fatal);
        Message msg = new Message(MessageType.SyntaxError, args);
        mp.Send(msg);
        Environment.Exit(-1);
 }
Ejemplo n.º 3
0
        public static void Flag(Token token, ErrorCode err, message.MessageProducer mp)
        {
            var args = Tuple.Create(token.LineNumber, token.Position, token.Lexeme, err.Message);
               Message msg = new Message(MessageType.SyntaxError, args);
               mp.Send(msg);

               if (++errors > MAX_ERRORS)
               {
               AbortTranslation(ErrorCode.TOO_MANY_ERRORS, mp);
               }
        }
Ejemplo n.º 4
0
        private void SendMessage(ICodeNode node, string variable_name, object value)
        {
            object line_number = node.GetAttribute(ICodeKey.LINE);

            // Send an ASSIGN message.
            if (line_number != null)
            {
                var args = Tuple.Create((int)line_number, variable_name, (object)value);
                Message msg = new Message(MessageType.Assign, args);
                Send(msg);
            }
        }
Ejemplo n.º 5
0
        private void SendSourceLineMessage(ICodeNode node)
        {
            object line_number = node.GetAttribute(ICodeKey.LINE);

            // send the SourceLine message
            if (line_number != null)
            {
                var args = (Tuple<int>)Tuple.Create((int)line_number);
                Message msg = new Message(MessageType.SourceLine, args);
                Send(msg);
            }
        }
Ejemplo n.º 6
0
        public override void Process(ICode iCode, SymbolTableStack symtabstack)
        {
            var start = DateTime.Now;
            //
            var end = DateTime.Now;
            double elapsed_time = (end - start).Ticks / (1.0 * TimeSpan.TicksPerSecond);
            int instruction_count = 0;

            // send the message
            var args = Tuple.Create(instruction_count, elapsed_time);
            Message msg = new Message(MessageType.CompilerSummary, args);
            Send(msg);
        }
Ejemplo n.º 7
0
        public void AcceptMessage(Message msg)
        {
            switch (msg.Type)
            {
                case MessageType.InterpreterSummary:
                    {
                        var args = (Tuple<int, int, double>)msg.Args;
                        Console.WriteLine(INTERPRETER_SUMMARY_FORMAT, args.Item1, args.Item2, args.Item3);
                    }
                    break;
                case MessageType.CompilerSummary:
                    {
                        var args = (Tuple<int, double>)msg.Args;
                        Console.WriteLine(COMPILER_SUMMARY_FORMAT, args.Item1, args.Item2);
                    }
                    break;
                case MessageType.Assign:
                    {
                        if (first_output_msg)
                        {
                            Console.WriteLine("\n===== OUTPUT =====\n");
                            first_output_msg = false;
                        }

                        var args = (Tuple<int, string, object>)msg.Args;
                        int line_number = args.Item1;
                        string variable = args.Item2;
                        object value = args.Item3;
                        Console.WriteLine(ASSIGN_FORMAT, line_number, variable, value);
                        break;
                    }
                case MessageType.RuntimeError:
                    {
                        var args = (Tuple<string, int>)msg.Args;
                        string error_msg = args.Item1;
                        int line_number = args.Item2;

                        Console.Write("*** RUNTIME ERROR");
                        Console.Write(" AT LINE {0,3}", line_number);
                        Console.WriteLine(": {0}", error_msg);
                        break;
                    }
                default:
                    break;
            }
        }
Ejemplo n.º 8
0
        public Tuple<ICode, SymbolTableStack> Parse()
        {
            var symtabstack = SymbolTableFactory.CreateStack();
            var icode = ICodeFactory.CreateICode();
            var start = DateTime.Now;
            try
            {
                Token token = scanner.GetNextToken();
                ICodeNode root = null;

                // look for the BEGIN token to parse a compound statement.
                if (token.TokenType == TokenType.BEGIN)
                {
                    StatementParser stmnt_parser = StatementParser.CreateWithObservers(scanner, symtabstack, observers);
                    root = stmnt_parser.Parse(token);
                    token = scanner.CurrentToken;
                } else
                {
                    ErrorHandler.Flag(token, ErrorCode.UNEXPECTED_TOKEN, this);
                }

                // look for the final period.
                if (token.TokenType != TokenType.DOT)
                {
                    ErrorHandler.Flag(token, ErrorCode.MISSING_PERIOD, this);
                }

                token = scanner.CurrentToken;
                if (root != null)
                {
                    icode.Root = root;
                }

                var end = DateTime.Now;
                double elapsedTime = (end - start).Ticks / (1.0 * TimeSpan.TicksPerSecond);
                var args = Tuple.Create(token.LineNumber, ErrorHandler.GetErrorCount(), elapsedTime);
                Message msg = new Message(MessageType.ParserSummary, args);
                Send(msg);
            } catch (Exception e)
            {
                Console.WriteLine(e.Message);
                ErrorHandler.AbortTranslation(ErrorCode.IO_ERROR, this);
            }
            return Tuple.Create(icode, symtabstack);
        }
Ejemplo n.º 9
0
        public override void Process(ICode iCode, SymbolTableStack symtabstack)
        {
            int execution_count = 0;
            var start = DateTime.Now;

            ICodeNode root = iCode.Root;
            StatementInterpreter stmnt_interpreter = StatementInterpreter.CreateWithObservers(observers);
            var execution_results = stmnt_interpreter.Execute(root, ref execution_count);

            var end = DateTime.Now;
            double elapsed_time = (end - start).Ticks / (1.0 * TimeSpan.TicksPerSecond);
            int runtime_errors = RuntimeErrorHandler.Errors;

            // send the message
            var args = Tuple.Create(execution_count, runtime_errors, elapsed_time);
            Message msg = new Message(MessageType.InterpreterSummary, args);
            Send(msg);
        }
Ejemplo n.º 10
0
        public static void Flag(ICodeNode node, RuntimeErrorCode error_code, MessageProducer backend)
        {
            //string line_number = null;
            while (node != null && node.GetAttribute(ICodeKey.LINE) == null)
            {
                node = node.Parent;
            }

            // notify observers
            var args = (Tuple<string, int>)Tuple.Create(error_code.Message, (int)node.GetAttribute(ICodeKey.LINE));
            Message msg = new Message(MessageType.RuntimeError, args);
            backend.Send(msg);

            if (++Errors > MAX_ERRORS)
            {
                Console.WriteLine("*** ABORTED AFTER TOO MANY RUNTIME ERRORS.*");
                Environment.Exit(-1);
            }
        }
Ejemplo n.º 11
0
        public void AcceptMessage(Message msg)
        {
            MessageType type = msg.Type;
            if (type == MessageType.ParserSummary)
            {
                var args = (Tuple<int, int, double>)msg.Args;
                Console.WriteLine(PARSER_SUMMARY_FORMAT, args.Item1, args.Item2, args.Item3);
            } else if (type == MessageType.Token)
            {
                var args = (Tuple<int, int, TokenType, string, object>)msg.Args;
                Console.WriteLine(PARSER_TOKEN_FORMAT, args.Item3.ToString(), args.Item1, args.Item2, args.Item4);
                if (args.Item5 != null)
                {
                    Console.WriteLine(PARSER_VALUE_FORMAT, args.Item5.ToString());
                }
            } else if (type == MessageType.SyntaxError)
            {
                var args = (Tuple<int, int, string, string>)msg.Args;
                int space = PREFIX_WIDTH + args.Item2;
                StringBuilder flagbuilder = new StringBuilder();
                flagbuilder.Append(' ', space - 1);
                flagbuilder.Append("^\n*** ").Append(args.Item4);

                if (!String.IsNullOrEmpty(args.Item3))
                {
                    flagbuilder.Append(" [at \"").Append(args.Item3).Append("\"]");
                }
                Console.WriteLine("{0}", flagbuilder.ToString());
            }
        }
Ejemplo n.º 12
0
        public void AcceptMessage(Message msg)
        {
            MessageType type = msg.Type;

            if (type == MessageType.SourceLine)
            {
                var args = (Tuple<int, string>)msg.Args;
                Console.WriteLine("{0:D3} {1}", args.Item1, args.Item2);
            }
        }
Ejemplo n.º 13
0
        private void ReadLine()
        {
            line = reader.ReadLine();
            Position = -1;

            if (line != null)
            {
                ++LineNumber;
                var args = Tuple.Create(LineNumber, line);
                Message msg = new Message(MessageType.SourceLine, args);
                Send(msg);
            }
        }