diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f879249..7bffbd1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to this project will be documented in this file. ## 0.4.0 - ?? +- Add janet\_getmethod to CAPI for easier use of method like syntax. +- Add get/set to abstract types to allow them to behave more + like objects with methods. - Add parser/insert to modify parser state programmatically - Add debug/stacktrace for easy, pretty stacktraces - Remove the status-pp function diff --git a/examples/numarray/build.janet b/examples/numarray/build.janet index 16edc0ed..75d688d8 100644 --- a/examples/numarray/build.janet +++ b/examples/numarray/build.janet @@ -20,5 +20,4 @@ (:scale a 5) (for i 0 10 (print (a i))) -# (print "sum=" (:sum a)) diff --git a/src/core/capi.c b/src/core/capi.c index d1cfe78a..192f8f63 100644 --- a/src/core/capi.c +++ b/src/core/capi.c @@ -73,6 +73,16 @@ type janet_get##name(const Janet *argv, int32_t n) { \ return janet_unwrap_##name(x); \ } +Janet janet_getmethod(const uint8_t *method, const JanetMethod *methods) { + while (methods->name) { + if (!janet_cstrcmp(method, methods->name)) + return janet_wrap_cfunction(methods->cfun); + methods++; + } + janet_panicf("unknown method %S invoked", method); + return janet_wrap_nil(); +} + DEFINE_GETTER(number, NUMBER, double) DEFINE_GETTER(array, ARRAY, JanetArray *) DEFINE_GETTER(tuple, TUPLE, const Janet *) diff --git a/src/core/parse.c b/src/core/parse.c index 855e1d05..09330a0a 100644 --- a/src/core/parse.c +++ b/src/core/parse.c @@ -609,11 +609,13 @@ static int parsergc(void *p, size_t size) { return 0; } +static Janet parserget(void *p, Janet key); + static JanetAbstractType janet_parse_parsertype = { "core/parser", parsergc, parsermark, - NULL, + parserget, NULL }; @@ -772,6 +774,26 @@ static Janet cfun_parse_state(int32_t argc, Janet *argv) { return janet_wrap_string(str); } +static const JanetMethod parser_methods[] = { + {"byte", cfun_parse_byte}, + {"consume", cfun_parse_consume}, + {"error", cfun_parse_error}, + {"flush", cfun_parse_flush}, + {"has-more", cfun_parse_has_more}, + {"insert", cfun_parse_insert}, + {"produce", cfun_parse_produce}, + {"state", cfun_parse_state}, + {"status", cfun_parse_status}, + {"where", cfun_parse_where}, + {NULL, NULL} +}; + +static Janet parserget(void *p, Janet key) { + (void) p; + if (!janet_checktype(key, JANET_KEYWORD)) janet_panicf("expected keyword method"); + return janet_getmethod(janet_unwrap_keyword(key), parser_methods); +} + static const JanetReg parse_cfuns[] = { { "parser/new", cfun_parse_parser, diff --git a/src/include/janet/janet.h b/src/include/janet/janet.h index fd6e16fa..e4a0f385 100644 --- a/src/include/janet/janet.h +++ b/src/include/janet/janet.h @@ -266,6 +266,7 @@ typedef struct JanetKV JanetKV; typedef struct JanetStackFrame JanetStackFrame; typedef struct JanetAbstractType JanetAbstractType; typedef struct JanetReg JanetReg; +typedef struct JanetMethod JanetMethod; typedef struct JanetSourceMapping JanetSourceMapping; typedef struct JanetView JanetView; typedef struct JanetByteView JanetByteView; @@ -754,6 +755,11 @@ struct JanetReg { const char *documentation; }; +struct JanetMethod { + const char *name; + JanetCFunction cfun; +}; + struct JanetView { const Janet *items; int32_t len; @@ -1146,6 +1152,7 @@ JANET_API void janet_panic_abstract(Janet x, int32_t n, const JanetAbstractType JANET_API void janet_arity(int32_t arity, int32_t min, int32_t max); JANET_API void janet_fixarity(int32_t arity, int32_t fix); +JANET_API Janet janet_getmethod(const uint8_t *method, const JanetMethod *methods); JANET_API double janet_getnumber(const Janet *argv, int32_t n); JANET_API JanetArray *janet_getarray(const Janet *argv, int32_t n); JANET_API const Janet *janet_gettuple(const Janet *argv, int32_t n);