1
0
mirror of https://github.com/janet-lang/janet synced 2025-01-26 07:06:51 +00:00

Add make format to format code.

A consistent style should help with contributors and
readability. We use astyle as the formatter as can make a pretty
good approximation of the current style and my preferred style.

Astyle can be found at http://astyle.sourceforge.net/astyle.html
This commit is contained in:
Calvin Rose 2019-02-19 20:51:34 -05:00
parent 7c19ed8a48
commit 9d4effc02e
37 changed files with 2472 additions and 2503 deletions

View File

@ -209,6 +209,13 @@ build/doc.html: $(JANET_TARGET) tools/gendoc.janet
##### Other #####
#################
STYLEOPTS=--style=attach --indent-switches --convert-tabs \
--align-pointer=name --pad-header --pad-oper --unpad-paren --indent-labels
format:
astyle $(STYLEOPTS) */*.c
astyle $(STYLEOPTS) */*/*.c
astyle $(STYLEOPTS) */*/*.h
grammar: build/janet.tmLanguage
build/janet.tmLanguage: tools/tm_lang_gen.janet $(JANET_TARGET)
$(JANET_TARGET) $< > $@
@ -236,5 +243,5 @@ uninstall:
-rm -rf $(INCLUDEDIR)
.PHONY: clean install repl debug valgrind test amalg \
valtest emscripten dist uninstall docs grammar \
valtest emscripten dist uninstall docs grammar format \
$(TEST_PROGRAM_PHONIES) $(TEST_PROGRAM_VALPHONIES)

View File

@ -99,11 +99,13 @@ Janet num_array_get(void *p, Janet key) {
}
static const JanetReg cfuns[] = {
{"numarray/new", num_array_new,
{
"numarray/new", num_array_new,
"(numarray/new size)\n\n"
"Create new numarray"
},
{"numarray/scale", num_array_scale,
{
"numarray/scale", num_array_scale,
"(numarray/scale numarray factor)\n\n"
"scale numarray by factor"
},

View File

@ -180,8 +180,7 @@ static Janet cfun_array_concat(int32_t argc, Janet *argv) {
janet_array_push(array, argv[i]);
break;
case JANET_ARRAY:
case JANET_TUPLE:
{
case JANET_TUPLE: {
int32_t j, len;
const Janet *vals;
janet_indexed_view(argv[i], &vals, &len);
@ -216,46 +215,54 @@ static Janet cfun_array_insert(int32_t argc, Janet *argv) {
}
static const JanetReg array_cfuns[] = {
{"array/new", cfun_array_new,
{
"array/new", cfun_array_new,
JDOC("(array/new capacity)\n\n"
"Creates a new empty array with a pre-allocated capacity. The same as "
"(array) but can be more efficient if the maximum size of an array is known.")
},
{"array/pop", cfun_array_pop,
{
"array/pop", cfun_array_pop,
JDOC("(array/pop arr)\n\n"
"Remove the last element of the array and return it. If the array is empty, will return nil. Modifies "
"the input array.")
},
{"array/peek", cfun_array_peek,
{
"array/peek", cfun_array_peek,
JDOC("(array/peek arr)\n\n"
"Returns the last element of the array. Does not modify the array.")
},
{"array/push", cfun_array_push,
{
"array/push", cfun_array_push,
JDOC("(array/push arr x)\n\n"
"Insert an element in the end of an array. Modifies the input array and returns it.")
},
{"array/ensure", cfun_array_ensure,
{
"array/ensure", cfun_array_ensure,
JDOC("(array/ensure arr capacity)\n\n"
"Ensures that the memory backing the array has enough memory for capacity "
"items. Capacity must be an integer. If the backing capacity is already enough, "
"then this function does nothing. Otherwise, the backing memory will be reallocated "
"so that there is enough space.")
},
{"array/slice", cfun_array_slice,
{
"array/slice", cfun_array_slice,
JDOC("(array/slice arrtup [, start=0 [, end=(length arrtup)]])\n\n"
"Takes a slice of array or tuple from start to end. The range is half open, "
"[start, end). Indexes can also be negative, indicating indexing from the end of the "
"end of the array. By default, start is 0 and end is the length of the array. "
"Returns a new array.")
},
{"array/concat", cfun_array_concat,
{
"array/concat", cfun_array_concat,
JDOC("(array/concat arr & parts)\n\n"
"Concatenates a variadic number of arrays (and tuples) into the first argument "
"which must an array. If any of the parts are arrays or tuples, their elements will "
"be inserted into the array. Otherwise, each part in parts will be appended to arr in order. "
"Return the modified array arr.")
},
{"array/insert", cfun_array_insert,
{
"array/insert", cfun_array_insert,
JDOC("(array/insert arr at & xs)\n\n"
"Insert all of xs into array arr at index at. at should be an integer "
"0 and the length of the array. A negative value for at will index from "

View File

@ -253,8 +253,7 @@ static int32_t doarg_1(
default:
goto error;
break;
case JANET_NUMBER:
{
case JANET_NUMBER: {
double y = janet_unwrap_number(x);
if (janet_checkintrange(y)) {
ret = (int32_t) y;
@ -263,8 +262,7 @@ static int32_t doarg_1(
}
break;
}
case JANET_TUPLE:
{
case JANET_TUPLE: {
const Janet *t = janet_unwrap_tuple(x);
if (argtype == JANET_OAT_TYPE) {
int32_t i = 0;
@ -277,8 +275,7 @@ static int32_t doarg_1(
}
break;
}
case JANET_KEYWORD:
{
case JANET_KEYWORD: {
if (NULL != c && argtype == JANET_OAT_LABEL) {
Janet result = janet_table_get(c, x);
if (janet_checktype(result, JANET_NUMBER)) {
@ -302,8 +299,7 @@ static int32_t doarg_1(
}
break;
}
case JANET_SYMBOL:
{
case JANET_SYMBOL: {
if (NULL != c) {
Janet result = janet_table_get(c, x);
if (janet_checktype(result, JANET_NUMBER)) {
@ -364,44 +360,38 @@ static uint32_t read_instruction(
uint32_t instr = idef->opcode;
enum JanetInstructionType type = janet_instructions[idef->opcode];
switch (type) {
case JINT_0:
{
case JINT_0: {
if (janet_tuple_length(argt) != 1)
janet_asm_error(a, "expected 0 arguments: (op)");
break;
}
case JINT_S:
{
case JINT_S: {
if (janet_tuple_length(argt) != 2)
janet_asm_error(a, "expected 1 argument: (op, slot)");
instr |= doarg(a, JANET_OAT_SLOT, 1, 2, 0, argt[1]);
break;
}
case JINT_L:
{
case JINT_L: {
if (janet_tuple_length(argt) != 2)
janet_asm_error(a, "expected 1 argument: (op, label)");
instr |= doarg(a, JANET_OAT_LABEL, 1, 3, 1, argt[1]);
break;
}
case JINT_SS:
{
case JINT_SS: {
if (janet_tuple_length(argt) != 3)
janet_asm_error(a, "expected 2 arguments: (op, slot, slot)");
instr |= doarg(a, JANET_OAT_SLOT, 1, 1, 0, argt[1]);
instr |= doarg(a, JANET_OAT_SLOT, 2, 2, 0, argt[2]);
break;
}
case JINT_SL:
{
case JINT_SL: {
if (janet_tuple_length(argt) != 3)
janet_asm_error(a, "expected 2 arguments: (op, slot, label)");
instr |= doarg(a, JANET_OAT_SLOT, 1, 1, 0, argt[1]);
instr |= doarg(a, JANET_OAT_LABEL, 2, 2, 1, argt[2]);
break;
}
case JINT_ST:
{
case JINT_ST: {
if (janet_tuple_length(argt) != 3)
janet_asm_error(a, "expected 2 arguments: (op, slot, type)");
instr |= doarg(a, JANET_OAT_SLOT, 1, 1, 0, argt[1]);
@ -409,24 +399,21 @@ static uint32_t read_instruction(
break;
}
case JINT_SI:
case JINT_SU:
{
case JINT_SU: {
if (janet_tuple_length(argt) != 3)
janet_asm_error(a, "expected 2 arguments: (op, slot, integer)");
instr |= doarg(a, JANET_OAT_SLOT, 1, 1, 0, argt[1]);
instr |= doarg(a, JANET_OAT_INTEGER, 2, 2, type == JINT_SI, argt[2]);
break;
}
case JINT_SD:
{
case JINT_SD: {
if (janet_tuple_length(argt) != 3)
janet_asm_error(a, "expected 2 arguments: (op, slot, funcdef)");
instr |= doarg(a, JANET_OAT_SLOT, 1, 1, 0, argt[1]);
instr |= doarg(a, JANET_OAT_FUNCDEF, 2, 2, 0, argt[2]);
break;
}
case JINT_SSS:
{
case JINT_SSS: {
if (janet_tuple_length(argt) != 4)
janet_asm_error(a, "expected 3 arguments: (op, slot, slot, slot)");
instr |= doarg(a, JANET_OAT_SLOT, 1, 1, 0, argt[1]);
@ -435,8 +422,7 @@ static uint32_t read_instruction(
break;
}
case JINT_SSI:
case JINT_SSU:
{
case JINT_SSU: {
if (janet_tuple_length(argt) != 4)
janet_asm_error(a, "expected 3 arguments: (op, slot, slot, integer)");
instr |= doarg(a, JANET_OAT_SLOT, 1, 1, 0, argt[1]);
@ -444,8 +430,7 @@ static uint32_t read_instruction(
instr |= doarg(a, JANET_OAT_INTEGER, 3, 1, type == JINT_SSI, argt[3]);
break;
}
case JINT_SES:
{
case JINT_SES: {
JanetAssembler *b = a;
uint32_t env;
if (janet_tuple_length(argt) != 4)
@ -461,8 +446,7 @@ static uint32_t read_instruction(
instr |= doarg(b, JANET_OAT_SLOT, 3, 1, 0, argt[3]);
break;
}
case JINT_SC:
{
case JINT_SC: {
if (janet_tuple_length(argt) != 3)
janet_asm_error(a, "expected 2 arguments: (op, slot, constant)");
instr |= doarg(a, JANET_OAT_SLOT, 1, 1, 0, argt[1]);
@ -934,13 +918,15 @@ static Janet cfun_disasm(int32_t argc, Janet *argv) {
}
static const JanetReg asm_cfuns[] = {
{"asm", cfun_asm,
{
"asm", cfun_asm,
JDOC("(asm assembly)\n\n"
"Returns a new function that is the compiled result of the assembly.\n"
"The syntax for the assembly can be found on the janet wiki. Will throw an\n"
"error on invalid assembly.")
},
{"disasm", cfun_disasm,
{
"disasm", cfun_disasm,
JDOC("(disasm func)\n\n"
"Returns assembly that could be used be compile the given function.\n"
"func must be a function, not a c function. Will throw on error on a badly\n"

View File

@ -326,72 +326,86 @@ static Janet cfun_buffer_format(int32_t argc, Janet *argv) {
}
static const JanetReg buffer_cfuns[] = {
{"buffer/new", cfun_buffer_new,
{
"buffer/new", cfun_buffer_new,
JDOC("(buffer/new capacity)\n\n"
"Creates a new, empty buffer with enough memory for capacity bytes. "
"Returns a new buffer.")
},
{"buffer/new-filled", cfun_buffer_new_filled,
{
"buffer/new-filled", cfun_buffer_new_filled,
JDOC("(buffer/new-filled count [, byte=0])\n\n"
"Creates a new buffer of length count filled with byte. "
"Returns the new buffer.")
},
{"buffer/push-byte", cfun_buffer_u8,
{
"buffer/push-byte", cfun_buffer_u8,
JDOC("(buffer/push-byte buffer x)\n\n"
"Append a byte to a buffer. Will expand the buffer as necessary. "
"Returns the modified buffer. Will throw an error if the buffer overflows.")
},
{"buffer/push-word", cfun_buffer_word,
{
"buffer/push-word", cfun_buffer_word,
JDOC("(buffer/push-word buffer x)\n\n"
"Append a machine word to a buffer. The 4 bytes of the integer are appended "
"in twos complement, big endian order, unsigned. Returns the modified buffer. Will "
"throw an error if the buffer overflows.")
},
{"buffer/push-string", cfun_buffer_chars,
{
"buffer/push-string", cfun_buffer_chars,
JDOC("(buffer/push-string buffer str)\n\n"
"Push a string onto the end of a buffer. Non string values will be converted "
"to strings before being pushed. Returns the modified buffer. "
"Will throw an error if the buffer overflows.")
},
{"buffer/popn", cfun_buffer_popn,
{
"buffer/popn", cfun_buffer_popn,
JDOC("(buffer/popn buffer n)\n\n"
"Removes the last n bytes from the buffer. Returns the modified buffer.")
},
{"buffer/clear", cfun_buffer_clear,
{
"buffer/clear", cfun_buffer_clear,
JDOC("(buffer/clear buffer)\n\n"
"Sets the size of a buffer to 0 and empties it. The buffer retains "
"its memory so it can be efficiently refilled. Returns the modified buffer.")
},
{"buffer/slice", cfun_buffer_slice,
{
"buffer/slice", cfun_buffer_slice,
JDOC("(buffer/slice bytes [, start=0 [, end=(length bytes)]])\n\n"
"Takes a slice of a byte sequence from start to end. The range is half open, "
"[start, end). Indexes can also be negative, indicating indexing from the end of the "
"end of the array. By default, start is 0 and end is the length of the buffer. "
"Returns a new buffer.")
},
{"buffer/bit-set", cfun_buffer_bitset,
{
"buffer/bit-set", cfun_buffer_bitset,
JDOC("(buffer/bit-set buffer index)\n\n"
"Sets the bit at the given bit-index. Returns the buffer.")
},
{"buffer/bit-clear", cfun_buffer_bitclear,
{
"buffer/bit-clear", cfun_buffer_bitclear,
JDOC("(buffer/bit-clear buffer index)\n\n"
"Clears the bit at the given bit-index. Returns the buffer.")
},
{"buffer/bit", cfun_buffer_bitget,
{
"buffer/bit", cfun_buffer_bitget,
JDOC("(buffer/bit buffer index)\n\n"
"Gets the bit at the given bit-index. Returns true if the bit is set, false if not.")
},
{"buffer/bit-toggle", cfun_buffer_bittoggle,
{
"buffer/bit-toggle", cfun_buffer_bittoggle,
JDOC("(buffer/bit-toggle buffer index)\n\n"
"Toggles the bit at the given bit index in buffer. Returns the buffer.")
},
{"buffer/blit", cfun_buffer_blit,
{
"buffer/blit", cfun_buffer_blit,
JDOC("(buffer/blit dest src [, dest-start=0 [, src-start=0 [, src-end=-1]]])\n\n"
"Insert the contents of src into dest. Can optionally take indices that "
"indicate which part of src to copy into which part of dest. Indices can be "
"negative to index from the end of src or dest. Returns dest.")
},
{"buffer/format", cfun_buffer_format,
{
"buffer/format", cfun_buffer_format,
JDOC("(buffer/format buffer format & args)\n\n"
"Snprintf like functionality for printing values into a buffer. Returns "
" the modified buffer.")

View File

@ -118,65 +118,55 @@ int32_t janet_verify(JanetFuncDef *def) {
switch (type) {
case JINT_0:
continue;
case JINT_S:
{
case JINT_S: {
if ((int32_t)(instr >> 8) >= sc) return 4;
continue;
}
case JINT_SI:
case JINT_SU:
case JINT_ST:
{
case JINT_ST: {
if ((int32_t)((instr >> 8) & 0xFF) >= sc) return 4;
continue;
}
case JINT_L:
{
case JINT_L: {
int32_t jumpdest = i + (((int32_t)instr) >> 8);
if (jumpdest < 0 || jumpdest >= def->bytecode_length) return 5;
continue;
}
case JINT_SS:
{
case JINT_SS: {
if ((int32_t)((instr >> 8) & 0xFF) >= sc ||
(int32_t)(instr >> 16) >= sc) return 4;
continue;
}
case JINT_SSI:
case JINT_SSU:
{
case JINT_SSU: {
if ((int32_t)((instr >> 8) & 0xFF) >= sc ||
(int32_t)((instr >> 16) & 0xFF) >= sc) return 4;
continue;
}
case JINT_SL:
{
case JINT_SL: {
int32_t jumpdest = i + (((int32_t)instr) >> 16);
if ((int32_t)((instr >> 8) & 0xFF) >= sc) return 4;
if (jumpdest < 0 || jumpdest >= def->bytecode_length) return 5;
continue;
}
case JINT_SSS:
{
case JINT_SSS: {
if (((int32_t)(instr >> 8) & 0xFF) >= sc ||
((int32_t)(instr >> 16) & 0xFF) >= sc ||
((int32_t)(instr >> 24) & 0xFF) >= sc) return 4;
continue;
}
case JINT_SD:
{
case JINT_SD: {
if ((int32_t)((instr >> 8) & 0xFF) >= sc) return 4;
if ((int32_t)(instr >> 16) >= def->defs_length) return 6;
continue;
}
case JINT_SC:
{
case JINT_SC: {
if ((int32_t)((instr >> 8) & 0xFF) >= sc) return 4;
if ((int32_t)(instr >> 16) >= def->constants_length) return 7;
continue;
}
case JINT_SES:
{
case JINT_SES: {
/* How can we check the last slot index? We need info parent funcdefs. Resort
* to runtime checks for now. Maybe invalid upvalue references could be defaulted
* to nil? (don't commit to this in the long term, though) */

View File

@ -206,8 +206,7 @@ JanetSlot janetc_resolve(
case JANET_BINDING_DEF:
case JANET_BINDING_MACRO: /* Macro should function like defs when not in calling pos */
return janetc_cslot(check);
case JANET_BINDING_VAR:
{
case JANET_BINDING_VAR: {
JanetSlot ret = janetc_cslot(check);
/* TODO save type info */
ret.flags |= JANET_SLOT_REF | JANET_SLOT_NAMED | JANET_SLOT_MUTABLE | JANET_SLOTTYPE_ANY;
@ -548,8 +547,7 @@ JanetSlot janetc_value(JanetFopts opts, Janet x) {
ret = spec->compile(opts, janet_tuple_length(tup) - 1, tup + 1);
} else {
switch (janet_type(x)) {
case JANET_TUPLE:
{
case JANET_TUPLE: {
JanetFopts subopts = janetc_fopts_default(c);
const Janet *tup = janet_unwrap_tuple(x);
/* Empty tuple is tuple literal */
@ -737,7 +735,8 @@ static Janet cfun(int32_t argc, Janet *argv) {
}
static const JanetReg compile_cfuns[] = {
{"compile", cfun,
{
"compile", cfun,
JDOC("(compile ast env [, source])\n\n"
"Compiles an Abstract Syntax Tree (ast) into a janet function. "
"Pair the compile function with parsing functionality to implement "

View File

@ -244,7 +244,8 @@ static Janet janet_core_hash(int32_t argc, Janet *argv) {
}
static const JanetReg corelib_cfuns[] = {
{"native", janet_core_native,
{
"native", janet_core_native,
JDOC("(native path [,env])\n\n"
"Load a native module from the given path. The path "
"must be an absolute or relative path on the file system, and is "
@ -252,95 +253,112 @@ static const JanetReg corelib_cfuns[] = {
"Returns an environment table that contains functions and other values "
"from the native module.")
},
{"print", janet_core_print,
{
"print", janet_core_print,
JDOC("(print & xs)\n\n"
"Print values to the console (standard out). Value are converted "
"to strings if they are not already. After printing all values, a "
"newline character is printed. Returns nil.")
},
{"describe", janet_core_describe,
{
"describe", janet_core_describe,
JDOC("(describe x)\n\n"
"Returns a string that is a human readable description of a value x.")
},
{"string", janet_core_string,
{
"string", janet_core_string,
JDOC("(string & parts)\n\n"
"Creates a string by concatenating values together. Values are "
"converted to bytes via describe if they are not byte sequences. "
"Returns the new string.")
},
{"symbol", janet_core_symbol,
{
"symbol", janet_core_symbol,
JDOC("(symbol & xs)\n\n"
"Creates a symbol by concatenating values together. Values are "
"converted to bytes via describe if they are not byte sequences. Returns "
"the new symbol.")
},
{"keyword", janet_core_keyword,
{
"keyword", janet_core_keyword,
JDOC("(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", janet_core_buffer,
JDOC("(buffer & xs)\n\n"
"Creates a new buffer by concatenating values together. Values are "
"converted to bytes via describe if they are not byte sequences. Returns "
"the new buffer.")
},
{"abstract?", janet_core_is_abstract,
{
"abstract?", janet_core_is_abstract,
JDOC("(abstract? x)\n\n"
"Check if x is an abstract type.")
},
{"table", janet_core_table,
{
"table", janet_core_table,
JDOC("(table & kvs)\n\n"
"Creates a new table from a variadic number of keys and values. "
"kvs is a sequence k1, v1, k2, v2, k3, v3, ... If kvs has "
"an odd number of elements, an error will be thrown. Returns the "
"new table.")
},
{"array", janet_core_array,
{
"array", janet_core_array,
JDOC("(array & items)\n\n"
"Create a new array that contains items. Returns the new array.")
},
{"scan-number", janet_core_scannumber,
{
"scan-number", janet_core_scannumber,
JDOC("(scan-number str)\n\n"
"Parse a number from a byte sequence an return that number, either and integer "
"or a real. The number "
"must be in the same format as numbers in janet source code. Will return nil "
"on an invalid number.")
},
{"tuple", janet_core_tuple,
{
"tuple", janet_core_tuple,
JDOC("(tuple & items)\n\n"
"Creates a new tuple that contains items. Returns the new tuple.")
},
{"struct", janet_core_struct,
{
"struct", janet_core_struct,
JDOC("(struct & kvs)\n\n"
"Create a new struct from a sequence of key value pairs. "
"kvs is a sequence k1, v1, k2, v2, k3, v3, ... If kvs has "
"an odd number of elements, an error will be thrown. Returns the "
"new struct.")
},
{"gensym", janet_core_gensym,
{
"gensym", janet_core_gensym,
JDOC("(gensym)\n\n"
"Returns a new symbol that is unique across the runtime. This means it "
"will not collide with any already created symbols during compilation, so "
"it can be used in macros to generate automatic bindings.")
},
{"gccollect", janet_core_gccollect,
{
"gccollect", janet_core_gccollect,
JDOC("(gccollect)\n\n"
"Run garbage collection. You should probably not call this manually.")
},
{"gcsetinterval", janet_core_gcsetinterval,
{
"gcsetinterval", janet_core_gcsetinterval,
JDOC("(gcsetinterval interval)\n\n"
"Set an integer number of bytes to allocate before running garbage collection. "
"Low valuesi for interval will be slower but use less memory. "
"High values will be faster but use more memory.")
},
{"gcinterval", janet_core_gcinterval,
{
"gcinterval", janet_core_gcinterval,
JDOC("(gcinterval)\n\n"
"Returns the integer number of bytes to allocate before running an iteration "
"of garbage collection.")
},
{"type", janet_core_type,
{
"type", janet_core_type,
JDOC("(type x)\n\n"
"Returns the type of x as a keyword symbol. x is one of\n"
"\t:nil\n"
@ -359,7 +377,8 @@ static const JanetReg corelib_cfuns[] = {
"\t:cfunction\n\n"
"or another symbol for an abstract type.")
},
{"next", janet_core_next,
{
"next", janet_core_next,
JDOC("(next dict key)\n\n"
"Gets the next key in a struct or table. Can be used to iterate through "
"the keys of a data structure in an unspecified order. Keys are guaranteed "
@ -367,7 +386,8 @@ static const JanetReg corelib_cfuns[] = {
"during iteration. If key is nil, next returns the first key. If next "
"returns nil, there are no more keys to iterate through. ")
},
{"hash", janet_core_hash,
{
"hash", janet_core_hash,
JDOC("(hash value)\n\n"
"Gets a hash value for any janet value. The hash is an integer can be used "
"as a cheap hash function for all janet objects. If two values are strictly equal, "

View File

@ -84,8 +84,7 @@ static void janetc_loadconst(JanetCompiler *c, Janet k, int32_t reg) {
case JANET_FALSE:
janetc_emit(c, (reg << 8) | JOP_LOAD_FALSE);
break;
case JANET_NUMBER:
{
case JANET_NUMBER: {
double dval = janet_unwrap_number(k);
int32_t i = (int32_t) dval;
if (dval != i || !(dval >= INT16_MIN && dval <= INT16_MAX))
@ -97,8 +96,7 @@ static void janetc_loadconst(JanetCompiler *c, Janet k, int32_t reg) {
break;
}
default:
do_constant:
{
do_constant: {
int32_t cindex = janetc_const(c, k);
janetc_emit(c,
(cindex << 16) |

View File

@ -60,18 +60,37 @@ void janet_mark(Janet x) {
if (depth) {
depth--;
switch (janet_type(x)) {
default: break;
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;
case JANET_TABLE: janet_mark_table(janet_unwrap_table(x)); break;
case JANET_STRUCT: janet_mark_struct(janet_unwrap_struct(x)); break;
case JANET_TUPLE: janet_mark_tuple(janet_unwrap_tuple(x)); break;
case JANET_BUFFER: janet_mark_buffer(janet_unwrap_buffer(x)); break;
case JANET_FIBER: janet_mark_fiber(janet_unwrap_fiber(x)); break;
case JANET_ABSTRACT: janet_mark_abstract(janet_unwrap_abstract(x)); break;
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;
case JANET_TABLE:
janet_mark_table(janet_unwrap_table(x));
break;
case JANET_STRUCT:
janet_mark_struct(janet_unwrap_struct(x));
break;
case JANET_TUPLE:
janet_mark_tuple(janet_unwrap_tuple(x));
break;
case JANET_BUFFER:
janet_mark_buffer(janet_unwrap_buffer(x));
break;
case JANET_FIBER:
janet_mark_fiber(janet_unwrap_fiber(x));
break;
case JANET_ABSTRACT:
janet_mark_abstract(janet_unwrap_abstract(x));
break;
}
depth++;
} else {
@ -252,15 +271,13 @@ static void janet_deinit_block(JanetGCMemoryHeader *block) {
janet_assert(!h->type->gc((void *)(h + 1), h->size), "finalizer failed");
}
break;
case JANET_MEMORY_FUNCENV:
{
case JANET_MEMORY_FUNCENV: {
JanetFuncEnv *env = (JanetFuncEnv *)mem;
if (0 == env->offset)
free(env->as.values);
}
break;
case JANET_MEMORY_FUNCDEF:
{
case JANET_MEMORY_FUNCDEF: {
JanetFuncDef *def = (JanetFuncDef *)mem;
/* TODO - get this all with one alloc and one free */
free(def->defs);
@ -417,5 +434,9 @@ void janet_clear_memory(void) {
}
/* Primitives for suspending GC. */
int janet_gclock(void) { return janet_vm_gc_suspend++; }
void janet_gcunlock(int handle) { janet_vm_gc_suspend = handle; }
int janet_gclock(void) {
return janet_vm_gc_suspend++;
}
void janet_gcunlock(int handle) {
janet_vm_gc_suspend = handle;
}

View File

@ -306,8 +306,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
case JANET_TRUE:
pushbyte(st, 200 + type);
goto done;
case JANET_NUMBER:
{
case JANET_NUMBER: {
double xval = janet_unwrap_number(x);
if (janet_checkintrange(xval)) {
pushint(st, (int32_t) xval);
@ -343,8 +342,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
/* Reference types */
switch (type) {
case JANET_NUMBER:
{
case JANET_NUMBER: {
union {
double d;
uint8_t bytes[8];
@ -353,10 +351,18 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
#ifdef JANET_BIG_ENDIAN
/* Swap byte order */
uint8_t temp;
temp = u.bytes[7]; u.bytes[7] = u.bytes[0]; u.bytes[0] = temp;
temp = u.bytes[6]; u.bytes[6] = u.bytes[1]; u.bytes[1] = temp;
temp = u.bytes[5]; u.bytes[5] = u.bytes[2]; u.bytes[2] = temp;
temp = u.bytes[4]; u.bytes[4] = u.bytes[3]; u.bytes[3] = temp;
temp = u.bytes[7];
u.bytes[7] = u.bytes[0];
u.bytes[0] = temp;
temp = u.bytes[6];
u.bytes[6] = u.bytes[1];
u.bytes[1] = temp;
temp = u.bytes[5];
u.bytes[5] = u.bytes[2];
u.bytes[2] = temp;
temp = u.bytes[4];
u.bytes[4] = u.bytes[3];
u.bytes[3] = temp;
#endif
pushbyte(st, LB_REAL);
pushbytes(st, u.bytes, 8);
@ -365,8 +371,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
goto done;
case JANET_STRING:
case JANET_SYMBOL:
case JANET_KEYWORD:
{
case JANET_KEYWORD: {
const uint8_t *str = janet_unwrap_string(x);
int32_t length = janet_string_length(str);
/* Record reference */
@ -379,8 +384,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
pushbytes(st, str, length);
}
goto done;
case JANET_BUFFER:
{
case JANET_BUFFER: {
JanetBuffer *buffer = janet_unwrap_buffer(x);
/* Record reference */
MARK_SEEN();
@ -389,8 +393,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
pushbytes(st, buffer->data, buffer->count);
}
goto done;
case JANET_ARRAY:
{
case JANET_ARRAY: {
int32_t i;
JanetArray *a = janet_unwrap_array(x);
MARK_SEEN();
@ -400,8 +403,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
marshal_one(st, a->data[i], flags + 1);
}
goto done;
case JANET_TUPLE:
{
case JANET_TUPLE: {
int32_t i, count, flag;
const Janet *tup = janet_unwrap_tuple(x);
count = janet_tuple_length(tup);
@ -415,8 +417,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
MARK_SEEN();
}
goto done;
case JANET_TABLE:
{
case JANET_TABLE: {
JanetTable *t = janet_unwrap_table(x);
MARK_SEEN();
pushbyte(st, t->proto ? LB_TABLE_PROTO : LB_TABLE);
@ -431,8 +432,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
}
}
goto done;
case JANET_STRUCT:
{
case JANET_STRUCT: {
int32_t count;
const JanetKV *struct_ = janet_unwrap_struct(x);
count = janet_struct_length(struct_);
@ -451,8 +451,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
case JANET_ABSTRACT:
case JANET_CFUNCTION:
goto noregval;
case JANET_FUNCTION:
{
case JANET_FUNCTION: {
pushbyte(st, LB_FUNCTION);
JanetFunction *func = janet_unwrap_function(x);
marshal_one_def(st, func->def, flags);
@ -462,8 +461,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
marshal_one_env(st, func->envs[i], flags + 1);
}
goto done;
case JANET_FIBER:
{
case JANET_FIBER: {
MARK_SEEN();
pushbyte(st, LB_FIBER);
marshal_one_fiber(st, janet_unwrap_fiber(x), flags + 1);
@ -972,8 +970,7 @@ static const uint8_t *unmarshal_one(
case LB_SYMBOL:
case LB_BUFFER:
case LB_KEYWORD:
case LB_REGISTRY:
{
case LB_REGISTRY: {
data++;
int32_t len = readint(st, &data);
EXTRA(len);
@ -1002,15 +999,13 @@ static const uint8_t *unmarshal_one(
janet_array_push(&st->lookup, *out);
return data + len;
}
case LB_FIBER:
{
case LB_FIBER: {
JanetFiber *fiber;
data = unmarshal_one_fiber(st, data + 1, &fiber, flags);
*out = janet_wrap_fiber(fiber);
return data;
}
case LB_FUNCTION:
{
case LB_FUNCTION: {
JanetFunction *func;
JanetFuncDef *def;
data = unmarshal_one_def(st, data + 1, &def, flags + 1);

View File

@ -191,17 +191,28 @@ static void popstate(JanetParser *p, Janet val) {
static int checkescape(uint8_t c) {
switch (c) {
default: return -1;
case 'x': return 1;
case 'n': return '\n';
case 't': return '\t';
case 'r': return '\r';
case '0': return '\0';
case 'z': return '\0';
case 'f': return '\f';
case 'e': return 27;
case '"': return '"';
case '\\': return '\\';
default:
return -1;
case 'x':
return 1;
case 'n':
return '\n';
case 't':
return '\t';
case 'r':
return '\r';
case '0':
return '\0';
case 'z':
return '\0';
case 'f':
return '\f';
case 'e':
return 27;
case '"':
return '"';
case '\\':
return '\\';
}
}
@ -475,8 +486,7 @@ static int root(JanetParser *p, JanetParseState *state, uint8_t c) {
return 1;
case ')':
case ']':
case '}':
{
case '}': {
Janet ds;
if (p->statecount == 1) {
p->error = "unexpected delimiter";

View File

@ -138,27 +138,23 @@ tail:
janet_panic("unexpected opcode");
return NULL;
case RULE_LITERAL:
{
case RULE_LITERAL: {
uint32_t len = rule[1];
if (text + len > s->text_end) return NULL;
return memcmp(text, rule + 2, len) ? NULL : text + len;
}
case RULE_NCHAR:
{
case RULE_NCHAR: {
uint32_t n = rule[1];
return (text + n > s->text_end) ? NULL : text + n;
}
case RULE_NOTNCHAR:
{
case RULE_NOTNCHAR: {
uint32_t n = rule[1];
return (text + n > s->text_end) ? text : NULL;
}
case RULE_RANGE:
{
case RULE_RANGE: {
uint8_t lo = rule[1] & 0xFF;
uint8_t hi = (rule[1] >> 16) & 0xFF;
return (text < s->text_end &&
@ -168,8 +164,7 @@ tail:
: NULL;
}
case RULE_SET:
{
case RULE_SET: {
uint32_t word = rule[1 + (text[0] >> 5)];
uint32_t mask = (uint32_t)1 << (text[0] & 0x1F);
return (text < s->text_end && (word & mask))
@ -177,8 +172,7 @@ tail:
: NULL;
}
case RULE_LOOK:
{
case RULE_LOOK: {
text += ((int32_t *)rule)[1];
if (text < s->text_start || text > s->text_end) return NULL;
int oldmode = s->mode;
@ -190,8 +184,7 @@ tail:
return result ? text : NULL;
}
case RULE_CHOICE:
{
case RULE_CHOICE: {
uint32_t len = rule[1];
const uint32_t *args = rule + 2;
if (len == 0) return NULL;
@ -210,8 +203,7 @@ tail:
goto tail;
}
case RULE_SEQUENCE:
{
case RULE_SEQUENCE: {
uint32_t len = rule[1];
const uint32_t *args = rule + 2;
if (len == 0) return text;
@ -225,8 +217,7 @@ tail:
}
case RULE_IF:
case RULE_IFNOT:
{
case RULE_IFNOT: {
const uint32_t *rule_a = s->bytecode + rule[1];
const uint32_t *rule_b = s->bytecode + rule[2];
int oldmode = s->mode;
@ -240,8 +231,7 @@ tail:
goto tail;
}
case RULE_NOT:
{
case RULE_NOT: {
const uint32_t *rule_a = s->bytecode + rule[1];
int oldmode = s->mode;
s->mode = PEG_MODE_NOCAPTURE;
@ -252,8 +242,7 @@ tail:
return (result) ? NULL : text;
}
case RULE_BETWEEN:
{
case RULE_BETWEEN: {
uint32_t lo = rule[1];
uint32_t hi = rule[2];
const uint32_t *rule_a = s->bytecode + rule[3];
@ -281,8 +270,7 @@ tail:
/* Capturing rules */
case RULE_GETTAG:
{
case RULE_GETTAG: {
uint32_t search = rule[1];
uint32_t tag = rule[2];
for (int32_t i = s->tags->count - 1; i >= 0; i--) {
@ -294,28 +282,24 @@ tail:
return NULL;
}
case RULE_POSITION:
{
case RULE_POSITION: {
pushcap(s, janet_wrap_number((double)(text - s->text_start)), rule[1]);
return text;
}
case RULE_ARGUMENT:
{
case RULE_ARGUMENT: {
int32_t index = ((int32_t *)rule)[1];
Janet capture = (index >= s->extrac) ? janet_wrap_nil() : s->extrav[index];
pushcap(s, capture, rule[2]);
return text;
}
case RULE_CONSTANT:
{
case RULE_CONSTANT: {
pushcap(s, s->constants[rule[1]], rule[2]);
return text;
}
case RULE_CAPTURE:
{
case RULE_CAPTURE: {
uint32_t tag = rule[2];
if (!tag && s->mode == PEG_MODE_NOCAPTURE) {
rule = s->bytecode + rule[1];
@ -334,8 +318,7 @@ tail:
return result;
}
case RULE_ACCUMULATE:
{
case RULE_ACCUMULATE: {
uint32_t tag = rule[2];
int oldmode = s->mode;
/* No capture mode, skip captures. Accumulate inside accumulate also does nothing. */
@ -356,8 +339,7 @@ tail:
return result;
}
case RULE_DROP:
{
case RULE_DROP: {
CapState cs = cap_save(s);
down1(s);
const uint8_t *result = peg_rule(s, s->bytecode + rule[1], text);
@ -367,8 +349,7 @@ tail:
return result;
}
case RULE_GROUP:
{
case RULE_GROUP: {
uint32_t tag = rule[2];
int oldmode = s->mode;
if (!tag && oldmode == PEG_MODE_NOCAPTURE) {
@ -394,8 +375,7 @@ tail:
}
case RULE_REPLACE:
case RULE_MATCHTIME:
{
case RULE_MATCHTIME: {
uint32_t tag = rule[3];
int oldmode = s->mode;
if (!tag && rule[0] == RULE_REPLACE && oldmode == PEG_MODE_NOCAPTURE) {
@ -440,8 +420,7 @@ tail:
return result;
}
case RULE_ERROR:
{
case RULE_ERROR: {
int oldmode = s->mode;
s->mode = PEG_MODE_NORMAL;
int32_t old_cap = s->captures->count;
@ -921,8 +900,7 @@ static uint32_t peg_compile1(Builder *b, Janet peg) {
default:
peg_panicf(b, "unexpected peg source");
return 0;
case JANET_NUMBER:
{
case JANET_NUMBER: {
int32_t n = peg_getinteger(b, peg);
Reserve r = reserve(b, 2);
if (n < 0) {
@ -932,23 +910,20 @@ static uint32_t peg_compile1(Builder *b, Janet peg) {
}
break;
}
case JANET_STRING:
{
case JANET_STRING: {
const uint8_t *str = janet_unwrap_string(peg);
int32_t len = janet_string_length(str);
emit_bytes(b, RULE_LITERAL, len, str);
break;
}
case JANET_KEYWORD:
{
case JANET_KEYWORD: {
Janet check = janet_table_get(b->grammar, peg);
if (janet_checktype(check, JANET_NIL))
peg_panicf(b, "unknown rule");
rule = peg_compile1(b, check);
break;
}
case JANET_STRUCT:
{
case JANET_STRUCT: {
JanetTable *grammar = janet_struct_to_table(janet_unwrap_struct(peg));
grammar->proto = b->grammar;
b->grammar = grammar;
@ -959,8 +934,7 @@ static uint32_t peg_compile1(Builder *b, Janet peg) {
b->grammar = grammar->proto;
break;
}
case JANET_TUPLE:
{
case JANET_TUPLE: {
const Janet *tup = janet_unwrap_tuple(peg);
int32_t len = janet_tuple_length(tup);
if (len == 0) peg_panic(b, "tuple in grammar must have non-zero length");
@ -1089,12 +1063,14 @@ static Janet cfun_peg_match(int32_t argc, Janet *argv) {
}
static const JanetReg peg_cfuns[] = {
{"peg/compile", cfun_peg_compile,
{
"peg/compile", cfun_peg_compile,
JDOC("(peg/compile peg)\n\n"
"Compiles a peg source data structure into a <core/peg>. This will speed up matching "
"if the same peg will be used multiple times.")
},
{"peg/match", cfun_peg_match,
{
"peg/match", cfun_peg_match,
JDOC("(peg/match peg text [,start=0])\n\n"
"Match a Parsing Expression Grammar to a byte string and return an array of captured values. "
"Returns nil if text does not match the language defined by peg. The syntax of PEGs are very "

View File

@ -193,14 +193,12 @@ void janet_description_b(JanetBuffer *buffer, Janet x) {
case JANET_BUFFER:
janet_escape_buffer_b(buffer, janet_unwrap_buffer(x));
return;
case JANET_ABSTRACT:
{
case JANET_ABSTRACT: {
const char *n = janet_abstract_type(janet_unwrap_abstract(x))->name;
string_description_b(buffer, n, janet_unwrap_abstract(x));
return;
}
case JANET_CFUNCTION:
{
case JANET_CFUNCTION: {
Janet check = janet_table_get(janet_vm_registry, x);
if (janet_checktype(check, JANET_SYMBOL)) {
janet_buffer_push_cstring(buffer, "<cfunction ");
@ -212,8 +210,7 @@ void janet_description_b(JanetBuffer *buffer, Janet x) {
}
goto fallthrough;
}
case JANET_FUNCTION:
{
case JANET_FUNCTION: {
JanetFunction *fun = janet_unwrap_function(x);
JanetFuncDef *def = fun->def;
if (def->name) {
@ -265,8 +262,7 @@ const uint8_t *janet_description(Janet x) {
* strings, symbols, and buffers will return their content. */
const uint8_t *janet_to_string(Janet x) {
switch (janet_type(x)) {
default:
{
default: {
JanetBuffer b;
janet_buffer_init(&b, 10);
janet_to_string_b(&b, x);
@ -313,8 +309,7 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) {
case JANET_TRUE:
case JANET_FALSE:
break;
default:
{
default: {
Janet seenid = janet_table_get(&S->seen, x);
if (janet_checktype(seenid, JANET_NUMBER)) {
janet_buffer_push_cstring(S->buffer, "<cycle ");
@ -333,8 +328,7 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) {
janet_description_b(S->buffer, x);
break;
case JANET_ARRAY:
case JANET_TUPLE:
{
case JANET_TUPLE: {
int32_t i, len;
const Janet *arr;
int isarray = janet_checktype(x, JANET_ARRAY);
@ -362,8 +356,7 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) {
break;
}
case JANET_STRUCT:
case JANET_TABLE:
{
case JANET_TABLE: {
int istable = janet_checktype(x, JANET_TABLE);
janet_buffer_push_cstring(S->buffer, istable ? "@" : "{");
@ -483,8 +476,7 @@ const uint8_t *janet_formatc(const char *format, ...) {
default:
janet_buffer_push_u8(bufp, c);
break;
case '%':
{
case '%': {
if (i + 1 >= len)
break;
switch (format[++i]) {
@ -497,8 +489,7 @@ const uint8_t *janet_formatc(const char *format, ...) {
case 'd':
integer_to_string_b(bufp, va_arg(args, long));
break;
case 'S':
{
case 'S': {
const uint8_t *str = va_arg(args, const uint8_t *);
janet_buffer_push_bytes(bufp, str, janet_string_length(str));
break;
@ -509,35 +500,29 @@ const uint8_t *janet_formatc(const char *format, ...) {
case 'c':
janet_buffer_push_u8(bufp, (uint8_t) va_arg(args, long));
break;
case 'q':
{
case 'q': {
const uint8_t *str = va_arg(args, const uint8_t *);
janet_escape_string_b(bufp, str);
break;
}
case 't':
{
case 't': {
janet_buffer_push_cstring(bufp, typestr(va_arg(args, Janet)));
break;
}
case 'T':
{
case 'T': {
int types = va_arg(args, long);
pushtypes(bufp, types);
break;
}
case 'V':
{
case 'V': {
janet_to_string_b(bufp, va_arg(args, Janet));
break;
}
case 'v':
{
case 'v': {
janet_description_b(bufp, va_arg(args, Janet));
break;
}
case 'p':
{
case 'p': {
janet_pretty(bufp, 4, va_arg(args, Janet));
}
}
@ -616,8 +601,7 @@ void janet_buffer_format(
janet_panic("not enough values for format");
strfrmt = scanformat(strfrmt, form, width, precision);
switch (*strfrmt++) {
case 'c':
{
case 'c': {
nb = snprintf(item, MAX_ITEM, form, (int)
janet_getinteger(argv, arg));
break;
@ -627,8 +611,7 @@ void janet_buffer_format(
case 'o':
case 'u':
case 'x':
case 'X':
{
case 'X': {
int32_t n = janet_getinteger(argv, arg);
nb = snprintf(item, MAX_ITEM, form, n);
break;
@ -639,14 +622,12 @@ void janet_buffer_format(
case 'E':
case 'f':
case 'g':
case 'G':
{
case 'G': {
double d = janet_getnumber(argv, arg);
nb = snprintf(item, MAX_ITEM, form, d);
break;
}
case 's':
{
case 's': {
const uint8_t *s = janet_getstring(argv, arg);
size_t l = janet_string_length(s);
if (form[2] == '\0')
@ -663,26 +644,23 @@ void janet_buffer_format(
}
break;
}
case 'V':
{
case 'V': {
janet_to_string_b(b, argv[arg]);
break;
}
case 'v':
{
case 'v': {
janet_description_b(b, argv[arg]);
break;
}
case 'p': /* janet pretty , precision = depth */
{
case 'p': { /* janet pretty , precision = depth */
int depth = atoi(precision);
if (depth < 1)
depth = 4;
janet_pretty(b, depth, argv[arg]);
break;
}
default:
{ /* also treat cases 'nLlh' */
default: {
/* also treat cases 'nLlh' */
janet_panicf("invalid conversion '%s' to 'format'",
form);
}

View File

@ -60,8 +60,7 @@ static JanetSlot quasiquote(JanetFopts opts, Janet x) {
switch (janet_type(x)) {
default:
return janetc_cslot(x);
case JANET_TUPLE:
{
case JANET_TUPLE: {
int32_t i, len;
const Janet *tup = janet_unwrap_tuple(x);
len = janet_tuple_length(tup);
@ -74,8 +73,7 @@ static JanetSlot quasiquote(JanetFopts opts, Janet x) {
janet_v_push(slots, quasiquote(opts, tup[i]));
return qq_slots(opts, slots, JOP_MAKE_TUPLE);
}
case JANET_ARRAY:
{
case JANET_ARRAY: {
int32_t i;
JanetArray *array = janet_unwrap_array(x);
for (i = 0; i < array->count; i++)
@ -83,8 +81,7 @@ static JanetSlot quasiquote(JanetFopts opts, Janet x) {
return qq_slots(opts, slots, JOP_MAKE_ARRAY);
}
case JANET_TABLE:
case JANET_STRUCT:
{
case JANET_STRUCT: {
const JanetKV *kv = NULL, *kvs = NULL;
int32_t len, cap;
janet_dictionary_view(x, &kvs, &len, &cap);
@ -136,8 +133,7 @@ static int destructure(JanetCompiler *c,
/* Leaf, assign right to left */
return leaf(c, janet_unwrap_symbol(left), right, attr);
case JANET_TUPLE:
case JANET_ARRAY:
{
case JANET_ARRAY: {
int32_t i, len;
const Janet *values;
janet_indexed_view(left, &values, &len);
@ -156,8 +152,7 @@ static int destructure(JanetCompiler *c,
}
return 1;
case JANET_TABLE:
case JANET_STRUCT:
{
case JANET_STRUCT: {
const JanetKV *kvs = NULL;
int32_t i, cap, len;
janet_dictionary_view(left, &kvs, &len, &cap);

View File

@ -540,7 +540,8 @@ static const JanetReg string_cfuns[] = {
"Joins an array of strings into one string, optionally separated by "
"a separator string sep.")
},
{ "string/format", cfun_string_format,
{
"string/format", cfun_string_format,
JDOC("(string/format format & values)\n\n"
"Similar to snprintf, but specialized for operating with janet. Returns "
"a new string.")

View File

@ -346,7 +346,8 @@ int janet_scan_number(
str++;
seenadigit = 1;
}
if (eneg) ex -= ee; else ex += ee;
if (eneg) ex -= ee;
else ex += ee;
}
if (!seenadigit)

View File

@ -161,8 +161,7 @@ Janet janet_get(Janet ds, Janet key) {
case JANET_TABLE:
value = janet_table_get(janet_unwrap_table(ds), key);
break;
case JANET_ARRAY:
{
case JANET_ARRAY: {
JanetArray *array = janet_unwrap_array(ds);
int32_t index;
if (!janet_checkint(key))
@ -175,8 +174,7 @@ Janet janet_get(Janet ds, Janet key) {
}
break;
}
case JANET_TUPLE:
{
case JANET_TUPLE: {
const Janet *tuple = janet_unwrap_tuple(ds);
int32_t index;
if (!janet_checkint(key))
@ -189,8 +187,7 @@ Janet janet_get(Janet ds, Janet key) {
}
break;
}
case JANET_BUFFER:
{
case JANET_BUFFER: {
JanetBuffer *buffer = janet_unwrap_buffer(ds);
int32_t index;
if (!janet_checkint(key))
@ -205,8 +202,7 @@ Janet janet_get(Janet ds, Janet key) {
}
case JANET_STRING:
case JANET_SYMBOL:
case JANET_KEYWORD:
{
case JANET_KEYWORD: {
const uint8_t *str = janet_unwrap_string(ds);
int32_t index;
if (!janet_checkint(key))
@ -219,8 +215,7 @@ Janet janet_get(Janet ds, Janet key) {
}
break;
}
case JANET_ABSTRACT:
{
case JANET_ABSTRACT: {
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds));
if (type->get) {
value = (type->get)(janet_unwrap_abstract(ds), key);
@ -278,8 +273,7 @@ Janet janet_getindex(Janet ds, int32_t index) {
case JANET_STRUCT:
value = janet_struct_get(janet_unwrap_struct(ds), janet_wrap_integer(index));
break;
case JANET_ABSTRACT:
{
case JANET_ABSTRACT: {
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds));
if (type->get) {
value = (type->get)(janet_unwrap_abstract(ds), janet_wrap_integer(index));
@ -321,8 +315,7 @@ void janet_putindex(Janet ds, int32_t index, Janet value) {
janet_panicf("expected %T, got %v",
JANET_TFLAG_ARRAY | JANET_TFLAG_BUFFER | JANET_TFLAG_TABLE, ds);
break;
case JANET_ARRAY:
{
case JANET_ARRAY: {
JanetArray *array = janet_unwrap_array(ds);
if (index >= array->count) {
janet_array_ensure(array, index + 1, 2);
@ -331,8 +324,7 @@ void janet_putindex(Janet ds, int32_t index, Janet value) {
array->data[index] = value;
break;
}
case JANET_BUFFER:
{
case JANET_BUFFER: {
JanetBuffer *buffer = janet_unwrap_buffer(ds);
if (!janet_checkint(value))
janet_panicf("can only put integers in buffers, got %v", value);
@ -343,14 +335,12 @@ void janet_putindex(Janet ds, int32_t index, Janet value) {
buffer->data[index] = janet_unwrap_integer(value);
break;
}
case JANET_TABLE:
{
case JANET_TABLE: {
JanetTable *table = janet_unwrap_table(ds);
janet_table_put(table, janet_wrap_integer(index), value);
break;
}
case JANET_ABSTRACT:
{
case JANET_ABSTRACT: {
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds));
if (type->put) {
(type->put)(janet_unwrap_abstract(ds), janet_wrap_integer(index), value);
@ -368,8 +358,7 @@ void janet_put(Janet ds, Janet key, Janet value) {
janet_panicf("expected %T, got %v",
JANET_TFLAG_ARRAY | JANET_TFLAG_BUFFER | JANET_TFLAG_TABLE, ds);
break;
case JANET_ARRAY:
{
case JANET_ARRAY: {
int32_t index;
JanetArray *array = janet_unwrap_array(ds);
if (!janet_checkint(key)) janet_panicf("expected integer key, got %v", key);
@ -381,8 +370,7 @@ void janet_put(Janet ds, Janet key, Janet value) {
array->data[index] = value;
break;
}
case JANET_BUFFER:
{
case JANET_BUFFER: {
int32_t index;
JanetBuffer *buffer = janet_unwrap_buffer(ds);
if (!janet_checkint(key)) janet_panicf("expected integer key, got %v", key);
@ -399,8 +387,7 @@ void janet_put(Janet ds, Janet key, Janet value) {
case JANET_TABLE:
janet_table_put(janet_unwrap_table(ds), key, value);
break;
case JANET_ABSTRACT:
{
case JANET_ABSTRACT: {
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds));
if (type->put) {
(type->put)(janet_unwrap_abstract(ds), key, value);

View File

@ -283,8 +283,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
vm_assert_types(stack[A], E);
vm_pcnext();
VM_OP(JOP_RETURN)
{
VM_OP(JOP_RETURN) {
Janet retval = stack[D];
int entrance_frame = janet_stack_frame(stack)->flags & JANET_STACKFRAME_ENTRANCE;
janet_fiber_popframe(fiber);
@ -294,8 +293,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
vm_checkgc_pcnext();
}
VM_OP(JOP_RETURN_NIL)
{
VM_OP(JOP_RETURN_NIL) {
Janet retval = janet_wrap_nil();
int entrance_frame = janet_stack_frame(stack)->flags & JANET_STACKFRAME_ENTRANCE;
janet_fiber_popframe(fiber);
@ -350,8 +348,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
VM_OP(JOP_BXOR)
vm_bitop(^);
VM_OP(JOP_BNOT)
{
VM_OP(JOP_BNOT) {
Janet op = stack[E];
vm_assert_type(op, JANET_NUMBER);
stack[A] = janet_wrap_integer(~janet_unwrap_integer(op));
@ -448,8 +445,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
stack[A] = janet_wrap_integer(ES);
vm_pcnext();
VM_OP(JOP_LOAD_CONSTANT)
{
VM_OP(JOP_LOAD_CONSTANT) {
int32_t cindex = (int32_t)E;
vm_assert(cindex < func->def->constants_length, "invalid constant");
stack[A] = func->def->constants[cindex];
@ -460,8 +456,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
stack[D] = janet_wrap_function(func);
vm_pcnext();
VM_OP(JOP_LOAD_UPVALUE)
{
VM_OP(JOP_LOAD_UPVALUE) {
int32_t eindex = B;
int32_t vindex = C;
JanetFuncEnv *env;
@ -478,8 +473,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
vm_pcnext();
}
VM_OP(JOP_SET_UPVALUE)
{
VM_OP(JOP_SET_UPVALUE) {
int32_t eindex = B;
int32_t vindex = C;
JanetFuncEnv *env;
@ -494,8 +488,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
vm_pcnext();
}
VM_OP(JOP_CLOSURE)
{
VM_OP(JOP_CLOSURE) {
JanetFuncDef *fd;
JanetFunction *fn;
int32_t elen;
@ -544,8 +537,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
stack = fiber->data + fiber->frame;
vm_checkgc_pcnext();
VM_OP(JOP_PUSH_ARRAY)
{
VM_OP(JOP_PUSH_ARRAY) {
const Janet *vals;
int32_t len;
if (janet_indexed_view(stack[D], &vals, &len)) {
@ -557,8 +549,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
stack = fiber->data + fiber->frame;
vm_checkgc_pcnext();
VM_OP(JOP_CALL)
{
VM_OP(JOP_CALL) {
Janet callee = stack[E];
if (fiber->stacktop > fiber->maxstack) {
vm_throw("stack overflow");
@ -597,8 +588,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
}
}
VM_OP(JOP_TAILCALL)
{
VM_OP(JOP_TAILCALL) {
Janet callee = stack[D];
if (janet_checktype(callee, JANET_KEYWORD)) {
vm_commit();
@ -638,8 +628,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
}
}
VM_OP(JOP_RESUME)
{
VM_OP(JOP_RESUME) {
Janet retreg;
vm_assert_type(stack[B], JANET_FIBER);
JanetFiber *child = janet_unwrap_fiber(stack[B]);
@ -652,8 +641,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
vm_checkgc_pcnext();
}
VM_OP(JOP_SIGNAL)
{
VM_OP(JOP_SIGNAL) {
int32_t s = C;
if (s > JANET_SIGNAL_USER9) s = JANET_SIGNAL_USER9;
if (s < 0) s = 0;
@ -685,8 +673,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
stack[A] = janet_wrap_integer(janet_length(stack[E]));
vm_pcnext();
VM_OP(JOP_MAKE_ARRAY)
{
VM_OP(JOP_MAKE_ARRAY) {
int32_t count = fiber->stacktop - fiber->stackstart;
Janet *mem = fiber->data + fiber->stackstart;
stack[D] = janet_wrap_array(janet_array_n(mem, count));
@ -694,8 +681,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
vm_checkgc_pcnext();
}
VM_OP(JOP_MAKE_TUPLE)
{
VM_OP(JOP_MAKE_TUPLE) {
int32_t count = fiber->stacktop - fiber->stackstart;
Janet *mem = fiber->data + fiber->stackstart;
stack[D] = janet_wrap_tuple(janet_tuple_n(mem, count));
@ -703,8 +689,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
vm_checkgc_pcnext();
}
VM_OP(JOP_MAKE_TABLE)
{
VM_OP(JOP_MAKE_TABLE) {
int32_t count = fiber->stacktop - fiber->stackstart;
Janet *mem = fiber->data + fiber->stackstart;
if (count & 1)
@ -717,8 +702,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
vm_checkgc_pcnext();
}
VM_OP(JOP_MAKE_STRUCT)
{
VM_OP(JOP_MAKE_STRUCT) {
int32_t count = fiber->stacktop - fiber->stackstart;
Janet *mem = fiber->data + fiber->stackstart;
if (count & 1)
@ -731,8 +715,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
vm_checkgc_pcnext();
}
VM_OP(JOP_MAKE_STRING)
{
VM_OP(JOP_MAKE_STRING) {
int32_t count = fiber->stacktop - fiber->stackstart;
Janet *mem = fiber->data + fiber->stackstart;
JanetBuffer buffer;
@ -745,8 +728,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in, JanetFiberStatus status)
vm_checkgc_pcnext();
}
VM_OP(JOP_MAKE_BUFFER)
{
VM_OP(JOP_MAKE_BUFFER) {
int32_t count = fiber->stacktop - fiber->stackstart;
Janet *mem = fiber->data + fiber->stackstart;
JanetBuffer *buffer = janet_buffer(10 * count);