[nucl] showcase calling into ecl_interpret

This commit is contained in:
Daniel Kochmański 2025-05-15 23:28:47 +02:00
parent 40c1571257
commit 778bcd4e9a

View file

@ -5,8 +5,7 @@
#include <ecl/ecl-inl.h>
#include <ecl/internal.h>
#include <ecl/external.h>
/* Test code */
#include <ecl/bytecodes.h>
ecl_def_constant(ecl_ct_resume_tag, ECL_NIL, "RESUME-TAG", 10);
@ -34,8 +33,52 @@ cl_object nucl_flamethrower(int narg) {
return ECL_NIL;
}
cl_object nucl_mirror(int narg, cl_object element) {
const cl_env_ptr the_env = ecl_process_env();
printf(">>> nucl_mirror: %li!\n", ecl_fixnum(element));
ecl_return1(the_env, element);
}
ecl_def_function(_nucl_flamethrower, nucl_flamethrower, static, const);
ecl_def_function(_nucl_extinguisher, nucl_extinguisher, static, const);
ecl_def_function(_nucl_mirror, nucl_mirror, static, const);
/* : smoke_bytecodes NOP POP CALL 1 POP CALL 0 EXIT ; */
/* _nucl_flamethrower 42 _nucl_mirror smoke_bytecodes */
static cl_opcode program [] = {
OP_NOP,
OP_POP,
OP_CALL,
1,
OP_POP, /* reg0 = _nucl_flamethrower */
OP_CALL,
0,
OP_EXIT
};
void smoke_bytecodes (void)
{
const cl_env_ptr the_env = ecl_process_env();
struct ecl_bytecodes aux_codes[1];
struct ecl_stack_frame aux_frame[1];
cl_object f = ecl_cast_ptr(cl_object,aux_frame);
cl_object o = ecl_cast_ptr(cl_object,aux_codes);
ecl_stack_frame_open(the_env, f, 3);
ecl_stack_frame_push(f, _nucl_flamethrower);
ecl_stack_frame_push(f, ecl_make_fixnum(42));
ecl_stack_frame_push(f, _nucl_mirror);
o->bytecodes.t = t_bytecodes;
o->bytecodes.name = ECL_NIL;
o->bytecodes.definition = ECL_NIL;
o->bytecodes.code_size = 2;
o->bytecodes.code = ecl_cast_ptr(char*,program);
o->bytecodes.data = ECL_NIL;
o->bytecodes.flex = ECL_NIL;
o->bytecodes.nlcl = ecl_make_fixnum(0);
ecl_interpret(f, ECL_NIL, o);
ecl_stack_frame_close(f);
}
int main() {
cl_env_ptr the_env = ecl_core.first_env;
@ -45,21 +88,26 @@ int main() {
printf("Hello ECL! %p\n", the_env);
printf("\n\n[:handler t :restart t] -----------------------\n");
printf("\n[:handler t :restart t] -----------------------\n");
ECL_CATCH_BEGIN(the_env, ecl_ct_resume_tag); {
ecl_call_with_handler(_nucl_extinguisher, _nucl_flamethrower);
} ECL_CATCH_END;
printf("-----------------------------------------------\n");
printf("-----------------------------------------------\n\n");
printf("\n\n[:handler t :restart nil] ---------------------\n");
printf("\n[:handler t :restart nil] ---------------------\n");
ecl_call_with_handler(_nucl_extinguisher, _nucl_flamethrower);
printf("-----------------------------------------------\n");
printf("-----------------------------------------------\n\n");
printf("\n\n[:handler nil] --------------------------------\n");
printf("\n[:handler nil] --------------------------------\n");
nucl_flamethrower(0);
printf("-----------------------------------------------\n");
printf("-----------------------------------------------\n\n");
printf("\n[:bytecodes t] --------------------------------\n");
smoke_bytecodes();
printf("-----------------------------------------------\n\n");
printf("Good bye ECL! %p\n", the_env);
printf("\n\nGood bye ECL! %p\n", the_env);
ecl_halt();
return 0;
}