mirror of
https://github.com/carp-lang/Carp.git
synced 2024-10-11 12:37:32 +03:00
can call "constructor" for structs
This commit is contained in:
parent
11fa6c61e5
commit
87330feb37
@ -33,3 +33,6 @@
|
||||
|
||||
;; (bake test-whatever-1)
|
||||
|
||||
|
||||
(defstruct Vec2 [x :float y :float])
|
||||
|
||||
|
14
src/eval.c
14
src/eval.c
@ -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);
|
||||
}
|
||||
|
34
src/obj.h
34
src/obj.h
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user