struct lookup/creation handles strings

This commit is contained in:
Erik 2016-02-27 00:02:13 +01:00
parent 6af584c184
commit c05199f44a
5 changed files with 66 additions and 32 deletions

View File

@ -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)

View File

@ -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))

View File

@ -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 <shared.h>\n"
(join "\n" (map :type-definition (values types)))
(join "\n" (map :func-proto (values baked-funcs))))))
(defn link-libs (dependencies)

View File

@ -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))

View File

@ -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;