Skip to content

Commit 63c8f44

Browse files
committed
it compiles!
1 parent 07370b4 commit 63c8f44

18 files changed

+191
-124
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ project(dlang)
33

44
set(CMAKE_CXX_STANDARD 20)
55

6-
add_executable(dlang main.cpp parse/lex.cpp parse/parse.cpp vm/value.cpp vm/value.hpp vm/gc.cpp vm/gc.hpp compile/bytecode.hpp compile/bytecode.cpp compile/compile.cpp compile/compile.hpp compile/semantics/process_tree.hpp compile/semantics/syntax.hpp compile/semantics/syntax.cpp compile/semantics/process_tree.cpp util.cpp util.hpp vm/closure.cpp vm/closure.hpp vm/handle.cpp vm/handle.hpp vm/vm.cpp vm/vm.hpp vm/literal.cpp vm/literal.hpp vm/read_bc.cpp vm/read_bc.hpp vm/exec_bc_instr.cpp vm/exec_bc_instr.hpp vm/operators.cpp vm/operators.hpp vm/builtin_operators/math.cpp vm/builtin_operators/math.hpp vm/lambda_return.cpp vm/lambda_return.hpp vm/global_ids.cpp vm/global_ids.hpp)
6+
add_executable(dlang main.cpp parse/lex.cpp parse/parse.cpp vm/value.cpp vm/value.hpp vm/gc.cpp vm/gc.hpp compile/bytecode.hpp compile/bytecode.cpp compile/compile.cpp compile/compile.hpp compile/semantics/process_tree.hpp compile/semantics/syntax.hpp compile/semantics/syntax.cpp compile/semantics/process_tree.cpp util.cpp util.hpp vm/closure.cpp vm/closure.hpp vm/handle.cpp vm/handle.hpp vm/vm.cpp vm/vm.hpp vm/literal.cpp vm/literal.hpp vm/read_bc.cpp vm/read_bc.hpp vm/exec_bc_instr.cpp vm/exec_bc_instr.hpp vm/operators.cpp vm/operators.hpp vm/builtin_operators/math.cpp vm/builtin_operators/math.hpp vm/lambda_return.cpp vm/lambda_return.hpp vm/global_ids.cpp vm/global_ids.hpp vm/bc.cpp vm/bc.hpp)

compile/command.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <cstdlib>
1010
#include <variant>
1111
#include <cstring>
12-
12+
#include <string>
1313

1414
class Command {
1515
public:

main.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "compile/bytecode.hpp"
1111
#include "util.hpp"
1212
#include "vm/vm.hpp"
13+
#include "vm/read_bc.hpp"
1314

1415

1516
// dlang <cmd> <in> options
@@ -65,10 +66,12 @@ int main(int argc, char** argv) {
6566
print_help_msg();
6667
}
6768

68-
6969
if (run) {
70-
VM interpreter{};
71-
return 0;
70+
std::ifstream bc_src = std::ifstream(fname);
71+
// TODO: capture argv...
72+
VM interpreter{read_lit_header(bc_src), { "argv not implemented" }};
73+
74+
return 0; // never gets called... (hopefully)
7275
}
7376
Program p;
7477
try {

vm/bc.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//
2+
// Created by tate on 24-05-20.
3+
//
4+
5+
#include "bc.hpp"

vm/bc.hpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Created by tate on 24-05-20.
3+
//
4+
5+
#ifndef DLANG_BC_HPP
6+
#define DLANG_BC_HPP
7+
8+
#include "../compile/command.hpp"
9+
10+
// command that falls within a
11+
class BCInstr {
12+
public:
13+
using OPCode = Command::OPCode;
14+
OPCode instr;
15+
union {
16+
int64_t i;
17+
double v;
18+
};
19+
BCInstr() = default;
20+
BCInstr(OPCode cmd, int64_t i): instr(cmd), i(i) {}
21+
BCInstr(OPCode cmd, double f): instr(cmd), v(f) {}
22+
23+
};
24+
25+
26+
#endif //DLANG_BC_HPP

vm/builtin_operators/math.cpp

+63-63
Original file line numberDiff line numberDiff line change
@@ -7,69 +7,69 @@
77
#include "../vm.hpp"
88

99
static void add_act(Frame& f) {
10-
11-
Value rhs = f.eval_stack.back();
12-
f.eval_stack.pop_back();
13-
Value& lhs = f.eval_stack.back();
14-
15-
16-
// defer references
17-
if (rhs.type() == Value::VType::REF) {
18-
Value* p = std::get<Handle<Value>>(rhs.v).ptr;
19-
if (p == nullptr)
20-
return; // TODO: type-error
21-
rhs = *p;
22-
}
23-
24-
if (lhs.type() == Value::VType::REF) {
25-
Value* p = std::get<Handle<Value>>(lhs.v).ptr;
26-
if (p == nullptr)
27-
return; // TODO: type-error
28-
lhs = *p;
29-
}
30-
31-
auto rhs_type = rhs.type();
32-
auto lhs_type = lhs.type();
33-
// perform relevant operation
34-
if (rhs_type == Value::VType::INT) {
35-
Value::int_t& i = std::get<Value::int_t>(rhs.v);
36-
if (lhs_type == Value::VType::INT) {
37-
std::get<Value::int_t>(lhs.v) += i;
38-
} else if (lhs_type == Value::VType::FLOAT) {
39-
lhs = Value(std::get<Value::float_t>(lhs.v) + (Value::float_t) i);
40-
} else if (lhs_type == Value::VType::STR) {
41-
Value(std::get<std::string>(lhs.v) += std::to_string(i);
42-
} else {
43-
// TODO: type-error
44-
}
45-
46-
} else if (rhs_type == Value::VType::STR) {
47-
std::string& s = std::get<std::string>(rhs.v);
48-
if (lhs_type == Value::VType::INT) {
49-
lhs = Value(std::to_string(std::get<Value::int_t>(lhs.v)) + s);
50-
} else if (lhs_type == Value::VType::FLOAT) {
51-
lhs = Value(std::to_string(std::get<Value::float_t>(lhs.v)) + s);
52-
} else if (lhs_type == Value::VType::STR) {
53-
std::get<std::string>(lhs.v) += s;
54-
} else {
55-
// TODO: type-error
56-
}
57-
} else if (rhs_type == Value::VType::FLOAT) {
58-
Value::float_t &fp = std::get<Value::float_t>(rhs.v);
59-
if (lhs_type == Value::VType::INT) {
60-
lhs = Value(std::get<Value::int_t>(lhs.v) + fp);
61-
} else if (lhs_type == Value::VType::FLOAT) {
62-
std::get<Value::float_t>(lhs.v) += fp;
63-
} else if (lhs_type == Value::VType::STR) {
64-
std::get<std::string>(lhs.v) += std::to_string(fp);
65-
} else {
66-
// TODO: type-error
67-
}
68-
} else {
69-
// TODO: type-error
70-
}
71-
72-
return;
10+
//
11+
// Value rhs = f.eval_stack.back();
12+
// f.eval_stack.pop_back();
13+
// Value& lhs = f.eval_stack.back();
14+
//
15+
//
16+
// // defer references
17+
// if (rhs.type() == Value::VType::REF) {
18+
// Value* p = std::get<Handle<Value>>(rhs.v).ptr;
19+
// if (p == nullptr)
20+
// return; // TODO: type-error
21+
// rhs = *p;
22+
// }
23+
//
24+
// if (lhs.type() == Value::VType::REF) {
25+
// Value* p = std::get<Handle<Value>>(lhs.v).ptr;
26+
// if (p == nullptr)
27+
// return; // TODO: type-error
28+
// lhs = *p;
29+
// }
30+
//
31+
// auto rhs_type = rhs.type();
32+
// auto lhs_type = lhs.type();
33+
// // perform relevant operation
34+
// if (rhs_type == Value::VType::INT) {
35+
// Value::int_t& i = std::get<Value::int_t>(rhs.v);
36+
// if (lhs_type == Value::VType::INT) {
37+
// std::get<Value::int_t>(lhs.v) += i;
38+
// } else if (lhs_type == Value::VType::FLOAT) {
39+
// lhs = Value(std::get<Value::float_t>(lhs.v) + (Value::float_t) i);
40+
// } else if (lhs_type == Value::VType::STR) {
41+
// std::get<std::string>(lhs.v) += std::to_string(i);
42+
// } else {
43+
// // TODO: type-error
44+
// }
45+
//
46+
// } else if (rhs_type == Value::VType::STR) {
47+
// std::string& s = std::get<std::string>(rhs.v);
48+
// if (lhs_type == Value::VType::INT) {
49+
// lhs = Value(std::to_string(std::get<Value::int_t>(lhs.v)) + s);
50+
// } else if (lhs_type == Value::VType::FLOAT) {
51+
// lhs = Value(std::to_string(std::get<Value::float_t>(lhs.v)) + s);
52+
// } else if (lhs_type == Value::VType::STR) {
53+
// std::get<std::string>(lhs.v) += s;
54+
// } else {
55+
// // TODO: type-error
56+
// }
57+
// } else if (rhs_type == Value::VType::FLOAT) {
58+
// Value::float_t &fp = std::get<Value::float_t>(rhs.v);
59+
// if (lhs_type == Value::VType::INT) {
60+
// lhs = Value(std::get<Value::int_t>(lhs.v) + fp);
61+
// } else if (lhs_type == Value::VType::FLOAT) {
62+
// std::get<Value::float_t>(lhs.v) += fp;
63+
// } else if (lhs_type == Value::VType::STR) {
64+
// std::get<std::string>(lhs.v) += std::to_string(fp);
65+
// } else {
66+
// // TODO: type-error
67+
// }
68+
// } else {
69+
// // TODO: type-error
70+
// }
71+
//
72+
// return;
7373

7474
}
7575

vm/closure.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
#include <cinttypes>
99
#include <unordered_map>
1010
#include <memory>
11+
#include <vector>
1112

1213
#include "handle.hpp"
13-
#include "literal.hpp"
14-
#include "value.hpp"
14+
#include "bc.hpp"
15+
1516

1617
class Frame;
1718
class Value;

vm/exec_bc_instr.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,21 @@ void exec_bc_instr(Frame& f, BCInstr cmd) {
5050

5151

5252
case BCInstr::OPCode::USE_LIT:
53-
const Literal& lit = f.rt->vm->literals[cmd.i];
54-
if (lit.type == Literal::Ltype::VAL) {
53+
Literal& lit = f.rt->vm->literals[cmd.i];
54+
if (lit.v.index() == Literal::Ltype::VAL) {
5555
f.eval_stack.emplace_back(std::get<Value>(lit.v));
5656
} else {
57-
const auto& cd = std::get<ClosureDef>(lit.v);
57+
ClosureDef& cd = std::get<ClosureDef>(lit.v);
58+
59+
Closure nc{};
5860

59-
const Closure nc();
60-
std::unordered_map<int64_t
6161
// capture lexical vars
6262
// TODO: this is prolly expensive
63-
for (auto & cid : cd.capture_ids)
64-
this->vars[cid] = f.closure->vars[cid];
63+
for (int64_t cid : cd.capture_ids)
64+
nc.vars[cid] = f.closure.vars[cid];
6565

66-
this->body = &cd.body;
66+
nc.body = &cd.body;
6767

68-
this->id = _uid++;
6968

7069
}
7170
}

vm/exec_bc_instr.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#ifndef DLANG_EXEC_BC_INSTR_HPP
66
#define DLANG_EXEC_BC_INSTR_HPP
77

8-
#include "literal.hpp"
9-
8+
#include "bc.hpp"
109
class Frame;
1110

1211
void exec_bc_instr(Frame& f, BCInstr cmd);

vm/handle.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class Handle {
1111
public:
1212
T* ptr;
1313

14-
explicit Handle(): ptr(nullptr) {};
15-
explicit Handle(T* p): ptr(p) {};
16-
Handle(Handle& other) = default;
14+
explicit Handle(): ptr(nullptr) {}
15+
explicit Handle(T* p): ptr(p) {}
16+
Handle(const Handle& other): ptr(other.ptr) {}
1717
~Handle() = default;
1818

1919
};

vm/lambda_return.hpp

+18-6
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,25 @@ class LambdaReturnMsg : public virtual RTMessage {
3030
std::shared_ptr<Frame> frame_target;
3131
std::shared_ptr<SyncCallStack> stack_target;
3232
Value ret;
33+
LambdaReturnMsg():
34+
frame_target(nullptr), stack_target(nullptr), ret() {}
3335

3436
LambdaReturnMsg(
35-
std::shared_ptr<Frame>& frame_target,
36-
std::shared_ptr<SyncCallStack>& stack_target,
37-
const Value& return_value):
38-
frame_target(frame_target), stack_target(stack_target), ret(return_value)
37+
std::shared_ptr<Frame> frame_target,
38+
std::shared_ptr<SyncCallStack> stack_target,
39+
Value& return_value):
40+
frame_target(std::move(frame_target)), stack_target(std::move(stack_target)), ret(return_value)
3941
{}
4042

4143
void action(Runtime& rt) override {
44+
if (!this->stack_target || !this->frame_target) {
45+
std::cout <<"invalid lambda return msg call ";
46+
}
47+
4248
// if stack target not in active stacks
4349
if (rt.running != this->stack_target &&
4450
std::find(rt.active.begin(), rt.active.end(), this->stack_target) == rt.active.end())
45-
rt.active.emplace_back(this->frame_target);
51+
rt.active.emplace_back(this->stack_target);
4652

4753
// find frame on stack
4854
size_t i;
@@ -69,8 +75,14 @@ class LambdaReturnNativeFn : public virtual NativeFunction {
6975
std::shared_ptr<Runtime> rt;
7076
LambdaReturnMsg msg;
7177

78+
LambdaReturnNativeFn(): rt(nullptr), msg() {}
79+
explicit LambdaReturnNativeFn(Frame& f) {
80+
this->rt = f.rt->vm->main_thread; // TODO: detect thread/convert rt to weak_ptr
81+
this->msg = LambdaReturnMsg(f.rt->running->back(), f.rt->running, f.eval_stack.back());
82+
}
83+
7284
void operator()(Frame& f) override {
73-
this->rt->recv_msg(this->msg);
85+
this->rt->recv_msg(new LambdaReturnMsg(this->msg));
7486
}
7587
};
7688

vm/literal.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,12 @@
22
// Created by tate on 17-05-20.
33
//
44

5+
56
#include "literal.hpp"
7+
#include "value.hpp"
8+
69

10+
Literal::Literal(const std::string& str, bool is_json) {
11+
// TODO: is_json determines how to parse...
12+
this->v = std::variant<ClosureDef, Value>(Value(str));
13+
}

vm/literal.hpp

+10-21
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,9 @@
1212
#include <unordered_set>
1313

1414
#include "../compile/command.hpp"
15-
#include "value.hpp"
16-
17-
// command that falls within a
18-
class BCInstr {
19-
public:
20-
using OPCode = Command::OPCode;
21-
OPCode instr;
22-
union {
23-
int64_t i;
24-
double v;
25-
};
26-
BCInstr() = default;
27-
BCInstr(OPCode cmd, int64_t i): instr(cmd), i(i) {}
28-
BCInstr(OPCode cmd, int64_t f): instr(cmd), v(f) {}
2915

30-
};
16+
#include "bc.hpp"
17+
#include "value.hpp"
3118

3219
class ClosureDef {
3320
public:
@@ -52,28 +39,30 @@ class ClosureDef {
5239
const int64_t i_id, const int64_t o_id):
5340
capture_ids(capture_ids), decl_ids(decl_ids), body(body), i_id(i_id), o_id(o_id)
5441
{}
42+
ClosureDef() = default;
5543
};
5644

5745
class Literal {
5846
public:
5947
std::variant<ClosureDef, Value> v;
6048
enum Ltype {
61-
LAM, VAL, ERR
49+
ERR = -1,
50+
LAM = 0,
51+
VAL = 1
6252
};
6353

64-
static inline Ltype type(int i) const {
54+
static inline Ltype type(int i) {
6555
if (i == std::variant_npos)
6656
return Ltype::ERR;
6757
return i ? Ltype::VAL : Ltype::LAM;
6858
}
6959

70-
inline Ltype type(int i) const {
60+
inline Ltype type() const {
7161
return Literal::type(v.index());
7262
}
7363

74-
75-
explicit Literal(ClosureDef c): v(c) {}
76-
Literal(Value v): v(v) {}
64+
explicit Literal(const ClosureDef& c): v(c) {}
65+
explicit Literal(const std::string& str, bool is_json = false);
7766

7867
};
7968

0 commit comments

Comments
 (0)