Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transitional changes to define a common type for nullable values (aka undefined symbols/labels) #42

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions src/assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ Number to_number(std::string_view txt, Base base, int skip = 0)
return static_cast<Number>(result);
}

std::string any_to_string(std::any const& val)
std::string any_to_string(std::any const& val, std::string_view name)
{
if (!val.has_value()) {
throw parse_error(fmt::format("Cannot convert 'None' value from Symbol {} to std::string", name));
}
if (auto const* n = std::any_cast<Number>(&val)) {
auto in = static_cast<int64_t>(*n);
if (*n == static_cast<double>(in)) return fmt::format("${:x}", in);
Expand Down Expand Up @@ -547,8 +550,11 @@ void Assembler::setSym(std::string_view sym, std::any val)
syms.set_final(sym);
}

AsmValue to_variant(std::any const& a)
AsmValue to_variant(std::any const& a, std::string_view identifier)
{
if (!a.has_value()) {
throw parse_error(fmt::format("Cannot convert a 'None' {} value to AsmValue", identifier));
}
if (auto const* n = std::any_cast<Number>(&a))
{
return *n;
Expand All @@ -565,7 +571,12 @@ AsmValue to_variant(std::any const& a)
{
return *vn;
}
return {};

// not implemented
static_assert(std::variant_size<AsmValue>() == 4);
//throw parse_error(fmt::format("No variant conversion available for type {}", a.type().name()));
auto s_err = fmt::format("No AsmValue conversion available for type {} (context: {})", a.type().name(), identifier);
return persist(s_err);
}

void Assembler::setupRules()
Expand Down Expand Up @@ -732,8 +743,21 @@ void Assembler::setupRules()
}

for(auto&& a : meta.args) {
auto v = to_variant(a);
meta.vargs.push_back(v);
auto identifier = fmt::format("MetaBlock::{} -> arg[{}]", meta.name, meta.vargs.size());
if (a.has_value()) {
auto v = to_variant(a, identifier);
meta.vargs.push_back(v);
if (auto strv = std::get_if<std::string_view>(&v)) {
if (strv->starts_with("No AsmValue conversion")) {
LOGD(fmt::format("{}\n", *strv));
}
}
} else {
// FIXME: keeps existing code intact (can we leave out such values?)
AsmValue v_default;
LOGD(fmt::format("AsmValue cannot hold a 'None' value -> creating value {} (context: {}))", std::get<0>(v_default), identifier));
meta.vargs.push_back(v_default);
}
}


Expand Down Expand Up @@ -969,7 +993,7 @@ void Assembler::setupRules()
auto buildArg = [](SV& sv) -> std::any {
auto mode = modeMap.at(std::string(sv.name()));
return Instruction{"", mode,
mode == Mode::ACC ? 0 : any_cast<Number>(sv[0])};
mode == Mode::ACC ? 0 : number<int32_t>(sv[0])};
};
for (auto const& [name, _] : modeMap) {
parser.after(name.c_str(), buildArg);
Expand All @@ -978,7 +1002,7 @@ void Assembler::setupRules()
parser.after("ZRel", [](SV& sv) -> Instruction {
auto v = (number<int32_t>(sv[1]) << 24) |
(number<int32_t>(sv[0]) << 16) | number<int32_t>(sv[2]);
return {"", Mode::ZP_REL, static_cast<Number>(v)};
return {"", Mode::ZP_REL, v};
});

parser.after("LabelRef", [this](SV& sv) {
Expand Down Expand Up @@ -1099,8 +1123,8 @@ void Assembler::setupRules()
}

auto ope = any_cast<std::string_view>(sv[1]);
auto arg1 = to_variant(sv[0]);
auto arg2 = to_variant(sv[2]);
auto arg1 = to_variant(sv[0], fmt::format("Expression2 -> arg[{}]", 0));
auto arg2 = to_variant(sv[2], fmt::format("Expression2 -> arg[{}]", 2));

try {
auto res = operation(ope, arg1, arg2);
Expand Down Expand Up @@ -1400,7 +1424,7 @@ void Assembler::printSymbols()
{
syms.forAll([](std::string const& name, std::any const& val) {
if (!utils::startsWith(name, "__"))
fmt::print("{} == {}\n", name, any_to_string(val));
fmt::print("{} == {}\n", name, any_to_string(val, name));
});
}

Expand All @@ -1411,7 +1435,7 @@ void Assembler::writeSymbols(fs::path const& p)
if (!utils::startsWith(name, "__") &&
(name.find('.') == std::string::npos) &&
val.type() == typeid(Number))
fmt::print(f.filePointer(), "{} = {}\n", name, any_to_string(val));
fmt::print(f.filePointer(), "{} = {}\n", name, any_to_string(val, name));
});
}

Expand Down
2 changes: 0 additions & 2 deletions src/assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

class Machine;

using AsmValue = std::variant<Number, std::string_view, std::vector<uint8_t>, std::vector<Number>>;

inline void Check(bool v, std::string const& txt)
{
if (!v) throw parse_error(txt);
Expand Down
18 changes: 13 additions & 5 deletions src/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <variant>
#include <vector>

#include <filesystem>
Expand Down Expand Up @@ -110,6 +111,7 @@ inline std::string_view operator+(std::string_view sv, std::string_view n)
}

using Number = double;
using AsmValue = std::variant<Number, std::string_view, std::vector<uint8_t>, std::vector<Number>>;

inline Num div(Num a, Num b)
{
Expand All @@ -125,12 +127,18 @@ inline Num pow(Num a, Num b)
template <typename T>
inline T number(std::any const& v)
{
return static_cast<T>(std::any_cast<Number>(v));
if (v.has_value()) {
return static_cast<T>(std::any_cast<Number>(v));
}
throw std::runtime_error(fmt::format("Cannot convert 'None' to arithmetic type '{}'.", typeid(T).name()));
}

inline Number number(std::any const& v)
{
return std::any_cast<Number>(v);
if (v.has_value()) {
return std::any_cast<Number>(v);
}
throw std::runtime_error(fmt::format("Cannot converted 'None' to Number type ({})", typeid(Number).name()));
}

template <typename T>
Expand Down Expand Up @@ -158,8 +166,8 @@ class assert_error : public std::exception

struct Instruction
{
Instruction(std::string_view op, sixfive::Mode m, double v)
: opcode(op), mode(m), val(static_cast<int32_t>(v))
Instruction(std::string_view op, sixfive::Mode m, int32_t v)
: opcode(op), mode(m), val(v)
{}
std::string opcode;
sixfive::Mode mode;
Expand Down Expand Up @@ -190,7 +198,7 @@ inline std::string getHomeDir()
return homeDir;
}

std::string any_to_string(std::any const& val);
std::string any_to_string(std::any const& val, std::string_view name);

inline void printArg(std::any const& arg)
{
Expand Down
2 changes: 1 addition & 1 deletion src/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ AsmResult Machine::assemble(Instruction const& instr)
// Find a matching addressing mode
auto it_op =
std::find_if(it_ins->opcodes.begin(), it_ins->opcodes.end(),
[&](auto const& o) {
[&](auto const& o) {
return modeMatches(arg.mode, o.mode, small);
});

Expand Down
4 changes: 2 additions & 2 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ std::any Parser::evaluate(AstNode const& node)
sv.name(), ast->line, sv.token_view());
for (size_t i = 0; i < sv.size(); i++) {
std::any const v = sv[i];
fmt::print(" {}: {}\n", i, any_to_string(v));
fmt::print(" {}: {}\n", i, any_to_string(v, sv.name()));
}
auto ret = callAction(sv, *ast->action);
fmt::print(">> {}\n", any_to_string(ret));
fmt::print(">> {}\n", any_to_string(ret, sv.name()));
return ret;
}
return callAction(sv, *ast->action);
Expand Down
6 changes: 0 additions & 6 deletions src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ class SemanticValues

AstNode get_node() const { return ast; }
std::string const& file_name() const;

template <typename T>
T to(size_t i) const
{
return std::any_cast<T>(operator[](i));
}
};

using ActionFn = std::function<std::any(SemanticValues const&)>;
Expand Down
1 change: 1 addition & 0 deletions src/symbol_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ struct SymbolTable
}
undefined.insert(std::string{name});
if constexpr (std::is_same_v<T, std::any>) {
LOGD(fmt::format("Returning zero value for undefined label '{}'\n", name));
return zero;
}
LOGD("Returning default (%s)", typeid(T{}).name());
Expand Down