mirror of
https://github.com/janet-lang/janet
synced 2024-11-25 01:37:19 +00:00
Merge pull request #1147 from zevv/error-messages
improved error messages for special forms
This commit is contained in:
commit
cc5545277d
@ -264,7 +264,7 @@ static const Janet *janetc_make_sourcemap(JanetCompiler *c) {
|
|||||||
|
|
||||||
static JanetSlot janetc_varset(JanetFopts opts, int32_t argn, const Janet *argv) {
|
static JanetSlot janetc_varset(JanetFopts opts, int32_t argn, const Janet *argv) {
|
||||||
if (argn != 2) {
|
if (argn != 2) {
|
||||||
janetc_cerror(opts.compiler, "expected 2 arguments");
|
janetc_cerror(opts.compiler, "expected 2 arguments to set");
|
||||||
return janetc_cslot(janet_wrap_nil());
|
return janetc_cslot(janet_wrap_nil());
|
||||||
}
|
}
|
||||||
JanetFopts subopts = janetc_fopts_default(opts.compiler);
|
JanetFopts subopts = janetc_fopts_default(opts.compiler);
|
||||||
@ -335,11 +335,11 @@ static JanetTable *handleattr(JanetCompiler *c, int32_t argn, const Janet *argv)
|
|||||||
return tab;
|
return tab;
|
||||||
}
|
}
|
||||||
|
|
||||||
static JanetSlot dohead(JanetCompiler *c, JanetFopts opts, Janet *head, int32_t argn, const Janet *argv) {
|
static JanetSlot dohead(const char *kind, JanetCompiler *c, JanetFopts opts, Janet *head, int32_t argn, const Janet *argv) {
|
||||||
JanetFopts subopts = janetc_fopts_default(c);
|
JanetFopts subopts = janetc_fopts_default(c);
|
||||||
JanetSlot ret;
|
JanetSlot ret;
|
||||||
if (argn < 2) {
|
if (argn < 2) {
|
||||||
janetc_cerror(c, "expected at least 2 arguments");
|
janetc_error(c, janet_formatc("expected at least 2 arguments to %s", kind));
|
||||||
return janetc_cslot(janet_wrap_nil());
|
return janetc_cslot(janet_wrap_nil());
|
||||||
}
|
}
|
||||||
*head = argv[0];
|
*head = argv[0];
|
||||||
@ -404,7 +404,7 @@ static JanetSlot janetc_var(JanetFopts opts, int32_t argn, const Janet *argv) {
|
|||||||
JanetCompiler *c = opts.compiler;
|
JanetCompiler *c = opts.compiler;
|
||||||
Janet head;
|
Janet head;
|
||||||
JanetTable *attr_table = handleattr(c, argn, argv);
|
JanetTable *attr_table = handleattr(c, argn, argv);
|
||||||
JanetSlot ret = dohead(c, opts, &head, argn, argv);
|
JanetSlot ret = dohead("var", c, opts, &head, argn, argv);
|
||||||
if (c->result.status == JANET_COMPILE_ERROR)
|
if (c->result.status == JANET_COMPILE_ERROR)
|
||||||
return janetc_cslot(janet_wrap_nil());
|
return janetc_cslot(janet_wrap_nil());
|
||||||
destructure(c, argv[0], ret, varleaf, attr_table);
|
destructure(c, argv[0], ret, varleaf, attr_table);
|
||||||
@ -454,7 +454,7 @@ static JanetSlot janetc_def(JanetFopts opts, int32_t argn, const Janet *argv) {
|
|||||||
Janet head;
|
Janet head;
|
||||||
opts.flags &= ~JANET_FOPTS_HINT;
|
opts.flags &= ~JANET_FOPTS_HINT;
|
||||||
JanetTable *attr_table = handleattr(c, argn, argv);
|
JanetTable *attr_table = handleattr(c, argn, argv);
|
||||||
JanetSlot ret = dohead(c, opts, &head, argn, argv);
|
JanetSlot ret = dohead("def", c, opts, &head, argn, argv);
|
||||||
if (c->result.status == JANET_COMPILE_ERROR)
|
if (c->result.status == JANET_COMPILE_ERROR)
|
||||||
return janetc_cslot(janet_wrap_nil());
|
return janetc_cslot(janet_wrap_nil());
|
||||||
destructure(c, argv[0], ret, defleaf, attr_table);
|
destructure(c, argv[0], ret, defleaf, attr_table);
|
||||||
@ -708,7 +708,7 @@ static JanetSlot janetc_while(JanetFopts opts, int32_t argn, const Janet *argv)
|
|||||||
uint8_t ifnjmp = JOP_JUMP_IF_NOT;
|
uint8_t ifnjmp = JOP_JUMP_IF_NOT;
|
||||||
|
|
||||||
if (argn < 2) {
|
if (argn < 2) {
|
||||||
janetc_cerror(c, "expected at least 2 arguments");
|
janetc_cerror(c, "expected at least 2 arguments to while");
|
||||||
return janetc_cslot(janet_wrap_nil());
|
return janetc_cslot(janet_wrap_nil());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -439,20 +439,21 @@ int janet_compare(Janet x, Janet y) {
|
|||||||
return status - 2;
|
return status - 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t getter_checkint(Janet key, int32_t max) {
|
static int32_t getter_checkint(JanetType type, Janet key, int32_t max) {
|
||||||
if (!janet_checkint(key)) goto bad;
|
if (!janet_checkint(key)) goto bad;
|
||||||
int32_t ret = janet_unwrap_integer(key);
|
int32_t ret = janet_unwrap_integer(key);
|
||||||
if (ret < 0) goto bad;
|
if (ret < 0) goto bad;
|
||||||
if (ret >= max) goto bad;
|
if (ret >= max) goto bad;
|
||||||
return ret;
|
return ret;
|
||||||
bad:
|
bad:
|
||||||
janet_panicf("expected integer key in range [0, %d), got %v", max, key);
|
janet_panicf("expected integer key for %s in range [0, %d), got %v", janet_type_names[type], max, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Gets a value and returns. Can panic. */
|
/* Gets a value and returns. Can panic. */
|
||||||
Janet janet_in(Janet ds, Janet key) {
|
Janet janet_in(Janet ds, Janet key) {
|
||||||
Janet value;
|
Janet value;
|
||||||
switch (janet_type(ds)) {
|
JanetType type = janet_type(ds);
|
||||||
|
switch (type) {
|
||||||
default:
|
default:
|
||||||
janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, ds);
|
janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, ds);
|
||||||
break;
|
break;
|
||||||
@ -464,19 +465,19 @@ Janet janet_in(Janet ds, Janet key) {
|
|||||||
break;
|
break;
|
||||||
case JANET_ARRAY: {
|
case JANET_ARRAY: {
|
||||||
JanetArray *array = janet_unwrap_array(ds);
|
JanetArray *array = janet_unwrap_array(ds);
|
||||||
int32_t index = getter_checkint(key, array->count);
|
int32_t index = getter_checkint(type, key, array->count);
|
||||||
value = array->data[index];
|
value = array->data[index];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case JANET_TUPLE: {
|
case JANET_TUPLE: {
|
||||||
const Janet *tuple = janet_unwrap_tuple(ds);
|
const Janet *tuple = janet_unwrap_tuple(ds);
|
||||||
int32_t len = janet_tuple_length(tuple);
|
int32_t len = janet_tuple_length(tuple);
|
||||||
value = tuple[getter_checkint(key, len)];
|
value = tuple[getter_checkint(type, key, len)];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case JANET_BUFFER: {
|
case JANET_BUFFER: {
|
||||||
JanetBuffer *buffer = janet_unwrap_buffer(ds);
|
JanetBuffer *buffer = janet_unwrap_buffer(ds);
|
||||||
int32_t index = getter_checkint(key, buffer->count);
|
int32_t index = getter_checkint(type, key, buffer->count);
|
||||||
value = janet_wrap_integer(buffer->data[index]);
|
value = janet_wrap_integer(buffer->data[index]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -484,7 +485,7 @@ Janet janet_in(Janet ds, Janet key) {
|
|||||||
case JANET_SYMBOL:
|
case JANET_SYMBOL:
|
||||||
case JANET_KEYWORD: {
|
case JANET_KEYWORD: {
|
||||||
const uint8_t *str = janet_unwrap_string(ds);
|
const uint8_t *str = janet_unwrap_string(ds);
|
||||||
int32_t index = getter_checkint(key, janet_string_length(str));
|
int32_t index = getter_checkint(type, key, janet_string_length(str));
|
||||||
value = janet_wrap_integer(str[index]);
|
value = janet_wrap_integer(str[index]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -752,13 +753,14 @@ void janet_putindex(Janet ds, int32_t index, Janet value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void janet_put(Janet ds, Janet key, Janet value) {
|
void janet_put(Janet ds, Janet key, Janet value) {
|
||||||
switch (janet_type(ds)) {
|
JanetType type = janet_type(ds);
|
||||||
|
switch (type) {
|
||||||
default:
|
default:
|
||||||
janet_panicf("expected %T, got %v",
|
janet_panicf("expected %T, got %v",
|
||||||
JANET_TFLAG_ARRAY | JANET_TFLAG_BUFFER | JANET_TFLAG_TABLE, ds);
|
JANET_TFLAG_ARRAY | JANET_TFLAG_BUFFER | JANET_TFLAG_TABLE, ds);
|
||||||
case JANET_ARRAY: {
|
case JANET_ARRAY: {
|
||||||
JanetArray *array = janet_unwrap_array(ds);
|
JanetArray *array = janet_unwrap_array(ds);
|
||||||
int32_t index = getter_checkint(key, INT32_MAX - 1);
|
int32_t index = getter_checkint(type, key, INT32_MAX - 1);
|
||||||
if (index >= array->count) {
|
if (index >= array->count) {
|
||||||
janet_array_setcount(array, index + 1);
|
janet_array_setcount(array, index + 1);
|
||||||
}
|
}
|
||||||
@ -767,7 +769,7 @@ void janet_put(Janet ds, Janet key, Janet value) {
|
|||||||
}
|
}
|
||||||
case JANET_BUFFER: {
|
case JANET_BUFFER: {
|
||||||
JanetBuffer *buffer = janet_unwrap_buffer(ds);
|
JanetBuffer *buffer = janet_unwrap_buffer(ds);
|
||||||
int32_t index = getter_checkint(key, INT32_MAX - 1);
|
int32_t index = getter_checkint(type, key, INT32_MAX - 1);
|
||||||
if (!janet_checkint(value))
|
if (!janet_checkint(value))
|
||||||
janet_panicf("can only put integers in buffers, got %v", value);
|
janet_panicf("can only put integers in buffers, got %v", value);
|
||||||
if (index >= buffer->count) {
|
if (index >= buffer->count) {
|
||||||
|
Loading…
Reference in New Issue
Block a user