1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-18 11:19:56 +00:00

Add native keyword type to replace symbols with leading ':'

character.
This commit is contained in:
Calvin Rose 2019-01-02 19:41:07 -05:00
parent 29ec30c79f
commit e9c94598e6
21 changed files with 245 additions and 190 deletions

View File

@ -32,10 +32,11 @@
/* Compare valid c numbers to system implementation. */
static void test_valid_str(const char *str) {
int err = 0;
int err;
double cnum, jnum;
jnum = 0.0;
cnum = atof(str);
jnum = janet_scan_number((const uint8_t *) str, strlen(str), &err);
err = janet_scan_number((const uint8_t *) str, strlen(str), &jnum);
assert(!err);
assert(cnum == jnum);
}

View File

@ -137,25 +137,26 @@ typedef struct TypeAlias {
} TypeAlias;
static const TypeAlias type_aliases[] = {
{":abstract", JANET_TFLAG_ABSTRACT},
{":array", JANET_TFLAG_ARRAY},
{":boolean", JANET_TFLAG_BOOLEAN},
{":buffer", JANET_TFLAG_BUFFER},
{":callable", JANET_TFLAG_CALLABLE},
{":cfunction", JANET_TFLAG_CFUNCTION},
{":dictionary", JANET_TFLAG_DICTIONARY},
{":false", JANET_TFLAG_FALSE},
{":fiber", JANET_TFLAG_FIBER},
{":function", JANET_TFLAG_FUNCTION},
{":indexed", JANET_TFLAG_INDEXED},
{":nil", JANET_TFLAG_NIL},
{":number", JANET_TFLAG_NUMBER},
{":string", JANET_TFLAG_STRING},
{":struct", JANET_TFLAG_STRUCT},
{":symbol", JANET_TFLAG_SYMBOL},
{":table", JANET_TFLAG_BOOLEAN},
{":true", JANET_TFLAG_TRUE},
{":tuple", JANET_TFLAG_BOOLEAN}
{"abstract", JANET_TFLAG_ABSTRACT},
{"array", JANET_TFLAG_ARRAY},
{"boolean", JANET_TFLAG_BOOLEAN},
{"buffer", JANET_TFLAG_BUFFER},
{"callable", JANET_TFLAG_CALLABLE},
{"cfunction", JANET_TFLAG_CFUNCTION},
{"dictionary", JANET_TFLAG_DICTIONARY},
{"false", JANET_TFLAG_FALSE},
{"fiber", JANET_TFLAG_FIBER},
{"function", JANET_TFLAG_FUNCTION},
{"indexed", JANET_TFLAG_INDEXED},
{"nil", JANET_TFLAG_NIL},
{"number", JANET_TFLAG_NUMBER},
{"string", JANET_TFLAG_STRING},
{"struct", JANET_TFLAG_STRUCT},
{"symbol", JANET_TFLAG_SYMBOL},
{"keyword", JANET_TFLAG_KEYWORD},
{"table", JANET_TFLAG_BOOLEAN},
{"true", JANET_TFLAG_TRUE},
{"tuple", JANET_TFLAG_BOOLEAN}
};
/* Deinitialize an Assembler. Does not deinitialize the parents. */
@ -273,6 +274,24 @@ static int32_t doarg_1(
}
break;
}
case JANET_KEYWORD:
{
if (argtype == JANET_OAT_TYPE || argtype == JANET_OAT_SIMPLETYPE) {
const TypeAlias *alias = janet_strbinsearch(
&type_aliases,
sizeof(type_aliases)/sizeof(TypeAlias),
sizeof(TypeAlias),
janet_unwrap_keyword(x));
if (alias) {
ret = alias->mask;
} else {
janet_asm_errorv(a, janet_formatc("unknown type %v", x));
}
} else {
goto error;
}
break;
}
case JANET_SYMBOL:
{
if (NULL != c) {
@ -286,17 +305,6 @@ static int32_t doarg_1(
} else {
janet_asm_errorv(a, janet_formatc("unknown name %v", x));
}
} else if (argtype == JANET_OAT_TYPE || argtype == JANET_OAT_SIMPLETYPE) {
const TypeAlias *alias = janet_strbinsearch(
&type_aliases,
sizeof(type_aliases)/sizeof(TypeAlias),
sizeof(TypeAlias),
janet_unwrap_symbol(x));
if (alias) {
ret = alias->mask;
} else {
janet_asm_errorv(a, janet_formatc("unknown type %v", x));
}
} else {
goto error;
}

View File

@ -480,7 +480,6 @@ static int macroexpand1(
!janet_checktype(macroval, JANET_FUNCTION))
return 0;
/* Evaluate macro */
JanetFiber *fiberp;
JanetFunction *macro = janet_unwrap_function(macroval);
@ -716,11 +715,11 @@ static int cfun(JanetArgs args) {
JANET_RETURN_FUNCTION(args, janet_thunk(res.funcdef));
} else {
t = janet_table(4);
janet_table_put(t, janet_csymbolv(":error"), janet_wrap_string(res.error));
janet_table_put(t, janet_csymbolv(":start"), janet_wrap_integer(res.error_mapping.start));
janet_table_put(t, janet_csymbolv(":end"), janet_wrap_integer(res.error_mapping.end));
janet_table_put(t, janet_ckeywordv("error"), janet_wrap_string(res.error));
janet_table_put(t, janet_ckeywordv("start"), janet_wrap_integer(res.error_mapping.start));
janet_table_put(t, janet_ckeywordv("end"), janet_wrap_integer(res.error_mapping.end));
if (res.macrofiber) {
janet_table_put(t, janet_csymbolv(":fiber"), janet_wrap_fiber(res.macrofiber));
janet_table_put(t, janet_ckeywordv("fiber"), janet_wrap_fiber(res.macrofiber));
}
JANET_RETURN_TABLE(args, t);
}

View File

@ -87,8 +87,7 @@
(defn fiber? "Check if x is a fiber." [x] (= (type x) :fiber))
(defn string? "Check if x is a string." [x] (= (type x) :string))
(defn symbol? "Check if x is a symbol." [x] (= (type x) :symbol))
(defn keyword? "Check if x is a keyword style symbol." [x]
(if (not= (type x) :symbol) nil (= 58 (get x 0))))
(defn keyword? "Check if x is a keyword." [x] (= (type x) :keyword))
(defn buffer? "Check if x is a buffer." [x] (= (type x) :buffer))
(defn function? "Check if x is a function (not a cfunction)." [x]
(= (type x) :function))
@ -100,7 +99,7 @@
(defn boolean? "Check if x is a boolean." [x] (= (type x) :boolean))
(defn bytes? "Check if x is a string, symbol, or buffer." [x]
(def t (type x))
(if (= t :string) true (if (= t :symbol) true (= t :buffer))))
(if (= t :string) true (if (= t :symbol) true (if (= t :keyword) true (= t :buffer)))))
(defn dictionary? "Check if x a table or struct." [x]
(def t (type x))
(if (= t :table) true (= t :struct)))

View File

@ -137,6 +137,21 @@ static int janet_core_symbol(JanetArgs args) {
return 0;
}
static int janet_core_keyword(JanetArgs args) {
int32_t i;
JanetBuffer b;
janet_buffer_init(&b, 0);
for (i = 0; i < args.n; ++i) {
int32_t len;
const uint8_t *str = janet_to_string(args.v[i]);
len = janet_string_length(str);
janet_buffer_push_bytes(&b, str, len);
}
*args.ret = janet_keywordv(b.data, b.count);
janet_buffer_deinit(&b);
return 0;
}
static int janet_core_buffer(JanetArgs args) {
int32_t i;
JanetBuffer *b = janet_buffer(0);
@ -157,12 +172,10 @@ static int janet_core_is_abstract(JanetArgs args) {
static int janet_core_scannumber(JanetArgs args) {
const uint8_t *data;
double val;
int status = 0;
int32_t len;
JANET_FIXARITY(args, 1);
JANET_ARG_BYTES(data, len, args, 0);
val = janet_scan_number(data, len, &status);
if (status)
if (janet_scan_number(data, len, &val))
JANET_THROW(args, "failed to scan number");
JANET_RETURN_NUMBER(args, val);
}
@ -230,9 +243,9 @@ static int janet_core_type(JanetArgs args) {
JANET_FIXARITY(args, 1);
JanetType t = janet_type(args.v[0]);
if (t == JANET_ABSTRACT) {
JANET_RETURN(args, janet_csymbolv(janet_abstract_type(janet_unwrap_abstract(args.v[0]))->name));
JANET_RETURN(args, janet_ckeywordv(janet_abstract_type(janet_unwrap_abstract(args.v[0]))->name));
} else {
JANET_RETURN(args, janet_csymbolv(janet_type_names[t]));
JANET_RETURN(args, janet_ckeywordv(janet_type_names[t]));
}
}
@ -296,6 +309,12 @@ static const JanetReg cfuns[] = {
"converted to bytes via describe if they are not byte sequences. Returns "
"the new symbol."
},
{"keyword", janet_core_keyword,
"(keyword & xs)\n\n"
"Creates a keyword by concatenating values together. Values are "
"converted to bytes via describe if they are not byte sequences. Returns "
"the new keyword."
},
{"buffer", janet_core_buffer,
"(buffer & xs)\n\n"
"Creates a new buffer by concatenating values together. Values are "

View File

@ -184,42 +184,42 @@ static Janet doframe(JanetStackFrame *frame) {
JanetTable *t = janet_table(3);
JanetFuncDef *def = NULL;
if (frame->func) {
janet_table_put(t, janet_csymbolv(":function"), janet_wrap_function(frame->func));
janet_table_put(t, janet_ckeywordv("function"), janet_wrap_function(frame->func));
def = frame->func->def;
if (def->name) {
janet_table_put(t, janet_csymbolv(":name"), janet_wrap_string(def->name));
janet_table_put(t, janet_ckeywordv("name"), janet_wrap_string(def->name));
}
} else {
JanetCFunction cfun = (JanetCFunction)(frame->pc);
if (cfun) {
Janet name = janet_table_get(janet_vm_registry, janet_wrap_cfunction(cfun));
if (!janet_checktype(name, JANET_NIL)) {
janet_table_put(t, janet_csymbolv(":name"), name);
janet_table_put(t, janet_ckeywordv("name"), name);
}
}
janet_table_put(t, janet_csymbolv(":c"), janet_wrap_true());
janet_table_put(t, janet_ckeywordv("c"), janet_wrap_true());
}
if (frame->flags & JANET_STACKFRAME_TAILCALL) {
janet_table_put(t, janet_csymbolv(":tail"), janet_wrap_true());
janet_table_put(t, janet_ckeywordv("tail"), janet_wrap_true());
}
if (frame->func && frame->pc) {
Janet *stack = (Janet *)frame + JANET_FRAME_SIZE;
JanetArray *slots;
off = (int32_t) (frame->pc - def->bytecode);
janet_table_put(t, janet_csymbolv(":pc"), janet_wrap_integer(off));
janet_table_put(t, janet_ckeywordv("pc"), janet_wrap_integer(off));
if (def->sourcemap) {
JanetSourceMapping mapping = def->sourcemap[off];
janet_table_put(t, janet_csymbolv(":source-start"), janet_wrap_integer(mapping.start));
janet_table_put(t, janet_csymbolv(":source-end"), janet_wrap_integer(mapping.end));
janet_table_put(t, janet_ckeywordv("source-start"), janet_wrap_integer(mapping.start));
janet_table_put(t, janet_ckeywordv("source-end"), janet_wrap_integer(mapping.end));
}
if (def->source) {
janet_table_put(t, janet_csymbolv(":source"), janet_wrap_string(def->source));
janet_table_put(t, janet_ckeywordv("source"), janet_wrap_string(def->source));
}
/* Add stack arguments */
slots = janet_array(def->slotcount);
memcpy(slots->data, stack, sizeof(Janet) * def->slotcount);
slots->count = def->slotcount;
janet_table_put(t, janet_csymbolv(":slots"), janet_wrap_array(slots));
janet_table_put(t, janet_ckeywordv("slots"), janet_wrap_array(slots));
}
return janet_wrap_table(t);
}

View File

@ -317,8 +317,6 @@ static int cfun_new(JanetArgs args) {
switch (flags[i]) {
default:
JANET_THROW(args, "invalid flag, expected a, d, e, u, or y");
case ':':
break;
case 'a':
fiber->flags |=
JANET_FIBER_MASK_DEBUG |
@ -351,7 +349,7 @@ static int cfun_status(JanetArgs args) {
JANET_ARG_FIBER(fiber, args, 0);
uint32_t s = (fiber->flags & JANET_FIBER_STATUS_MASK) >>
JANET_FIBER_STATUS_OFFSET;
JANET_RETURN_CSYMBOL(args, janet_status_names[s]);
JANET_RETURN_CKEYWORD(args, janet_status_names[s]);
}
static int cfun_current(JanetArgs args) {
@ -384,7 +382,7 @@ static const JanetReg cfuns[] = {
"(fiber/new func [,sigmask])\n\n"
"Create a new fiber with function body func. Can optionally "
"take a set of signals to block from the current parent fiber "
"when called. The mask is specified as a symbol where each character "
"when called. The mask is specified as a keyword where each character "
"is used to indicate a signal to block. The default sigmask is :y. "
"For example, \n\n"
"\t(fiber/new myfun :e123)\n\n"

View File

@ -60,6 +60,7 @@ void janet_mark(Janet x) {
switch (janet_type(x)) {
default: break;
case JANET_STRING:
case JANET_KEYWORD:
case JANET_SYMBOL: janet_mark_string(janet_unwrap_string(x)); break;
case JANET_FUNCTION: janet_mark_function(janet_unwrap_function(x)); break;
case JANET_ARRAY: janet_mark_array(janet_unwrap_array(x)); break;

View File

@ -47,7 +47,7 @@ struct IOFile {
static int janet_io_gc(void *p, size_t len);
JanetAbstractType janet_io_filetype = {
":core/file",
"core/file",
janet_io_gc,
NULL
};
@ -148,10 +148,6 @@ static int janet_io_popen(JanetArgs args) {
fmode = (const uint8_t *)"r";
modelen = 1;
}
if (fmode[0] == ':') {
fmode++;
modelen--;
}
if (modelen != 1 || !(fmode[0] == 'r' || fmode[0] == 'w')) {
JANET_THROW(args, "invalid file mode");
}
@ -191,10 +187,6 @@ static int janet_io_fopen(JanetArgs args) {
fmode = (const uint8_t *)"r";
modelen = 1;
}
if (fmode[0] == ':') {
fmode++;
modelen--;
}
if ((flags = checkflags(fmode, modelen)) < 0) {
JANET_THROW(args, "invalid file mode");
}
@ -226,9 +218,10 @@ static int janet_io_fread(JanetArgs args) {
JANET_THROW(args, "file is closed");
b = checkbuffer(args, 2, 1);
if (!b) return 1;
if (janet_checktype(args.v[1], JANET_SYMBOL)) {
JANET_MINARITY(args, 2);
if (janet_checktype(args.v[1], JANET_KEYWORD)) {
const uint8_t *sym = janet_unwrap_symbol(args.v[1]);
if (!janet_cstrcmp(sym, ":all")) {
if (!janet_cstrcmp(sym, "all")) {
/* Read whole file */
int status = fseek(iof->file, 0, SEEK_SET);
if (status) {
@ -247,7 +240,7 @@ static int janet_io_fread(JanetArgs args) {
const char *maybeErr = read_chunk(iof, b, (int32_t) fsize);;
if (maybeErr) JANET_THROW(args, maybeErr);
}
} else if (!janet_cstrcmp(sym, ":line")) {
} else if (!janet_cstrcmp(sym, "line")) {
for (;;) {
int x = fgetc(iof->file);
if (x != EOF && janet_buffer_push_u8(b, (uint8_t)x))
@ -342,14 +335,14 @@ static int janet_io_fseek(JanetArgs args) {
JANET_THROW(args, "file is closed");
if (args.n >= 2) {
const uint8_t *whence_sym;
if (!janet_checktype(args.v[1], JANET_SYMBOL))
JANET_THROW(args, "expected symbol");
if (!janet_checktype(args.v[1], JANET_KEYWORD))
JANET_THROW(args, "expected keyword");
whence_sym = janet_unwrap_symbol(args.v[1]);
if (!janet_cstrcmp(whence_sym, ":cur")) {
if (!janet_cstrcmp(whence_sym, "cur")) {
whence = SEEK_CUR;
} else if (!janet_cstrcmp(whence_sym, ":set")) {
} else if (!janet_cstrcmp(whence_sym, "set")) {
whence = SEEK_SET;
} else if (!janet_cstrcmp(whence_sym, ":end")) {
} else if (!janet_cstrcmp(whence_sym, "end")) {
whence = SEEK_END;
} else {
JANET_THROW(args, "expected one of :cur, :set, :end");

View File

@ -69,6 +69,7 @@ enum {
LB_INTEGER,
LB_STRING,
LB_SYMBOL,
LB_KEYWORD,
LB_ARRAY,
LB_TUPLE,
LB_TABLE,
@ -87,16 +88,16 @@ enum {
static Janet entry_getval(Janet env_entry) {
if (janet_checktype(env_entry, JANET_TABLE)) {
JanetTable *entry = janet_unwrap_table(env_entry);
Janet checkval = janet_table_get(entry, janet_csymbolv(":value"));
Janet checkval = janet_table_get(entry, janet_ckeywordv("value"));
if (janet_checktype(checkval, JANET_NIL)) {
checkval = janet_table_get(entry, janet_csymbolv(":ref"));
checkval = janet_table_get(entry, janet_ckeywordv("ref"));
}
return checkval;
} else if (janet_checktype(env_entry, JANET_STRUCT)) {
const JanetKV *entry = janet_unwrap_struct(env_entry);
Janet checkval = janet_struct_get(entry, janet_csymbolv(":value"));
Janet checkval = janet_struct_get(entry, janet_ckeywordv("value"));
if (janet_checktype(checkval, JANET_NIL)) {
checkval = janet_struct_get(entry, janet_csymbolv(":ref"));
checkval = janet_struct_get(entry, janet_ckeywordv("ref"));
}
return checkval;
} else {
@ -356,12 +357,15 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
goto done;
case JANET_STRING:
case JANET_SYMBOL:
case JANET_KEYWORD:
{
const uint8_t *str = janet_unwrap_string(x);
int32_t length = janet_string_length(str);
/* Record reference */
MARK_SEEN();
uint8_t lb = (type == JANET_STRING) ? LB_STRING : LB_SYMBOL;
uint8_t lb = (type == JANET_STRING) ? LB_STRING :
(type == JANET_SYMBOL) ? LB_SYMBOL :
LB_KEYWORD;
pushbyte(st, lb);
pushint(st, length);
pushbytes(st, str, length);
@ -949,6 +953,7 @@ static const uint8_t *unmarshal_one(
case LB_STRING:
case LB_SYMBOL:
case LB_BUFFER:
case LB_KEYWORD:
case LB_REGISTRY:
{
data++;
@ -960,6 +965,9 @@ static const uint8_t *unmarshal_one(
} else if (lead == LB_SYMBOL) {
const uint8_t *str = janet_symbol(data, len);
*out = janet_wrap_symbol(str);
} else if (lead == LB_KEYWORD) {
const uint8_t *str = janet_keyword(data, len);
*out = janet_wrap_keyword(str);
} else if (lead == LB_REGISTRY) {
if (st->reg) {
Janet regkey = janet_symbolv(data, len);

View File

@ -28,12 +28,10 @@
/* Parse a part of a symbol that can be used for building up code. */
static JanetSlot multisym_parse_part(JanetCompiler *c, const uint8_t *sympart, int32_t len) {
if (sympart[0] == ':') {
return janetc_cslot(janet_symbolv(sympart, len));
return janetc_cslot(janet_keywordv(sympart + 1, len - 1));
} else {
double index;
int err;
index = janet_scan_number(sympart + 1, len - 1, &err);
if (err) {
if (janet_scan_number(sympart + 1, len - 1, &index)) {
/* not a number */
return janetc_resolve(c, janet_symbol(sympart + 1, len - 1));
} else {
@ -99,12 +97,7 @@ static JanetSlot multisym_do_parts(JanetFopts opts, int put, const uint8_t *sym,
* it and emit the code for treating it as a bunch of nested
* gets. */
JanetSlot janetc_sym_rvalue(JanetFopts opts, const uint8_t *sym) {
if (janet_string_length(sym) && sym[0] != ':') {
return multisym_do_parts(opts, 0, sym, janet_wrap_nil());
} else {
/* keyword */
return janetc_cslot(janet_wrap_symbol(sym));
}
return multisym_do_parts(opts, 0, sym, janet_wrap_nil());
}
/* Check if a symbol is a multisym, and if so, transform

View File

@ -42,13 +42,13 @@
static int os_which(JanetArgs args) {
#ifdef JANET_WINDOWS
JANET_RETURN_CSYMBOL(args, ":windows");
JANET_RETURN_CKEYWORD(args, "windows");
#elif __APPLE__
JANET_RETURN_CSYMBOL(args, ":macos");
JANET_RETURN_CKEYWORD(args, "macos");
#elif defined(__EMSCRIPTEN__)
JANET_RETURN_CSYMBOL(args, ":web");
JANET_RETURN_CKEYWORD(args, "web");
#else
JANET_RETURN_CSYMBOL(args, ":posix");
JANET_RETURN_CKEYWORD(args, "posix");
#endif
}

View File

@ -282,7 +282,6 @@ static int tokenchar(JanetParser *p, JanetParseState *state, uint8_t c) {
Janet ret;
double numval;
int32_t blen;
int scanerr;
if (is_symbol_char(c)) {
push_buf(p, (uint8_t) c);
if (c > 127) state->argn = 1; /* Use to indicate non ascii */
@ -290,9 +289,9 @@ static int tokenchar(JanetParser *p, JanetParseState *state, uint8_t c) {
}
/* Token finished */
blen = (int32_t) p->bufcount;
scanerr = 0;
numval = janet_scan_number(p->buf, blen, &scanerr);
if (!scanerr) {
if (p->buf[0] == ':') {
ret = janet_keywordv(p->buf + 1, blen - 1);
} else if (!janet_scan_number(p->buf, blen, &numval)) {
ret = janet_wrap_number(numval);
} else if (!check_str_const("nil", p->buf, blen)) {
ret = janet_wrap_nil();
@ -612,7 +611,7 @@ static int parsergc(void *p, size_t size) {
}
static JanetAbstractType janet_parse_parsertype = {
":core/parser",
"core/parser",
parsergc,
parsermark
};
@ -684,19 +683,19 @@ static int cfun_status(JanetArgs args) {
p = (JanetParser *) janet_unwrap_abstract(args.v[0]);
switch (janet_parser_status(p)) {
case JANET_PARSE_FULL:
stat = ":full";
stat = "full";
break;
case JANET_PARSE_PENDING:
stat = ":pending";
stat = "pending";
break;
case JANET_PARSE_ERROR:
stat = ":error";
stat = "error";
break;
case JANET_PARSE_ROOT:
stat = ":root";
stat = "root";
break;
}
JANET_RETURN_CSYMBOL(args, stat);
JANET_RETURN_CKEYWORD(args, stat);
}
static int cfun_error(JanetArgs args) {
@ -707,7 +706,7 @@ static int cfun_error(JanetArgs args) {
p = (JanetParser *) janet_unwrap_abstract(args.v[0]);
err = janet_parser_error(p);
if (err) {
JANET_RETURN_CSYMBOL(args, err);
JANET_RETURN_CSTRING(args, err);
} else {
JANET_RETURN_NIL(args);
}

View File

@ -207,11 +207,11 @@ static JanetTable *handleattr(JanetCompiler *c, int32_t argn, const Janet *argv)
default:
janetc_cerror(c, "could not add metadata to binding");
break;
case JANET_SYMBOL:
case JANET_KEYWORD:
janet_table_put(tab, attr, janet_wrap_true());
break;
case JANET_STRING:
janet_table_put(tab, janet_csymbolv(":doc"), attr);
janet_table_put(tab, janet_ckeywordv("doc"), attr);
break;
}
}
@ -262,8 +262,8 @@ static int varleaf(
reftab->proto = attr;
JanetArray *ref = janet_array(1);
janet_array_push(ref, janet_wrap_nil());
janet_table_put(reftab, janet_csymbolv(":ref"), janet_wrap_array(ref));
janet_table_put(reftab, janet_csymbolv(":source-map"),
janet_table_put(reftab, janet_ckeywordv("ref"), janet_wrap_array(ref));
janet_table_put(reftab, janet_ckeywordv("source-map"),
janet_wrap_tuple(janetc_make_sourcemap(c)));
janet_table_put(c->env, janet_wrap_symbol(sym), janet_wrap_table(reftab));
refslot = janetc_cslot(janet_wrap_array(ref));
@ -293,10 +293,10 @@ static int defleaf(
janetc_cerror(c, "cannot create binding to keyword symbol");
if (c->scope->flags & JANET_SCOPE_TOP) {
JanetTable *tab = janet_table(2);
janet_table_put(tab, janet_csymbolv(":source-map"),
janet_table_put(tab, janet_ckeywordv("source-map"),
janet_wrap_tuple(janetc_make_sourcemap(c)));
tab->proto = attr;
JanetSlot valsym = janetc_cslot(janet_csymbolv(":value"));
JanetSlot valsym = janetc_cslot(janet_ckeywordv("value"));
JanetSlot tabslot = janetc_cslot(janet_wrap_table(tab));
/* Add env entry to env */

View File

@ -320,6 +320,9 @@ void janet_description_b(JanetBuffer *buffer, Janet x) {
case JANET_NUMBER:
number_to_string_b(buffer, janet_unwrap_number(x));
return;
case JANET_KEYWORD:
janet_buffer_push_u8(buffer, ':');
/* fallthrough */
case JANET_SYMBOL:
janet_buffer_push_bytes(buffer,
janet_unwrap_string(x),
@ -384,6 +387,7 @@ void janet_to_string_b(JanetBuffer *buffer, Janet x) {
break;
case JANET_STRING:
case JANET_SYMBOL:
case JANET_KEYWORD:
janet_buffer_push_bytes(buffer,
janet_unwrap_string(x),
janet_string_length(janet_unwrap_string(x)));
@ -403,6 +407,14 @@ const uint8_t *janet_description(Janet x) {
return number_to_string(janet_unwrap_number(x));
case JANET_SYMBOL:
return janet_unwrap_symbol(x);
case JANET_KEYWORD:
{
const uint8_t *kw = janet_unwrap_keyword(x);
uint8_t *str = janet_string_begin(janet_string_length(kw) + 1);
memcpy(str + 1, kw, janet_string_length(kw) + 1);
str[0] = ':';
return janet_string_end(str);
}
case JANET_STRING:
return janet_escape_string(janet_unwrap_string(x));
case JANET_BUFFER:
@ -455,6 +467,7 @@ const uint8_t *janet_to_string(Janet x) {
return janet_string(janet_unwrap_buffer(x)->data, janet_unwrap_buffer(x)->count);
case JANET_STRING:
case JANET_SYMBOL:
case JANET_KEYWORD:
return janet_unwrap_string(x);
}
}
@ -1137,12 +1150,12 @@ static struct formatter {
const char *f1;
const char *f2;
} formatters[] = {
{":g", "%g", "%.*g"},
{":G", "%G", "%.*G"},
{":e", "%e", "%.*e"},
{":E", "%E", "%.*E"},
{":f", "%f", "%.*f"},
{":F", "%F", "%.*F"}
{"g", "%g", "%.*g"},
{"G", "%G", "%.*G"},
{"e", "%e", "%.*e"},
{"E", "%E", "%.*E"},
{"f", "%f", "%.*f"},
{"F", "%F", "%.*F"}
};
static int cfun_number(JanetArgs args) {
@ -1156,7 +1169,7 @@ static int cfun_number(JanetArgs args) {
JANET_ARG_NUMBER(x, args, 0);
if (args.n >= 2) {
const uint8_t *flag;
JANET_ARG_SYMBOL(flag, args, 1);
JANET_ARG_KEYWORD(flag, args, 1);
for (int i = 0; i < 6; i++) {
struct formatter fmttest = formatters[i];
if (!janet_cstrcmp(flag, fmttest.lead)) {

View File

@ -238,10 +238,10 @@ static double convert(
/* Scan a real (double) from a string. If the string cannot be converted into
* and integer, set *err to 1 and return 0. */
double janet_scan_number(
int janet_scan_number(
const uint8_t *str,
int32_t len,
int *err) {
double *out) {
const uint8_t *end = str + len;
int seenadigit = 0;
int ex = 0;
@ -357,13 +357,11 @@ double janet_scan_number(
if (!seenadigit)
goto error;
double result = convert(neg, &mant, base, ex);
*out = convert(neg, &mant, base, ex);
free(mant.digits);
*err = 0;
return result;
return 0;
error:
*err = 1;
free(mant.digits);
return 0.0;
return 1;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 Calvin Rose
* Copyright (c) 2019 Calvin Rose
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
@ -234,16 +234,16 @@ const uint8_t *janet_symbol_gen(void) {
* is enough for resolving collisions. */
do {
hash = janet_string_calchash(
gensym_counter,
gensym_counter,
sizeof(gensym_counter) - 1);
bucket = janet_symcache_findmem(
gensym_counter,
gensym_counter,
sizeof(gensym_counter) - 1,
hash,
&status);
} while (status && (inc_gensym(), 1));
sym = (uint8_t *) janet_gcalloc(
JANET_MEMORY_SYMBOL,
JANET_MEMORY_SYMBOL,
2 * sizeof(int32_t) + sizeof(gensym_counter)) +
(2 * sizeof(int32_t));
memcpy(sym, gensym_counter, sizeof(gensym_counter));

View File

@ -35,58 +35,59 @@ const char janet_base64[65] =
/* The JANET value types in order. These types can be used as
* mnemonics instead of a bit pattern for type checking */
const char *const janet_type_names[15] = {
":number",
":nil",
":boolean",
":boolean",
":fiber",
":string",
":symbol",
":array",
":tuple",
":table",
":struct",
":buffer",
":function",
":cfunction",
":abstract"
const char *const janet_type_names[16] = {
"number",
"nil",
"boolean",
"boolean",
"fiber",
"string",
"symbol",
"keyword",
"array",
"tuple",
"table",
"struct",
"buffer",
"function",
"cfunction",
"abstract"
};
const char *const janet_signal_names[14] = {
":ok",
":error",
":debug",
":yield",
":user0",
":user1",
":user2",
":user3",
":user4",
":user5",
":user6",
":user7",
":user8",
":user9"
"ok",
"error",
"debug",
"yield",
"user0",
"user1",
"user2",
"user3",
"user4",
"user5",
"user6",
"user7",
"user8",
"user9"
};
const char *const janet_status_names[16] = {
":dead",
":error",
":debug",
":pending",
":user0",
":user1",
":user2",
":user3",
":user4",
":user5",
":user6",
":user7",
":user8",
":user9",
":new",
":alive"
"dead",
"error",
"debug",
"pending",
"user0",
"user1",
"user2",
"user3",
"user4",
"user5",
"user6",
"user7",
"user8",
"user9",
"new",
"alive"
};
/* Calculate hash for string */
@ -239,9 +240,9 @@ void janet_register(const char *name, JanetCFunction cfun) {
/* Add a def to an environment */
void janet_def(JanetTable *env, const char *name, Janet val, const char *doc) {
JanetTable *subt = janet_table(2);
janet_table_put(subt, janet_csymbolv(":value"), val);
janet_table_put(subt, janet_ckeywordv("value"), val);
if (doc)
janet_table_put(subt, janet_csymbolv(":doc"), janet_cstringv(doc));
janet_table_put(subt, janet_ckeywordv("doc"), janet_cstringv(doc));
janet_table_put(env, janet_csymbolv(name), janet_wrap_table(subt));
}
@ -250,9 +251,9 @@ void janet_var(JanetTable *env, const char *name, Janet val, const char *doc) {
JanetArray *array = janet_array(1);
JanetTable *subt = janet_table(2);
janet_array_push(array, val);
janet_table_put(subt, janet_csymbolv(":ref"), janet_wrap_array(array));
janet_table_put(subt, janet_ckeywordv("ref"), janet_wrap_array(array));
if (doc)
janet_table_put(subt, janet_csymbolv(":doc"), janet_cstringv(doc));
janet_table_put(subt, janet_ckeywordv("doc"), janet_cstringv(doc));
janet_table_put(env, janet_csymbolv(name), janet_wrap_table(subt));
}
@ -269,7 +270,7 @@ void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns)
uint8_t *longname_buffer =
janet_string_begin(reglen + 1 + nmlen);
memcpy(longname_buffer, regprefix, reglen);
longname_buffer[reglen] = '.';
longname_buffer[reglen] = '/';
memcpy(longname_buffer + reglen + 1, cfuns->name, nmlen);
longname = janet_wrap_symbol(janet_string_end(longname_buffer));
}
@ -289,17 +290,17 @@ JanetBindingType janet_resolve(JanetTable *env, const uint8_t *sym, Janet *out)
return JANET_BINDING_NONE;
entry_table = janet_unwrap_table(entry);
if (!janet_checktype(
janet_table_get(entry_table, janet_csymbolv(":macro")),
janet_table_get(entry_table, janet_ckeywordv("macro")),
JANET_NIL)) {
*out = janet_table_get(entry_table, janet_csymbolv(":value"));
*out = janet_table_get(entry_table, janet_ckeywordv("value"));
return JANET_BINDING_MACRO;
}
ref = janet_table_get(entry_table, janet_csymbolv(":ref"));
ref = janet_table_get(entry_table, janet_ckeywordv("ref"));
if (janet_checktype(ref, JANET_ARRAY)) {
*out = ref;
return JANET_BINDING_VAR;
}
*out = janet_table_get(entry_table, janet_csymbolv(":value"));
*out = janet_table_get(entry_table, janet_ckeywordv("value"));
return JANET_BINDING_DEF;
}
@ -333,7 +334,8 @@ int janet_indexed_view(Janet seq, const Janet **data, int32_t *len) {
/* Read both strings and buffer as unsigned character array + int32_t len.
* Returns 1 if the view can be constructed and 0 if the type is invalid. */
int janet_bytes_view(Janet str, const uint8_t **data, int32_t *len) {
if (janet_checktype(str, JANET_STRING) || janet_checktype(str, JANET_SYMBOL)) {
if (janet_checktype(str, JANET_STRING) || janet_checktype(str, JANET_SYMBOL) ||
janet_checktype(str, JANET_KEYWORD)) {
*data = janet_unwrap_string(str);
*len = janet_string_length(janet_unwrap_string(str));
return 1;

View File

@ -74,6 +74,7 @@ int32_t janet_hash(Janet x) {
break;
case JANET_STRING:
case JANET_SYMBOL:
case JANET_KEYWORD:
hash = janet_string_hash(janet_unwrap_string(x));
break;
case JANET_TUPLE:
@ -127,6 +128,7 @@ int janet_compare(Janet x, Janet y) {
}
case JANET_STRING:
case JANET_SYMBOL:
case JANET_KEYWORD:
return janet_string_compare(janet_unwrap_string(x), janet_unwrap_string(y));
case JANET_TUPLE:
return janet_tuple_compare(janet_unwrap_tuple(x), janet_unwrap_tuple(y));
@ -198,6 +200,7 @@ int janet_get(Janet ds, Janet key, Janet *out) {
}
case JANET_STRING:
case JANET_SYMBOL:
case JANET_KEYWORD:
{
const uint8_t *str = janet_unwrap_string(ds);
int32_t index;
@ -224,6 +227,7 @@ int janet_getindex(Janet ds, int32_t index, Janet *out) {
return -1;
case JANET_STRING:
case JANET_SYMBOL:
case JANET_KEYWORD:
if (index >= janet_string_length(janet_unwrap_string(ds))) {
value = janet_wrap_nil();
} else {
@ -269,6 +273,7 @@ int janet_length(Janet x, int32_t *out) {
return -1;
case JANET_STRING:
case JANET_SYMBOL:
case JANET_KEYWORD:
len = janet_string_length(janet_unwrap_string(x));
break;
case JANET_ARRAY:

View File

@ -203,7 +203,7 @@ extern "C" {
#include <stdarg.h>
/* Names of all of the types */
extern const char *const janet_type_names[15];
extern const char *const janet_type_names[16];
extern const char *const janet_signal_names[14];
extern const char *const janet_status_names[16];
@ -281,6 +281,7 @@ typedef enum JanetType {
JANET_FIBER,
JANET_STRING,
JANET_SYMBOL,
JANET_KEYWORD,
JANET_ARRAY,
JANET_TUPLE,
JANET_TABLE,
@ -301,6 +302,7 @@ typedef enum JanetType {
#define JANET_TFLAG_NUMBER (1 << JANET_NUMBER)
#define JANET_TFLAG_STRING (1 << JANET_STRING)
#define JANET_TFLAG_SYMBOL (1 << JANET_SYMBOL)
#define JANET_TFLAG_KEYWORD (1 << JANET_KEYWORD)
#define JANET_TFLAG_ARRAY (1 << JANET_ARRAY)
#define JANET_TFLAG_TUPLE (1 << JANET_TUPLE)
#define JANET_TFLAG_TABLE (1 << JANET_TABLE)
@ -313,7 +315,7 @@ typedef enum JanetType {
/* Some abstractions */
#define JANET_TFLAG_BOOLEAN (JANET_TFLAG_TRUE | JANET_TFLAG_FALSE)
#define JANET_TFLAG_CALLABLE (JANET_TFLAG_FUNCTION | JANET_TFLAG_CFUNCTION)
#define JANET_TFLAG_BYTES (JANET_TFLAG_STRING | JANET_TFLAG_SYMBOL | JANET_TFLAG_BUFFER)
#define JANET_TFLAG_BYTES (JANET_TFLAG_STRING | JANET_TFLAG_SYMBOL | JANET_TFLAG_BUFFER | JANET_KEYWORD)
#define JANET_TFLAG_INDEXED (JANET_TFLAG_ARRAY | JANET_TFLAG_TUPLE)
#define JANET_TFLAG_DICTIONARY (JANET_TFLAG_TABLE | JANET_TFLAG_STRUCT)
#define JANET_TFLAG_LENGTHABLE (JANET_TFLAG_BYTES | JANET_TFLAG_INDEXED | JANET_TFLAG_DICTIONARY)
@ -411,6 +413,7 @@ JANET_API Janet janet_nanbox_from_bits(uint64_t bits);
#define janet_wrap_buffer(s) janet_nanbox_wrap_((s), JANET_BUFFER)
#define janet_wrap_string(s) janet_nanbox_wrap_c((s), JANET_STRING)
#define janet_wrap_symbol(s) janet_nanbox_wrap_c((s), JANET_SYMBOL)
#define janet_wrap_keyword(s) janet_nanbox_wrap_c((s), JANET_KEYWORD)
#define janet_wrap_abstract(s) janet_nanbox_wrap_((s), JANET_ABSTRACT)
#define janet_wrap_function(s) janet_nanbox_wrap_((s), JANET_FUNCTION)
#define janet_wrap_cfunction(s) janet_nanbox_wrap_((s), JANET_CFUNCTION)
@ -424,6 +427,7 @@ JANET_API Janet janet_nanbox_from_bits(uint64_t bits);
#define janet_unwrap_buffer(x) ((JanetBuffer *)janet_nanbox_to_pointer(x))
#define janet_unwrap_string(x) ((const uint8_t *)janet_nanbox_to_pointer(x))
#define janet_unwrap_symbol(x) ((const uint8_t *)janet_nanbox_to_pointer(x))
#define janet_unwrap_keyword(x) ((const uint8_t *)janet_nanbox_to_pointer(x))
#define janet_unwrap_abstract(x) (janet_nanbox_to_pointer(x))
#define janet_unwrap_pointer(x) (janet_nanbox_to_pointer(x))
#define janet_unwrap_function(x) ((JanetFunction *)janet_nanbox_to_pointer(x))
@ -479,6 +483,7 @@ JANET_API Janet janet_nanbox32_from_tagp(uint32_t tag, void *pointer);
#define janet_wrap_buffer(s) janet_nanbox32_from_tagp(JANET_BUFFER, (void *)(s))
#define janet_wrap_string(s) janet_nanbox32_from_tagp(JANET_STRING, (void *)(s))
#define janet_wrap_symbol(s) janet_nanbox32_from_tagp(JANET_SYMBOL, (void *)(s))
#define janet_wrap_keyword(s) janet_nanbox32_from_tagp(JANET_SYMBOL, (void *)(s))
#define janet_wrap_abstract(s) janet_nanbox32_from_tagp(JANET_ABSTRACT, (void *)(s))
#define janet_wrap_function(s) janet_nanbox32_from_tagp(JANET_FUNCTION, (void *)(s))
#define janet_wrap_cfunction(s) janet_nanbox32_from_tagp(JANET_CFUNCTION, (void *)(s))
@ -491,6 +496,7 @@ JANET_API Janet janet_nanbox32_from_tagp(uint32_t tag, void *pointer);
#define janet_unwrap_buffer(x) ((JanetBuffer *)(x).tagged.payload.pointer)
#define janet_unwrap_string(x) ((const uint8_t *)(x).tagged.payload.pointer)
#define janet_unwrap_symbol(x) ((const uint8_t *)(x).tagged.payload.pointer)
#define janet_unwrap_keyword(x) ((const uint8_t *)(x).tagged.payload.pointer)
#define janet_unwrap_abstract(x) ((x).tagged.payload.pointer)
#define janet_unwrap_pointer(x) ((x).tagged.payload.pointer)
#define janet_unwrap_function(x) ((JanetFunction *)(x).tagged.payload.pointer)
@ -526,6 +532,7 @@ struct Janet {
#define janet_unwrap_buffer(x) ((JanetBuffer *)(x).as.pointer)
#define janet_unwrap_string(x) ((const uint8_t *)(x).as.pointer)
#define janet_unwrap_symbol(x) ((const uint8_t *)(x).as.pointer)
#define janet_unwrap_keyword(x) ((const uint8_t *)(x).as.pointer)
#define janet_unwrap_abstract(x) ((x).as.pointer)
#define janet_unwrap_pointer(x) ((x).as.pointer)
#define janet_unwrap_function(x) ((JanetFunction *)(x).as.pointer)
@ -540,6 +547,7 @@ JANET_API Janet janet_wrap_false(void);
JANET_API Janet janet_wrap_boolean(int x);
JANET_API Janet janet_wrap_string(const uint8_t *x);
JANET_API Janet janet_wrap_symbol(const uint8_t *x);
JANET_API Janet janet_wrap_keyword(const uint8_t *x);
JANET_API Janet janet_wrap_array(JanetArray *x);
JANET_API Janet janet_wrap_tuple(const Janet *x);
JANET_API Janet janet_wrap_struct(const JanetKV *x);
@ -908,7 +916,7 @@ JANET_API int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len,
JANET_API int janet_dostring(JanetTable *env, const char *str, const char *sourcePath, Janet *out);
/* Number scanning */
JANET_API double janet_scan_number(const uint8_t *str, int32_t len, int *err);
JANET_API int janet_scan_number(const uint8_t *str, int32_t len, double *out);
/* Debugging */
JANET_API int janet_debug_break(JanetFuncDef *def, int32_t pc);
@ -985,6 +993,13 @@ JANET_API const uint8_t *janet_symbol_gen(void);
#define janet_symbolv(str, len) janet_wrap_symbol(janet_symbol((str), (len)))
#define janet_csymbolv(cstr) janet_wrap_symbol(janet_csymbol(cstr))
/* Keyword functions */
#define janet_keyword janet_symbol
#define janet_keyword_from_string janet_symbol_from_string
#define janet_ckeyword janet_csymbol
#define janet_keywordv(str, len) janet_wrap_keyword(janet_keyword((str), (len)))
#define janet_ckeywordv(cstr) janet_wrap_keyword(janet_ckeyword(cstr))
/* Structs */
#define janet_struct_raw(t) ((int32_t *)(t) - 4)
#define janet_struct_length(t) (janet_struct_raw(t)[0])
@ -1182,6 +1197,7 @@ JANET_API int janet_typeabstract_err(JanetArgs args, int32_t n, const JanetAbstr
#define JANET_ARG_NUMBER(DEST, A, N) _JANET_ARG(JANET_NUMBER, number, DEST, A, N)
#define JANET_ARG_STRING(DEST, A, N) _JANET_ARG(JANET_STRING, string, DEST, A, N)
#define JANET_ARG_SYMBOL(DEST, A, N) _JANET_ARG(JANET_SYMBOL, symbol, DEST, A, N)
#define JANET_ARG_KEYWORD(DEST, A, N) _JANET_ARG(JANET_KEYWORD, keyword, 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_TABLE(DEST, A, N) _JANET_ARG(JANET_TABLE, table, DEST, A, N)
@ -1227,6 +1243,7 @@ JANET_API int janet_typeabstract_err(JanetArgs args, int32_t n, const JanetAbstr
#define JANET_RETURN_NUMBER(A, X) JANET_RETURN(A, janet_wrap_number(X))
#define JANET_RETURN_STRING(A, X) JANET_RETURN(A, janet_wrap_string(X))
#define JANET_RETURN_SYMBOL(A, X) JANET_RETURN(A, janet_wrap_symbol(X))
#define JANET_RETURN_KEYWORD(A, X) JANET_RETURN(A, janet_wrap_keyword(X))
#define JANET_RETURN_ARRAY(A, X) JANET_RETURN(A, janet_wrap_array(X))
#define JANET_RETURN_TUPLE(A, X) JANET_RETURN(A, janet_wrap_tuple(X))
#define JANET_RETURN_TABLE(A, X) JANET_RETURN(A, janet_wrap_table(X))
@ -1238,6 +1255,7 @@ JANET_API int janet_typeabstract_err(JanetArgs args, int32_t n, const JanetAbstr
#define JANET_RETURN_CSTRING(A, X) JANET_RETURN(A, janet_cstringv(X))
#define JANET_RETURN_CSYMBOL(A, X) JANET_RETURN(A, janet_csymbolv(X))
#define JANET_RETURN_CKEYWORD(A, X) JANET_RETURN(A, janet_ckeywordv(X))
#define JANET_RETURN_INTEGER(A, X) JANET_RETURN(A, janet_wrap_number((double) (X)))

View File

@ -41,6 +41,7 @@
(fiber/new (fn [] 1))
"hi"
(quote hello)
:hello
(array 1 2 3)
(tuple 1 2 3)
(table "a" "b" "c" "d")
@ -201,7 +202,7 @@
(def 🦊 :fox)
(def 🐮 :cow)
(assert (= (string "🐼" 🦊 🐮) "🐼:fox:cow") "emojis 🙉 :)")
(assert (= (string "🐼" 🦊 🐮) "🐼foxcow") "emojis 🙉 :)")
(assert (not= 🦊 "🦊") "utf8 strings are not symbols and vice versa")
# Symbols with @ character