diff --git a/src/arena.cpp b/src/arena.cpp index 9383a28..4388331 100644 --- a/src/arena.cpp +++ b/src/arena.cpp @@ -69,6 +69,8 @@ Result Arena::gc_pod(PodObject* obj) { return gc_string((PodString*)obj); case Tag::Symbol: return gc_symbol((PodSymbol*)obj); + case Tag::SrcLoc: + return gc_srcloc((PodSrcLoc*)obj); case Tag::Syntax: return gc_syntax((PodSyntax*)obj); case Tag::Pair: @@ -137,13 +139,23 @@ Result Arena::gc_symbol(PodSymbol* obj) { return nobj; } +Result Arena::gc_srcloc(PodSrcLoc* obj) { + auto nobj = TRY(alloc()); + + nobj->header.tag = Tag::SrcLoc; + + nobj->sourcerange = obj->sourcerange; + + return nobj; +} + Result Arena::gc_syntax(PodSyntax* obj) { auto nobj = TRY(alloc()); nobj->header.tag = Tag::Syntax; nobj->filename = TRY(gc_pod(obj->filename.get())); - nobj->sourcerange = obj->sourcerange; + nobj->srcloc = TRY(gc_pod(obj->srcloc.get())); nobj->expression = TRY(gc_pod(obj->expression.get())); return nobj; diff --git a/src/arena.hpp b/src/arena.hpp index 9fa736f..6270020 100644 --- a/src/arena.hpp +++ b/src/arena.hpp @@ -149,6 +149,7 @@ class Arena { Result gc_float(PodFloat* obj); Result gc_string(PodString* obj); Result gc_symbol(PodSymbol* obj); + Result gc_srcloc(PodSrcLoc* obj); Result gc_syntax(PodSyntax* obj); Result gc_pair(PodPair* obj); Result gc_array(PodArray* obj); diff --git a/src/common.cpp b/src/common.cpp index 75d0f4b..f7c5b8d 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -28,6 +28,8 @@ Result Value::create(PodObject* obj) { return Value(TRY(String::create((PodString*)obj))); case Tag::Symbol: return Value(TRY(Symbol::create((PodSymbol*)obj))); + case Tag::SrcLoc: + return Value(TRY(SrcLoc::create((PodSrcLoc*)obj))); case Tag::Syntax: return Value(TRY(Syntax::create((PodSyntax*)obj))); case Tag::Pair: @@ -77,15 +79,12 @@ Result ByteArray::create(const String& str) { return ByteArray(TRY(MkGcRoot(pod))); } -Result Syntax::create(String filename, SourceRange sourcerange, - Value expression) { - auto pod = TRY(arena_alloc()); - pod->header.tag = Tag::Syntax; - pod->filename = filename.pod(); +Result SrcLoc::create(const SourceRange& sourcerange) { + auto pod = TRY(arena_alloc()); + pod->header.tag = Tag::SrcLoc; pod->sourcerange = sourcerange; - pod->expression = expression.pod(); - return Syntax(TRY(MkGcRoot(pod))); + return SrcLoc(TRY(MkGcRoot(pod))); } short sourceposition_cmp(const SourcePosition& lhs, const SourcePosition& rhs) { @@ -103,11 +102,32 @@ short sourcerange_cmp(const SourceRange& lhs, const SourceRange rhs) { return sourceposition_cmp(lhs.end, rhs.end); } +SourceRange SrcLoc::sourcerange() const { return _value->sourcerange; } + +Result SrcLoc::cmp(const SrcLoc& rhs) const { + auto lhs_sourcerange = sourcerange(); + auto rhs_sourcerange = rhs.sourcerange(); + return sourcerange_cmp(lhs_sourcerange, rhs_sourcerange); +} + +Result Syntax::create(String filename, SrcLoc srcloc, + Value expression) { + auto pod = TRY(arena_alloc()); + pod->header.tag = Tag::Syntax; + pod->filename = filename.pod(); + pod->srcloc = srcloc.pod(); + pod->expression = expression.pod(); + + return Syntax(TRY(MkGcRoot(pod))); +} + Result Syntax::filename() const { return Value::create(_value->filename.get()); } -SourceRange Syntax::sourcerange() const { return _value->sourcerange; } +Result Syntax::srcloc() const { + return Value::create(_value->srcloc.get()); +} Result Syntax::expression() const { return Value::create(_value->expression.get()); @@ -119,10 +139,10 @@ Result Syntax::cmp(const Syntax& rhs) const { short res = TRY(lhs_filename.cmp(rhs_filename)); if (res != 0) return res; - auto lhs_sourcerange = sourcerange(); - auto rhs_sourcerange = rhs.sourcerange(); - res = sourcerange_cmp(lhs_sourcerange, rhs_sourcerange); - if (res != 0) return res; + auto lhs_srcloc = TRY(srcloc()); + auto rhs_srcloc = TRY(rhs.srcloc()); + res = TRY(lhs_srcloc.cmp(rhs_srcloc)); + return res; auto lhs_expression = TRY(expression()); auto rhs_expression = TRY(rhs.expression()); @@ -173,6 +193,14 @@ Result Syntax::copy_value() const { return Value(Syntax(TRY(_value.copy()))); } +Result Syntax::copy() const { return Syntax(TRY(_value.copy())); } + +Result SrcLoc::copy_value() const { + return Value(SrcLoc(TRY(_value.copy()))); +} + +Result SrcLoc::copy() const { return SrcLoc(TRY(_value.copy())); } + Result Pair::copy_value() const { return Value(Pair(TRY(_value.copy()))); } diff --git a/src/common.hpp b/src/common.hpp index 99bb12a..d4d6d3f 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -22,6 +22,8 @@ class Float; class Bool; class String; class Symbol; +class SrcLoc; +class Symbol; class Syntax; class Pair; class Array; @@ -63,6 +65,9 @@ class Object { virtual Result cmp(const Symbol&) const { return cmp_tag(tag(), Tag::Symbol); } + virtual Result cmp(const SrcLoc&) const { + return cmp_tag(tag(), Tag::SrcLoc); + } virtual Result cmp(const Syntax&) const { return cmp_tag(tag(), Tag::Syntax); } @@ -636,12 +641,40 @@ class Symbol : public Object { GcRoot _value; }; +class SrcLoc : public Object { + public: + SrcLoc() {} + SrcLoc(SrcLoc&& rhs) : _value(std::move(rhs._value)) {} + SrcLoc(GcRoot&& val) : _value(std::move(val)) {} + static Result create(const SourceRange& sourcerange); + virtual Tag tag() const final { return Tag::SrcLoc; } + virtual PodObject* pod() const final { return _value.get(); } + virtual Result cmp(const Object& rhs) const final { + return -TRY(rhs.cmp(*this)); + } + virtual Result cmp(const SrcLoc& rhs) const final; + + virtual void move(Object* obj) final { new (obj) SrcLoc(std::move(_value)); } + + static Result create(PodSrcLoc* obj) { + return SrcLoc(TRY(MkGcRoot(obj))); + } + + SourceRange sourcerange() const; + + virtual Result copy_value() const final; + Result copy() const; + + private: + GcRoot _value; +}; + class Syntax : public Object { public: Syntax() {} Syntax(Syntax&& rhs) : _value(std::move(rhs._value)) {} Syntax(GcRoot&& val) : _value(std::move(val)) {} - static Result create(String filename, SourceRange sourcerange, + static Result create(String filename, SrcLoc srcloc, Value expression); virtual Tag tag() const final { return Tag::Syntax; } virtual PodObject* pod() const final { return _value.get(); } @@ -657,7 +690,7 @@ class Syntax : public Object { } Result filename() const; - SourceRange sourcerange() const; + Result srcloc() const; Result expression() const; virtual Result copy_value() const final; diff --git a/src/compiler.cpp b/src/compiler.cpp index 836e69b..68f8286 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -227,6 +227,7 @@ Result Compiler::compile_expr(Context& context, const Value& expr) { return compile_constant(context, expr); case Tag::Float: return compile_constant(context, expr); + case Tag::SrcLoc: case Tag::Syntax: case Tag::Array: case Tag::ByteArray: diff --git a/src/pod.hpp b/src/pod.hpp index f618dec..5054e02 100644 --- a/src/pod.hpp +++ b/src/pod.hpp @@ -13,6 +13,7 @@ enum class Tag : uint8_t { Bool, String, Symbol, + SrcLoc, Syntax, Pair, Array, @@ -132,11 +133,17 @@ class PodSymbol final : public PodObject { char32_t data[]; }; +class PodSrcLoc : public PodObject { + public: + PodSrcLoc() : PodObject(Tag::SrcLoc) {}; + SourceRange sourcerange; +}; + class PodSyntax : public PodObject { public: PodSyntax() : PodObject(Tag::Syntax) {}; OffPtr filename; - SourceRange sourcerange; + OffPtr srcloc; OffPtr expression; }; diff --git a/src/stdlib.cpp b/src/stdlib.cpp index 06b4e8a..b4d6c00 100644 --- a/src/stdlib.cpp +++ b/src/stdlib.cpp @@ -2,6 +2,7 @@ #include "common.hpp" #include "pod.hpp" +#include "sourcerange.hpp" #include "writer.hpp" typedef Result (*StdlibFunctionIdPtr)(const Array& params); @@ -86,6 +87,42 @@ Result stdlib_get(const Array& params) { return TRY(collection.get(key)); } +Result stdlib_srcloc(const Array& params) { + Array array_copy = TRY(params.copy()); + if (params.size() != 6) return ERROR(ArgumentCountMismatch); + + SourceRange sr; + for (uint64_t i = 0; i < 6; i++) { + auto val = TRY(array_copy.get(i)); + if (!val.is()) return ERROR(TypeMismatch); + int64_t intval = val.to()->value(); + switch (i) { + case 0: + sr.start.line = intval; + break; + case 1: + sr.start.column = intval; + break; + case 2: + sr.start.offset = intval; + break; + case 3: + sr.end.line = intval; + break; + case 4: + sr.end.column = intval; + break; + case 5: + sr.end.offset = intval; + break; + default: + break; + } + } + + return Value(TRY(SrcLoc::create(sr))); +} + #define STDLIB_FUNCTION(name, id) \ [(uint64_t)StdlibFunctionId::id] = {#name, StdlibFunctionId::id, \ stdlib_##name} @@ -100,6 +137,7 @@ static StdlibFunctionEntry function_entries[] = { STDLIB_FUNCTION(list, List), STDLIB_FUNCTION(array, Array), STDLIB_FUNCTION(get, Get), + STDLIB_FUNCTION(srcloc, SrcLoc), [(uint64_t)StdlibFunctionId::Max] = {0, StdlibFunctionId::Max, stdlib_unknown}, }; diff --git a/src/stdlib.hpp b/src/stdlib.hpp index fd3baab..0e963dc 100644 --- a/src/stdlib.hpp +++ b/src/stdlib.hpp @@ -14,6 +14,7 @@ enum class StdlibFunctionId : uint64_t { List, Array, Get, + SrcLoc, Max, }; diff --git a/src/valeri.cpp b/src/valeri.cpp index 443d864..29f7c04 100644 --- a/src/valeri.cpp +++ b/src/valeri.cpp @@ -40,7 +40,7 @@ Result run_repl() { globals = TRY(vm.globals()); if (!res.is()) { - debug_print(res); + TRY(debug_print(res)); } } diff --git a/src/writer.cpp b/src/writer.cpp index 5a58771..04fa00c 100644 --- a/src/writer.cpp +++ b/src/writer.cpp @@ -2,6 +2,7 @@ #include "error.hpp" #include "opcode.hpp" +#include "sourcerange.hpp" Result Writer::write_one(const Value& obj) { switch (obj.tag()) { @@ -23,6 +24,8 @@ Result Writer::write_one(const Value& obj) { return write_string(*obj.to()); case Tag::Symbol: return write_symbol(*obj.to()); + case Tag::SrcLoc: + return write_srcloc(*obj.to()); case Tag::Syntax: return write_syntax(*obj.to()); case Tag::Pair: @@ -179,6 +182,19 @@ Result Writer::write_symbol(const Symbol& val) { return res; } +Result Writer::write_srcloc(const SrcLoc& val) { + char buf[1024]; + String res = TRY(String::create("#")); + return res; +} + Result Writer::write_syntax(const Syntax& val) { return ERROR(NotImplemented); } diff --git a/src/writer.hpp b/src/writer.hpp index 9092bec..84e3e30 100644 --- a/src/writer.hpp +++ b/src/writer.hpp @@ -21,6 +21,7 @@ class Writer { Result write_bytearray(const ByteArray& val); Result write_dict(const Dict& val); Result write_symbol(const Symbol& val); + Result write_srcloc(const SrcLoc& val); Result write_syntax(const Syntax& val); Result write_opcode(const Opcode& val); Result write_function(const Function& val);