This repository has been archived by the owner on Aug 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bean_vm.hpp
77 lines (58 loc) · 1.68 KB
/
bean_vm.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once
#include "utils.hpp"
#include "bean_ast.hpp"
#include <sstream>
#include <algorithm>
#include <string>
#include <fstream>
#include <streambuf>
#include "bean_vm_caller.hpp"
namespace bean {
class bean_vm
{
public:
std::shared_ptr<bean_object> eval_result(const std::string& script)
{
if (script.empty()) return std::make_shared<bean_object>(BeanObjectType::None);
tokenizer token_gen;
const auto tokens = tokenizer::tokenize(script);
auto res = ast_builder::parse(tokens, state);
return res->eval(state);
}
void eval(const std::string& script)
{
eval_result(script);
}
std::shared_ptr<bean_object> eval_file_result(const std::string& file_path)
{
std::ifstream t(file_path);
const std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return eval_result(str);
}
void eval_file(const std::string& file_path)
{
eval_file_result(file_path);
}
bean_state& get_state()
{
return state;
}
template<typename Ret, typename ...Args>
void bind_function(const std::string& function_name, Ret(__cdecl* func)(Args...))
{
auto new_function = std::make_shared<bean_function>(function_name);
new_function->set_caller(bind_non_member_function(function_name, func));
state.functions[function_name] = new_function;
}
template<typename Ret, typename ...Args>
void bind_function(const std::string& function_name, std::function<Ret(Args...)> func)
{
auto new_function = std::make_shared<bean_function>(function_name);
new_function->set_caller(bind_non_member_function(function_name, func));
state.functions[function_name] = new_function;
}
private:
bean_state state;
};
}