From ab782d889671494fd357f5a647f04b30c03f320d Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Tue, 6 Aug 2019 18:10:48 -0500 Subject: [PATCH] Add optional default value to get. Also update CHANGELOG.md --- CHANGELOG.md | 3 +++ src/core/corelib.c | 13 +++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e1b99ca..36180ed6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to this project will be documented in this file. ## Unreleased +- Add `take` and `drop` functions that are easier to use compared to the + existing slice functions. +- Add optional default value to `get`. - Add function literal short-hand via `|` reader macro, which maps to the `short-fn` macro. - Add `int?` and `nat?` functions to the core. diff --git a/src/core/corelib.c b/src/core/corelib.c index 12533f44..5f1e19cf 100644 --- a/src/core/corelib.c +++ b/src/core/corelib.c @@ -882,7 +882,11 @@ static const uint32_t resume_asm[] = { }; static const uint32_t get_asm[] = { JOP_GET | (1 << 24), - JOP_RETURN + JOP_LOAD_NIL | (3 << 8), + JOP_EQUALS | (3 << 8) | (3 << 24), + JOP_JUMP_IF | (3 << 8) | (2 << 16), + JOP_RETURN, + JOP_RETURN | (2 << 8) }; static const uint32_t put_asm[] = { JOP_PUT | (1 << 16) | (2 << 24), @@ -937,13 +941,14 @@ JanetTable *janet_core_env(JanetTable *replacements) { "the dispatch function in the case of a new fiber. Returns either the return result of " "the fiber's dispatch function, or the value from the next yield call in fiber.")); janet_quick_asm(env, JANET_FUN_GET, - "get", 2, 2, 2, 2, get_asm, sizeof(get_asm), - JDOC("(get ds key)\n\n" + "get", 3, 2, 3, 4, get_asm, sizeof(get_asm), + JDOC("(get ds key &opt dflt)\n\n" "Get a value from any associative data structure. Arrays, tuples, tables, structs, strings, " "symbols, and buffers are all associative and can be used with get. Order structures, name " "arrays, tuples, strings, buffers, and symbols must use integer keys. Structs and tables can " "take any value as a key except nil and return a value except nil. Byte sequences will return " - "integer representations of bytes as result of a get call.")); + "integer representations of bytes as result of a get call. If no values is found, will return " + "dflt or nil if no default is provided.")); janet_quick_asm(env, JANET_FUN_PUT, "put", 3, 3, 3, 3, put_asm, sizeof(put_asm), JDOC("(put ds key value)\n\n"