Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
// Copyright (C) 2011-2014, Gabriel Dos Reis.
2
// All rights reserved.
3
// Written by Gabriel Dos Reis.
4
//
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
7
// met:
8
//
9
// - Redistributions of source code must retain the above copyright
10
// notice, this list of conditions and the following disclaimer.
11
//
12
// - Redistributions in binary form must reproduce the above copyright
13
// notice, this list of conditions and the following disclaimer in
14
// the documentation and/or other materials provided with the
15
// distribution.
16
//
17
// - Neither the name of OpenAxiom nor the names of its contributors
18
// may be used to endorse or promote products derived from this
19
// software without specific prior written permission.
20
//
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33
// --% Author: Gabriel Dos Reis
34
35
#include <open-axiom/vm>
36
#include <iterator>
37
#include <algorithm>
38
#include <ostream>
39
40
namespace OpenAxiom {
41
namespace VM {
42
void Dynamic::Visitor::visit(const NullaryOperator& x) {
43
visit(as<FunctionBase>(x));
44
}
45
46
void Dynamic::Visitor::visit(const UnaryOperator& x) {
47
visit(as<FunctionBase>(x));
48
}
49
50
void Dynamic::Visitor::visit(const BinaryOperator& x) {
51
visit(as<FunctionBase>(x));
52
}
53
54
void FunctionBase::accept(Visitor& v) const { v.visit(*this); }
55
56
// -- Environement
57
Environment::Environment() = default;
58
59
Environment::~Environment() {
60
// Restore value of special variables bound in this environment.
61
const auto end = dynamic.rend();
62
for (auto p = dynamic.rbegin(); p != end; ++p)
63
p->symbol->value = p->value;
64
}
65
66
Environment::Binding*
67
Environment::lookup(InternedString name) {
68
for (auto& b : lexical) {
69
if (b.symbol->name == name)
70
return &b;
71
}
72
return nullptr;
73
}
74
75
// -- Dynamic
76
Dynamic::~Dynamic() = default;
77
78
// -- Symbol
79
Symbol::Symbol(InternedString s)
80
: name(s),
81
value(),
82
function(),
83
properties(),
84
package(),
85
attributes()
86
{ }
87
88
void Symbol::accept(Visitor& v) const { v.visit(*this); }
89
90
// -- Binding
91
void Binding::accept(Visitor& v) const { v.visit(*this); }
92
93
// -- Package
94
Package::Package(InternedString s)
95
: name(s)
96
{ }
97
98
Symbol*
99
Package::make_symbol(InternedString s) {
100
auto sym = const_cast<Symbol*>(&*symbols.insert(Symbol(s)).first);
101
sym->package = this;
102
return sym;
103
}
104
105
Symbol*
106
Package::find_symbol(InternedString s) {
107
auto p = symbols.find(Symbol(s));
108
return p == symbols.end() ? nullptr : const_cast<Symbol*>(&*p);
109
}
110
111
void Package::accept(Visitor& v) const { v.visit(*this); }
112
113
Fixnum
114
count_nodes(Pair p) {
115
FixnumBits n = 1;
116
for (; auto q = to_pair_if_can(p->tail); p = q)
117
++n;
118
return Fixnum(n);
119
}
120
121
122
// -- BasicContext --
123
Package*
124
BasicContext::make_package(InternedString n) {
125
auto p = &*packages.insert(Package(n)).first;
126
return const_cast<Package*>(p);
127
}
128
129
Symbol*
130
BasicContext::make_keyword(InternedString n) {
131
auto sym = keyword_package()->make_symbol(n);
132
sym->value = to_value(sym);
133
sym->attributes = SymbolAttribute::Keyword;
134
return sym;
135
}
136
137
Pair BasicContext::make_pair(Value h, Value t) {
138
return conses.make(h, t);
139
}
140
141
const NullaryOperator*
142
BasicContext::make_operator(Symbol* n, NullaryCode c) {
143
return setf_symbol_function(n, nullaries.make(n, c));
144
}
145
146
const UnaryOperator*
147
BasicContext::make_operator(Symbol* n, UnaryCode c) {
148
return setf_symbol_function(n, unaries.make(n, c));
149
}
150
151
const BinaryOperator*
152
BasicContext::make_operator(Symbol* n, BinaryCode c) {
153
return setf_symbol_function(n, binaries.make(n, c));
154
}
155
156
const TernaryOperator*
157
BasicContext::make_operator(Symbol* n, TernaryCode c) {
158
return setf_symbol_function(n, ternaries.make(n, c));
159
}
160
161
BasicContext::BasicContext()
162
: keywords(make_package(intern("KEYWORD"))),
163
homeless(make_package(nullptr))
164
{
165
}
166
167
BasicContext::~BasicContext() {
168
}
169
}
170
}
171
172