diff --git a/Makefile b/Makefile index 0b1e3324..7ec43ad6 100644 --- a/Makefile +++ b/Makefile @@ -48,23 +48,76 @@ endif # For other unix likes, add flags here! $(shell mkdir -p build/core build/mainclient build/webclient build/boot) - -# Source headers -JANET_HEADERS=$(sort $(wildcard src/include/*.h)) -JANET_LOCAL_HEADERS=$(sort $(wildcard src/*/*.h)) - -# Source files -JANET_CORE_SOURCES=$(sort $(wildcard src/core/*.c)) -JANET_MAINCLIENT_SOURCES=$(sort $(wildcard src/mainclient/*.c)) -JANET_WEBCLIENT_SOURCES=$(sort $(wildcard src/webclient/*.c)) - all: $(JANET_TARGET) $(JANET_LIBRARY) +###################### +##### Name Files ##### +###################### + +JANET_HEADERS=src/include/janet.h src/include/janetconf.h + +JANET_LOCAL_HEADERS=src/core/util.h \ + src/core/state.h \ + src/core/gc.h \ + src/core/vector.h \ + src/core/fiber.h \ + src/core/regalloc.h \ + src/core/compile.h \ + src/core/emit.h \ + src/core/symcache.h + +JANET_CORE_SOURCES=src/core/abstract.c \ + src/core/array.c \ + src/core/asm.c \ + src/core/buffer.c \ + src/core/bytecode.c \ + src/core/capi.c \ + src/core/cfuns.c \ + src/core/compile.c \ + src/core/corelib.c \ + src/core/debug.c \ + src/core/emit.c \ + src/core/fiber.c \ + src/core/gc.c \ + src/core/inttypes.c \ + src/core/io.c \ + src/core/marsh.c \ + src/core/math.c \ + src/core/os.c \ + src/core/parse.c \ + src/core/peg.c \ + src/core/pp.c \ + src/core/regalloc.c \ + src/core/run.c \ + src/core/specials.c \ + src/core/string.c \ + src/core/strtod.c \ + src/core/struct.c \ + src/core/symcache.c \ + src/core/table.c \ + src/core/tuple.c \ + src/core/typedarray.c \ + src/core/util.c \ + src/core/value.c \ + src/core/vector.c \ + src/core/vm.c \ + src/core/wrap.c + +JANET_BOOT_SOURCES=src/boot/array_test.c \ + src/boot/boot.c \ + src/boot/buffer_test.c \ + src/boot/number_test.c \ + src/boot/system_test.c \ + src/boot/table_test.c + +JANET_MAINCLIENT_SOURCES=src/mainclient/line.c src/mainclient/main.c + +JANET_WEBCLIENT_SOURCES=src/webclient/main.c + ################################################################## ##### The bootstrap interpreter that compiles the core image ##### ################################################################## -JANET_BOOT_SOURCES=$(sort $(wildcard src/boot/*.c)) JANET_BOOT_OBJECTS=$(patsubst src/%.c,build/%.boot.o,$(JANET_CORE_SOURCES) $(JANET_BOOT_SOURCES)) \ build/boot.gen.o @@ -150,8 +203,9 @@ build/boot.gen.c: src/boot/boot.janet build/xxd amalg: build/janet.c build/janet.h build/core_image.c -build/janet.c: $(JANET_LOCAL_HEADERS) $(JANET_CORE_SOURCES) tools/amalg.janet $(JANET_TARGET) - $(JANET_TARGET) tools/amalg.janet > $@ +AMALG_SOURCE=$(JANET_LOCAL_HEADERS) $(JANET_CORE_SOURCES) build/core_image.c +build/janet.c: $(AMALG_SOURCE) tools/amalg.janet $(JANET_TARGET) + $(JANET_TARGET) tools/amalg.janet $(AMALG_SOURCE) > $@ build/janet.h: src/include/janet.h cp $< $@ @@ -207,12 +261,8 @@ 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 + tools/format.sh grammar: build/janet.tmLanguage build/janet.tmLanguage: tools/tm_lang_gen.janet $(JANET_TARGET) diff --git a/README.md b/README.md index 24b79196..60062100 100644 --- a/README.md +++ b/README.md @@ -116,17 +116,19 @@ $ The C API for Janet is not yet documented but coming soon. -Janet can be embedded in a host program very easily. There is a make target `make amalg` -which creates the file `build/janet.c`, which is a single C file that contains all the source -to Janet. This file, along with `src/include/janet/janet.h` can dragged into any C project -and compiled into the project. Janet should be compiled with `-std=c99` on most compilers, and -will need to be linked to the math library, `-lm`, and the dynamic linker, `-ldl`, if one wants -to be able to load dynamic modules. If there is no need for dynamic modules, add the define +Janet can be embedded in a host program very easily. There is a make target +`make amalg` which creates the file `build/janet.c`, which is a single C file +that contains all the source to Janet. This file, along with +`src/include/janet.h` and `src/include/janetconf.h` can dragged into any C +project and compiled into the project. Janet should be compiled with `-std=c99` +on most compilers, and will need to be linked to the math library, `-lm`, and +the dynamic linker, `-ldl`, if one wants to be able to load dynamic modules. If +there is no need for dynamic modules, add the define `-DJANET_NO_DYNAMIC_MODULES` to the compiler options. ## Compiling and Running -Janet only uses Make and batch files to compile on Posix and windows +Janet only requires Make and batch files to compile on Posix and windows respectively. To configure janet, edit the header file src/include/janet/janet.h before compilation. @@ -180,7 +182,7 @@ Building with emscripten on windows is currently unsupported. Janet also has a build file for [Meson](https://mesonbuild.com/), a cross platform build system. This is not currently the main supported build system, but should work on any -system that supports meson. +system that supports meson. Meson also provides much better IDE integration than Make or batch files. ## Examples diff --git a/meson.build b/meson.build index 77bcfc4d..84906a59 100644 --- a/meson.build +++ b/meson.build @@ -20,6 +20,9 @@ project('janet', 'c', default_options : ['c_std=c99']) +# Global settings +janet_path = join_paths(get_option('prefix'), get_option('libdir'), 'janet') + # Link math library on all systems cc = meson.get_compiler('c') m_dep = cc.find_library('m', required : false) @@ -39,6 +42,20 @@ gen = generator(xxd, boot_gen = gen.process('src/boot/boot.janet', extra_args: 'janet_gen_boot') init_gen = gen.process('src/mainclient/init.janet', extra_args: 'janet_gen_init') +# Order is important here, as some headers +# depend on other headers for the amalg target +core_headers = [ + 'src/core/util.h', + 'src/core/state.h', + 'src/core/gc.h', + 'src/core/vector.h', + 'src/core/fiber.h', + 'src/core/regalloc.h', + 'src/core/compile.h', + 'src/core/emit.h', + 'src/core/symcache.h' +] + core_src = [ 'src/core/abstract.c', 'src/core/array.c', @@ -93,7 +110,7 @@ mainclient_src = [ ] # Build boot binary -janet_boot = executable('janet_boot', core_src, boot_src, boot_gen, +janet_boot = executable('janet-boot', core_src, boot_src, boot_gen, c_args : '-DJANET_BOOTSTRAP', dependencies : [m_dep, dl_dep]) @@ -101,8 +118,7 @@ janet_boot = executable('janet_boot', core_src, boot_src, boot_gen, core_image = custom_target('core_image', input : [janet_boot], output : 'core_image.gen.c', - command : [janet_boot, '@OUTPUT@', - 'JANET_PATH', join_paths(get_option('prefix'), get_option('datadir'), 'janet')]) + command : [janet_boot, '@OUTPUT@', 'JANET_PATH', janet_path]) libjanet = shared_library('janet', core_src, core_image, dependencies : [m_dep, dl_dep], @@ -111,6 +127,25 @@ janet_mainclient = executable('janet', core_src, core_image, init_gen, mainclien dependencies : [m_dep, dl_dep], install : true) +# Documentation +docs = custom_target('docs', + input : ['tools/gendoc.janet'], + output : ['doc.html'], + capture : true, + command : [janet_mainclient, '@INPUT@']) + +# Amalgamated source +amalg = custom_target('amalg', + input : ['tools/amalg.janet', core_headers, core_src, core_image], + output : ['janet.c'], + capture : true, + command : [janet_mainclient, '@INPUT@']) + +# Amalgamated client +janet_amalgclient = executable('janet-amalg', amalg, init_gen, mainclient_src, + dependencies : [m_dep, dl_dep], + build_by_default : false) + # Tests test_files = [ 'test/suite0.janet', @@ -138,4 +173,4 @@ janet_libs = [ 'tools/cook.janet', 'tools/highlight.janet' ] -install_data(sources : janet_libs) +install_data(sources : janet_libs, install_dir : janet_path) diff --git a/src/core/array.c b/src/core/array.c index ebf01c0d..7515e202 100644 --- a/src/core/array.c +++ b/src/core/array.c @@ -182,8 +182,8 @@ static Janet cfun_array_concat(int32_t argc, Janet *argv) { break; case JANET_ARRAY: case JANET_TUPLE: { - int32_t j, len; - const Janet *vals; + int32_t j, len = 0; + const Janet *vals = NULL; janet_indexed_view(argv[i], &vals, &len); for (j = 0; j < len; j++) janet_array_push(array, vals[j]); diff --git a/src/core/compile.c b/src/core/compile.c index 304d6f68..e7dcd9e3 100644 --- a/src/core/compile.c +++ b/src/core/compile.c @@ -309,9 +309,9 @@ JanetSlot *janetc_toslotskv(JanetCompiler *c, Janet ds) { JanetSlot *ret = NULL; JanetFopts subopts = janetc_fopts_default(c); const JanetKV *kvs = NULL; - int32_t cap, i, len; + int32_t cap = 0, len = 0; janet_dictionary_view(ds, &kvs, &len, &cap); - for (i = 0; i < cap; i++) { + for (int32_t i = 0; i < cap; i++) { if (janet_checktype(kvs[i].key, JANET_NIL)) continue; janet_v_push(ret, janetc_value(subopts, kvs[i].key)); janet_v_push(ret, janetc_value(subopts, kvs[i].value)); diff --git a/src/core/pp.c b/src/core/pp.c index 0ca8e5be..44791d51 100644 --- a/src/core/pp.c +++ b/src/core/pp.c @@ -341,8 +341,8 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) { break; case JANET_ARRAY: case JANET_TUPLE: { - int32_t i, len; - const Janet *arr; + int32_t i = 0, len = 0; + const Janet *arr = NULL; int isarray = janet_checktype(x, JANET_ARRAY); janet_indexed_view(x, &arr, &len); int hasbrackets = !isarray && (janet_tuple_flag(arr) & JANET_TUPLE_FLAG_BRACKETCTOR); @@ -391,9 +391,9 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) { if (S->depth == 0) { janet_buffer_push_cstring(S->buffer, "..."); } else { - int32_t i, len, cap; + int32_t i = 0, len = 0, cap = 0; int first_kv_pair = 1; - const JanetKV *kvs; + const JanetKV *kvs = NULL; janet_dictionary_view(x, &kvs, &len, &cap); if (!istable && len >= 4) janet_buffer_push_u8(S->buffer, ' '); diff --git a/src/core/specials.c b/src/core/specials.c index 12674e46..210786b1 100644 --- a/src/core/specials.c +++ b/src/core/specials.c @@ -83,7 +83,7 @@ static JanetSlot quasiquote(JanetFopts opts, Janet x) { case JANET_TABLE: case JANET_STRUCT: { const JanetKV *kv = NULL, *kvs = NULL; - int32_t len, cap; + int32_t len, cap = 0; janet_dictionary_view(x, &kvs, &len, &cap); while ((kv = janet_dictionary_next(kvs, cap, kv))) { JanetSlot key = quasiquote(opts, kv->key); @@ -134,10 +134,10 @@ static int destructure(JanetCompiler *c, return leaf(c, janet_unwrap_symbol(left), right, attr); case JANET_TUPLE: case JANET_ARRAY: { - int32_t i, len; - const Janet *values; + int32_t len = 0; + const Janet *values = NULL; janet_indexed_view(left, &values, &len); - for (i = 0; i < len; i++) { + for (int32_t i = 0; i < len; i++) { JanetSlot nextright = janetc_farslot(c); Janet subval = values[i]; if (i < 0x100) { @@ -154,9 +154,9 @@ static int destructure(JanetCompiler *c, case JANET_TABLE: case JANET_STRUCT: { const JanetKV *kvs = NULL; - int32_t i, cap, len; + int32_t cap = 0, len = 0; janet_dictionary_view(left, &kvs, &len, &cap); - for (i = 0; i < cap; i++) { + for (int32_t i = 0; i < cap; i++) { if (janet_checktype(kvs[i].key, JANET_NIL)) continue; JanetSlot nextright = janetc_farslot(c); JanetSlot k = janetc_value(janetc_fopts_default(c), kvs[i].key); diff --git a/src/core/util.h b/src/core/util.h index a3a6dcdb..7bc8da52 100644 --- a/src/core/util.h +++ b/src/core/util.h @@ -96,5 +96,4 @@ void janet_lib_typed_array(JanetTable *env); void janet_lib_inttypes(JanetTable *env); #endif - #endif diff --git a/test/suite6.janet b/test/suite6.janet index 3e679a14..3b4f8b70 100644 --- a/test/suite6.janet +++ b/test/suite6.janet @@ -47,7 +47,7 @@ # max double we can convert to int (2^53) (def b (i64 0x1fffffffffffff)) (def b (i64 (math/pow 2 53))) - # from string + # from string (def c (i64 "0x7fff_ffff_ffff_ffff")) (def d (i64 "123456789")))) @@ -88,6 +88,4 @@ )) "int64 typed arrays") - - (end-suite) diff --git a/tools/amalg.janet b/tools/amalg.janet index ae06208b..fee4cdde 100644 --- a/tools/amalg.janet +++ b/tools/amalg.janet @@ -1,77 +1,14 @@ -# Creates an amalgamated janet.c and janet.h to -# allow for easy embedding +# Creates an amalgamated janet.c +# Head (def {:year YY :month MM :month-day DD} (os/date)) - -(defn dofile - "Print one file to stdout" - [path] - (print (slurp path))) - -# Order is important here, as some headers -# depend on other headers. -(def headers - @["src/core/util.h" - "src/core/state.h" - "src/core/gc.h" - "src/core/vector.h" - "src/core/fiber.h" - "src/core/regalloc.h" - "src/core/compile.h" - "src/core/emit.h" - "src/core/symcache.h"]) - -(def sources - @["src/core/abstract.c" - "src/core/array.c" - "src/core/asm.c" - "src/core/buffer.c" - "src/core/bytecode.c" - "src/core/capi.c" - "src/core/cfuns.c" - "src/core/compile.c" - "src/core/corelib.c" - "src/core/debug.c" - "src/core/emit.c" - "src/core/fiber.c" - "src/core/gc.c" - "src/core/io.c" - "src/core/inttypes.c" - "src/core/marsh.c" - "src/core/math.c" - "src/core/os.c" - "src/core/parse.c" - "src/core/peg.c" - "src/core/pp.c" - "src/core/regalloc.c" - "src/core/run.c" - "src/core/specials.c" - "src/core/string.c" - "src/core/strtod.c" - "src/core/struct.c" - "src/core/symcache.c" - "src/core/table.c" - "src/core/tuple.c" - "src/core/typedarray.c" - "src/core/util.c" - "src/core/value.c" - "src/core/vector.c" - "src/core/vm.c" - "src/core/wrap.c"]) - (print "/* Amalgamated build - DO NOT EDIT */") (print "/* Generated " YY "-" (inc MM) "-" (inc DD) " with janet version " janet/version "-" janet/build " */") - -# Assume the version of janet used to run this script is the same -# as the version being generated (print "#define JANET_BUILD \"" janet/build "\"") - (print ```#define JANET_AMALG```) (print ```#include "janet.h"```) -(each h headers (dofile h)) -(each s sources (dofile s)) - -# Relies on these files being built -(dofile "build/core_image.c") +# Body +(each path (tuple/slice process/args 2) + (print (slurp path))) diff --git a/tools/format.sh b/tools/format.sh new file mode 100755 index 00000000..8d2ba436 --- /dev/null +++ b/tools/format.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# Format all code with astyle + +STYLEOPTS="--style=attach --indent-switches --convert-tabs \ + --align-pointer=name --pad-header --pad-oper --unpad-paren --indent-labels" + +astyle $STYLEOPTS */*.c +astyle $STYLEOPTS */*/*.c +astyle $STYLEOPTS */*/*.h