diff --git a/lisp/core.carp b/lisp/core.carp index d9e36f9c..74a4019f 100644 --- a/lisp/core.carp +++ b/lisp/core.carp @@ -231,6 +231,7 @@ (register-builtin "substring" '(:string :int) :string) (register-builtin "file_path_component" '(:string) :string) (register-builtin "get_input" '() :string) +(register-builtin "spawn" '((:fn () :void)) :void) (register-builtin "call" '((:fn () :void)) :void) (register-builtin "call1" '((:fn (:int) :void)) :void) diff --git a/lisp/glfw_test.carp b/lisp/glfw_test.carp index 2adb98f8..92da3384 100644 --- a/lisp/glfw_test.carp +++ b/lisp/glfw_test.carp @@ -43,6 +43,9 @@ (glVertex3f x y 0.0f) (glEnd))) +(defn red () + 0.6) + (defn gl-demo () (if (glfwInit) (let [window (glfwCreateWindow 640 480 "Yeah!" NULL NULL)] @@ -52,20 +55,20 @@ (glfwMakeContextCurrent window) (while (not (glfwWindowShouldClose window)) (do - (glClearColor 0.6f 0.85f 0.85f 1.0f) + (glClearColor (red) 0.85f 0.85f 1.0f) (glClear gl-color-buffer-bit) - (glColor3f 1.0f 0.9f 0.2f) + (glColor3f 1.0 0.9f 0.2f) (draw-rect -0.5f -0.5f 1.0f 1.0f) (glfwSwapBuffers window) (glfwPollEvents))) (glfwTerminate)))) (panic "Failed to initialize glfw."))) -(bake draw-rect) +;;(bake draw-rect) ;(def app-ast (lambda-to-ast (code app))) ;(def app-asta (annotate-ast app-ast)) ;;(bake-gl-exe) -(bake* gl-demo '(draw-rect)) +;;(bake* gl-demo '(draw-rect)) diff --git a/shared/shared.h b/shared/shared.h index 78fb8a4f..c342f031 100644 --- a/shared/shared.h +++ b/shared/shared.h @@ -136,6 +136,20 @@ void async(void *f) { printf("Async done.\n"); } +void spawn(void (*f)()) { + printf("FORK start.\n"); + int pid = fork(); + if(pid == 0) { + printf("In parent\n"); + f(); + printf("FORK done.\n"); + exit(0); + } + else { + printf("In child\n"); + } +} + int last_index_of(string s, char c) { int len = strlen(s); for(int i = len - 1; i >= 0; i--) { diff --git a/src/primops.c b/src/primops.c index 98844498..3328b450 100644 --- a/src/primops.c +++ b/src/primops.c @@ -9,6 +9,8 @@ #include "eval.h" #include "reader.h" +#include + void register_primop(char *name, Primop primop) { Obj *o = obj_new_primop(primop); env_extend(global_env, obj_new_symbol(name), o); @@ -1395,3 +1397,18 @@ Obj *p_array_to_list(Obj** args, int arg_count) { } return list; } + +Obj *p_spork(Obj** args, int arg_count) { + printf("SPORK start.\n"); + int pid = fork(); + if(pid == 0) { + printf("In parent\n"); + apply(args[0], NULL, 0); + printf("SPORK done.\n"); + exit(0); + } + else { + printf("In child\n"); + } + return nil; +} diff --git a/src/primops.h b/src/primops.h index 6b241c69..ba8dfed0 100644 --- a/src/primops.h +++ b/src/primops.h @@ -63,6 +63,7 @@ Obj *p_builtin_p(Obj** args, int arg_count); Obj *p_meta_set_BANG(Obj** args, int arg_count); Obj *p_meta_get(Obj** args, int arg_count); Obj *p_array_to_list(Obj** args, int arg_count); +Obj *p_spork(Obj** args, int arg_count); Obj *register_ffi_internal(char *name, VoidFn funptr, Obj *args, Obj *return_type_obj, bool builtin); diff --git a/src/repl.c b/src/repl.c index 52bea22c..a57155c0 100644 --- a/src/repl.c +++ b/src/repl.c @@ -177,6 +177,7 @@ void env_new_global() { register_primop("meta-set!", p_meta_set_BANG); register_primop("meta-get", p_meta_get); register_primop("array-to-list", p_array_to_list); + register_primop("spork", p_spork); Obj *abs_args = obj_list(type_int); register_ffi_internal("abs", (VoidFn)abs, abs_args, type_int, true);