-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.h
More file actions
180 lines (156 loc) · 3.86 KB
/
Copy pathutils.h
File metadata and controls
180 lines (156 loc) · 3.86 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#pragma once
#include <variant>
#include <string>
#include <map>
#include <vector>
#include <iostream>
#include <functional>
#include <memory>
#include <fstream>
#include <sstream>
#include "expression.h"
#include "parser.h"
#include "interpreter.h"
class list_adaptor_iterator
{
public:
const Expression* data;
const Expression* next;
using iterator_category = std::forward_iterator_tag;
using value_type = const Expression;
using difference_type = size_t;
using pointer = const Expression*;
using reference = const Expression&;
list_adaptor_iterator(){}
list_adaptor_iterator(pointer _data, pointer _next) : data(_data), next(_next) {}
reference operator*() { return *data; }
bool operator!=(const list_adaptor_iterator& other)
{
return data != other.data;
}
bool operator==(const list_adaptor_iterator& other)
{
return data == other.data;
}
list_adaptor_iterator& operator++()
{
if(std::holds_alternative<Null>(*next))
{
data = nullptr;
next = nullptr;
}
else
{
const auto& p = std::get<Pair>(*next);
data = p.first.get();
next = p.second.get();
}
return *this;
}
list_adaptor_iterator operator++(int)
{
if(std::holds_alternative<Null>(*next))
{
return list_adaptor_iterator(nullptr, nullptr);
}
else
{
const auto& p = std::get<Pair>(*next);
return list_adaptor_iterator(p.first.get(), p.second.get());
}
}
};
class List
{
const Expression& source;
public:
List(const Expression& _source) : source(_source) {}
const Expression& at(int index) const
{
const Pair* ref = &std::get<Pair>(source);
while(index != 0)
{
ref = &std::get<Pair>(*ref->second);
index -= 1;
}
return *ref->first;
}
const Expression& all() const
{
return source;
}
const Expression& last() const
{
const Pair* ref = &std::get<Pair>(source);
while(!std::holds_alternative<Null>(*ref->second))
{
ref = &std::get<Pair>(*ref->second);
}
return *ref->first;
}
list_adaptor_iterator begin() const
{
const auto& p = std::get<Pair>(source);
return list_adaptor_iterator(p.first.get(), p.second.get());
}
list_adaptor_iterator end() const
{
return list_adaptor_iterator(nullptr, nullptr);
}
};
// environments
struct Environment : public std::map<Symbol,Expression>
{
bool contains(const Symbol& s) const
{
const auto it = find(s);
return it != end();
}
friend std::ostream& operator<< (std::ostream& os, const Environment& env)
{
os << '{';
for(const auto&[k,v] : env)
{
os << k << ':' << v << ", ";
}
os << '}';
return os;
}
};
struct Environments : public std::vector<std::shared_ptr<Environment>>
{
void add()
{
emplace_back(std::make_shared<Environment>());
}
const Expression& get(const Symbol& s) const
{
auto it = rbegin();
while(it != rend())
{
if((*it)->contains(s))
return (*(*it))[s];
++it;
}
throw std::runtime_error("Symbol is not bound to value");
}
void set(const Symbol s, const Expression& value)
{
back()->insert({s, value});
}
void set(const Symbol s, const Expression&& value)
{
back()->insert({s, value});
}
friend std::ostream& operator<<(std::ostream& os, const Environments& env)
{
os << '{';
for(const auto& e : env)
{
os << *e;
}
os << '}';
return os;
}
};
// end environments