@@ -40,18 +40,19 @@ bool needs_escape(char c) {
40
40
return strchr (" {}\b\t\n\v\f\r\a\\\" " , c) != NULL ;
41
41
}
42
42
43
- static void init_metadata (object* self, va_list args) {
44
- self->cells = new cell[4 ];
45
- self->cells [0 ].as_obj = va_arg (args, object*); // line
46
- self->cells [1 ].as_obj = va_arg (args, object*); // column
47
- self->cells [2 ].as_obj = va_arg (args, object*); // file
48
- self->cells [3 ].as_obj = va_arg (args, object*); // prototypes list
43
+ template <int n> void initmulti (object* self, va_list args) {
44
+ self->cells = new cell[n];
45
+ DBG (" creating %s (%i cells)" , self->schema ->name , n);
46
+ for (int i = 0 ; i < n; i++) self->cells [i].as_obj = va_arg (args, object*);
47
+
49
48
}
50
49
51
- static void mark4 (object* self) {
52
- for (int i = 0 ; i < 3 ; i++) self->cells [i].as_obj ->mark ();
50
+ template < int n> void markmulti (object* self) {
51
+ for (int i = 0 ; i < n ; i++) self->cells [i].as_obj ->mark ();
53
52
}
54
53
54
+ #define freemulti tinobsy::schema_functions::finalize_cons
55
+
55
56
static void init_c_function (object* self, va_list args) {
56
57
self->as_ptr = (void *)va_arg (args, func_ptr);
57
58
DBG (" Function is eval(): %s" , self->as_ptr == funcs::eval ? " true" : " false" );
@@ -62,19 +63,6 @@ static int cmp_c_function(object* a, object* b) {
62
63
return (uintptr_t )a->as_ptr - (uintptr_t )b->as_ptr ;
63
64
}
64
65
65
- static void init_function_partial (object* self, va_list args) {
66
- self->cells = new cell[5 ];
67
- self->cells [0 ].as_obj = va_arg (args, object*); // function
68
- self->cells [1 ].as_obj = va_arg (args, object*); // args
69
- self->cells [2 ].as_obj = va_arg (args, object*); // env
70
- self->cells [3 ].as_obj = va_arg (args, object*); // cont
71
- self->cells [4 ].as_obj = va_arg (args, object*); // failcont
72
- }
73
-
74
- static void mark_function_partial (object* self) {
75
- for (int i = 0 ; i < 5 ; i++) self->cells [i].as_obj ->mark ();
76
- }
77
-
78
66
static void init_string (object* self, va_list args) {
79
67
self->cells = new cell[2 ];
80
68
self->cells [0 ].as_ptr = (void *)strdup (va_arg (args, char *));
@@ -95,14 +83,6 @@ static void del_string(object* self) {
95
83
delete[] self->cells ;
96
84
}
97
85
98
- static void init_error (object* self, va_list args) {
99
- DBG (" Creating an error" );
100
- self->cells = new cell[3 ];
101
- self->cells [0 ].as_obj = va_arg (args, object*);
102
- self->cells [1 ].as_obj = va_arg (args, object*);
103
- self->cells [2 ].as_obj = va_arg (args, object*);
104
- }
105
-
106
86
static void init_int (object* self, va_list args) {
107
87
self->as_big_int = va_arg (args, int64_t );
108
88
}
@@ -119,13 +99,17 @@ static int cmp_float(object* a, object* b) {
119
99
return (int )(a->as_double - b->as_double );
120
100
}
121
101
122
- const object_schema metadata_type (" object_metadata" , init_metadata, NULL , mark4, tinobsy::schema_functions::finalize_cons);
123
- const object_schema cons_type (" cons" , tinobsy::schema_functions::init_cons, NULL , tinobsy::schema_functions::mark_cons, tinobsy::schema_functions::finalize_cons);
124
- const object_schema partial_type (" function_partial" , init_function_partial, NULL , NULL , tinobsy::schema_functions::finalize_cons);
102
+ // metadata = line, column, file, prototypes
103
+ const object_schema metadata_type (" object_metadata" , initmulti<4 >, NULL , markmulti<4 >, freemulti);
104
+ // cons = car, cdr
105
+ const object_schema cons_type (" cons" , tinobsy::schema_functions::init_cons, NULL , tinobsy::schema_functions::mark_cons, freemulti);
106
+ // partial = function, args, env, then, catch
107
+ const object_schema partial_type (" function_partial" , initmulti<5 >, NULL , markmulti<5 >, freemulti);
108
+ // error = type, message, detail, then
109
+ const object_schema error_type (" error" , initmulti<4 >, NULL , markmulti<4 >, freemulti);
125
110
const object_schema string_type (" string" , init_string, cmp_string, mark_string, del_string);
126
111
const object_schema symbol_type (" symbol" , tinobsy::schema_functions::init_str, tinobsy::schema_functions::cmp_str, NULL , tinobsy::schema_functions::finalize_str);
127
112
const object_schema c_function_type (" c_function" , init_c_function, cmp_c_function, NULL , NULL );
128
- const object_schema error_type (" error" , init_error, NULL , mark4, tinobsy::schema_functions::finalize_cons);
129
113
const object_schema integer_type (" int" , init_int, cmp_int, NULL , NULL );
130
114
const object_schema float_type (" float" , init_float, cmp_float, NULL , NULL );
131
115
@@ -217,6 +201,7 @@ void pickle::run_next_thunk() {
217
201
void pickle::mark_globals () {
218
202
this ->queue_head ->mark ();
219
203
this ->queue_tail ->mark (); // in case queue gets detached
204
+ this ->globals ->mark ();
220
205
}
221
206
222
207
// Can be called by the program
@@ -225,12 +210,12 @@ void funcs::parse(pickle* runner, object* args, object* env, object* cont, objec
225
210
object* s = car (args);
226
211
const char * str = (const char *)(s->cells [0 ].as_chars );
227
212
object* result = s->cells [1 ].as_obj ;
228
- if (result != NULL ) { // Saved preparse
213
+ if (result) { // Saved preparse
229
214
if (result->schema == &error_type) goto failure;
230
215
else goto success;
231
216
}
232
217
TODO;
233
- // result = runner->wrap_error(runner->wrap_symbol("SyntaxError"), runner->list(1, result), cont)
218
+ // result = runner->wrap_error(runner->wrap_symbol("SyntaxError"), runner->wrap_string(message), runner-> list(1, result), cont)
234
219
success:
235
220
runner->set_retval (runner->list (1 , result), env, cont, fail_cont);
236
221
s->cells [1 ].as_obj = result; // Save parse for later if constantly reparsing string (i.e. a loop)
0 commit comments