diff --git a/lang/func.c b/lang/func.c new file mode 100644 index 0000000..2ae068a --- /dev/null +++ b/lang/func.c @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2023 John Watts and the LuminaSensum contributors + +#include "error.h" +#include "number.h" +#include "object.h" +#include "stdio.h" +#include + +static struct object_class func_class; + +struct func { + struct object obj; +}; + +struct object *func_create(void) { + struct func *func = malloc(sizeof(struct func)); + abort_if(!func, "unable to allocate func"); + func->obj.class_data = &func_class; + func->obj.ref_count = 1; + return (struct object *)func; +} + +static void func_cleanup(struct object *obj) { + abort_if(obj->class_data != &func_class, + "func_cleanup obj is not a func"); + struct func *func = (struct func *)obj; + free(func); +} + +static void func_call(struct object *obj, int arg_count, struct object **args) { + abort_if(arg_count != 0, "func_add called with more than 0 arguments"); + abort_if(obj->class_data != &func_class, "func_call obj is not a func"); + args[0] = number_create(1234); +} + +static struct object_call calls[] = { + {.name = "Call", .handler = func_call}, {.name = NULL, /* end */}}; + +static struct object_class func_class = { + .cleanup = func_cleanup, + .calls = &calls[0], +}; diff --git a/lang/func.h b/lang/func.h new file mode 100644 index 0000000..883f653 --- /dev/null +++ b/lang/func.h @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2023 John Watts and the LuminaSensum contributors + +#ifndef FUNC_H +#define FUNC_H + +#include "object.h" + +// Creates a function +struct object *func_create(void); + +#endif diff --git a/lang/main.c b/lang/main.c index 030511b..7aab790 100644 --- a/lang/main.c +++ b/lang/main.c @@ -1,11 +1,11 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2023 John Watts and the LuminaSensum contributors +#include "func.h" #include "number.h" #include -int lang_main(void) { - printf("Hello world!\n"); +static void test_number(void) { struct object *numA = number_create(5); struct object *numB = number_create(3); struct object *args[] = {numB}; @@ -21,5 +21,25 @@ printf("numA is %p\n", (void *)numA); printf("numB is %p\n", (void *)numB); printf("numC is %p\n", (void *)numC); +} + +static void test_func(void) { + struct object *funcA = func_create(); + struct object *args[] = {NULL}; + dispatch_call(funcA, "Call", 0, &args[0]); + printf("funcA is %p\n", (void *)funcA); + object_drop(&funcA); + printf("funcA is %p\n", (void *)funcA); + struct object *numA = args[0]; + printf("numA is %p\n", (void *)numA); + printf("code return value is %i\n", number_value(numA)); + object_drop(&numA); + printf("numA is %p\n", (void *)numA); +} + +int lang_main(void) { + printf("Hello world!\n"); + test_number(); + test_func(); return 0; }