コード例 #1
0
        private void HandleMessage(InstructionInfoEventArgs e, string messageText)
        {
            int indentSize        = GetIndentSize(messageText);
            int realSplitPosition = FindCharacterIndexOfRightMargin(messageText, maxCharactersPerLine);

            if (realSplitPosition < messageText.Length || e.Handled == true)
            {
                e.Handled = true;
                while (realSplitPosition < messageText.Length)
                {
                    int position = FindSplitPoint(messageText, realSplitPosition);
                    if (position <= indentSize)
                    {
                        //for really long lines - split them at the wrap position instead of an infinite loop
                        position = realSplitPosition;
                    }

                    string line = messageText.Substring(0, position);

                    e.WriteLine(" " + AssemblerProjectWriter.EscapeAndQuoteMessage(line) + " ");
                    GenerateNextLine(e);
                    e.WriteLine("");

                    messageText = messageText.Substring(position);

                    //eat spaces
                    position = 0;
                    while (position < messageText.Length && messageText[position] == ' ')
                    {
                        position++;
                    }
                    if (position > 0)
                    {
                        messageText = messageText.Substring(position);
                    }

                    //add indent if enabled
                    if (MaintainIndentation)
                    {
                        if (indentSize > 0)
                        {
                            messageText = "".PadLeft(indentSize, ' ') + messageText;
                        }
                    }

                    //find next split
                    realSplitPosition = FindCharacterIndexOfRightMargin(messageText, maxCharactersPerLine);
                }

                //remaining text is set to something if this is a "next message" call, so output any leftover text at that time.
                //Otherwise, if we have the leftover text feature disabled, output it now.
                if (!this.RemoveLineBreaksIfWordWrapping || this.remainingText != "")
                {
                    e.WriteText(" " + AssemblerProjectWriter.EscapeAndQuoteMessage(messageText) + " ");
                    this.remainingText = "";
                }
                else
                {
                    this.remainingText = messageText;
                }
            }
        }
コード例 #2
0
        static void ToStringReal(TokenExpression ex, MyIndentedTextWriter tw)
        {
            if (ex == null)
            {
                return;
            }
            switch (ex.TokenType)
            {
            default:
                if (!String.IsNullOrEmpty(ex.Token.Value) && ex.Token.Value != "...")
                {
                    tw.Write(ex.Token.Value);
                }
                if (ex.Subexpressions.Count > 0)
                {
                    tw.Write(" ");
                    ToStringReal(ex.Subexpressions, tw);
                }
                break;

            case TokenType.Array:
            case TokenType.AddressOf:
            case TokenType.At:
            case TokenType.Complement:
            case TokenType.Increment:
            case TokenType.Decrement:
                if (!String.IsNullOrEmpty(ex.Token.Value) && ex.Token.Value != "...")
                {
                    tw.Write(ex.Token.Value);
                }
                if (ex.Subexpressions.Count > 0)
                {
                    ToStringReal(ex.Subexpressions, tw);
                }
                break;

            case TokenType.Block:
                if (!tw.TabsPending)
                {
                    tw.WriteLine();
                }
                tw.WriteLine("{");
                tw.Indent++;
                ToStringReal(ex.Subexpressions, tw);
                tw.Indent--;
                if (!tw.TabsPending)
                {
                    tw.WriteLine();
                }
                tw.WriteLine("}");
                break;

            case TokenType.Statement:
                ToStringReal(ex.Subexpressions, tw);
                tw.WriteLine(";");
                break;

            case TokenType.StringLiteral:
                tw.Write(AssemblerProjectWriter.EscapeAndQuoteString(ex.Token.Value));
                break;

            case TokenType.CharLiteral:
            case TokenType.Message:
                tw.Write(AssemblerProjectWriter.EscapeAndQuoteMessage(ex.Token.Value));
                break;

            case TokenType.ArrayIndex:
                tw.Write("[");
                ToStringReal(ex.Subexpressions, tw);
                tw.Write("]");
                break;

            case TokenType.Assert:
                tw.Write("assert (");
                for (int i = 0; i < ex.Subexpressions.Count; i++)
                {
                    if (i != 0)
                    {
                        tw.Write(", ");
                    }
                    ToStringReal(ex.Subexpressions[i], tw);
                }
                tw.Write(")");
                break;

            case TokenType.For:
                tw.Write("for (");
                for (int i = 0; i < 3; i++)
                {
                    if (i != 0)
                    {
                        tw.Write("; ");
                    }
                    var subex = ex.Subexpressions.GetOrNull(i);
                    ToStringReal(subex, tw);
                }
                tw.WriteLine(")");
                ToStringReal(ex.Subexpressions.GetOrNull(3), tw);
                break;

            case TokenType.While:
                tw.Write("while (");
                ToStringReal(ex.Subexpressions.GetOrNull(0), tw);
                tw.WriteLine(")");
                ToStringReal(ex.Subexpressions.GetOrNull(1), tw);
                break;

            case TokenType.If:
                tw.Write("if (");
                ToStringReal(ex.Subexpressions.GetOrNull(0), tw);
                tw.WriteLine(")");
                ToStringReal(ex.Subexpressions.GetOrNull(1), tw);
                {
                    var subex = ex.Subexpressions.GetOrNull(2);
                    if (subex != null)
                    {
                        ToStringReal(subex, tw);
                    }
                }
                break;

            case TokenType.Switch:
                tw.Write("switch (");
                ToStringReal(ex.Subexpressions.GetOrNull(0), tw);
                tw.WriteLine(")");
                ToStringReal(ex.Subexpressions.Skip(1), tw);
                break;

            case TokenType.FunctionCall:
                ToStringReal(ex.Subexpressions.GetOrNull(0), tw);
                tw.Write("(");
                ToStringReal(ex.Subexpressions.Skip(1), tw);
                tw.Write(")");
                break;

            case TokenType.PostDecrement:
            case TokenType.PostIncrement:
                ToStringReal(ex.Subexpressions, tw);
                tw.Write(ex.Token.Value);
                break;

            //infix binary operators
            case TokenType.And:
            case TokenType.AndAssign:
            case TokenType.Assign:
            case TokenType.Colon:
            case TokenType.Comma:
            case TokenType.Divide:
            case TokenType.DivideAssign:
            case TokenType.Dot:
            case TokenType.EqualTo:
            case TokenType.LeftShift:
            case TokenType.LeftShiftAssign:
            case TokenType.LessThan:
            case TokenType.LessThanOrEqualTo:
            case TokenType.LogicalAnd:
            case TokenType.LogicalOr:
            case TokenType.Minus:
            case TokenType.MinusAssign:
            case TokenType.Modulo:
            case TokenType.ModuloAssign:
            case TokenType.Multiply:
            case TokenType.MultiplyAssign:
            case TokenType.NotEqualTo:
            case TokenType.Or:
            case TokenType.OrAssign:
            case TokenType.Plus:
            case TokenType.PlusAssign:
            case TokenType.QuestionMark:
            case TokenType.ReferenceAssign:
            case TokenType.ReferenceEqualTo:
            case TokenType.ReferenceNotEqualTo:
            case TokenType.ReferenceSwap:
            case TokenType.RightShift:
            case TokenType.RightShiftAssign:
            case TokenType.Xor:
            case TokenType.XorAssign:
                //output left side
                ToStringReal(ex.Subexpressions.GetOrNull(0), tw);

                if (ex.TokenType != TokenType.Comma && ex.TokenType != TokenType.Dot)
                {
                    tw.Write(" ");
                }

                if (String.IsNullOrEmpty(ex.Token.Value) || ex.Token.Value == "...")
                {
                    if (keywordTableInverse.ContainsKey(ex.TokenType))
                    {
                        tw.Write(keywordTableInverse[ex.TokenType]);
                    }
                }
                else
                {
                    tw.Write(ex.Token.Value);
                }
                if (ex.TokenType != TokenType.Dot)
                {
                    tw.Write(" ");
                }

                ToStringReal(ex.Subexpressions.Skip(1), tw);
                break;
            }



            //foreach (var ex in exBase.GetAllSubexpressionsRecursive())
            //{
            //    switch (ex.TokenType)
            //    {
            //        case TokenType.And:
            //            case TokenType.

            //        default:
            //            sb.Append(ex.Token.Value);
            //            break;
            //    }
            //}
            //return sb.ToString();
        }
コード例 #3
0
            void writer_BeforeWritingInstruction(object sender, InstructionInfoEventArgs e)
            {
                var instructionInfo = e.InstructionInfo;
                var instruction     = instructionInfo.instruction;
                int word1           = instructionInfo.word1;

                if (InsideFunction)
                {
                    if (!hasOutputFunction)
                    {
                        if (this.CodePatches.ContainsKey(currentFunctionName))
                        {
                            var codePatch = this.CodePatches[currentFunctionName];
                            e.WriteLine(codePatch);
                        }
                        hasOutputFunction = true;
                    }

                    if (instruction == Instruction.FUNC || instruction == Instruction.ENDFUNC || instruction == Instruction.EOF)
                    {
                        //will emit the ENDFUNC or next FUNC instruction
                        InsideFunction     = false;
                        e.StopEmittingCode = false;
                    }
                    else
                    {
                        e.Handled          = true;
                        e.StopEmittingCode = true;
                        return;
                    }
                }


                if (instruction == Instruction.FUNC)
                {
                    int functionNumber = instructionInfo.word1;
                    var function       = ainFile.GetFunction(functionNumber);
                    if (function != null)
                    {
                        stringNumber        = 0;
                        messageNumber       = 0;
                        currentFunctionName = function.Name;
                        stringDictionary    = stringEntries.GetOrNull(currentFunctionName);
                        messageDictionary   = messageEntries.GetOrNull(currentFunctionName);

                        if (this.CodePatches.ContainsKey(currentFunctionName))
                        {
                            //var codePatch = this.CodePatches[currentFunctionName];
                            //e.WriteLine(codePatch);
                            e.Handled          = false;
                            e.StopEmittingCode = false;
                            //will emit the FUNC instruction, but then not the rest
                            this.InsideFunction    = true;
                            this.hasOutputFunction = false;
                        }
                    }
                }
                else /*if (stringDictionary != null || messageDictionary != null)*/
                {
                    if (instruction == Instruction.MSG)
                    {
                        string originalMessage = originalAinFile.GetMessage(word1);
                        string newMessage      = null;
                        if (messageDictionary != null)
                        {
                            newMessage = messageDictionary.GetOrNull(messageNumber);
                        }

                        if (newMessage != null && newMessage != originalMessage)
                        {
                            e.Text = newMessage;
                        }

                        if (this.WordWrap)
                        {
                            if (this.wordWrapper.HasRemainingText == false && e.Text == originalMessage)
                            {
                                //don't wrap text because we match the original and don't have remaining text
                            }
                            else
                            {
                                wordWrapper.projectWriter_BeforeWritingInstruction(sender, e);
                            }
                        }
                        if (e.Dirty && !e.Handled)
                        {
                            //recheck this!
                            newMessage = e.Text;
                            e.WriteLine("\tMSG " + AssemblerProjectWriter.EscapeAndQuoteMessage(newMessage));
                            e.Handled = true;
                        }
                        messageNumber++;
                    }
                    //string instructions are handled by writer_BeforeWritingString
                    else if (instruction == Instruction.CALLFUNC)
                    {
                        //for text wrapping (to do later)
                        if (this.WordWrap)
                        {
                            wordWrapper.projectWriter_BeforeWritingInstruction(sender, e);
                        }
                    }
                }
            }