fix program

This commit is contained in:
SimonFJ20 2024-11-20 12:54:03 +01:00
parent e269ad7e77
commit 2dbdd5c772
3 changed files with 145 additions and 97 deletions

View File

@ -295,8 +295,8 @@ public:
auto parse_val() -> Res<std::unique_ptr<Value>>; auto parse_val() -> Res<std::unique_ptr<Value>>;
private: private:
inline auto unexpected_tok_err( inline auto unexpected_tok_err(TokTyp expected, std::string_view msg)
TokTyp expected, std::string_view msg) -> Res<std::unique_ptr<Value>> -> Res<std::unique_ptr<Value>>
{ {
return Err { return Err {
.pos = this->cur.val().pos, .pos = this->cur.val().pos,

View File

@ -2,12 +2,91 @@
#include "vm.hpp" #include "vm.hpp"
#include <format> #include <format>
#include <iostream> #include <iostream>
#include <string>
#include <utility> #include <utility>
#include <variant> #include <variant>
enum class AsmLineType {
Op,
Lit,
Loc,
Ref,
};
struct Loc {
explicit Loc(std::string value)
: value(value)
{
}
std::string value;
};
struct Ref {
explicit Ref(std::string value)
: value(value)
{
}
std::string value;
};
struct AsmLine {
/* clang-format off */
AsmLine(sliger::Op value) : type(AsmLineType::Op), value(value) {}
AsmLine(uint32_t value) : type(AsmLineType::Lit), value(value) {}
AsmLine(Loc value) : type(AsmLineType::Loc), value(value) {}
AsmLine(Ref value) : type(AsmLineType::Ref), value(value) {}
/* clang-format on */
AsmLineType type;
std::variant<sliger::Op, uint32_t, Loc, Ref> value;
};
auto compile_asm(const std::vector<AsmLine>& lines) -> std::vector<uint32_t>
{
size_t ip = 0;
auto output = std::vector<uint32_t>();
auto locs = std::unordered_map<std::string, size_t>();
auto refs = std::unordered_map<size_t, std::string>();
for (const auto& line : lines) {
switch (line.type) {
case AsmLineType::Op: {
output.push_back(
std::to_underlying(std::get<sliger::Op>(line.value)));
ip += 1;
break;
}
case AsmLineType::Lit: {
output.push_back(std::get<uint32_t>(line.value));
ip += 1;
break;
}
case AsmLineType::Loc: {
locs.insert_or_assign(std::get<Loc>(line.value).value, ip + 1);
break;
}
case AsmLineType::Ref: {
output.push_back(0);
refs.insert_or_assign(ip, std::get<Ref>(line.value).value);
ip += 1;
break;
}
}
}
for (size_t i = 0; i < output.size(); ++i) {
if (refs.contains(i)) {
output.at(i) = static_cast<uint32_t>(locs.at(refs.at(i)));
}
}
return output;
}
int main() int main()
{ {
using sliger::Op; using R = Ref;
using L = Loc;
using enum sliger::Op;
// fn add(a, b) { // fn add(a, b) {
// + a b // + a b
@ -21,101 +100,56 @@ int main()
// } // }
// result = add(result, 5); // result = add(result, 5);
// } // }
auto program = std::vector<uint32_t> { auto program_asm = std::vector<AsmLine> {
std::to_underlying(Op::SourceMap), // clang-format off
0, SourceMap, 0, 0, 0,
0, PushPtr, R("main"),
0, Pop,
std::to_underlying(Op::PushPtr), PushPtr, R("_exit"),
15, // main Jump,
std::to_underlying(Op::Call), Pop,
0, L("add"),
std::to_underlying(Op::Pop), SourceMap, 19, 2, 5,
std::to_underlying(Op::PushPtr), Add,
87, // .l3 Return,
std::to_underlying(Op::Jump), SourceMap, 28, 5, 1,
std::to_underlying(Op::Pop), PushInt, 0,
/*add*/ std::to_underlying(Op::SourceMap), SourceMap, 44, 6, 1,
19, PushInt, 0,
2, SourceMap, 55, 7, 1,
5, L("1"),
std::to_underlying(Op::Add), SourceMap, 66, 8, 5,
std::to_underlying(Op::Return), LoadLocal, 1,
/*main*/ std::to_underlying(Op::SourceMap), PushInt, 0,
28, LessThan,
5, Not,
1, PushPtr, R("1"),
std::to_underlying(Op::PushInt), JumpIfFalse,
0, SourceMap, 87, 9, 9,
std::to_underlying(Op::SourceMap), PushPtr, R("2"),
44, Jump,
6, SourceMap, 104, 11, 5,
1, LoadLocal, 0,
std::to_underlying(Op::PushInt), PushInt, 5,
0, PushPtr, R("add"),
std::to_underlying(Op::SourceMap), Call, 2,
55, StoreLocal, 0,
7, SourceMap, 133, 12, 5,
1, LoadLocal, 1,
/*.l0*/ std::to_underlying(Op::SourceMap), PushInt, 1,
66, Add,
8, StoreLocal, 1,
5, PushPtr, R("0"),
std::to_underlying(Op::LoadLocal), Jump,
1, L("2"),
std::to_underlying(Op::PushInt), Pop,
0, PushNull,
std::to_underlying(Op::LessThan), Return,
std::to_underlying(Op::Not), L("_exit"),
std::to_underlying(Op::PushPtr), SourceMap, 147, 15, 1
53, // .l1 // clang-format on
std::to_underlying(Op::JumpIfFalse),
std::to_underlying(Op::SourceMap),
87,
9,
9,
std::to_underlying(Op::PushPtr),
83, // .l2
std::to_underlying(Op::Jump),
/*.l1*/ std::to_underlying(Op::SourceMap),
104,
11,
5,
std::to_underlying(Op::LoadLocal),
0,
std::to_underlying(Op::PushInt),
5,
std::to_underlying(Op::PushPtr),
9, // add
std::to_underlying(Op::Call),
2,
std::to_underlying(Op::StoreLocal),
0,
std::to_underlying(Op::SourceMap),
133,
12,
5,
std::to_underlying(Op::LoadLocal),
1,
std::to_underlying(Op::PushInt),
1,
std::to_underlying(Op::Add),
std::to_underlying(Op::StoreLocal),
1,
std::to_underlying(Op::PushPtr),
33, // .l0
std::to_underlying(Op::Jump),
/*.l2*/ std::to_underlying(Op::Pop),
std::to_underlying(Op::Pop),
std::to_underlying(Op::PushNull),
std::to_underlying(Op::Return),
/*.l3*/ std::to_underlying(Op::SourceMap),
147,
15,
1,
std::to_underlying(Op::PushInt),
}; };
auto program = compile_asm(program_asm);
auto vm = sliger::VM(program, auto vm = sliger::VM(program,
{ {
.flame_graph = true, .flame_graph = true,

14
runtime/to_json_2.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "json.hpp"
#include "vm.hpp"
using namespace sliger;
void FlameGraphBuilder::to_json(json::Writer& writer) const
{
writer.add("null");
}
void CodeCoverageBuilder::to_json(json::Writer& writer) const
{
writer.add("null");
}