Skip to content

Commit

Permalink
[Asm] Add default input RADIX
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Sep 30, 2024
1 parent 2163b03 commit adea12b
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 72 deletions.
3 changes: 2 additions & 1 deletion src/asm_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Assembler::Assembler(
}

void Assembler::reset() {
setInputRadix(RADIX_10);
setListRadix(RADIX_16);
setSmartBranch(false);
}
Expand Down Expand Up @@ -121,7 +122,7 @@ Value Assembler::parseInteger(StrScanner &expr, ErrorAt &error, char delim) cons
}

Value Assembler::parseExpr(StrScanner &expr, ErrorAt &error, char delim) const {
ParserContext context{_currentLocation, _symtab, delim};
ParserContext context{_currentLocation, _symtab, delim, _inputRadix};
return _parser.eval(expr, error, context);
}

Expand Down
6 changes: 6 additions & 0 deletions src/asm_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ struct Assembler {
const BoolOption<Assembler> _opt_smartBranch;
const TextOption<Assembler> _opt_fpu;

Radix _inputRadix;
Radix _listRadix;
bool _smartBranch;
const SymbolTable *_symtab;
Expand All @@ -103,6 +104,11 @@ struct Assembler {
Assembler(const ValueParser::Plugins &plugins, const pseudo::Pseudos &pseudos,
const OptionBase *option = nullptr);

Error setInputRadix(Radix radix) {
_inputRadix = radix;
return OK;
}

int32_t branchDelta(uint32_t base, uint32_t target, ErrorAt &error, const ErrorAt &at) const;

void generateString(StrScanner &scan, const StrScanner &end, Insn &insn, DataType type,
Expand Down
4 changes: 2 additions & 2 deletions src/asm_cdp1802.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)};
* RCA style numbers are the same as IBM plus '#hh' for hexadecimal.
*/
struct RcaNumberParser final : NumberParser {
Error parseNumber(StrScanner &scan, Value &val) const override {
Error parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const override {
auto p = scan;
if (*p == '#' && isxdigit(p[1])) {
const auto error = val.read(++p, RADIX_16);
if (error == OK)
scan = p;
return error;
}
return _ibm.parseNumber(scan, val);
return _ibm.parseNumber(scan, val, defaultRadix);
}

private:
Expand Down
36 changes: 7 additions & 29 deletions src/asm_pdp8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,6 @@ constexpr Pseudo PSEUDOS[] PROGMEM = {
// clang-format on
PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)};

struct RadixNumberParser final : NumberParser {
RadixNumberParser() : _radix(RADIX_8) {}
Radix radix() const { return _radix; }
void setRadix(Radix radix) { _radix = radix; }
Error parseNumber(StrScanner &scan, Value &val) const {
auto p = scan;
const auto radix = IntelNumberParser::hasSuffix(p);
if (radix != RADIX_NONE)
return IntelNumberParser::singleton().parseNumber(scan, val, radix, p);
return val.read(scan, _radix);
}

private:
Radix _radix;
} NUMBER_PARSER;

struct Pdp8SymbolParser final : SymbolParser {
bool labelDelimitor(StrScanner &scan) const override { return scan.expect(','); }
bool instructionLetter(char c) const override {
Expand Down Expand Up @@ -128,7 +112,7 @@ struct Pdp8OperatorParser final : OperatorParser {

const ValueParser::Plugins &AsmPdp8::defaultPlugins() {
static const struct final : ValueParser::Plugins {
const NumberParser &number() const override { return NUMBER_PARSER; }
const NumberParser &number() const override { return IntelNumberParser::singleton(); }
const SymbolParser &symbol() const override { return _symbol; }
const LetterParser &letter() const override { return _letter; }
const CommentParser &comment() const override { return _comment; }
Expand All @@ -153,26 +137,20 @@ AsmPdp8::AsmPdp8(const ValueParser::Plugins &plugins)

void AsmPdp8::reset() {
Assembler::reset();
setInputRadix(RADIX_8);
setListRadix(RADIX_8);
setImplicitWord(false);
}

Error AsmPdp8::setInputRadix(StrScanner &scan, Insn &insn, uint8_t extra) {
UNUSED(scan);
UNUSED(insn);
NUMBER_PARSER.setRadix(static_cast<Radix>(extra));
return OK;
}

Error AsmPdp8::setImplicitWord(bool enable) {
_implicitWord = enable;
return OK;
}

Error AsmPdp8::defineDoubleDecimal(StrScanner &scan, Insn &insn, uint8_t extra) {
UNUSED(extra);
const auto radix = NUMBER_PARSER.radix();
NUMBER_PARSER.setRadix(RADIX_10);
const auto radix = _inputRadix;
setInputRadix(RADIX_10);
const auto save = scan;
const auto val = parseInteger(scan, insn);
constexpr auto UINT24_MAX = INT32_C(0x00FF'FFFF);
Expand All @@ -184,7 +162,7 @@ Error AsmPdp8::defineDoubleDecimal(StrScanner &scan, Insn &insn, uint8_t extra)
insn.setErrorIf(insn.emitUint16Be((val24 >> 12) & 07777));
insn.setErrorIf(insn.emitUint16Be(val24 & 07777));
}
NUMBER_PARSER.setRadix(radix);
setInputRadix(radix);
return insn.getError();
}

Expand Down Expand Up @@ -361,9 +339,9 @@ Error AsmPdp8::parseOperateOperand(StrScanner &scan, AsmInsn &insn) const {

Error AsmPdp8::processPseudo(StrScanner &scan, Insn &insn) {
if (strcasecmp_P(insn.name(), TEXT_OCTAL) == 0)
return setInputRadix(scan, insn, RADIX_8);
return setInputRadix(RADIX_8);
if (strcasecmp_P(insn.name(), TEXT_DECIMAL) == 0)
return setInputRadix(scan, insn, RADIX_10);
return setInputRadix(RADIX_10);
if (strcasecmp_P(insn.name(), TEXT_DUBL) == 0)
return defineDoubleDecimal(scan, insn);
if (strcasecmp_P(insn.name(), TEXT_PAGE) == 0)
Expand Down
1 change: 0 additions & 1 deletion src/asm_pdp8.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ struct AsmPdp8 final : Assembler, Config {

bool _implicitWord;

Error setInputRadix(StrScanner &scan, Insn &insn, uint8_t extra);
Error defineDoubleDecimal(StrScanner &scan, Insn &insn, uint8_t extra = 0);
Error alignOnPage(StrScanner &scan, Insn &insn, uint8_t extra = 0);
Error defineField(StrScanner &scan, Insn &insn, uint8_t extra = 0);
Expand Down
47 changes: 24 additions & 23 deletions src/parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ Radix PrefixNumberParser::hasPrefix(StrScanner &scan) const {
return RADIX_NONE;
}

Error PrefixNumberParser::parseNumber(StrScanner &scan, Value &val) const {
Error PrefixNumberParser::parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const {
auto p = scan;
const auto radix = hasPrefix(p);
if (radix == RADIX_NONE)
return IntelNumberParser::singleton().parseNumber(scan, val);
return IntelNumberParser::singleton().parseNumber(scan, val, defaultRadix);
const auto error = val.read(p, radix);
if (error != NOT_AN_EXPECTED)
scan = p;
return error;
}

Error IbmNumberParser::parseNumber(StrScanner &scan, Value &val) const {
Error IbmNumberParser::parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const {
auto p = scan;
const auto radix = hasPrefix(p);
if (radix == RADIX_NONE)
return IntelNumberParser::singleton().parseNumber(scan, val);
return IntelNumberParser::singleton().parseNumber(scan, val, defaultRadix);

if (!p.expect('\'') || !isValidDigit(*p, radix))
return NOT_AN_EXPECTED;
Expand All @@ -94,11 +94,11 @@ Radix NationalNumberParser::hasPrefix(StrScanner &scan) const {
return PrefixNumberParser::hasPrefix(scan);
}

Error NationalNumberParser::parseNumber(StrScanner &scan, Value &val) const {
Error NationalNumberParser::parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const {
auto p = scan;
const auto radix = hasPrefix(p);
if (radix == RADIX_NONE)
return IntelNumberParser::singleton().parseNumber(scan, val);
return IntelNumberParser::singleton().parseNumber(scan, val, defaultRadix);

if (!p.expect('\'') || !isValidDigit(*p, radix))
return NOT_AN_EXPECTED;
Expand All @@ -110,21 +110,21 @@ Error NationalNumberParser::parseNumber(StrScanner &scan, Value &val) const {
return error;
}

Radix CStyleNumberParser::hasPrefix(StrScanner &scan) const {
Radix CStyleNumberParser::hasPrefix(StrScanner &scan, Radix defaultRadix) const {
if (scan.iexpectText_P(text::common::PSTR_ZERO_X))
return RADIX_16;
if (scan.iexpectText_P(text::common::PSTR_ZERO_B))
return RADIX_2;
if (*scan == '0' && isoctal(scan[1]))
return RADIX_8;
if (isdigit(*scan))
return RADIX_10;
if (isValidDigit(*scan, defaultRadix))
return defaultRadix;
return RADIX_NONE;
}

Error CStyleNumberParser::parseNumber(StrScanner &scan, Value &val) const {
Error CStyleNumberParser::parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const {
auto p = scan;
const auto radix = hasPrefix(p);
const auto radix = hasPrefix(p, defaultRadix);
if (radix == RADIX_NONE)
return NOT_AN_EXPECTED;

Expand All @@ -134,7 +134,7 @@ Error CStyleNumberParser::parseNumber(StrScanner &scan, Value &val) const {
return error;
}

Radix IntelNumberParser::hasSuffix(StrScanner &scan) {
Radix IntelNumberParser::hasSuffix(StrScanner &scan, Radix defaultRadix) {
if (scanNumberEnd(scan, RADIX_16, 'H') == OK)
return RADIX_16;
if (scanNumberEnd(scan, RADIX_8, 'O') == OK || scanNumberEnd(scan, RADIX_8, 'Q') == OK)
Expand All @@ -146,18 +146,19 @@ Radix IntelNumberParser::hasSuffix(StrScanner &scan) {
return RADIX_NONE;
}

Error IntelNumberParser::parseNumber(StrScanner &scan, Value &val) const {
auto p = scan;
return parseNumber(scan, val, hasSuffix(p), p);
Error IntelNumberParser::parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const {
auto end = scan;
const auto radix = hasSuffix(end, defaultRadix);
if (radix == RADIX_NONE)
return CStyleNumberParser::singleton().parseNumber(scan, val, defaultRadix);
return parseNumber(scan, val, radix, end);
}

Error IntelNumberParser::parseNumber(
StrScanner &scan, Value &val, Radix radix, StrScanner &next) const {
if (radix == RADIX_NONE)
return CStyleNumberParser::singleton().parseNumber(scan, val);
StrScanner &scan, Value &val, Radix radix, StrScanner &end) const {
const auto error = val.read(scan, radix);
if (error != NOT_AN_EXPECTED)
scan = next;
scan = end;
return error;
}

Expand Down Expand Up @@ -196,27 +197,27 @@ Radix ZilogNumberParser::hasPrefix(StrScanner &scan) const {
return RADIX_NONE;
}

Error ZilogNumberParser::parseNumber(StrScanner &scan, Value &val) const {
Error ZilogNumberParser::parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const {
auto p = scan;
const auto radix = hasPrefix(p);
if (radix == RADIX_NONE)
return IntelNumberParser::singleton().parseNumber(scan, val);
return IntelNumberParser::singleton().parseNumber(scan, val, defaultRadix);

const auto error = val.read(p, radix);
if (error != NOT_AN_EXPECTED)
scan = p;
return error;
}

Error FairchildNumberParser::parseNumber(StrScanner &scan, Value &val) const {
Error FairchildNumberParser::parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const {
auto p = scan;
if (*p == '$' && isxdigit(p[1])) {
const auto error = val.read(++p, RADIX_16);
if (error != NOT_AN_EXPECTED)
scan = p;
return error;
}
return _ibm.parseNumber(scan, val);
return _ibm.parseNumber(scan, val, defaultRadix);
}

bool SymbolParser::symbolLetter(char c, bool headOfSymbol) const {
Expand Down
22 changes: 11 additions & 11 deletions src/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ struct LetterParser {
* - Returns NOT_AN_EXPECTED when |scan| doesn't look like a number. |scan| is unchanged.
*/
struct NumberParser {
virtual Error parseNumber(StrScanner &scan, Value &val) const = 0;
virtual Error parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const = 0;
};

/**
Expand All @@ -137,10 +137,10 @@ struct NumberParser {
* - Binary: "[01]+[bB]"
*/
struct IntelNumberParser final : NumberParser, Singleton<IntelNumberParser> {
Error parseNumber(StrScanner &scan, Value &val) const override;
Error parseNumber(StrScanner &scan, Value &val, Radix radix, StrScanner &next) const;
Error parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const override;
Error parseNumber(StrScanner &scan, Value &val, Radix radix, StrScanner &end) const;

static Radix hasSuffix(StrScanner &scan);
static Radix hasSuffix(StrScanner &scan, Radix defaultRadix);

private:
/**
Expand All @@ -161,10 +161,10 @@ struct IntelNumberParser final : NumberParser, Singleton<IntelNumberParser> {
* - Binary: "0[bB][01]+"
*/
struct CStyleNumberParser final : NumberParser, Singleton<CStyleNumberParser> {
Error parseNumber(StrScanner &scan, Value &val) const override;
Error parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const override;

private:
Radix hasPrefix(StrScanner &scan) const;
Radix hasPrefix(StrScanner &scan, Radix defaultRadix) const;
};

/**
Expand All @@ -175,7 +175,7 @@ struct CStyleNumberParser final : NumberParser, Singleton<CStyleNumberParser> {
* - Binary: "%(2)[01]+"
*/
struct ZilogNumberParser final : NumberParser, Singleton<ZilogNumberParser> {
Error parseNumber(StrScanner &scan, Value &val) const override;
Error parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const override;

private:
Radix hasPrefix(StrScanner &scan) const;
Expand All @@ -187,7 +187,7 @@ struct ZilogNumberParser final : NumberParser, Singleton<ZilogNumberParser> {
struct PrefixNumberParser : NumberParser {
PrefixNumberParser(char hex, char bin, char oct, char dec);

Error parseNumber(StrScanner &scan, Value &val) const override;
Error parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const override;

protected:
const char _hex;
Expand Down Expand Up @@ -219,7 +219,7 @@ struct MotorolaNumberParser final : PrefixNumberParser, Singleton<MotorolaNumber
struct IbmNumberParser : PrefixNumberParser {
IbmNumberParser(char hex, char bin, char oct, char dec)
: PrefixNumberParser(hex, bin, oct, dec) {}
Error parseNumber(StrScanner &scan, Value &val) const override;
Error parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const override;
};

/**
Expand All @@ -232,7 +232,7 @@ struct IbmNumberParser : PrefixNumberParser {
struct NationalNumberParser final : IbmNumberParser {
NationalNumberParser(char hex, char bin, char oct, char dec)
: IbmNumberParser(hex, bin, oct, dec) {}
Error parseNumber(StrScanner &scan, Value &val) const override;
Error parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const override;

private:
Radix hasPrefix(StrScanner &scan) const override;
Expand All @@ -242,7 +242,7 @@ struct NationalNumberParser final : IbmNumberParser {
* Fairchild style numbers are the same as IBM plus '$hh' for hexadecimal.
*/
struct FairchildNumberParser final : NumberParser, Singleton<FairchildNumberParser> {
Error parseNumber(StrScanner &scan, Value &val) const override;
Error parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const override;

private:
IbmNumberParser _ibm{'H', 'B', 'O', 'D'};
Expand Down
2 changes: 1 addition & 1 deletion src/value_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Error ValueParser::parseConstant(StrScanner &scan, Value &val, ParserContext &co
if (err != NOT_AN_EXPECTED)
return err;

err = _number.parseNumber(p, val);
err = _number.parseNumber(p, val, context.defaultRadix);
#ifdef LIBASM_ASM_NOFLOAT
UNUSED(delim);
#else
Expand Down
6 changes: 4 additions & 2 deletions src/value_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
namespace libasm {

struct ParserContext final {
ParserContext(uint32_t loc = 0, const SymbolTable *symtab = nullptr, char delim = 0)
: currentLocation(loc), symbolTable(symtab), delimitor(delim) {}
ParserContext(uint32_t loc = 0, const SymbolTable *symtab = nullptr, char delim = 0,
Radix radix = RADIX_10)
: currentLocation(loc), symbolTable(symtab), delimitor(delim), defaultRadix(radix) {}
ParserContext(const ParserContext &) = default;

uint32_t currentLocation;
const SymbolTable *symbolTable;
char delimitor;
Radix defaultRadix;
};

struct ValueParser {
Expand Down
Loading

0 comments on commit adea12b

Please sign in to comment.