diff options
author | dos-reis <gdr@axiomatics.org> | 2013-06-28 05:02:14 +0000 |
---|---|---|
committer | dos-reis <gdr@axiomatics.org> | 2013-06-28 05:02:14 +0000 |
commit | 50ec7502934e4632348f4c3c8e060181f199dfc8 (patch) | |
tree | cd60fa76d27d654344f093a326222ff21685701b /src/utils | |
parent | b52f0164b18f06db386d527be26e3a11deb1ab7d (diff) | |
download | open-axiom-50ec7502934e4632348f4c3c8e060181f199dfc8.tar.gz |
Add src/rt for core runtime support.
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/Lisp.cc | 264 | ||||
-rw-r--r-- | src/utils/Makefile.in | 2 | ||||
-rw-r--r-- | src/utils/vm.cc | 89 |
3 files changed, 1 insertions, 354 deletions
diff --git a/src/utils/Lisp.cc b/src/utils/Lisp.cc deleted file mode 100644 index c4533ab9..00000000 --- a/src/utils/Lisp.cc +++ /dev/null @@ -1,264 +0,0 @@ -// Copyright (C) 2013, Gabriel Dos Reis. -// All rights reserved. -// Written by Gabriel Dos Reis. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// - Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// -// - Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in -// the documentation and/or other materials provided with the -// distribution. -// -// - Neither the name of OpenAxiom nor the names of its contributors -// may be used to endorse or promote products derived from this -// software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER -// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#include <open-axiom/Lisp> -#include <typeinfo> -#include <ostream> -#include <sstream> - -namespace OpenAxiom { - namespace Lisp { - Unimplemented::Unimplemented(const std::string& s) - : BasicError(s) - { } - - IntegerOverflow::IntegerOverflow(const std::string& s) - : BasicError(s) - { } - - std::string - show(Value v) { - std::ostringstream os; - format(v, os); - return os.str(); - } - - Fixnum - retract_to_fixnum(Value v) { - if (not is_fixnum(v)) - throw Diagnostics::BasicError(show(v) + " is not a fixnum"); - return to_fixnum(v); - } - - Pair - retract_to_pair(Value v) { - if (not is_pair(v)) - throw Diagnostics::BasicError(show(v) + " is not a pair"); - return to_pair(v); - } - - - static void - unimplemented(const Sexpr::Syntax& x) { - std::string s = "unimplemented eval for "; - throw Unimplemented{ s + typeid(x).name() }; - } - - static void - integer_too_large(const Sexpr::IntegerSyntax& x) { - std::string s { x.lexeme().begin(), x.lexeme().end() }; - throw IntegerOverflow{ s + " is too large for Fixnum; max value is " - + std::to_string(fixnum_maximum) }; - } - - constexpr auto fixmax_by_ten = fixnum_maximum / 10; - constexpr auto fixmax_lsd = fixnum_maximum % 10; - - static Value - construct(Evaluator* ctx, const Sexpr::IntegerSyntax& x) { - bool neg = false; - auto cur = x.lexeme().begin(); - Fixnum val = 0; - switch (*cur) { - case '-': neg = true; - case '+': ++cur; - default: - for (; cur < x.lexeme().end(); ++cur) { - auto d = *cur - '0'; - if (val < fixmax_by_ten) - val = 10 * val + d; - else if (val > fixmax_by_ten or d > fixmax_lsd) - integer_too_large(x); - else - val = 10 * val + d; - } - if (neg) { - if (val > fixnum_maximum) - integer_too_large(x); - val = -val; - } - } - return VM::from_fixnum(val); - } - - static Value - construct(Evaluator* ctx, const Sexpr::ListSyntax& x) { - if (x.empty()) - return nil; - auto result = nil; - auto p = x.rbegin(); - if (x.dotted()) - result = ctx->make_value(*p++); - while (p != x.rend()) - result = from_pair(ctx->make_pair(ctx->make_value(*p++), result)); - return result; - } - - static Value - construct(Evaluator* ctx, const Sexpr::StringSyntax& x) { - auto s = ctx->intern(x.lexeme().begin(), x.lexeme().size()); - return from_string(s); - } - - static Value - construct(Evaluator* ctx, const Sexpr::SymbolSyntax& x) { - auto s = ctx->intern(x.lexeme().begin(), x.lexeme().size()); - switch (x.kind()) { - case Sexpr::SymbolSyntax::uninterned: - return from_symbol(ctx->make_symbol(s, nullptr)); - - case Sexpr::SymbolSyntax::keyword: - return from_symbol(ctx->make_symbol(s, ctx->keyword_namespace())); - - default: - return from_symbol(ctx->make_symbol(s, ctx->active_namespace())); - } - } - - VM::Value - Evaluator::make_value(const Sexpr::Syntax* x) { - using namespace Sexpr; - struct V : Sexpr::Syntax::Visitor { - Evaluator* ctx; - Value result; - V(Evaluator* e) : ctx(e), result(nil) { } - void visit(const IntegerSyntax& x) { result = construct(ctx, x); } - void visit(const CharacterSyntax& x) { unimplemented(x); } - void visit(const StringSyntax& x) { result = construct(ctx, x); } - void visit(const SymbolSyntax& x) { result = construct(ctx, x); } - void visit(const ReferenceSyntax& x) { - auto p = ctx->anchor_map.find(x.tag()); - if (p == ctx->anchor_map.end()) - throw Diagnostics::BasicError{ "undefined anchor " - + std::to_string(x.tag()) - }; - result = p->second; - } - void visit(const AnchorSyntax& x) { - auto& v = ctx->anchor_map[x.ref()]; - if (v != nil) - throw Diagnostics::BasicError{ - "duplicate anchor " + std::to_string(x.ref()) - }; - result = v = ctx->make_value(x.value()); - } - void visit(const QuoteSyntax& x) { unimplemented(x); } - void visit(const AntiquoteSyntax& x) { unimplemented(x); } - void visit(const Expand& x) { unimplemented(x); } - void visit(const Eval& x) { unimplemented(x); } - void visit(const Splice& x) { unimplemented(x); } - void visit(const Function& x) { unimplemented(x); } - void visit(const Include& x) { unimplemented(x); } - void visit(const Exclude& x) { unimplemented(x); } - void visit(const ListSyntax& x) { result = construct(ctx, x); } - void visit(const VectorSyntax& x) { unimplemented(x); } - }; - - if (x == nullptr) - return nil; - V v { this }; - x->accept(v); - return v.result; - } - - Value - Evaluator::toplevel_form(const Sexpr::Syntax* x) { - auto anchors = std::move(anchor_map); - anchor_map = AnchorTable{ }; - auto v = make_value(x); - anchor_map = std::move(anchors); - return v; - } - - Evaluator::Evaluator() : keys(intern("KEYWORD")), ns() { - env_stack.push_back(Environment{ }); - } - - Environment* - Evaluator::global_environment() { - return &env_stack.front(); - } - - - // -- Formatting - - static void format(Pair p, std::ostream& os) { - os << '('; - while (true) { - format(p->head, os); - auto v = p->tail; - if (v == nil) - break; - os << ' '; - if (auto q = to_pair_if_can(v)) { - p = q; - continue; - } - os << '.' << ' '; - format(v, os); - break; - } - os << ')'; - } - - static void format(String s, std::ostream& os) { - os << '"'; - for (auto c : *s) { - if (c == '"') - os << '\\'; - os << char(c); - } - os << '"'; - } - - static void format(const Symbol* s, std::ostream& os) { - // FIXME: Handle escapes. - auto n = s->name(); - std::copy(n->begin(), n->end(), std::ostream_iterator<char>(os)); - } - - void format(Value v, std::ostream& os) { - if (v == nil) - os << "NIL"; - else if (is_fixnum(v)) - os << to_fixnum(v); - else if (auto p = to_pair_if_can(v)) - format(p, os); - else if (auto s = to_string_if_can(v)) - format(s, os); - else if (auto s = to_symbol_if_can(v)) - format(s, os); - else - os << "<unprintable>"; - } - } -} diff --git a/src/utils/Makefile.in b/src/utils/Makefile.in index 758c1ae9..354d0933 100644 --- a/src/utils/Makefile.in +++ b/src/utils/Makefile.in @@ -39,7 +39,7 @@ hammer_LDADD = -L. -lOpenAxiom libOpenAxiom_HEADERS = hash-table.H string-pool.H libOpenAxiom_SOURCES = \ storage.cc string-pool.cc command.cc \ - filesystem.cc vm.cc Lisp.cc + filesystem.cc libOpenAxiom_OBJECTS = $(libOpenAxiom_SOURCES:.cc=.lo) diff --git a/src/utils/vm.cc b/src/utils/vm.cc deleted file mode 100644 index 2fe4da1c..00000000 --- a/src/utils/vm.cc +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (C) 2011-2013, Gabriel Dos Reis. -// All rights reserved. -// Written by Gabriel Dos Reis. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// - Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// -// - Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in -// the documentation and/or other materials provided with the -// distribution. -// -// - Neither the name of OpenAxiom nor the names of its contributors -// may be used to endorse or promote products derived from this -// software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER -// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// --% Author: Gabriel Dos Reis - -#include <open-axiom/vm> - -namespace OpenAxiom { - namespace VM { - Dynamic::~Dynamic() { } - - Symbol::Symbol(String n, Scope* s) - : std::pair<String, Scope*>(n, s) - { } - - Fixnum - count_nodes(Pair p) { - Fixnum n = 1; - for (; auto q = to_pair_if_can(p->tail); p = q) - ++n; - return n; - } - - // -- BasicContext -- - Pair BasicContext::make_pair(Value h, Value t) { - return conses.make(h, t); - } - - const Symbol* - BasicContext::make_symbol(String n, Scope* s) { - return &*syms.insert({ n, s }).first; - } - - const NullaryOperator* - BasicContext::make_operator(Symbol n, NullaryCode c) { - return nullaries.make(n, c); - } - - const UnaryOperator* - BasicContext::make_operator(Symbol n, UnaryCode c) { - return unaries.make(n, c); - } - - const BinaryOperator* - BasicContext::make_operator(Symbol n, BinaryCode c) { - return binaries.make(n, c); - } - - const TernaryOperator* - BasicContext::make_operator(Symbol n, TernaryCode c) { - return ternaries.make(n, c); - } - - BasicContext::BasicContext() { - } - - BasicContext::~BasicContext() { - } - } -} |