Skip to content

Commit

Permalink
first program run
Browse files Browse the repository at this point in the history
  • Loading branch information
michael.burzan committed Jan 3, 2025
1 parent e4abcc2 commit 17ca7dd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
8 changes: 8 additions & 0 deletions include/ExpressionNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class ExpressionNode : public AstNode, public std::enable_shared_from_this<Expre
}
void accept(const std::shared_ptr<Visitor>& v) override;

auto kind() const -> ExpressionKind {
return _kind;
}

auto constante() const -> const std::string& {
return _constante;
}

private:
std::string _constante;
AstPtr _exp;
Expand Down
5 changes: 5 additions & 0 deletions include/FunctionNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class FunctionNode : public AstNode, public std::enable_shared_from_this<Functio
FunctionNode(const std::string& fname, const AstPtr& returnType, const AstPtr& statements);
void accept(const std::shared_ptr<Visitor>& v) override;

public:
auto function_name() const -> const std::string& {
return _fname;
}

private:
std::string _fname;
AstPtr _returnType;
Expand Down
18 changes: 17 additions & 1 deletion include/VirtualMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ template <class POLICY> class VirtualMachine {
static auto make(const std::vector<InstructionTypeV>& program) -> VirtualMachine<P> {
return VirtualMachine<P>(program);
}

VirtualMachine(std::size_t mem_size = 1024 * 1024 * 1024)
: _registers(10, 0), _pc(0), _stack(10, 0), _sp(-1), _memory(mem_size) {
}

VirtualMachine(const std::vector<InstructionTypeV>& program, std::size_t mem_size = 1024 * 1024 * 1024)
: _program(program), _registers(10, 0), _pc(0), _stack(10, 0), _sp(-1), _memory(mem_size) {
}
Expand All @@ -90,6 +95,10 @@ template <class POLICY> class VirtualMachine {
panic("function " + fname + " not exist");
}

void add_program(const std::vector<InstructionTypeV>& program) {
_program = program;
}

void add_native_function(const std::string fname, const NativeFunction<VirtualMachine<POLICY>>& code,
uint8_t arg_count) {

Expand All @@ -110,6 +119,7 @@ template <class POLICY> class VirtualMachine {
old_pc = _pc;
std::visit(
[&](auto& instruction) {
std::cout << "STEP" << std::endl;
InstructionResult res = instruction.execute(this);
res.error([](const Error& err) {
std::cerr << "Instruction failed: " << err.msg() << "\n";
Expand Down Expand Up @@ -214,7 +224,6 @@ template <class POLICY> class VirtualMachine {
std::cout << _memory;
}

private:
void print_registers() {
std::cout << "Registers:\n";
for (std::size_t i = 0; i < _registers.size(); ++i) {
Expand All @@ -224,6 +233,13 @@ template <class POLICY> class VirtualMachine {
}
}
void print_stack() {
std::cout << "Stack:" << std::endl;
std::cout << "----------------------" << std::endl;
for (const auto& x : _stack) {
std::cout << "\t" << to_string(x).result_or("---");
std::cout << std::endl;
;
}
}

private:
Expand Down
46 changes: 13 additions & 33 deletions tests/VisitorTest.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,19 @@
#include "purge.hpp"
#include <memory>
#include "Codegeneration.h"
#include "Parser.h"
#include "Visitor.h"
#include "TranslationUnitNode.h"

class TestVisitor : public Visitor{
public:
TestVisitor(): count(0) {}

auto begin(const std::shared_ptr<TranslationUnitNode> & node)->VisitResult override{
UNUSED(node);
count++;
return true;
}

auto visit(const std::shared_ptr<TranslationUnitNode> & node)->std::shared_ptr<Visitor> override{
UNUSED(node);
count++;
return shared_from_this();
}

auto end(const std::shared_ptr<TranslationUnitNode> & node)->VisitResult override {
UNUSED(node);
count++;
return true;
}
using Visitor::begin;
using Visitor::end;
using Visitor::visit;

int count;
};

#include "VMType.h"

using VM = VirtualMachine<AggresivPolicy>;
PURGE_MAIN

SIMPLE_TEST_CASE(SimpleCallTest) {
// only compile test
SIMPLE_TEST_CASE(TEST_1) {
Parser p("fn main() -> i32 { return 22; }");
auto res = p.parse();
auto visitor = std::make_shared<TranslationUnitVisitor>();
res.result()->accept(visitor);
VMPtr vm = visitor->vm();
REQUIRE(vm->function_entry("main").name() == "main");
vm->run();
REQUIRE(std::get<VMPrimitive>(vm->stack_top()) == VMPrimitive(22));
}

0 comments on commit 17ca7dd

Please sign in to comment.