Skip to content

Commit

Permalink
[PDP8] Refactor PDP-8 OperatorParser
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Sep 30, 2024
1 parent d01b0d6 commit 767eab7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 37 deletions.
21 changes: 20 additions & 1 deletion src/asm_pdp8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ struct Pdp8CommentParser final : CommentParser {
};
};

struct Pdp8OperatorParser final : OperatorParser {
const Operator *readOperator(
StrScanner &scan, ErrorAt &error, Operator::Type type) const override {
UNUSED(error);
if (type == Operator::INFIX && scan.expect('!')) {
static const Operator OP_LOGICAL_OR(15, Operator::LEFT, 2, bitwise_or);
return &OP_LOGICAL_OR;
}
return CStyleOperatorParser::singleton().readOperator(scan, error, type);
}
static Error bitwise_or(ValueStack &stack) {
const auto rhs = stack.pop();
const auto lhs = stack.pop();
stack.push(lhs | rhs);
return OK;
}
};

} // namespace

const ValueParser::Plugins &AsmPdp8::defaultPlugins() {
Expand All @@ -114,11 +132,12 @@ const ValueParser::Plugins &AsmPdp8::defaultPlugins() {
const SymbolParser &symbol() const override { return _symbol; }
const LetterParser &letter() const override { return _letter; }
const CommentParser &comment() const override { return _comment; }
const OperatorParser &operators() const override { return DecOperatorParser::singleton(); }
const OperatorParser &operators() const override { return _operators; }
const LocationParser &location() const override { return _location; }
const Pdp8SymbolParser _symbol{};
const Pdp8LetterParser _letter{};
const Pdp8CommentParser _comment{};
const Pdp8OperatorParser _operators{};
const SimpleLocationParser _location{PSTR_DOT_DOLLAR};
} PLUGINS{};
return PLUGINS;
Expand Down
31 changes: 0 additions & 31 deletions src/operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,37 +537,6 @@ const Operator *ZilogOperatorParser::readOperator(
return CStyleOperatorParser::singleton().readOperator(scan, error, type);
}

const Operator *DecOperatorParser::readOperator(
StrScanner &scan, ErrorAt &error, Operator::Type type) const {
UNUSED(error);
auto p = scan;
const Operator *opr = nullptr;
if (type == Operator::PREFIX) {
if (p.expect('~')) {
opr = &OP_BITWISE_NOT;
} else if (p.expect('-')) {
opr = &OP_UNARY_MINUS;
} else if (p.expect('+')) {
opr = &OP_UNARY_PLUS;
}
} else if (type == Operator::INFIX) {
if (p.expect('+')) {
opr = &OP_ADD;
} else if (p.expect('-')) {
opr = &OP_SUB;
} else if (p.expect('&')) {
opr = &OP_BITWISE_AND;
} else if (p.expect('^')) {
opr = &OP_BITWISE_XOR;
} else if (p.expect('!')) {
opr = &OP_BITWISE_OR;
}
}
if (opr)
scan = p;
return opr;
}

} // namespace libasm

// Local Variables:
Expand Down
5 changes: 0 additions & 5 deletions src/operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,6 @@ struct ZilogOperatorParser final : OperatorParser, Singleton<ZilogOperatorParser
StrScanner &scan, ErrorAt &error, Operator::Type type) const override;
};

struct DecOperatorParser final : OperatorParser, Singleton<DecOperatorParser> {
const Operator *readOperator(
StrScanner &scan, ErrorAt &error, Operator::Type type) const override;
};

} // namespace libasm

#endif
Expand Down

0 comments on commit 767eab7

Please sign in to comment.