Пример #1
0
        private static void StripComments(ref StringSection line)
        {
            int iComment = line.IndexOf(';');

            if (iComment >= 0)
            {
                line = line.Substring(0, iComment);
            }
            line = line.Trim();
        }
Пример #2
0
        public IncBinDirective(int instructionIndex, int sourceLine, StringSection file)
            : base(instructionIndex, sourceLine)
        {
            file = file.Trim();
            if (file.Length > 1 && file[0] == '\"' && file[file.Length - 1] == '\"')
            {
                file = file.Substring(1, file.Length - 2);
            }

            this.Filename = file.ToString();
        }
Пример #3
0
        /// <summary>
        /// Returns true of a directive was parsed, even if it was not parsed successfully due to an error
        /// </summary>
        /// <param name="directiveName"></param>
        /// <param name="line"></param>
        /// <param name="sourceLine"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        private bool ParseDirective(StringSection directiveName, StringSection line, int sourceLine, out Error error)
        {
            error = Error.None;

            if (StringEquals(directiveName, "org", true))
            {
                assembly.Directives.Add(new OrgDirective(NextInstructionIndex, sourceLine, new AsmValue(line.ToString())));
            }
            else if (StringEquals(directiveName, "base", true))
            {
                assembly.Directives.Add(new BaseDirective(NextInstructionIndex, sourceLine, new AsmValue(line.ToString())));
            }
            else if (StringEquals(directiveName, "incbin", true))
            {
                assembly.Directives.Add(new IncBinDirective(NextInstructionIndex, sourceLine, line));
            }
            else if (StringEquals(directiveName, "error", true))
            {
                assembly.Directives.Add(new ErrorDirective(NextInstructionIndex, sourceLine, line));
            }
            else if (StringEquals(directiveName, "patch", true))
            {
                assembly.Directives.Add(new PatchDirective(NextInstructionIndex, sourceLine, line.ToString()));
            }
            else if (StringEquals(directiveName, "define", true))
            {
                line = line.Trim();
                if (GetSymbol(line).Length == line.Length)   // line should contain a only a symbol
                {
                    assembly.Directives.Add(new DefineDirective(NextInstructionIndex, sourceLine, line));
                }
                else
                {
                    error = new Error(ErrorCode.Expected_LValue, string.Format(Error.Msg_InvalidSymbolName_name, line.ToString()), sourceLine);
                }
            }
            else if (StringEquals(directiveName, "hex", true))
            {
                assembly.Directives.Add(new HexDirective(NextInstructionIndex, sourceLine, line));
            }
            else if (StringEquals(directiveName, "db", true))
            {
                assembly.Directives.Add(new DataDirective(NextInstructionIndex, sourceLine, line, DataDirective.DataType.Bytes));
            }
            else if (StringEquals(directiveName, "byte", true))
            {
                assembly.Directives.Add(new DataDirective(NextInstructionIndex, sourceLine, line, DataDirective.DataType.Bytes));
            }
            else if (StringEquals(directiveName, "dw", true))
            {
                assembly.Directives.Add(new DataDirective(NextInstructionIndex, sourceLine, line, DataDirective.DataType.Words));
            }
            else if (StringEquals(directiveName, "word", true))
            {
                assembly.Directives.Add(new DataDirective(NextInstructionIndex, sourceLine, line, DataDirective.DataType.Words));
            }
            else if (StringEquals(directiveName, "data", true))
            {
                assembly.Directives.Add(new DataDirective(NextInstructionIndex, sourceLine, line, DataDirective.DataType.Implicit));
            }
            else if (StringEquals(directiveName, "dsb", true))
            {
                assembly.Directives.Add(new StorageDirective(NextInstructionIndex, sourceLine, line, StorageDirective.DataType.Bytes));
            }
            else if (StringEquals(directiveName, "dsw", true))
            {
                assembly.Directives.Add(new StorageDirective(NextInstructionIndex, sourceLine, line, StorageDirective.DataType.Words));
            }
            else if (StringEquals(directiveName, "overflow", true))
            {
                assembly.Directives.Add(new OptionDirective(NextInstructionIndex, sourceLine, directiveName.ToString(), line.Trim().ToString()));
            }
            else if (StringEquals(directiveName, "if", true) ||
                     StringEquals(directiveName, "else", true) ||
                     StringEquals(directiveName, "ifdef", true) ||
                     StringEquals(directiveName, "ifndef", true) ||
                     StringEquals(directiveName, "endif", true))
            {
                assembly.Directives.Add(new ConditionalDirective(NextInstructionIndex, sourceLine, directiveName, line.Trim(), out error));
            }
            else if (StringEquals(directiveName, "ENUM", true))
            {
                assembly.Directives.Add(new EnumDirective(NextInstructionIndex, sourceLine, line));
            }
            else if (StringEquals(directiveName, "ENDE", true) || StringEquals(directiveName, "ENDENUM", true))
            {
                if (line.IsNullOrEmpty)
                {
                    assembly.Directives.Add(new EndEnumDirective(NextInstructionIndex, sourceLine));
                }
                else
                {
                    error = new Error(ErrorCode.Unexpected_Text, Error.Msg_NoTextExpected, sourceLine);
                }
            }
            else if (StringEquals(directiveName, "signed", true))
            {
                assembly.Directives.Add(new OptionDirective(NextInstructionIndex, sourceLine, directiveName.ToString(), line.Trim().ToString()));
            }
            else if (StringEquals(directiveName, "needdot", true))
            {
                assembly.Directives.Add(new OptionDirective(NextInstructionIndex, sourceLine, directiveName.ToString(), line.Trim().ToString()));
            }
            else if (StringEquals(directiveName, "needcolon", true))
            {
                assembly.Directives.Add(new OptionDirective(NextInstructionIndex, sourceLine, directiveName.ToString(), line.Trim().ToString()));
            }
            else if (StringEquals(directiveName, "alias", true))
            {
                var varName = GetSymbol(line);
                if (varName.IsNullOrEmpty)
                {
                    error = new Error(ErrorCode.Syntax_Error, Error.Msg_ExpectedText, sourceLine);
                    return(true);
                }
                line = line.Substring(varName.Length).Trim();

                assembly.Directives.Add(new Assignment(NextInstructionIndex, sourceLine, varName, true, new AsmValue(line.ToString())));
            }
            else
            {
                return(false);
            }

            return(true);
        }
Пример #4
0
        private bool ParseInstruction(StringSection instruction, StringSection operand, int iSourceLine, out Error error)
        {
            error = Error.None;
            if (operand.Length > 0 && operand[0] == '@')
            {
            }
            var addressing = ParseAddressing(ref operand);
            int opcodeVal  = FindOpcode(instruction, addressing);

            // Some instructions only support zero-page addressing variants of certain addressing modes
            if ((OpcodeError)opcodeVal == OpcodeError.InvalidAddressing)
            {
                var newAddressing = addressing;
                if (TryZeroPageEqivalent(ref newAddressing))
                {
                    opcodeVal = FindOpcode(instruction, newAddressing);
                    if (opcodeVal >= 0)
                    {
                        addressing = newAddressing;                 // Keep the zero-page addressing
                    }
                }
            }

            if (opcodeVal < 0)
            {
                // We don't throw an error if 'instruction' isn't an instruction (the line could be anything else other than instruction)
                if ((OpcodeError)opcodeVal == OpcodeError.UnknownInstruction)
                {
                    return(false);
                }
                else
                {
                    SetOpcodeError(iSourceLine, instruction.ToString(), addressing, opcodeVal, out error);
                    return(true);
                }
            }

            if (addressing != Opcode.addressing.implied)
            {
                LiteralValue operandValue      = new LiteralValue();
                string       operandExpression = null;

                // Todo: consider method(s) such as assembly.AddInstruction
                if (ExpressionEvaluator.TryParseLiteral(operand, out operandValue))
                {
                    assembly.ParsedInstructions.Add(new ParsedInstruction((byte)opcodeVal, operandValue, iSourceLine));
                }
                else if (operand.Length > 0)
                {
                    assembly.ParsedInstructions.Add(new ParsedInstruction((byte)opcodeVal, operand.Trim().ToString(), iSourceLine));
                }
                else
                {
                    assembly.ParsedInstructions.Add(new ParsedInstruction((byte)opcodeVal, default(LiteralValue), iSourceLine));
                }
            }
            else
            {
                assembly.ParsedInstructions.Add(new ParsedInstruction((byte)opcodeVal, default(LiteralValue), iSourceLine));
            }

            return(true);
        }