diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/include/open-axiom/vm | 67 | ||||
-rw-r--r-- | src/rt/Lisp.cc | 22 | ||||
-rw-r--r-- | src/rt/vm.cc | 13 |
3 files changed, 48 insertions, 54 deletions
diff --git a/src/include/open-axiom/vm b/src/include/open-axiom/vm index f7862249..301c6b2d 100644 --- a/src/include/open-axiom/vm +++ b/src/include/open-axiom/vm @@ -174,7 +174,8 @@ namespace OpenAxiom { constexpr bool to_bool(Value v) { return v != Value::nil; } // -- Convert a C++ Boolean value to VM view. - constexpr Value to_value(bool b) { + template<std::same_as<bool> T> + constexpr Value to_value(T b) { return b ? Value::t : Value::nil; } @@ -214,13 +215,13 @@ namespace OpenAxiom { three = 3, // Exactly three arguments. }; - // ------------- - // -- Dynamic -- - // ------------- + // ----------- + // -- Boxed -- + // ----------- // Any internal value is of a class derived from this. - internal_type Dynamic { + internal_type Boxed { struct Visitor; - virtual ~Dynamic(); + virtual ~Boxed() = default; virtual void accept(Visitor&) const = 0; }; @@ -230,26 +231,26 @@ namespace OpenAxiom { inline const S& as(const T& t) { return t; } template<> - struct ValueTrait<Dynamic> { + struct ValueTrait<Boxed> { enum Tag : ValueBits { tag = 0x6 }; enum Mask : ValueBits { tag_mask = 0xF }; }; - inline Dynamic* to_dynamic(Value v) { - return reinterpret_cast<Dynamic*>(native<Dynamic>(v)); + inline Boxed* to_boxed(Value v) { + return reinterpret_cast<Boxed*>(native<Boxed>(v)); } - inline Dynamic* to_dynamic_if_can(Value v) { - return is<Dynamic>(v) ? to_dynamic(v) : nullptr; + inline Boxed* if_boxed(Value v) { + return is<Boxed>(v) ? to_boxed(v) : nullptr; } - template<std::derived_from<Dynamic> T> + template<std::derived_from<Boxed> T> inline Value to_value(const T* o) { - return Value(ValueBits(o) | ValueTrait<Dynamic>::tag); + return Value(ValueBits(o) | ValueTrait<Boxed>::tag); } // -- Callable -- - struct Callable : Dynamic { + struct Callable : Boxed { }; // ------------- @@ -276,7 +277,7 @@ namespace OpenAxiom { return Fixnum(FixnumBits(v) >> 1); } - constexpr Value from_fixnum(Fixnum i) { + constexpr Value to_value(Fixnum i) { return Value((ValueBits(i) << 1 ) | ValueTrait<Fixnum>::tag); } @@ -295,11 +296,11 @@ namespace OpenAxiom { return reinterpret_cast<String>(native<String>(v)); } - inline Value from_string(InternedString s) { + inline Value to_value(InternedString s) { return Value(ValueBits(s) | ValueTrait<String>::tag); } - inline InternedString to_string_if_can(Value v) { + inline InternedString if_string(Value v) { return is<String>(v) ? to_string(v) : nullptr; } @@ -319,7 +320,7 @@ namespace OpenAxiom { return Pointer(ValueBits(v)); } - inline Value from_pointer(Pointer p) { + inline Value to_value(Pointer p) { return Value(ValueBits(p) | ValueTrait<Memory::Pointer>::tag); } @@ -343,7 +344,7 @@ namespace OpenAxiom { return reinterpret_cast<Pair>(native<Pair>(v)); } - inline Value from_pair(Pair p) { + inline Value to_value(Pair p) { return Value(ValueBits(p) | ValueTrait<Pair>::tag); } @@ -358,13 +359,13 @@ namespace OpenAxiom { // If `v' designates a pair, return a pointer to its // concrete representation. - inline Pair to_pair_if_can(Value v) { + inline Pair if_pair(Value v) { return consp(v) == Value::t ? to_pair(v) : nullptr; } Fixnum count_nodes(Pair); inline Fixnum count_nodes(Value v) { - if (auto p = to_pair_if_can(v)) + if (auto p = if_pair(v)) return count_nodes(p); return Fixnum::zero; } @@ -386,7 +387,7 @@ namespace OpenAxiom { return Character(ValueBits(v) >> 4); } - constexpr Value from_character(Character c) { + constexpr Value to_value(Character c) { return Value((ValueBits(c) << 4) | ValueTrait<Character>::tag); } @@ -416,7 +417,7 @@ namespace OpenAxiom { // ------------ // -- Symbol -- // ------------ - struct Symbol : Dynamic { + struct Symbol : Boxed { const InternedString name; Value value; const Callable* function; @@ -428,22 +429,18 @@ namespace OpenAxiom { bool has(SymbolAttribute x) const { return (attributes & x) == x; } }; - inline Symbol* to_symbol_if_can(Value v) { - return dynamic_cast<Symbol*>(to_dynamic_if_can(v)); - } - - inline bool is_symbol(Value v) { - return to_symbol_if_can(v) != nullptr; + inline Symbol* if_symbol(Value v) { + return dynamic_cast<Symbol*>(if_boxed(v)); } // -- Test if a value is a symbol. inline Value symbolp(Value v) { - return to_value(v == Value::nil or v == Value::t or is_symbol(v)); + return to_value(v == Value::nil or v == Value::t or if_symbol(v) != nullptr); } // -- Test if a value is a keyword symbol. inline Value keywordp(Value v) { - if (auto sym = to_symbol_if_can(v)) + if (auto sym = if_symbol(v)) return to_value(sym->has(SymbolAttribute::Keyword)); return Value::nil; } @@ -463,7 +460,7 @@ namespace OpenAxiom { // -- Argument binding as value. // Binding a parameter to a value in a call. - struct Binding : Dynamic { + struct Binding : Boxed { Symbol* symbol; Value value; void accept(Visitor&) const override; @@ -489,7 +486,7 @@ namespace OpenAxiom { // ------------- // -- Package -- // ------------- - struct Package : Dynamic { + struct Package : Boxed { const InternedString name; std::set<Symbol, CmpByName> symbols; @@ -554,8 +551,8 @@ namespace OpenAxiom { } }; - // -- Dynamic::Visitor -- - struct Dynamic::Visitor { + // -- Boxed::Visitor -- + struct Boxed::Visitor { virtual void visit(const Symbol&) = 0; virtual void visit(const Binding&) = 0; virtual void visit(const Package&) = 0; diff --git a/src/rt/Lisp.cc b/src/rt/Lisp.cc index 71b3c2b1..fae3e02f 100644 --- a/src/rt/Lisp.cc +++ b/src/rt/Lisp.cc @@ -68,8 +68,8 @@ namespace OpenAxiom { constexpr NamedConstant<Value> value_constants[] = { { "NIL", Value::nil }, { "T", Value::t }, - { "MOST-NEGATIVE-FIXNUM", from_fixnum(Fixnum::minimum) }, - { "MOST-POSITIVE-FIXNUM", from_fixnum(Fixnum::maximum) }, + { "MOST-NEGATIVE-FIXNUM", to_value(Fixnum::minimum) }, + { "MOST-POSITIVE-FIXNUM", to_value(Fixnum::maximum) }, }; static void define_special_constants(Evaluator* ctx) { @@ -151,7 +151,7 @@ namespace OpenAxiom { val = -val; } } - return VM::from_fixnum(Fixnum(val)); + return VM::to_value(Fixnum(val)); } static Value @@ -163,14 +163,14 @@ namespace OpenAxiom { if (x.dotted()) result = ctx->make_value(*p++); while (p != x.rend()) - result = from_pair(ctx->make_pair(ctx->make_value(*p++), result)); + result = to_value(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); + return to_value(s); } static Value @@ -457,7 +457,7 @@ namespace OpenAxiom { if (v == Value::nil) break; os << ' '; - if (auto q = to_pair_if_can(v)) { + if (auto q = if_pair(v)) { p = q; continue; } @@ -478,7 +478,7 @@ namespace OpenAxiom { os << '"'; } - static void format(const Dynamic*, std::ostream&); + static void format(const Boxed*, std::ostream&); void format(Value v, std::ostream& os) { if (v == Value::nil) @@ -491,14 +491,14 @@ namespace OpenAxiom { format(to_pair(v), os); else if (is<String>(v)) format(to_string(v), os); - else if (is<Dynamic>(v)) - format(to_dynamic(v), os); + else if (is<Boxed>(v)) + format(to_boxed(v), os); else os << "#<unprintable>"; } - static void format(const Dynamic* x, std::ostream& os) { - struct V : Dynamic::Visitor { + static void format(const Boxed* x, std::ostream& os) { + struct V : Boxed::Visitor { std::ostream& os; V(std::ostream& s) : os(s) { } void visit(const Symbol& s) { diff --git a/src/rt/vm.cc b/src/rt/vm.cc index b099fa70..dbeb7fd0 100644 --- a/src/rt/vm.cc +++ b/src/rt/vm.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2014, Gabriel Dos Reis. +// Copyright (C) 2011-2022, Gabriel Dos Reis. // All rights reserved. // Written by Gabriel Dos Reis. // @@ -39,15 +39,15 @@ namespace OpenAxiom { namespace VM { - void Dynamic::Visitor::visit(const NullaryOperator& x) { + void Boxed::Visitor::visit(const NullaryOperator& x) { visit(as<FunctionBase>(x)); } - void Dynamic::Visitor::visit(const UnaryOperator& x) { + void Boxed::Visitor::visit(const UnaryOperator& x) { visit(as<FunctionBase>(x)); } - void Dynamic::Visitor::visit(const BinaryOperator& x) { + void Boxed::Visitor::visit(const BinaryOperator& x) { visit(as<FunctionBase>(x)); } @@ -72,9 +72,6 @@ namespace OpenAxiom { return nullptr; } - // -- Dynamic - Dynamic::~Dynamic() = default; - // -- Symbol Symbol::Symbol(InternedString s) : name(s), @@ -113,7 +110,7 @@ namespace OpenAxiom { Fixnum count_nodes(Pair p) { FixnumBits n = 1; - for (; auto q = to_pair_if_can(p->tail); p = q) + for (; auto q = if_pair(p->tail); p = q) ++n; return Fixnum(n); } |