1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-29 03:19:54 +00:00

A few fixes to things like macro expand, etc.

This commit is contained in:
Calvin Rose 2018-11-07 22:27:06 -05:00
parent 853a839f6c
commit 2a127af1ca
7 changed files with 44 additions and 36 deletions

View File

@ -63,10 +63,11 @@ JanetArray *janet_array_n(const Janet *elements, int32_t n) {
} }
/* Ensure the array has enough capacity for elements */ /* Ensure the array has enough capacity for elements */
void janet_array_ensure(JanetArray *array, int32_t capacity) { void janet_array_ensure(JanetArray *array, int32_t capacity, int32_t growth) {
Janet *newData; Janet *newData;
Janet *old = array->data; Janet *old = array->data;
if (capacity <= array->capacity) return; if (capacity <= array->capacity) return;
capacity *= growth;
newData = realloc(old, capacity * sizeof(Janet)); newData = realloc(old, capacity * sizeof(Janet));
if (NULL == newData) { if (NULL == newData) {
JANET_OUT_OF_MEMORY; JANET_OUT_OF_MEMORY;
@ -81,7 +82,7 @@ void janet_array_setcount(JanetArray *array, int32_t count) {
return; return;
if (count > array->count) { if (count > array->count) {
int32_t i; int32_t i;
janet_array_ensure(array, count); janet_array_ensure(array, count, 1);
for (i = array->count; i < count; i++) { for (i = array->count; i < count; i++) {
array->data[i] = janet_wrap_nil(); array->data[i] = janet_wrap_nil();
} }
@ -92,9 +93,7 @@ void janet_array_setcount(JanetArray *array, int32_t count) {
/* Push a value to the top of the array */ /* Push a value to the top of the array */
void janet_array_push(JanetArray *array, Janet x) { void janet_array_push(JanetArray *array, Janet x) {
int32_t newcount = array->count + 1; int32_t newcount = array->count + 1;
if (newcount >= array->capacity) { janet_array_ensure(array, newcount, 2);
janet_array_ensure(array, newcount * 2);
}
array->data[array->count] = x; array->data[array->count] = x;
array->count = newcount; array->count = newcount;
} }
@ -148,7 +147,7 @@ static int cfun_push(JanetArgs args) {
JANET_MINARITY(args, 1); JANET_MINARITY(args, 1);
JANET_ARG_ARRAY(array, args, 0); JANET_ARG_ARRAY(array, args, 0);
newcount = array->count - 1 + args.n; newcount = array->count - 1 + args.n;
janet_array_ensure(array, newcount); janet_array_ensure(array, newcount, 2);
if (args.n > 1) memcpy(array->data + array->count, args.v + 1, (args.n - 1) * sizeof(Janet)); if (args.n > 1) memcpy(array->data + array->count, args.v + 1, (args.n - 1) * sizeof(Janet));
array->count = newcount; array->count = newcount;
JANET_RETURN(args, args.v[0]); JANET_RETURN(args, args.v[0]);
@ -168,11 +167,13 @@ static int cfun_setcount(JanetArgs args) {
static int cfun_ensure(JanetArgs args) { static int cfun_ensure(JanetArgs args) {
JanetArray *array; JanetArray *array;
int32_t newcount; int32_t newcount;
JANET_FIXARITY(args, 2); int32_t growth;
JANET_FIXARITY(args, 3);
JANET_ARG_ARRAY(array, args, 0); JANET_ARG_ARRAY(array, args, 0);
JANET_ARG_INTEGER(newcount, args, 1); JANET_ARG_INTEGER(newcount, args, 1);
JANET_ARG_INTEGER(growth, args, 2);
if (newcount < 0) JANET_THROW(args, "expected positive integer"); if (newcount < 0) JANET_THROW(args, "expected positive integer");
janet_array_ensure(array, newcount); janet_array_ensure(array, newcount, growth);
JANET_RETURN(args, args.v[0]); JANET_RETURN(args, args.v[0]);
} }

View File

@ -50,10 +50,11 @@ JanetBuffer *janet_buffer(int32_t capacity) {
} }
/* Ensure that the buffer has enough internal capacity */ /* Ensure that the buffer has enough internal capacity */
void janet_buffer_ensure(JanetBuffer *buffer, int32_t capacity) { void janet_buffer_ensure(JanetBuffer *buffer, int32_t capacity, int32_t growth) {
uint8_t *new_data; uint8_t *new_data;
uint8_t *old = buffer->data; uint8_t *old = buffer->data;
if (capacity <= buffer->capacity) return; if (capacity <= buffer->capacity) return;
capacity *= growth;
new_data = realloc(old, capacity * sizeof(uint8_t)); new_data = realloc(old, capacity * sizeof(uint8_t));
if (NULL == new_data) { if (NULL == new_data) {
JANET_OUT_OF_MEMORY; JANET_OUT_OF_MEMORY;
@ -68,7 +69,7 @@ void janet_buffer_setcount(JanetBuffer *buffer, int32_t count) {
return; return;
if (count > buffer->count) { if (count > buffer->count) {
int32_t oldcount = buffer->count; int32_t oldcount = buffer->count;
janet_buffer_ensure(buffer, count); janet_buffer_ensure(buffer, count, 1);
memset(buffer->data + oldcount, 0, count - oldcount); memset(buffer->data + oldcount, 0, count - oldcount);
} }
buffer->count = count; buffer->count = count;

View File

@ -864,7 +864,7 @@ value, one key will be ignored."
(def complex? (> (length y) 4)) (def complex? (> (length y) 4))
((if complex? pp-dict-nested pp-dict-simple) y)) ((if complex? pp-dict-nested pp-dict-simple) y))
(def printers (def printers
{:array (fn [y] (do-ds y "@[" "]" true pp-seq)) {:array (fn [y] (do-ds y "@[" "]" true pp-seq))
:tuple (fn [y] (do-ds y "(" ")" false pp-seq)) :tuple (fn [y] (do-ds y "(" ")" false pp-seq))
:table (fn [y] (do-ds y "@{" "}" true pp-dict)) :table (fn [y] (do-ds y "@{" "}" true pp-dict))
@ -902,8 +902,8 @@ value, one key will be ignored."
(defn expand-bindings [x] (defn expand-bindings [x]
(case (type x) (case (type x)
:array (map expand-bindings x) :array (mapa expand-bindings x)
:tuple (tuple.slice (map expand-bindings x) 0) :tuple (map expand-bindings x)
:table (dotable x expand-bindings) :table (dotable x expand-bindings)
:struct (table.to-struct (dotable x expand-bindings)) :struct (table.to-struct (dotable x expand-bindings))
(macroexpand-1 x))) (macroexpand-1 x)))
@ -916,11 +916,11 @@ value, one key will be ignored."
@[(expand-bindings last2) (macroexpand-1 last)]) 0)) @[(expand-bindings last2) (macroexpand-1 last)]) 0))
(defn expandall [t] (defn expandall [t]
(def args (map macroexpand-1 (tuple.slice t 1))) (def args (mapa macroexpand-1 (tuple.slice t 1)))
(apply tuple (get t 0) args)) (apply tuple (get t 0) args))
(defn expandfn [t] (defn expandfn [t]
(def args (map macroexpand-1 (tuple.slice t 2))) (def args (mapa macroexpand-1 (tuple.slice t 2)))
(apply tuple 'fn (get t 1) args)) (apply tuple 'fn (get t 1) args))
(def specs (def specs
@ -942,12 +942,12 @@ value, one key will be ignored."
(cond (cond
s (s t) s (s t)
m? (apply m (tuple.slice t 1)) m? (apply m (tuple.slice t 1))
(tuple.slice (map macroexpand-1 t) 0))) (map macroexpand-1 t) 0))
(def ret (def ret
(case (type x) (case (type x)
:tuple (dotup x) :tuple (dotup x)
:array (map macroexpand-1 x) :array (mapa macroexpand-1 x)
:struct (table.to-struct (dotable x macroexpand-1)) :struct (table.to-struct (dotable x macroexpand-1))
:table (dotable x macroexpand-1) :table (dotable x macroexpand-1)
x)) x))
@ -1001,7 +1001,7 @@ value, one key will be ignored."
### ###
### ###
(defn make-env (defn make-env
@[parent] @[parent]
(def parent (if parent parent _env)) (def parent (if parent parent _env))
(def newenv (table.setproto @{} parent)) (def newenv (table.setproto @{} parent))
@ -1166,7 +1166,7 @@ value, one key will be ignored."
(string syspath janet.version "/?/??.so") (string syspath janet.version "/?/??.so")
(string syspath "/?.so") (string syspath "/?.so")
(string syspath "/?/??.so")])) (string syspath "/?/??.so")]))
(if (= :windows (os.which)) (if (= :windows (os.which))
(loop [i :range [0 (length module.native-paths)]] (loop [i :range [0 (length module.native-paths)]]
(def x (get module.native-paths i)) (def x (get module.native-paths i))

View File

@ -112,7 +112,7 @@ static int32_t real_to_string_impl(uint8_t *buf, double x) {
} }
static void real_to_string_b(JanetBuffer *buffer, double x) { static void real_to_string_b(JanetBuffer *buffer, double x) {
janet_buffer_ensure(buffer, buffer->count + BUFSIZE); janet_buffer_ensure(buffer, buffer->count + BUFSIZE, 2);
buffer->count += real_to_string_impl(buffer->data + buffer->count, x); buffer->count += real_to_string_impl(buffer->data + buffer->count, x);
} }
@ -202,7 +202,7 @@ static int32_t string_description_impl(uint8_t *buf, const char *title, void *po
} }
static void string_description_b(JanetBuffer *buffer, const char *title, void *pointer) { static void string_description_b(JanetBuffer *buffer, const char *title, void *pointer) {
janet_buffer_ensure(buffer, buffer->count + BUFSIZE); janet_buffer_ensure(buffer, buffer->count + BUFSIZE, 2);
buffer->count += string_description_impl(buffer->data + buffer->count, title, pointer); buffer->count += string_description_impl(buffer->data + buffer->count, title, pointer);
} }

View File

@ -906,20 +906,26 @@ static void *op_lookup[255] = {
retreg = ds; retreg = ds;
goto vm_type_error; goto vm_type_error;
case JANET_ARRAY: case JANET_ARRAY:
if (index >= janet_unwrap_array(ds)->count) { {
janet_array_ensure(janet_unwrap_array(ds), 2 * index); JanetArray *array = janet_unwrap_array(ds);
janet_unwrap_array(ds)->count = index + 1; if (index >= array->count) {
janet_array_ensure(array, index + 1, 2);
array->count = index + 1;
}
array->data[index] = value;
break;
} }
janet_unwrap_array(ds)->data[index] = value;
break;
case JANET_BUFFER: case JANET_BUFFER:
vm_assert_type(value, JANET_INTEGER); {
if (index >= janet_unwrap_buffer(ds)->count) { JanetBuffer *buffer = janet_unwrap_buffer(ds);
janet_buffer_ensure(janet_unwrap_buffer(ds), 2 * index); vm_assert_type(value, JANET_INTEGER);
janet_unwrap_buffer(ds)->count = index + 1; if (index >= buffer->count) {
janet_buffer_ensure(buffer, index + 1, 2);
buffer->count = index + 1;
}
buffer->data[index] = janet_unwrap_integer(value);
break;
} }
janet_unwrap_buffer(ds)->data[index] = janet_unwrap_integer(value);
break;
} }
++pc; ++pc;
vm_checkgc_next(); vm_checkgc_next();

View File

@ -916,7 +916,7 @@ JANET_API JanetArray *janet_array(int32_t capacity);
JANET_API JanetArray *janet_array_n(const Janet *elements, int32_t n); JANET_API JanetArray *janet_array_n(const Janet *elements, int32_t n);
JANET_API JanetArray *janet_array_init(JanetArray *array, int32_t capacity); JANET_API JanetArray *janet_array_init(JanetArray *array, int32_t capacity);
JANET_API void janet_array_deinit(JanetArray *array); JANET_API void janet_array_deinit(JanetArray *array);
JANET_API void janet_array_ensure(JanetArray *array, int32_t capacity); JANET_API void janet_array_ensure(JanetArray *array, int32_t capacity, int32_t growth);
JANET_API void janet_array_setcount(JanetArray *array, int32_t count); JANET_API void janet_array_setcount(JanetArray *array, int32_t count);
JANET_API void janet_array_push(JanetArray *array, Janet x); JANET_API void janet_array_push(JanetArray *array, Janet x);
JANET_API Janet janet_array_pop(JanetArray *array); JANET_API Janet janet_array_pop(JanetArray *array);
@ -926,7 +926,7 @@ JANET_API Janet janet_array_peek(JanetArray *array);
JANET_API JanetBuffer *janet_buffer(int32_t capacity); JANET_API JanetBuffer *janet_buffer(int32_t capacity);
JANET_API JanetBuffer *janet_buffer_init(JanetBuffer *buffer, int32_t capacity); JANET_API JanetBuffer *janet_buffer_init(JanetBuffer *buffer, int32_t capacity);
JANET_API void janet_buffer_deinit(JanetBuffer *buffer); JANET_API void janet_buffer_deinit(JanetBuffer *buffer);
JANET_API void janet_buffer_ensure(JanetBuffer *buffer, int32_t capacity); JANET_API void janet_buffer_ensure(JanetBuffer *buffer, int32_t capacity, int32_t growth);
JANET_API void janet_buffer_setcount(JanetBuffer *buffer, int32_t count); JANET_API void janet_buffer_setcount(JanetBuffer *buffer, int32_t count);
JANET_API int janet_buffer_extra(JanetBuffer *buffer, int32_t n); JANET_API int janet_buffer_extra(JanetBuffer *buffer, int32_t n);
JANET_API int janet_buffer_push_bytes(JanetBuffer *buffer, const uint8_t *string, int32_t len); JANET_API int janet_buffer_push_bytes(JanetBuffer *buffer, const uint8_t *string, int32_t len);
@ -1185,7 +1185,7 @@ JANET_API int janet_typeabstract_err(JanetArgs args, int32_t n, const JanetAbstr
#define JANET_ARG_ARRAY(DEST, A, N) _JANET_ARG(JANET_ARRAY, array, DEST, A, N) #define JANET_ARG_ARRAY(DEST, A, N) _JANET_ARG(JANET_ARRAY, array, DEST, A, N)
#define JANET_ARG_TUPLE(DEST, A, N) _JANET_ARG(JANET_TUPLE, tuple, DEST, A, N) #define JANET_ARG_TUPLE(DEST, A, N) _JANET_ARG(JANET_TUPLE, tuple, DEST, A, N)
#define JANET_ARG_TABLE(DEST, A, N) _JANET_ARG(JANET_TABLE, table, DEST, A, N) #define JANET_ARG_TABLE(DEST, A, N) _JANET_ARG(JANET_TABLE, table, DEST, A, N)
#define JANET_ARG_STRUCT(DEST, A, N) _JANET_ARG(JANET_STRUCT, st, DEST, A, N) #define JANET_ARG_STRUCT(DEST, A, N) _JANET_ARG(JANET_STRUCT, struct, DEST, A, N)
#define JANET_ARG_BUFFER(DEST, A, N) _JANET_ARG(JANET_BUFFER, buffer, DEST, A, N) #define JANET_ARG_BUFFER(DEST, A, N) _JANET_ARG(JANET_BUFFER, buffer, DEST, A, N)
#define JANET_ARG_FUNCTION(DEST, A, N) _JANET_ARG(JANET_FUNCTION, function, DEST, A, N) #define JANET_ARG_FUNCTION(DEST, A, N) _JANET_ARG(JANET_FUNCTION, function, DEST, A, N)
#define JANET_ARG_CFUNCTION(DEST, A, N) _JANET_ARG(JANET_CFUNCTION, cfunction, DEST, A, N) #define JANET_ARG_CFUNCTION(DEST, A, N) _JANET_ARG(JANET_CFUNCTION, cfunction, DEST, A, N)

View File

@ -465,7 +465,7 @@ void janet_line_get(const uint8_t *p, JanetBuffer *buffer) {
} }
norawmode(); norawmode();
fputc('\n', stdout); fputc('\n', stdout);
janet_buffer_ensure(buffer, len + 1); janet_buffer_ensure(buffer, len + 1, 2);
memcpy(buffer->data, buf, len); memcpy(buffer->data, buf, len);
buffer->data[len] = '\n'; buffer->data[len] = '\n';
buffer->count = len + 1; buffer->count = len + 1;