Slightly cleaner returns in stdlib
This commit is contained in:
parent
e2247d9bc8
commit
00c874957b
3 changed files with 28 additions and 64 deletions
|
@ -620,6 +620,19 @@ Result<StackFrame> StackFrame::ret(uint64_t regnum) const {
|
|||
return std::move(parent_stack);
|
||||
}
|
||||
|
||||
Result<StackFrame> StackFrame::ret(const Value& res) const {
|
||||
auto parent_frame = TRY(parent());
|
||||
if (parent_frame.is<Nil>()) {
|
||||
return ERROR(EndOfProgram);
|
||||
}
|
||||
|
||||
StackFrame& parent_stack = *parent_frame.to<StackFrame>();
|
||||
uint64_t parent_size = TRY(parent_stack.size());
|
||||
parent_stack = TRY(parent_stack.set(parent_size, res));
|
||||
|
||||
return std::move(parent_stack);
|
||||
}
|
||||
|
||||
Result<StackFrame> StackFrame::detach(uint64_t depth) const {
|
||||
if (depth == 0) {
|
||||
uint64_t size = _value->size;
|
||||
|
|
|
@ -1164,6 +1164,7 @@ class StackFrame : public Object {
|
|||
|
||||
Result<StackFrame> call(const Value& fun, uint64_t start, uint64_t end) const;
|
||||
Result<StackFrame> ret(uint64_t regno) const;
|
||||
Result<StackFrame> ret(const Value& val) const;
|
||||
|
||||
Result<StackFrame> detach(uint64_t depth) const;
|
||||
Result<StackFrame> attach(const StackFrame& frame) const;
|
||||
|
|
|
@ -175,11 +175,7 @@ Result<StackFrame> stdlib_dict(const StackFrame& stack) {
|
|||
if (!params.is<Array>()) return ERROR(TypeMismatch);
|
||||
Value d = TRY(Dict::create(*params.to<Array>()));
|
||||
|
||||
auto res = TRY(stack.set(0, d));
|
||||
|
||||
res = TRY(res.ret(0));
|
||||
|
||||
return res;
|
||||
return stack.ret(d);
|
||||
}
|
||||
|
||||
Result<StackFrame> stdlib_list(const StackFrame& stack) {
|
||||
|
@ -187,21 +183,13 @@ Result<StackFrame> stdlib_list(const StackFrame& stack) {
|
|||
if (!params.is<Array>()) return ERROR(TypeMismatch);
|
||||
Value d = TRY(Pair::create(*params.to<Array>()));
|
||||
|
||||
auto res = TRY(stack.set(0, d));
|
||||
|
||||
res = TRY(res.ret(0));
|
||||
|
||||
return res;
|
||||
return stack.ret(d);
|
||||
}
|
||||
|
||||
Result<StackFrame> stdlib_array(const StackFrame& stack) {
|
||||
auto params = TRY(stack.get(0));
|
||||
|
||||
auto res = TRY(stack.set(0, params));
|
||||
|
||||
res = TRY(res.ret(0));
|
||||
|
||||
return res;
|
||||
return stack.ret(params);
|
||||
}
|
||||
|
||||
Result<StackFrame> stdlib_get(const StackFrame& stack) {
|
||||
|
@ -214,11 +202,7 @@ Result<StackFrame> stdlib_get(const StackFrame& stack) {
|
|||
|
||||
auto val = TRY(collection.get(key));
|
||||
|
||||
auto res = TRY(stack.set(0, val));
|
||||
|
||||
res = TRY(res.ret(0));
|
||||
|
||||
return res;
|
||||
return stack.ret(val);
|
||||
}
|
||||
|
||||
Result<StackFrame> stdlib_set(const StackFrame& stack) {
|
||||
|
@ -232,11 +216,7 @@ Result<StackFrame> stdlib_set(const StackFrame& stack) {
|
|||
|
||||
collection = TRY(collection.set(key, value));
|
||||
|
||||
auto res = TRY(stack.set(0, collection));
|
||||
|
||||
res = TRY(res.ret(0));
|
||||
|
||||
return res;
|
||||
return stack.ret(collection);
|
||||
}
|
||||
|
||||
Result<StackFrame> stdlib_srcloc(const StackFrame& stack) {
|
||||
|
@ -275,11 +255,7 @@ Result<StackFrame> stdlib_srcloc(const StackFrame& stack) {
|
|||
|
||||
auto val = Value(TRY(SrcLoc::create(sr)));
|
||||
|
||||
auto res = TRY(stack.set(0, val));
|
||||
|
||||
res = TRY(res.ret(0));
|
||||
|
||||
return res;
|
||||
return stack.ret(val);
|
||||
}
|
||||
|
||||
Result<StackFrame> stdlib_size(const StackFrame& stack) {
|
||||
|
@ -293,11 +269,7 @@ Result<StackFrame> stdlib_size(const StackFrame& stack) {
|
|||
|
||||
auto val = Value(TRY(Int64::create((int64_t)psize)));
|
||||
|
||||
auto res = TRY(stack.set(0, val));
|
||||
|
||||
res = TRY(res.ret(0));
|
||||
|
||||
return res;
|
||||
return stack.ret(val);
|
||||
}
|
||||
|
||||
Result<StackFrame> stdlib_map(const StackFrame& stack_param) {
|
||||
|
@ -347,10 +319,7 @@ Result<StackFrame> stdlib_map(const StackFrame& stack_param) {
|
|||
|
||||
accumulator = TRY(reverse(accumulator));
|
||||
|
||||
auto res = TRY(stack.set(0, accumulator));
|
||||
res = TRY(res.ret(0));
|
||||
|
||||
return res;
|
||||
return stack.ret(accumulator);
|
||||
}
|
||||
|
||||
Result<StackFrame> stdlib_error(const StackFrame& stack) {
|
||||
|
@ -444,11 +413,7 @@ Result<StackFrame> stdlib_task(const StackFrame& stack) {
|
|||
|
||||
auto val = Value(TRY(Task::create(num_task_id, *task_params.to<Array>())));
|
||||
|
||||
auto res = TRY(stack.set(0, val));
|
||||
|
||||
res = TRY(res.ret(0));
|
||||
|
||||
return res;
|
||||
return stack.ret(val);
|
||||
}
|
||||
|
||||
Result<StackFrame> stdlib_is_task(const StackFrame& stack) {
|
||||
|
@ -461,11 +426,7 @@ Result<StackFrame> stdlib_is_task(const StackFrame& stack) {
|
|||
|
||||
auto val = Value(TRY(Bool::create(task.is<Task>())));
|
||||
|
||||
auto res = TRY(stack.set(0, val));
|
||||
|
||||
res = TRY(res.ret(0));
|
||||
|
||||
return res;
|
||||
return stack.ret(val);
|
||||
}
|
||||
|
||||
Result<StackFrame> stdlib_guard(const StackFrame& stack) {
|
||||
|
@ -540,11 +501,7 @@ Result<StackFrame> stdlib_serialize(const StackFrame& stack) {
|
|||
auto val = TRY(params.get(0));
|
||||
auto serialized = Value(TRY(serialize(val)));
|
||||
|
||||
auto res = TRY(stack.set(0, serialized));
|
||||
|
||||
res = TRY(res.ret(0));
|
||||
|
||||
return res;
|
||||
return stack.ret(serialized);
|
||||
}
|
||||
|
||||
Result<StackFrame> stdlib_deserialize(const StackFrame& stack) {
|
||||
|
@ -559,11 +516,7 @@ Result<StackFrame> stdlib_deserialize(const StackFrame& stack) {
|
|||
if (!val.is<ByteArray>()) return ERROR(TypeMismatch);
|
||||
auto deserialized = TRY(deserialize(*val.to<ByteArray>()));
|
||||
|
||||
auto res = TRY(stack.set(0, deserialized));
|
||||
|
||||
res = TRY(res.ret(0));
|
||||
|
||||
return res;
|
||||
return stack.ret(deserialized);
|
||||
}
|
||||
|
||||
Result<StackFrame> stdlib_disassemble(const StackFrame& stack) {
|
||||
|
@ -593,12 +546,9 @@ Result<StackFrame> stdlib_disassemble(const StackFrame& stack) {
|
|||
return ERROR(TypeMismatch);
|
||||
}
|
||||
|
||||
auto res = TRY(stack.set(0, disassembled));
|
||||
|
||||
res = TRY(res.ret(0));
|
||||
|
||||
return res;
|
||||
return stack.ret(disassembled);
|
||||
}
|
||||
|
||||
#define STDLIB_FUNCTION(name, fun, id) \
|
||||
[(uint64_t)StdlibFunctionId::id] = {name, StdlibFunctionId::id, stdlib_##fun}
|
||||
|
||||
|
|
Loading…
Reference in a new issue