can call "constructor" for structs

This commit is contained in:
Erik 2016-02-24 16:59:59 +01:00
parent 11fa6c61e5
commit 87330feb37
3 changed files with 38 additions and 13 deletions

View File

@ -33,3 +33,6 @@
;; (bake test-whatever-1)
(defstruct Vec2 [x :float y :float])

View File

@ -424,6 +424,14 @@ void apply(Obj *function, Obj **args, int arg_count) {
}
}
}
else if(function->tag == 'E' && obj_eq(env_lookup(function, obj_new_keyword("struct")), lisp_true)) {
char *name = env_lookup(function, obj_new_keyword("name"))->s;
int size = env_lookup(function, obj_new_keyword("size"))->i;
printf("Will create a %s of size %d.\n", name, size);
void *p = malloc(sizeof(size));
Obj *new_struct = obj_new_ptr(p);
stack_push(new_struct);
}
else {
set_error("Can't call non-function: ", function);
}
@ -583,8 +591,12 @@ void eval_list(Obj *env, Obj *o) {
else { size = sizeof(void*); }
offset += size;
}
env_extend(struct_description, obj_new_keyword("offsets"), offsets);
env_extend(struct_description, obj_new_keyword("size"), obj_new_int(offset));
env_extend(struct_description, obj_new_keyword("struct"), lisp_true);
env_extend(env, obj_new_symbol(name), struct_description);
stack_push(struct_description);
}

View File

@ -15,22 +15,32 @@
typedef void (*VoidFn)(void);
/* Type tags
A = Array
B = Char
C = Cons cell
I = Integer
S = String
K = Keyword (:keyword)
Y = Symbol
L = Lambda
E = Environment
P = Primop / raw C function pointer
M = Macro
F = libffi function
D = Dylib
E = Environment
F = libffi function
G
H
I = Integer
J
K = Keyword (:keyword)
L = Lambda
M = Macro
N
O
P = Primop / raw C function pointer
Q = Void pointer
R
S = String
T
U
V = Float
W = Double (not implemented yet)
A = Array
Q = Void pointer
B = Char
X
Y = Symbol
Z
*/
typedef struct Obj {