-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope.hpp
146 lines (109 loc) · 2.89 KB
/
scope.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef scope_hpp_
#define scope_hpp_
#include <iostream>
#include <map>
#include <memory>
#include <vector>
template<typename func>
class Function;
class Object;
class Arguments;
using obj_ptr=std::shared_ptr<Object>;
using func_ptr=obj_ptr (*) (obj_ptr, Arguments&);
class Object {
public:
Object(): callable{false} {}
Object(bool c): callable{c} {}
Object(Object&& f) = default;
Object(const Object& f) = delete;
Object& operator=(Object&& f) = default;
Object& operator=(const Object& f) = delete;
virtual ~Object() = default;
obj_ptr getSlot(const std::string& ObjectName) const;
template<typename str>
void addIntoSlot(str&& ObjectName, obj_ptr obj) {
Objects.insert_or_assign(std::forward<str>(ObjectName),
std::move(obj));
}
virtual obj_ptr operator()(obj_ptr function_scope, Arguments& args);
const bool callable;
virtual obj_ptr clone() const;
void addUpperScope(obj_ptr upper) {
UpperScope=upper;
}
obj_ptr getUpperScope() const {
return UpperScope;
}
private:
std::map<std::string, obj_ptr> Objects;
obj_ptr UpperScope;
protected:
void cloneScope(const obj_ptr& new_obj) const;
};
template<typename func>
class Function: public Object {
public:
Function(func f_): f{f_}, Object{true} {}
Function(Function&& f) = default;
Function(const Function& f) = delete;
Function& operator=(Function&& f) = default;
Function& operator=(const Function& f) = default;
virtual ~Function() {};
obj_ptr operator()(obj_ptr function_scope, Arguments& args) override {
return f(function_scope, args);
}
obj_ptr clone() const override {
obj_ptr new_obj=std::make_shared<Function<func>>(f);
cloneScope(new_obj);
return new_obj;
}
private:
func f;
};
#include "tokenizer.hpp"
class Arguments {
public:
Arguments();
Arguments(tokenizerBase& tok);
void addTilClose(tokenizerBase &tok);
Arguments(Arguments && a) = default;
Arguments(const Arguments & a) = default;
Arguments& operator=(Arguments && a) = default;
Arguments& operator=(const Arguments & a) = default;
obj_ptr execute(obj_ptr& scope);
obj_ptr execute(obj_ptr& scope, Arguments& args_values);
obj_ptr exec_curr_part(obj_ptr& scope);
void next_argument();
void addToken(token t, const std::string& s) {
tokens.emplace_back(t, s);
}
void move() {
++it;
}
token currToken() const {
return it->first;
}
std::string flush() const {
return it->second;
}
bool eof() const {
return tokens.end()==it;
}
void restart() {
it=tokens.begin();
}
private:
std::vector<std::pair<token, std::string>> tokens;
std::vector<std::pair<token, std::string>>::iterator it;
};
inline size_t symbolPriority(const std::string& sym) {
if(sym=="==" || sym=="!=" || sym=="<" || sym=="<=" || sym==">" || sym==">=")
return 1;
if(sym=="+" || sym=="-")
return 2;
if(sym=="*" || sym=="/")
return 3;
// functions
return 4;
}
#endif