diff --git a/src/asm_cdp1802.cpp b/src/asm_cdp1802.cpp index 5ed71d18..b36d94eb 100644 --- a/src/asm_cdp1802.cpp +++ b/src/asm_cdp1802.cpp @@ -99,7 +99,6 @@ const ValueParser::Plugins &AsmCdp1802::defaultPlugins() { const CommentParser &comment() const override { return RcaCommentParser::singleton(); } const SymbolParser &symbol() const override { return RcaSymbolParser::singleton(); } const LetterParser &letter() const override { return RcaLetterParser::singleton(); } - const LocationParser &location() const override { return StarLocationParser::singleton(); } const FunctionTable &function() const override { return RcaFunctionTable::singleton(); } } PLUGINS{}; return PLUGINS; diff --git a/src/asm_ins8060.cpp b/src/asm_ins8060.cpp index 816870b1..8eb52d3c 100644 --- a/src/asm_ins8060.cpp +++ b/src/asm_ins8060.cpp @@ -48,9 +48,6 @@ const ValueParser::Plugins &AsmIns8060::defaultPlugins() { static const struct final : ValueParser::Plugins { const NumberParser &number() const override { return Ins80xxNumberParser::singleton(); } const SymbolParser &symbol() const override { return Ins80xxSymbolParser::singleton(); } - const LocationParser &location() const override { - return DotDollarLocationParser::singleton(); - } const FunctionTable &function() const override { return Ins8060FunctionTable::singleton(); } } PLUGINS{}; return PLUGINS; diff --git a/src/asm_ins8070.cpp b/src/asm_ins8070.cpp index 4c7a83ec..a2a542f0 100644 --- a/src/asm_ins8070.cpp +++ b/src/asm_ins8070.cpp @@ -48,9 +48,6 @@ const ValueParser::Plugins &AsmIns8070::defaultPlugins() { static const struct final : ValueParser::Plugins { const NumberParser &number() const override { return Ins80xxNumberParser::singleton(); } const SymbolParser &symbol() const override { return Ins80xxSymbolParser::singleton(); } - const LocationParser &location() const override { - return DotDollarLocationParser::singleton(); - } const FunctionTable &function() const override { return Ins8070FunctionTable::singleton(); } } PLUGINS{}; return PLUGINS; diff --git a/src/asm_mc68000.cpp b/src/asm_mc68000.cpp index 61f371c5..165977a1 100644 --- a/src/asm_mc68000.cpp +++ b/src/asm_mc68000.cpp @@ -67,6 +67,9 @@ PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)}; struct Mc68000SymbolParser final : PrefixSymbolParser, Singleton { Mc68000SymbolParser() : PrefixSymbolParser(PSTR_DOT, PSTR_UNDER_DOT_DOLLAR) {} + bool locationSymbol(StrScanner &scan) const override { + return SymbolParser::locationSymbol(scan, '*'); + } bool instructionLetter(char c) const override { return PrefixSymbolParser::instructionLetter(c) || c == '.'; } diff --git a/src/asm_mn1610.cpp b/src/asm_mn1610.cpp index cf3f25f1..c6b44899 100644 --- a/src/asm_mn1610.cpp +++ b/src/asm_mn1610.cpp @@ -43,14 +43,20 @@ struct Mn1610NumberParser final : IbmNumberParser, Singleton Mn1610NumberParser() : IbmNumberParser('X', 0, 0, 0) {} }; +struct Mn1610SymbolParser final : SymbolParser, Singleton { + bool locationSymbol(StrScanner &scan) const override { + return SymbolParser::locationSymbol(scan, '*'); + } +}; + } // namespace const ValueParser::Plugins &AsmMn1610::defaultPlugins() { static const struct final : ValueParser::Plugins { const NumberParser &number() const override { return Mn1610NumberParser::singleton(); } + const SymbolParser &symbol() const override { return Mn1610SymbolParser::singleton(); } const CommentParser &comment() const override { return StarCommentParser::singleton(); } const LetterParser &letter() const override { return IbmLetterParser::singleton(); } - const LocationParser &location() const override { return StarLocationParser::singleton(); } } PLUGINS{}; return PLUGINS; } diff --git a/src/asm_mos6502.cpp b/src/asm_mos6502.cpp index 449604c3..579a3325 100644 --- a/src/asm_mos6502.cpp +++ b/src/asm_mos6502.cpp @@ -29,10 +29,9 @@ using namespace text::common; using namespace text::mos6502; namespace { - -// clang-format off constexpr char TEXT_aequal[] PROGMEM = "*="; +// clang-format off constexpr Pseudo PSEUDOS[] PROGMEM = { {TEXT_aequal, &Assembler::defineOrigin}, {TEXT_dALIGN, &Assembler::alignOrigin}, @@ -58,6 +57,9 @@ struct MostekCommentParser final : CommentParser, Singleton struct MostekSymbolParser final : SimpleSymbolParser, Singleton { MostekSymbolParser() : SimpleSymbolParser(PSTR_UNDER) {} + bool locationSymbol(StrScanner &scan) const override { + return SymbolParser::locationSymbol(scan, '*'); + } bool instructionLetter(char c) const override { return SymbolParser::instructionLetter(c) || c == '.' || c == '=' || c == '*' || c == ':'; } @@ -82,7 +84,6 @@ const ValueParser::Plugins &AsmMos6502::defaultPlugins() { const CommentParser &comment() const override { return MostekCommentParser::singleton(); } const SymbolParser &symbol() const override { return MostekSymbolParser::singleton(); } const LetterParser &letter() const override { return MostekLetterParser::singleton(); } - const LocationParser &location() const override { return StarLocationParser::singleton(); } } PLUGINS{}; return PLUGINS; } diff --git a/src/asm_ns32000.cpp b/src/asm_ns32000.cpp index d4c1b9e0..f73123de 100644 --- a/src/asm_ns32000.cpp +++ b/src/asm_ns32000.cpp @@ -70,6 +70,9 @@ struct Ns32000NumberParser final : NationalNumberParser, Singleton { Ns32000SymbolParser() : SimpleSymbolParser(PSTR_UNDER_DOT) {} + bool locationSymbol(StrScanner &scan) const override { + return SymbolParser::locationSymbol(scan, '.') || SymbolParser::locationSymbol(scan, '*'); + } bool instructionLetter(char c) const override { return SimpleSymbolParser::instructionLetter(c) || c == '.'; } @@ -96,9 +99,6 @@ const ValueParser::Plugins &AsmNs32000::defaultPlugins() { const CommentParser &comment() const override { return SharpCommentParser::singleton(); } const SymbolParser &symbol() const override { return Ns32000SymbolParser::singleton(); } const LetterParser &letter() const override { return Ns32000LetterParser::singleton(); } - const LocationParser &location() const override { - return DotStarLocationParser::singleton(); - } } PLUGINS{}; return PLUGINS; } @@ -244,7 +244,7 @@ Error AsmNs32000::parseBaseOperand(StrScanner &scan, Operand &op) const { } auto l = p; - if (parser().locationSymbol(l)) { + if (Ns32000SymbolParser::singleton().locationSymbol(l)) { op.val = parseInteger(p, op); if (op.hasError()) return op.getError(); diff --git a/src/asm_pdp8.cpp b/src/asm_pdp8.cpp index 0b48d77e..480d754b 100644 --- a/src/asm_pdp8.cpp +++ b/src/asm_pdp8.cpp @@ -46,6 +46,9 @@ constexpr Pseudo PSEUDOS[] PROGMEM = { PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)}; struct Pdp8SymbolParser final : SymbolParser, Singleton { + bool locationSymbol(StrScanner &scan) const override { + return SymbolParser::locationSymbol(scan, '.') || SymbolParser::locationSymbol(scan, '$'); + } bool labelDelimitor(StrScanner &scan) const override { return scan.expect(','); } bool instructionLetter(char c) const override { return SymbolParser::symbolLetter(c) || c == '*' || c == '='; @@ -106,9 +109,6 @@ const ValueParser::Plugins &AsmPdp8::defaultPlugins() { const LetterParser &letter() const override { return Pdp8LetterParser::singleton(); } const CommentParser &comment() const override { return Pdp8CommentParser::singleton(); } const OperatorParser &operators() const override { return Pdp8OperatorParser::singleton(); } - const LocationParser &location() const override { - return DotDollarLocationParser::singleton(); - } } PLUGINS{}; return PLUGINS; } diff --git a/src/asm_scn2650.cpp b/src/asm_scn2650.cpp index 488bafd5..604d6909 100644 --- a/src/asm_scn2650.cpp +++ b/src/asm_scn2650.cpp @@ -50,8 +50,8 @@ struct SigneticsNumberParser final : IbmNumberParser, Singleton { - bool letterPrefix(StrScanner &scan) const override { return scan.iexpect('A') != 0; } - bool stringPrefix(StrScanner &scan) const override { return scan.iexpect('A') != 0; } + bool letterPrefix(StrScanner &scan) const override { return scan.iexpect('A'); } + bool stringPrefix(StrScanner &scan) const override { return scan.iexpect('A'); } }; } // namespace diff --git a/src/location_parser.h b/src/location_parser.h deleted file mode 100644 index e9a8f4ad..00000000 --- a/src/location_parser.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright 2024 Tadashi G. Takaoka - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __LIBASM_LOCATION_PARSER_H__ -#define __LIBASM_LOCATION_PARSER_H__ - -#include -#include -#include "config_host.h" -#include "str_scanner.h" -#include "text_common.h" - -namespace libasm { - -/** - * Location counter parser. - */ -struct LocationParser { - virtual bool locationSymbol(StrScanner &scan) const = 0; -}; - -struct SimpleLocationParser : LocationParser { - SimpleLocationParser(const /*PROGMEM*/ char *letters) : _letters_P(letters) {} - bool locationSymbol(StrScanner &scan) const override { - auto p = scan; - const auto c = *p++; - if (c && strchr_P(_letters_P, c) && !isalnum(*p)) { - scan = p; - return true; - } - return false; - } - -private: - const /*PROGMEM*/ char *_letters_P; -}; - -struct DollarLocationParser final : SimpleLocationParser, Singleton { - DollarLocationParser() : SimpleLocationParser(text::common::PSTR_DOLLAR) {} -}; - -struct DotStarLocationParser final : SimpleLocationParser, Singleton { - DotStarLocationParser() : SimpleLocationParser(text::common::PSTR_DOT_STAR) {} -}; - -struct DotDollarLocationParser final : SimpleLocationParser, Singleton { - DotDollarLocationParser() : SimpleLocationParser(text::common::PSTR_DOT_DOLLAR) {} -}; - -struct StarLocationParser final : SimpleLocationParser, Singleton { - StarLocationParser() : SimpleLocationParser(text::common::PSTR_STAR) {} -}; - -} // namespace libasm - -#endif - -// Local Variables: -// mode: c++ -// c-basic-offset: 4 -// tab-width: 4 -// End: -// vim: set ft=cpp et ts=4 sw=4: diff --git a/src/parsers.h b/src/parsers.h index 7e1ad0f3..06956f36 100644 --- a/src/parsers.h +++ b/src/parsers.h @@ -19,7 +19,6 @@ #include "comment_parser.h" #include "letter_parser.h" -#include "location_parser.h" #include "number_parser.h" #include "symbol_parser.h" diff --git a/src/symbol_parser.h b/src/symbol_parser.h index 03418b2a..33f5efcf 100644 --- a/src/symbol_parser.h +++ b/src/symbol_parser.h @@ -32,6 +32,9 @@ struct SymbolParser { return isalpha(c) || (!headOfSymbol && isdigit(c)) || c == '_'; } + /** Default location symbol is '$' */ + virtual bool locationSymbol(StrScanner &scan) const { return locationSymbol(scan, '$'); } + /** Label may have delimitor letter */ virtual bool labelDelimitor(StrScanner &scan) const { return scan.expect(':'); } @@ -43,6 +46,16 @@ struct SymbolParser { /** Function name is same as symbol */ virtual bool functionNameLetter(char c) const { return symbolLetter(c); } + +protected: + bool locationSymbol(StrScanner &scan, char location) const { + auto p = scan; + if (p.expect(location) && !symbolLetter(*p)) { + scan = p; + return true; + } + return false; + } }; /** @@ -86,10 +99,16 @@ struct UnderQuestionSymbolParser final : PrefixSymbolParser, Singleton { Mc68xxSymbolParser() : PrefixSymbolParser(text::common::PSTR_DOT, text::common::PSTR_UNDER_DOT_DOLLAR) {} + bool locationSymbol(StrScanner &scan) const override { + return SymbolParser::locationSymbol(scan, '*'); + } }; struct Ins80xxSymbolParser final : PrefixSymbolParser, Singleton { Ins80xxSymbolParser() : PrefixSymbolParser(text::common::PSTR_DOLLAR) {} + bool locationSymbol(StrScanner &scan) const override { + return SymbolParser::locationSymbol(scan, '.') || SymbolParser::locationSymbol(scan, '$'); + } bool instructionLetter(char c) const override { return PrefixSymbolParser::instructionLetter(c) || c == '=' || c == '.'; } diff --git a/src/value_parser.cpp b/src/value_parser.cpp index 47b93993..355b9ca1 100644 --- a/src/value_parser.cpp +++ b/src/value_parser.cpp @@ -14,12 +14,12 @@ * limitations under the License. */ +#include "value_parser.h" #include #include #include "config_base.h" #include "stack.h" #include "text_common.h" -#include "value_parser.h" namespace libasm { @@ -41,10 +41,6 @@ const LetterParser &ValueParser::Plugins::letter() const { return SINGLETON; } -const LocationParser &ValueParser::Plugins::location() const { - return DollarLocationParser::singleton(); -} - const OperatorParser &ValueParser::Plugins::operators() const { return CStyleOperatorParser::singleton(); } @@ -80,18 +76,19 @@ const ValueParser::Plugins &ValueParser::Plugins::motorola() { const NumberParser &ValueParser::MotorolaPlugins::number() const { return MotorolaNumberParser::singleton(); } + const CommentParser &ValueParser::MotorolaPlugins::comment() const { return StarCommentParser::singleton(); } + const SymbolParser &ValueParser::MotorolaPlugins::symbol() const { return Mc68xxSymbolParser::singleton(); } + const LetterParser &ValueParser::MotorolaPlugins::letter() const { return MotorolaLetterParser::singleton(); } -const LocationParser &ValueParser::MotorolaPlugins::location() const { - return StarLocationParser::singleton(); -} + const OperatorParser &ValueParser::MotorolaPlugins::operators() const { return Mc68xxOperatorParser::singleton(); } @@ -116,6 +113,13 @@ struct FairchildNumberParser final : NumberParser, Singleton { + bool locationSymbol(StrScanner &scan) const override { + return SymbolParser::locationSymbol(scan, '*') || SymbolParser::locationSymbol(scan, '$') || + SymbolParser::locationSymbol(scan, '.'); + } +}; + struct FairchildLetterParser final : LetterParser, Singleton { /** Fairchild style letter is [cC]'[:print:]', #[:print:], '[:print:]'? */ Error parseLetter(StrScanner &scan, char &letter) const override { @@ -152,10 +156,6 @@ struct FairchildLetterParser final : LetterParser, Singleton { - FairchildLocationParser() : SimpleLocationParser(text::common::PSTR_DOT_STAR_DOLLAR) {} -}; - } // namespace fairchild const ValueParser::Plugins &ValueParser::Plugins::fairchild() { @@ -163,13 +163,13 @@ const ValueParser::Plugins &ValueParser::Plugins::fairchild() { const NumberParser &number() const override { return fairchild::FairchildNumberParser::singleton(); } + const SymbolParser &symbol() const override { + return fairchild::FairchildSymbolParser::singleton(); + } const CommentParser &comment() const override { return StarCommentParser::singleton(); } const LetterParser &letter() const override { return fairchild::FairchildLetterParser::singleton(); } - const LocationParser &location() const override { - return fairchild::FairchildLocationParser::singleton(); - } } PLUGINS{}; return PLUGINS; } @@ -457,7 +457,7 @@ Error ValueParser::parseConstant(StrScanner &scan, Value &val, ParserContext &co return err; } - if (_location.locationSymbol(p)) { + if (_symbol.locationSymbol(p)) { val.setUnsigned(context.currentLocation); scan = p; return OK; diff --git a/src/value_parser.h b/src/value_parser.h index ec3a0e33..0a56b198 100644 --- a/src/value_parser.h +++ b/src/value_parser.h @@ -41,7 +41,6 @@ struct ValueParser { virtual const CommentParser &comment() const; virtual const SymbolParser &symbol() const; virtual const LetterParser &letter() const; - virtual const LocationParser &location() const; virtual const OperatorParser &operators() const; virtual const FunctionTable &function() const; @@ -59,7 +58,6 @@ struct ValueParser { const CommentParser &comment() const override; const SymbolParser &symbol() const override; const LetterParser &letter() const override; - const LocationParser &location() const override; const OperatorParser &operators() const override; }; @@ -68,7 +66,6 @@ struct ValueParser { _comment(plugins.comment()), _symbol(plugins.symbol()), _letter(plugins.letter()), - _location(plugins.location()), _operators(plugins.operators()), _function(plugins.function()) {} @@ -92,7 +89,6 @@ struct ValueParser { Error readSymbol(StrScanner &scan, StrScanner &symbol) const; Error readLabel(StrScanner &scan, StrScanner &label) const; Error readInstruction(StrScanner &scan, StrScanner &inst) const; - bool locationSymbol(StrScanner &scan) const { return _location.locationSymbol(scan); } bool commentLine(StrScanner &scan) const { return _comment.commentLine(scan); } bool endOfLine(StrScanner &scan) const { return _comment.endOfLine(scan); } bool stringPrefix(StrScanner &scan) const { return _letter.stringPrefix(scan); } @@ -103,7 +99,6 @@ struct ValueParser { const CommentParser &_comment; const SymbolParser &_symbol; const LetterParser &_letter; - const LocationParser &_location; const OperatorParser &_operators; const FunctionTable &_function; diff --git a/test/test_expr_national.cpp b/test/test_expr_national.cpp index 057472d6..716de958 100644 --- a/test/test_expr_national.cpp +++ b/test/test_expr_national.cpp @@ -21,20 +21,8 @@ using namespace libasm::test; const struct final : ValueParser::Plugins { const NumberParser &number() const override { return _number; } - const SymbolParser &symbol() const override { return _symbol; } - const LetterParser &letter() const override { return _letter; } + const SymbolParser &symbol() const override { return Ins80xxSymbolParser::singleton(); } const NationalNumberParser _number{/* 'X' or 'H' */ 0, 'B', /* 'O' or */ 'Q', 'D'}; - const SimpleSymbolParser _symbol{PSTR("$")}; - const struct : LetterParser { - bool letterPrefix(StrScanner &scan) const override { - scan.iexpect('A'); // optional - return true; - } - bool stringPrefix(StrScanner &scan) const override { - scan.iexpect('A'); // optional - return true; - } - } _letter{}; } plugins{}; const ValueParser parser{plugins}; @@ -58,16 +46,7 @@ static void test_char_constant() { X8("''", MISSING_CLOSING_QUOTE, "''", "''"); E8("''''", 0x27); - E8("A'a'", 0x61); - E8("a'a'+5", 0x66); - E8("5+A'a'", 0x66); - X8("A'a", MISSING_CLOSING_QUOTE, "A'a", "A'a"); - X8("A'a+5", MISSING_CLOSING_QUOTE, "A'a+5", "A'a+5"); - X8("5+A'a", MISSING_CLOSING_QUOTE, "A'a", "A'a"); - X8("a' ", MISSING_CLOSING_QUOTE, "a' ", "a' "); - X8("a''", MISSING_CLOSING_QUOTE, "a''", "a''"); - - E8("''''", 0x27); + E8("''''", 0x27); E16("'a'", 0x61); E32("'a'", 0x61); }