From b99b938b2f33e4d12d63629f3d80e987c31d1892 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 19 Nov 2023 15:47:38 +0000 Subject: Implement basic effect handling in the runtime --- runtime/src/effects.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 runtime/src/effects.c (limited to 'runtime/src/effects.c') diff --git a/runtime/src/effects.c b/runtime/src/effects.c new file mode 100644 index 00000000..d456fbe7 --- /dev/null +++ b/runtime/src/effects.c @@ -0,0 +1,53 @@ +#include "./shed.h" + +struct ShedEffectHandler { + ShedEffectTag effect_tag; + struct ShedEffectHandler* parent; + ShedClosurePointer methods[]; +}; + +typedef struct ShedEffectHandler ShedEffectHandler; + +struct ShedEffectHandler* shed__runtime__current_effect_handler = NULL; + +ShedEffectHandler* shed__runtime__effects_push_handler( + ShedEffectTag effect_tag, + ShedEffectMethodIndex method_count +) { + ShedEffectHandler* handler = shed__runtime__alloc( + sizeof(ShedEffectHandler) + sizeof(ShedClosurePointer) * method_count + ); + handler->effect_tag = effect_tag; + handler->parent = shed__runtime__current_effect_handler; + + shed__runtime__current_effect_handler = handler; + + return handler; +} + +void shed__runtime__effects_set_handler_method( + ShedEffectHandler* effect_handler, + ShedEffectMethodIndex method_index, + ShedClosurePointer handler_method +) { + effect_handler->methods[method_index] = handler_method; +} + +void shed__runtime__effects_pop_handler() { + shed__runtime__current_effect_handler = shed__runtime__current_effect_handler->parent; +} + +ShedClosurePointer shed__runtime__effects_find_handler_method( + ShedEffectTag effect_tag, + ShedEffectMethodIndex method_index +) { + ShedEffectHandler* handler = shed__runtime__current_effect_handler; + while (handler != NULL) { + if (handler->effect_tag == effect_tag) { + return handler->methods[method_index]; + } + } + + // TODO: more informative error + return NULL; +} -- cgit v1.2.3