aboutsummaryrefslogtreecommitdiff
path: root/src/include
diff options
context:
space:
mode:
authordos-reis <gdr@axiomatics.org>2014-11-16 20:32:55 +0000
committerdos-reis <gdr@axiomatics.org>2014-11-16 20:32:55 +0000
commit452696064e43c23f2a44edcded311f3d7b466d7e (patch)
tree375ea3690ca7ea469675313cae728a670dd26b09 /src/include
parent564aff97f80abac84be64552f5238903cb126c33 (diff)
downloadopen-axiom-452696064e43c23f2a44edcded311f3d7b466d7e.tar.gz
Check for LLVM framework
Diffstat (limited to 'src/include')
-rw-r--r--src/include/Lisp.H13
-rw-r--r--src/include/sexpr.H17
-rw-r--r--src/include/vm.H91
3 files changed, 107 insertions, 14 deletions
diff --git a/src/include/Lisp.H b/src/include/Lisp.H
index 119d07f8..a189dd38 100644
--- a/src/include/Lisp.H
+++ b/src/include/Lisp.H
@@ -78,6 +78,16 @@ namespace OpenAxiom {
struct IntegerOverflow : Diagnostics::BasicError {
explicit IntegerOverflow(const std::string&);
};
+
+ // -- Unbound symbol
+ struct UnboundSymbol : Diagnostics::BasicError {
+ explicit UnboundSymbol(const std::string&);
+ };
+
+ // -- Unbound functiom symbol
+ struct UnboundFunctionSymbol : Diagnostics::BasicError {
+ explicit UnboundFunctionSymbol(const std::string&);
+ };
// -- Anchor maps
using AnchorTable = std::map<Ordinal, Value>;
@@ -85,14 +95,17 @@ namespace OpenAxiom {
// -- Evaluator --
struct Evaluator : VM::BasicContext {
Evaluator();
+ Value eval(const Sexpr::Syntax*);
Package* core_package() { return core; }
Package* current_package() { return ns; }
Value toplevel_form(const Sexpr::Syntax*);
Value make_value(const Sexpr::Syntax*);
+ Value* lexical_binding(String);
Environment* global_environment();
private:
Package* core;
Package* ns;
+ Symbol* feature_list;
std::list<Environment> env_stack;
AnchorTable anchor_map;
};
diff --git a/src/include/sexpr.H b/src/include/sexpr.H
index d3dafcb7..82dba2a3 100644
--- a/src/include/sexpr.H
+++ b/src/include/sexpr.H
@@ -146,6 +146,10 @@ namespace OpenAxiom {
};
SymbolSyntax(const Lexeme&, Kind);
Kind kind() const { return sort; }
+ const Byte* begin() const { return lexeme().begin(); }
+ const Byte* end() const { return lexeme().end(); }
+ std::size_t size() const { return lexeme().size(); }
+ Byte operator[](std::size_t i) const { return begin()[i]; }
void accept(Visitor&) const;
private:
const Kind sort;
@@ -386,19 +390,24 @@ namespace OpenAxiom {
};
// -- Reader --
+ struct RawInput {
+ const Byte* start;
+ const Byte* end;
+ Ordinal lineno;
+ };
+
struct Reader {
struct State {
- const Byte* start;
- const Byte* end;
+ RawInput bytes;
const Byte* cur;
const Byte* line;
- Ordinal lineno;
Allocator alloc;
};
+ explicit Reader(const RawInput&);
Reader(const Byte*, const Byte*);
const Byte* position(Ordinal);
- bool at_start() const { return st.cur == st.start; }
+ bool at_start() const { return st.cur == st.bytes.start; }
const Syntax* read();
private:
State st;
diff --git a/src/include/vm.H b/src/include/vm.H
index 9337955b..2c1eafde 100644
--- a/src/include/vm.H
+++ b/src/include/vm.H
@@ -163,14 +163,22 @@ namespace OpenAxiom {
t = 0x10, // distinguished T value
};
- constexpr bool to_bool(Value v) {
- return v != Value::nil;
+ // -- Testing for nil value.
+ constexpr Value null(Value v) {
+ return v == Value::nil ? Value::t : Value::nil;
}
+ // -- Convert VM Boolean value to C++ view
+ constexpr bool to_bool(Value v) { return v != Value::nil; }
+
+ // -- Convert a C++ Boolean value to VM view.
constexpr Value to_value(bool b) {
return b ? Value::t : Value::nil;
}
+ // -- Identity equality.
+ constexpr Value eq(Value x, Value y) { return to_value(x == y); }
+
template<typename>
struct ValueTrait {
};
@@ -194,12 +202,23 @@ namespace OpenAxiom {
return ValueBits(v) & ~ValueTrait<T>::tag_mask;
}
+ // -- Arity: number of arguments or forms taken by a function
+ // or a special operator.
+ enum class Arity : intptr_t {
+ variable = -1, // Any number of arguments.
+ zero = 0, // Exactly no argument.
+ one = 1, // Exactly one argument.
+ two = 2, // Exactly two arguments.
+ three = 3, // Exactly three arguments.
+ };
+
// -------------
// -- Dynamic --
// -------------
// Any internal value is of a class derived from this.
internal_type Dynamic {
virtual ~Dynamic();
+ virtual void format_on(std::ostream&) const = 0;
};
template<>
@@ -324,10 +343,19 @@ namespace OpenAxiom {
return Value(ValueBits(p) | ValueTrait<Pair>::tag);
}
+ // Return true if argument designates a pair.
+ constexpr Value consp(Value v) {
+ return to_value(v != Value::nil and v != Value::t and is<Pair>(v));
+ }
+
+ inline Value atom(Value v) {
+ return null(consp(v));
+ }
+
// If `v' designates a pair, return a pointer to its
// concrete representation.
inline Pair to_pair_if_can(Value v) {
- return is<Pair>(v) ? to_pair(v) : nullptr;
+ return consp(v) == Value::t ? to_pair(v) : nullptr;
}
Fixnum count_nodes(Pair);
@@ -368,14 +396,19 @@ namespace OpenAxiom {
struct Package;
- enum class SymbolAttribute {
+ enum class SymbolAttribute : ValueBits {
None = 0x0, // No particular attribute.
Constant = 0x1, // Symbol defined constant.
- SpecialBinding = 0x2, // Symbol declared special.
+ Special = 0x2, // Symbol declared special.
Keyword = 0x4, // A keyword symbol.
- SpecialConstant = Constant | SpecialBinding,
+ SpecialConstant = Constant | Special,
};
+ constexpr SymbolAttribute
+ operator&(SymbolAttribute x, SymbolAttribute y) {
+ return SymbolAttribute(ValueBits(x) & ValueBits(y));
+ }
+
// ------------
// -- Symbol --
// ------------
@@ -387,6 +420,8 @@ namespace OpenAxiom {
Package* package;
SymbolAttribute attributes;
explicit Symbol(InternedString);
+ void format_on(std::ostream&) const override;
+ bool has(SymbolAttribute x) const { return (attributes & x) == x; }
};
inline Symbol* to_symbol_if_can(Value v) {
@@ -397,6 +432,18 @@ namespace OpenAxiom {
return to_symbol_if_can(v) != nullptr;
}
+ // -- 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));
+ }
+
+ // -- Test if a value is a keyword symbol.
+ inline Value keywordp(Value v) {
+ if (auto sym = to_symbol_if_can(v))
+ return to_value(sym->has(SymbolAttribute::Keyword));
+ return Value::nil;
+ }
+
struct CmpByName {
template<typename T>
bool operator()(const T& x, const T& y) const {
@@ -421,6 +468,7 @@ namespace OpenAxiom {
~Environment();
void bind(Symbol*, Value);
+ Binding* lookup(InternedString);
private:
std::vector<Binding> lexical;
std::vector<Binding> dynamic;
@@ -434,7 +482,9 @@ namespace OpenAxiom {
std::set<Symbol, CmpByName> symbols;
explicit Package(InternedString);
+ void format_on(std::ostream&) const override;
Symbol* make_symbol(InternedString);
+ Symbol* find_symbol(InternedString);
};
// --------------
@@ -445,6 +495,7 @@ namespace OpenAxiom {
Value type;
FunctionBase(const Symbol* n, Value t = Value::nil)
: name(n), type(t) { }
+ void format_on(std::ostream&) const override;
};
// ------------------------
@@ -452,10 +503,13 @@ namespace OpenAxiom {
// ------------------------
// Types for native implementation of builtin operators.
struct BasicContext;
- using NullaryCode = Value (*)(BasicContext*);
- using UnaryCode = Value (*)(BasicContext*, Value);
- using BinaryCode = Value (*)(BasicContext*, Value, Value);
- using TernaryCode = Value (*)(BasicContext*, Value, Value, Value);
+
+ template<typename... Ts>
+ using RuntimeOperation = Value(*)(BasicContext*, Ts...);
+ using NullaryCode = RuntimeOperation<>;
+ using UnaryCode = RuntimeOperation<Value>;
+ using BinaryCode = RuntimeOperation<Value, Value>;
+ using TernaryCode = RuntimeOperation<Value, Value, Value>;
template<typename Code>
struct BuiltinFunction : FunctionBase {
@@ -470,6 +524,23 @@ namespace OpenAxiom {
using BinaryOperator = BuiltinFunction<BinaryCode>;
using TernaryOperator = BuiltinFunction<TernaryCode>;
+ // -- Operand stack.
+ struct OperandStack : private std::vector<Value> {
+ using super = std::vector<Value>;
+ using iterator = std::reverse_iterator<super::iterator>;
+ using super::size;
+ using super::empty;
+ iterator begin() { return rbegin(); }
+ iterator end() { return rend(); }
+ Value top() { return back(); }
+ void push(Value v) { push_back(v); }
+ Value pop() { auto v = back(); pop_back(); return v; }
+ void operator-=(std::size_t i) { resize(size() - i); }
+ Value operator[](std::size_t i) {
+ return super::operator[](size() - i - 1);
+ }
+ };
+
// ------------------
// -- BasicContext --
// ------------------