Skip to content

Commit

Permalink
Add ValueParser::Context
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Sep 30, 2024
1 parent 6db6cd2 commit 2163b03
Show file tree
Hide file tree
Showing 22 changed files with 120 additions and 143 deletions.
3 changes: 2 additions & 1 deletion driver/asm_directive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ Error AsmDirective::defineSymbol(

auto &parser = _assembler.parser();
ErrorAt error;
context.value = parser.eval(scan, error, &context.symbols);
ParserContext c{_assembler.currentLocation(), &context.symbols};
context.value = parser.eval(scan, error, c);
if (error.hasError()) {
context.value.clear();
return setError(scan, error);
Expand Down
9 changes: 6 additions & 3 deletions driver/function_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ const Functor *FunctionStore::Binding::lookupFunction(const StrScanner &symbol)
return parent->lookupFunction(symbol);
}

Error FunctionStore::Function::eval(ValueStack &stack, uint8_t argc) const {
Binding binding{paramsAt, stack, symtab};
Error FunctionStore::Function::eval(
ValueStack &stack, ParserContext &parent, uint_fast8_t argc) const {
StrScanner body_scan(body.c_str());
ErrorAt error;
const auto val = parser.eval(body_scan, error, &binding);
Binding binding{paramsAt, stack, parent.symbolTable};
ParserContext context{parent};
context.symbolTable = &binding;
const auto val = parser.eval(body_scan, error, context);
while (argc) {
stack.pop();
argc--;
Expand Down
4 changes: 2 additions & 2 deletions driver/function_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ struct FunctionStore final {

bool operator==(const Function &other) const;
bool operator!=(const Function &other) const { return !(*this == other); }
int8_t nargs() const override { return paramsAt.size(); }
Error eval(ValueStack &stack, uint8_t argc) const override;
int_fast8_t nargs() const override { return paramsAt.size(); }
Error eval(ValueStack &stack, ParserContext &context, uint_fast8_t argc) const override;

private:
const std::string body;
Expand Down
9 changes: 5 additions & 4 deletions src/asm_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)};

Assembler::Assembler(
const ValueParser::Plugins &plugins, const Pseudos &pseudos, const OptionBase *option)
: _parser(plugins, *this),
: _parser(plugins),
_pseudos(pseudos),
_commonOptions(&_opt_listRadix),
_options(option),
Expand Down Expand Up @@ -77,7 +77,7 @@ Error Assembler::setCurrentLocation(uint32_t location, bool align) {
return config().checkAddr(_currentLocation = location, align);
}

Error Assembler::encode(const char *line, Insn &insn, SymbolTable *symtab) {
Error Assembler::encode(const char *line, Insn &insn, const SymbolTable *symtab) {
_symtab = symtab;
StrScanner scan{line};
insn.setError(scan.skipSpaces(), OK);
Expand Down Expand Up @@ -113,15 +113,16 @@ Error Assembler::processPseudo(StrScanner &scan, Insn &insn) {

Value Assembler::parseInteger(StrScanner &expr, ErrorAt &error, char delim) const {
auto p = expr;
const auto value = _parser.eval(p, error, _symtab, delim);
const auto value = parseExpr(p, error, delim);
if (value.isFloat())
error.setErrorIf(expr, INTEGER_REQUIRED);
expr = p;
return value;
}

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

int32_t Assembler::branchDelta(
Expand Down
6 changes: 3 additions & 3 deletions src/asm_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

namespace libasm {

struct Assembler : private ValueParser::Locator {
Error encode(const char *line, Insn &insn, SymbolTable *symtab = nullptr);
struct Assembler {
Error encode(const char *line, Insn &insn, const SymbolTable *symtab = nullptr);
virtual const ConfigBase &config() const = 0;
virtual void reset();

Expand Down Expand Up @@ -97,7 +97,7 @@ struct Assembler : private ValueParser::Locator {

Radix _listRadix;
bool _smartBranch;
SymbolTable *_symtab;
const SymbolTable *_symtab;
uint32_t _currentLocation;

Assembler(const ValueParser::Plugins &plugins, const pseudo::Pseudos &pseudos,
Expand Down
12 changes: 6 additions & 6 deletions src/asm_cdp1802.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,25 @@ Error AsmCdp1802::setUseReg(bool enable) {
namespace {

const struct : Functor {
int8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, uint8_t) const override {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
// Mark that this is 2 bytes value.
stack.pushUnsigned((stack.pop().getUnsigned() & UINT16_MAX) | UINT32_C(0x1000'0000));
return OK;
}
} FN_A;

const struct : Functor {
int8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, uint8_t) const override {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
stack.pushUnsigned(stack.pop().getUnsigned() & UINT8_MAX);
return OK;
}
} FN_A0;

const struct : Functor {
int8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, uint8_t) const override {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
stack.pushUnsigned((stack.pop().getUnsigned() >> 8) & UINT8_MAX);
return OK;
}
Expand Down
12 changes: 6 additions & 6 deletions src/asm_ins8060.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,24 @@ AsmIns8060::AsmIns8060(const ValueParser::Plugins &plugins)
namespace {

const struct : Functor {
int8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, uint8_t) const override {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
stack.pushUnsigned((stack.pop().getUnsigned() >> 8) & 0xFF);
return OK;
}
} FN_HIGH;

const struct : Functor {
int8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, uint8_t) const override {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
stack.pushUnsigned(stack.pop().getUnsigned() & 0xFF);
return OK;
}
} FN_LOW;

const struct : Functor {
int8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, uint8_t) const override {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
const auto v = stack.pop().getUnsigned();
stack.pushUnsigned(page(v) | offset(v - 1));
return OK;
Expand Down
14 changes: 7 additions & 7 deletions src/asm_ins8070.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,24 @@ AsmIns8070::AsmIns8070(const ValueParser::Plugins &plugins)
namespace {

const struct : Functor {
int8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, uint8_t) const override {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
stack.pushUnsigned((stack.pop().getUnsigned() >> 8) & 0xFF);
return OK;
}
} FN_HIGH;

const struct : Functor {
int8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, uint8_t) const override {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
stack.pushUnsigned(stack.pop().getUnsigned() & 0xFF);
return OK;
}
} FN_LOW;

const struct : Functor {
int8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, uint8_t) const override {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
stack.pushUnsigned((stack.pop().getUnsigned() - 1) & 0xFFFF);
return OK;
}
Expand All @@ -111,7 +111,7 @@ const Functor *Ins8070FunctionTable::lookupFunction(const StrScanner &name) cons

void AsmIns8070::emitAbsolute(AsmInsn &insn, const Operand &op) const {
// PC will be +1 before fetching instruction.
const auto target = op.getError() ? 0 : op.val.getUnsigned()- 1;
const auto target = op.getError() ? 0 : op.val.getUnsigned() - 1;
insn.emitOperand16(target);
}

Expand Down
4 changes: 2 additions & 2 deletions src/operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ bool Operator::isNoneAssoc(const Operator &o) const {
return _prec == o._prec && _assoc == NONE;
}

Error Operator::eval(ValueStack &stack, uint8_t argc) const {
Error Operator::eval(ValueStack &stack, ParserContext &context, uint_fast8_t argc) const {
if (argc == 0)
argc = stack.size();
if (_fn) {
const auto nargs = _fn->nargs();
if (nargs < 0 || argc == nargs)
return _fn->eval(stack, argc);
return _fn->eval(stack, context, argc);
return argc > nargs ? TOO_MANY_FUNC_ARGUMENT : TOO_FEW_FUNC_ARGUMENT;
}
return argc >= _nargs ? (*_op)(stack) : MISSING_OPERAND;
Expand Down
12 changes: 7 additions & 5 deletions src/operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

namespace libasm {

struct ParserContext;

struct ValueStack : Stack<Value, 8> {
ValueStack() : Stack() {}
void push(const Value &value) { _contents[_size++] = value; }
Expand All @@ -39,16 +41,16 @@ struct ValueStack : Stack<Value, 8> {
*/
struct Functor {
/** Returns the number of required arguments. Negative value means variable arguments. */
virtual int8_t nargs() const { return -1; }
virtual int_fast8_t nargs() const { return -1; }
/** Evaluate function with |arguments|. */
virtual Error eval(ValueStack &, uint8_t) const { return OK; }
virtual Error eval(ValueStack &, ParserContext &context, uint_fast8_t) const { return OK; }
};

/**
* Immutable instance to represent prefix or infix operators and functions.
*/
struct Operator : ErrorAt {
enum Type : uint8_t {
enum Type : uint_fast8_t {
PREFIX,
INFIX,
};
Expand All @@ -74,7 +76,7 @@ struct Operator : ErrorAt {
}

/** Pop operands in |stack|, evaluate and push result onto |stack|. */
Error eval(ValueStack &stack, uint8_t argc = 0) const;
Error eval(ValueStack &stack, ParserContext &context, uint_fast8_t argc = 0) const;
/** Returns true when |this| operator has higher precedence than |o| */
bool isHigher(const Operator &o) const;
/** Returns true when |this| operator is none associative and has the same precedence of
Expand All @@ -83,7 +85,7 @@ struct Operator : ErrorAt {

/** Constructor for operators */
typedef Error(OperatorEval)(ValueStack &stack);
Operator(uint8_t prec, Assoc assoc, int8_t nargs = 0, OperatorEval *op = nullptr)
Operator(uint8_t prec, Assoc assoc, int_fast8_t nargs = 0, OperatorEval *op = nullptr)
: ErrorAt(), _prec(prec), _assoc(assoc), _nargs(nargs), _op(op), _fn(nullptr) {}

/** Constructor for function */
Expand Down
21 changes: 4 additions & 17 deletions src/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "error_reporter.h"
#include "operators.h"
#include "str_scanner.h"
#include "symbol_table.h"
#include "value.h"

namespace libasm {
Expand Down Expand Up @@ -54,10 +53,7 @@ struct SymbolParser : Singleton<SymbolParser> {
virtual bool instructionLetter(char c) const { return symbolLetter(c); }

/** Instruction terminates when this returns true */
virtual bool instructionTerminator(char c) const {
UNUSED(c);
return false;
}
virtual bool instructionTerminator(char) const { return false; }

/** Function name is same as symbol */
virtual bool functionNameLetter(char c) const { return symbolLetter(c); }
Expand All @@ -70,10 +66,7 @@ struct FunctionTable : Singleton<FunctionTable> {
/**
* Look up |name| and returns a Functor pointer for a function, otherwise return nullptr.
*/
virtual const Functor *lookupFunction(const StrScanner &name) const {
UNUSED(name);
return nullptr;
}
virtual const Functor *lookupFunction(const StrScanner &) const { return nullptr; }
};

/**
Expand All @@ -95,10 +88,7 @@ struct LetterParser {
* Returns true and consumes prefix for a letter surrounded by letterdelimiters. Also returns
* true if no prefix is required.
*/
virtual bool letterPrefix(StrScanner &scan) const {
UNUSED(scan);
return true;
}
virtual bool letterPrefix(StrScanner &) const { return true; }

/**
* Returns a delimiters for a letter and cosume it. Returns zero if no delimiter exists.
Expand All @@ -117,10 +107,7 @@ struct LetterParser {
* Returns true and consumes prefix for a string surrounded by string delimiters. Also returns
* true if no prefix is required.
*/
virtual bool stringPrefix(StrScanner &scan) const {
UNUSED(scan);
return true;
}
virtual bool stringPrefix(StrScanner &) const { return true; }

/**
* Returns a delimiter which encloses a string and cosume it. Returns zero if no delimiter
Expand Down
Loading

0 comments on commit 2163b03

Please sign in to comment.