Shallow clone (runtime function)

This commit is contained in:
Dmitry Boulytchev 2020-01-15 22:33:46 +03:00
parent a453b65fd3
commit 46dfd58bda
6 changed files with 31 additions and 7 deletions

View file

@ -0,0 +1,5 @@
Cloning int: 5
Cloning string: abc
Cloning array: [1, 2, 3, 4, 5]
Cloning sexp: A (1, 2, 3, 4, 5)
Cloning closure: address ok, 5, 6

View file

@ -0,0 +1,14 @@
fun f (x, y) {
fun () {x+y}
}
printf ("Cloning int: %d\n", clone (5));
printf ("Cloning string: %s\n", clone ("abc"));
printf ("Cloning array: %s\n", clone ([1, 2, 3, 4, 5]).string);
printf ("Cloning sexp: %s\n", clone (A (1, 2, 3, 4, 5)).string);
{
local c = f (5, 6), cc = clone (c);
printf ("Cloning closure: address %s, %d, %d\n", if cc[0] == c[0] then "ok" else "fail" fi, c[1], c[2])
}

View file

@ -485,7 +485,7 @@ void *Lclone (void *p) {
if (UNBOXED(p)) return p;
else {
data *a = TO_DATA(p), *res;
data *a = TO_DATA(p);
int t = TAG(a->tag), l = LEN(a->tag);
switch (t) {
@ -496,12 +496,15 @@ void *Lclone (void *p) {
case ARRAY_TAG:
case CLOSURE_TAG:
res = (data*) alloc (sizeof(int) * (l+1));
strncpy (res, p, sizeof(int) * (l+1));
memcpy (res, a, sizeof(int) * (l+1));
res = res->contents;
break;
case SEXP_TAG:
res = (data*) alloc (sizeof(int) * (l+2));
strncpy (res, TO_SEXP(p), sizeof(int) * (l+2));
res = (sexp*) alloc (sizeof(int) * (l+2));
memcpy (res, TO_SEXP(p), sizeof(int) * (l+2));
res = res->contents;
res = res->contents;
break;
default:
@ -1142,7 +1145,7 @@ extern void __gc_root_scan_stack ();
/* Mark-and-copy */
/* ======================================== */
static size_t SPACE_SIZE = 32;
static size_t SPACE_SIZE = 32 * 1024;
// static size_t SPACE_SIZE = 128;
// static size_t SPACE_SIZE = 1024 * 1024;

View file

@ -183,7 +183,7 @@ let compile cmd env code =
match scode with
| [] -> env, []
| instr :: scode' ->
let stack = env#show_stack in
let stack = "" (* env#show_stack*) in
let env', code' =
match instr with
| PUBLIC name -> env#register_public name, []

View file

@ -255,5 +255,5 @@ public fun emptyMemo () {
}
public fun lookupMemo (m, v) {
skip
}

View file

@ -6,6 +6,8 @@ RC=../src/rc.opt
all: $(ALL)
Collection.o: List.o
%.o: %.expr
$(RC) -I . -c $<