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(); }
private void ProcessDirective(StringSection directiveName, StringSection directiveText) { string directive = directiveName.ToString().ToUpper(); switch (directive) { case "ORG": default: break; } }
bool ParseOperand(StringSection operand, out LiteralValue value, out string expression) { if (ExpressionEvaluator.TryParseLiteral(operand, out value)) { expression = null; return(true); } else if (operand.Length > 0) { value = default(LiteralValue); expression = operand.ToString(); return(true); } else { value = default(LiteralValue); expression = null; return(false); } }
public ConditionalDirective(int iInstruction, int iSourceLine, StringSection directive, StringSection line, out Error error) : base(iInstruction, iSourceLine) { error = Error.None; condition = line; switch (directive.ToString().ToUpper()) { case "IF": part = ConditionalPart.IF; break; case "ELSEIF": part = ConditionalPart.ELSEIF; break; case "ELSE": part = ConditionalPart.ELSE; break; case "IFDEF": part = ConditionalPart.IFDEF; break; case "IFNDEF": part = ConditionalPart.IFNDEF; break; case "ENDIF": part = ConditionalPart.ENDIF; break; default: part = ConditionalPart.none; break; } return; }
private void ParseAssignment(StringSection symbolName, bool isLabel, StringSection expression, int iSourceLine) { // Call site SHOULD check for this condition and specify an Error. if (expression.Length == 0) { throw new SyntaxErrorException("Expected: expression.", iSourceLine); } AsmValue assignedValue; LiteralValue assignedLiteral; if (ExpressionEvaluator.TryParseLiteral(expression, out assignedLiteral)) { assignedValue = new AsmValue(assignedLiteral); } else { assignedValue = new AsmValue(expression.ToString()); } assembly.Directives.Add(new Assignment(NextInstructionIndex, iSourceLine, symbolName, isLabel, assignedValue)); }
public DefineDirective(int iInstruction, int iSourceLine, StringSection varName) : base(iInstruction, iSourceLine) { this.VarName = varName.ToString(); }
public ErrorDirective(int instructionIndex, int sourceLine, StringSection msg) : base(instructionIndex, sourceLine) { this.errorMessage = msg.ToString(); }
/// <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); }
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); }