Skip to content

Commit

Permalink
it works now
Browse files Browse the repository at this point in the history
  • Loading branch information
dragoncoder047 committed Oct 11, 2023
1 parent ad7fe10 commit 05e57d3
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 120 deletions.
25 changes: 19 additions & 6 deletions pickle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,25 @@ static void del_string(object* self) {
delete[] self->cells;
}

static void init_error(object* self, va_list args) {
DBG("Creating an error");
self->cells = new cell[3];
self->cells[0].as_obj = va_arg(args, object*);
self->cells[1].as_obj = va_arg(args, object*);
self->cells[2].as_obj = va_arg(args, object*);
}

static void mark_error(object* self) {
for (int i = 0; i < 3; i++) self->cells[i].as_obj->mark();
}

const object_schema metadata_type("object_metadata", init_metadata, NULL, mark_metadata, finalize_metadata);
const object_schema cons_type("cons", tinobsy::schema_functions::init_cons, cmp_c_function, tinobsy::schema_functions::mark_cons, tinobsy::schema_functions::finalize_cons);
const object_schema partial_type("function_partial", init_function_partial, NULL, NULL, tinobsy::schema_functions::finalize_cons);
const object_schema c_function_type("c_function", init_c_function, NULL, NULL, NULL);
const object_schema string_type("string", init_string, cmp_string, mark_string, del_string);
const object_schema symbol_type("symbol", tinobsy::schema_functions::init_str, tinobsy::schema_functions::cmp_str, NULL, tinobsy::schema_functions::finalize_str);
const object_schema error_type("error", NULL, NULL, NULL, NULL);
const object_schema error_type("error", init_error, NULL, mark_error, tinobsy::schema_functions::finalize_cons);

object* pickle::cons_list(size_t len, ...) {
va_list args;
Expand Down Expand Up @@ -165,10 +177,9 @@ void pickle::set_retval(object* args, object* env, object* cont, object* fail_co
this->do_later(thunk);
}

void pickle::set_failure(object* type, object* details, object* env, object* cont, object* fail_cont) {
void pickle::set_failure(object* err, object* env, object* cont, object* fail_cont) {
if (fail_cont == NULL) return; // No failure continuation -> ignore the error
object* args = this->cons_list(3, type, details, cont);
object* thunk = this->make_partial(fail_cont->cells[0].as_obj, this->append(fail_cont->cells[1].as_obj, args), env, fail_cont->cells[3].as_obj, fail_cont->cells[4].as_obj);
object* thunk = this->make_partial(fail_cont->cells[0].as_obj, this->append(fail_cont->cells[1].as_obj, this->cons(err, NULL)), env, fail_cont->cells[3].as_obj, fail_cont->cells[4].as_obj);
this->do_later(thunk);
}

Expand Down Expand Up @@ -241,15 +252,17 @@ void funcs::parse(pickle* runner, object* args, object* env, object* cont, objec
const char* str = (const char*)(s->cells[0].as_chars);
object* result = s->cells[1].as_obj;
if (result != NULL) { // Saved preparse
goto success;
if (result->schema == &error_type) goto failure;
else goto success;
}
TODO;
// result = runner->make_error(runner->wrap_symbol("SyntaxError"), runner->cons_list(1, result), cont)
success:
runner->set_retval(runner->cons_list(1, result), env, cont, fail_cont);
s->cells[1].as_obj = result; // Save parse for later if constantly reparsing string (i.e. a loop)
return;
failure:
runner->set_failure(runner->wrap_symbol("SyntaxError"), runner->cons_list(1, result), env, cont, fail_cont);
runner->set_failure(result, env, cont, fail_cont);
// TODO: copy error as cached parse result
}

Expand Down
5 changes: 4 additions & 1 deletion pickle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class pickle : public tinobsy::vm {
object* cons_list(size_t len, ...);
object* append(object* l1, object* l2);
void set_retval(object* args, object* env, object* cont, object* fail_cont);
void set_failure(object* type, object* details, object* env, object* cont, object* fail_cont);
void set_failure(object* err, object* env, object* cont, object* fail_cont);
void do_later(object* thunk);
void do_next(object* thunk);
void run_next_thunk();
Expand All @@ -66,6 +66,9 @@ class pickle : public tinobsy::vm {
inline object* cons(object* car, object* cdr) {
return this->allocate(&cons_type, car, cdr);
}
inline object* make_error(object* type, object* details, object* continuation) {
return this->allocate(&error_type, type, details, continuation);
}
private:
void mark_globals();
};
Expand Down
16 changes: 8 additions & 8 deletions test/out64.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[pickle.cpp:106-init_string] init_string: thisWillCauseASyntaxError
[tinobsy/tinobsy.cpp:38-allocate] Trying to intern a string
[tinobsy/tinobsy.cpp:47-allocate] New string not interned
[pickle.cpp:225-wrap_string] Starting task to parse string thisWillCauseASyntaxError
[pickle.cpp:236-wrap_string] Starting task to parse string thisWillCauseASyntaxError
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a cons
[tinobsy/tinobsy.cpp:110-init_cons]
Expand All @@ -15,15 +15,15 @@
[pickle.cpp:83-init_c_function] Function is parse(): true
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a function_partial
[pickle.cpp:176-do_later] do_later: Adding cons to tail
[pickle.cpp:187-do_later] do_later: Adding cons to tail
[tinobsy/tinobsy.cpp:33-allocate] Assertion succeeded: schema != NULL
[tinobsy/tinobsy.cpp:34-allocate] vm::allocate() a cons
[tinobsy/tinobsy.cpp:110-init_cons]
[tinobsy/tinobsy.cpp:38-allocate] Trying to intern a cons
[tinobsy/tinobsy.cpp:47-allocate] New cons not interned
[pickle.cpp:189-run_next_thunk] run_next_thunk
[pickle.cpp:192-run_next_thunk] Have thunk
[pickle.cpp:196-run_next_thunk] Have func
[pickle.cpp:198-run_next_thunk] Native function
[pickle.cpp:239-parse] parsing
[pickle.cpp:246-parse] parse: Function not implemented
[pickle.cpp:200-run_next_thunk] run_next_thunk
[pickle.cpp:203-run_next_thunk] Have thunk
[pickle.cpp:207-run_next_thunk] Have func
[pickle.cpp:209-run_next_thunk] Native function
[pickle.cpp:250-parse] parsing
[pickle.cpp:258-parse] parse: Function not implemented
Loading

0 comments on commit 05e57d3

Please sign in to comment.