Skip to content
Merged
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
21 changes: 21 additions & 0 deletions src/cmd/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <utility>
#include <variant>

#include "core/context.hpp"
#include "core/platform.hpp"
#include "error.hpp"
#include "object.hpp"
#include "token.hpp"
Expand All @@ -22,6 +24,8 @@ void Environment::define(const std::string& name, Object value) {
}

Object Environment::get(const Token& name) {
if (name.m_lexeme[0] == '$') return Environment::getRegister(name);

if (m_values.contains(name.m_lexeme)) return m_values[name.m_lexeme];
CmdError::error(name.m_type,
std::format("Undefined variable '{}'.", name.m_lexeme),
Expand Down Expand Up @@ -55,3 +59,20 @@ Environment& Environment::getInstance() {
}

std::map<std::string, Object> Environment::getAll() { return m_values; }

Object Environment::getRegister(const Token& reg) {
auto& target = Context::getTarget();

std::string regName = reg.m_lexeme;
regName.erase(0, 1);

auto regEntry = findRegEntry(regName);
if (!regEntry) {
CmdError::error(reg.m_type, "$ prefix can only be used to access registers",
CmdErrorType::RUNTIME_ERROR);
return std::monostate{};
}

return static_cast<double>(
readRegValue(target->getLastKnownThreadState(), *regEntry.value()));
}
3 changes: 3 additions & 0 deletions src/cmd/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
class Environment {
private:
std::map<std::string, Object> m_values;

static Object getRegister(const Token& reg);

Environment() : m_values({}) {
this->define("print", std::make_shared<PrintFn>(PrintFn()));
this->define("len", std::make_shared<LenFn>(LenFn()));
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ Object Interpreter::visitCallStmnt(const CallStmnt& stmnt) {

for (const auto& x : stmnt.m_args) {
if (auto* id = dynamic_cast<Variable*>(x.get());
id != nullptr && !(m_env.getAll().contains(id->m_name.m_lexeme))) {
id != nullptr && !(m_env.getAll().contains(id->m_name.m_lexeme)) &&
id->m_name.m_lexeme[0] != '$') {
argList.emplace_back(id->m_name.m_lexeme);
continue;
}
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <utility>
#include <variant>

#include "cmd/token_type.hpp"
#include "error.hpp"
#include "token.hpp"
#include "util.hpp"
Expand All @@ -25,6 +26,9 @@ bool Scanner::isAtEnd() const { return m_current >= m_source.length(); }
void Scanner::scanToken() {
const char c = advance();
switch (c) {
case '$':
identifier();
break;
case '(':
addToken(TokenType::LEFT_PAREN);
break;
Expand Down
1 change: 1 addition & 0 deletions src/cmd/scanner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Scanner {
void number();
[[nodiscard]] char peekNext() const;
void identifier();
void registerVariable();

public:
explicit Scanner(std::string source) noexcept;
Expand Down
25 changes: 23 additions & 2 deletions src/cmd/stdlib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,28 @@ class PrintFn : public Callable {
return "<native fn: print>";
}

Object call(std::vector<Object> args) override { return args[0]; }
Object call(std::vector<Object> args) override {
enum class FmtOpt : u8 { DEC, HEX };

FmtOpt fmt = FmtOpt::DEC;

for (const auto& arg : args) {
if (const auto* str = std::get_if<std::string>(&arg)) {
if (*str == "hex") {
fmt = FmtOpt::HEX;
}
}
}

switch (fmt) {
case FmtOpt::DEC:
return args[0];
case FmtOpt::HEX:
auto val = detail::asU64(args[1]);
if (!val) return val.error();
return detail::toHex(*val);
}
}
};

class BreakpointFn : public SubcommandCallable {
Expand Down Expand Up @@ -282,7 +303,7 @@ class RegisterFn : public SubcommandCallable {
if (target->writeRegValue(*entry.value(), *val) != 0)
return "Error writing to register!";

return std::format("{}: {}", *regName, *val);
return std::format("{}: {}", *regName, detail::toHex(*val));
});

public:
Expand Down
1 change: 0 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <cmd/token.hpp>
#include <core/context.hpp>
#include <core/target.hpp>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <error.hpp>
Expand Down
19 changes: 19 additions & 0 deletions test/cmd/test_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,22 @@ TEST_CASE("Test errors on malformed input", "[scanner]") {
REQUIRE(captured == error_message);
}
}

TEST_CASE("Test register variable tokenisation", "[scanner]") {
auto [input, expected_lexeme] = GENERATE(table<std::string, std::string>(
{{"$pc", "$pc"}, {"$rax", "$rax"}, {"$sp", "$sp"}, {"$x0", "$x0"}}));

auto tokens = helpers::scan(input);
REQUIRE(helpers::checkTokensSize(tokens.size(), 1));
REQUIRE(tokens[0].m_type == TokenType::IDENTIFIER);
REQUIRE(tokens[0].m_lexeme == expected_lexeme);
}

TEST_CASE("Test register variable in expression", "[scanner]") {
auto tokens = helpers::scan("$pc+4");
REQUIRE(helpers::checkTokensSize(tokens.size(), 3));
REQUIRE(tokens[0].m_type == TokenType::IDENTIFIER);
REQUIRE(tokens[0].m_lexeme == "$pc");
REQUIRE(tokens[1].m_type == TokenType::PLUS);
REQUIRE(tokens[2].m_type == TokenType::NUMBER);
}
Loading