From c05199f44acf294227d6b3118df411145f6b2872 Mon Sep 17 00:00:00 2001 From: Erik Date: Sat, 27 Feb 2016 00:02:13 +0100 Subject: [PATCH] struct lookup/creation handles strings --- TODO.md | 1 + lisp/array_tests.carp | 37 +++++++++++++++++++++++++++++++++++++ lisp/compiler.carp | 10 +++++++--- out/project.carp | 36 ++++++++---------------------------- src/eval.c | 14 +++++++++++++- 5 files changed, 66 insertions(+), 32 deletions(-) diff --git a/TODO.md b/TODO.md index e1e93deb..83c92ae4 100644 --- a/TODO.md +++ b/TODO.md @@ -11,6 +11,7 @@ - copy - delete - Want to be able to bake struct constructors! (they are dictionaries) + - Use 'delete' instead of free for memory management - Can't declare array literals inside array literals (works with temp variables inside though) - Ownership in while loops - Ownership tracking to enable returning refs from functions (it's forbidden at the moment) diff --git a/lisp/array_tests.carp b/lisp/array_tests.carp index fd0d1383..f86a894d 100644 --- a/lisp/array_tests.carp +++ b/lisp/array_tests.carp @@ -138,3 +138,40 @@ (str (ref [(string-copy "yeah") (string-copy "oh")]))) (bake star-s) + + + + + + +(defn call-id [] + (id 10)) + +;;(bake call-id) + +(defn call-nth [x] + (+ 1 (nth x 0))) + +;;(bake call-nth) + +(defn aha [] + [1 2 3 4 5]) +;;(bake aha) +;;(def a (aha)) + +(defn star [] + (str (ref [7 3 4]))) + +;;(bake star) + + +(defn starr [] + [100 200 300]) + +;;(bake starr) + +(defn foo [] + (str (ref (starr)))) +;;(bake foo) + +(def a (starr)) diff --git a/lisp/compiler.carp b/lisp/compiler.carp index 23c92266..0ddca14c 100644 --- a/lisp/compiler.carp +++ b/lisp/compiler.carp @@ -57,9 +57,6 @@ (defn func-baked? [func-name] (not (nil? (get-maybe baked-funcs func-name)))) -;; (defn add-baked-primop! [func-name] -;; (reset! baked-primops (cons func-name baked-primops))) - (def log-unloading-of-dylibs false) ;; Takes the name of a function and unloads it if it is in the list of baked functions @@ -75,11 +72,18 @@ (defn unload-all-baked () (join "\n" (map (fn (x) (str (unload-dylib (:func-dylib x)))) (values baked-funcs)))) +(def types {}) + +(defn add-type! [type-name type-definition] + (swap! types (fn (ts) (assoc ts type-name {:type-name type-name + :type-definition type-definition})))) + ;; Saves the signatures of all the baked functions to a header file so that they can include each other (defn save-function-prototypes () (save (str out-dir "functions.h") (str "#include \n" + (join "\n" (map :type-definition (values types))) (join "\n" (map :func-proto (values baked-funcs)))))) (defn link-libs (dependencies) diff --git a/out/project.carp b/out/project.carp index c362ff4f..80c273d9 100644 --- a/out/project.carp +++ b/out/project.carp @@ -7,34 +7,14 @@ nil )) -(defn call-id [] - (id 10)) +(defstruct Vec2 + [x :float + y :float]) -;;(bake call-id) +(def pos (Vec2 3.4 5.5)) -(defn call-nth [x] - (+ 1 (nth x 0))) +(defstruct Person + [name :string + age :int]) -;;(bake call-nth) - -(defn aha [] - [1 2 3 4 5]) -;;(bake aha) -;;(def a (aha)) - -(defn star [] - (str (ref [7 3 4]))) - -;;(bake star) - - -(defn starr [] - [100 200 300]) - -;;(bake starr) - -(defn foo [] - (str (ref (starr)))) -;;(bake foo) - -(def a (starr)) +(def me (Person "erik" 29)) diff --git a/src/eval.c b/src/eval.c index 60c113c2..bd1ce7c8 100644 --- a/src/eval.c +++ b/src/eval.c @@ -462,13 +462,20 @@ void apply(Obj *function, Obj **args, int arg_count) { void **vp = (void**)(((char*)new_struct->void_ptr) + offset); *vp = args[i]->void_ptr; } + else if(args[i]->tag == 'S') { + char **sp = (char**)(((char*)new_struct->void_ptr) + offset); + *sp = args[i]->s; + } else { eval_error = obj_new_string("Can't set member "); - //obj_string_mut_append(eval_error, ); + char buffer[32]; + sprintf(buffer, "%d", i); + obj_string_mut_append(eval_error, buffer); obj_string_mut_append(eval_error, " of struct "); obj_string_mut_append(eval_error, name); obj_string_mut_append(eval_error, " to "); obj_string_mut_append(eval_error, obj_to_string(args[i])->s); + obj_string_mut_append(eval_error, " (unhandled type)."); return; } } @@ -509,6 +516,11 @@ void apply(Obj *function, Obj **args, int arg_count) { int x = *xp; lookup = obj_new_int(x); } + else if(obj_eq(member_type, type_string)) { + char **sp = location; + char *s = *sp; + lookup = obj_new_string(s); + } else { void **pp = location; void *p = *pp;