diff --git a/4orth.porth b/4orth.porth index 6f955a0..0b91324 100644 --- a/4orth.porth +++ b/4orth.porth @@ -440,6 +440,31 @@ in else drop drop end end +proc inc-by-char-count ptr ptr in + memory str sizeof(Str) end + @Str str !Str + memory count sizeof(ptr) end + count !ptr + + while + while + str @Str.count 0 > "\\" + str @Str str-starts-with + lnot land + do + count @ptr inc64 + str str-chop-one-left + end + str @Str.count 3 >= "\\" + str @Str str-starts-with + land + do + str 3 + str-chop-left-by + count @ptr inc64 + end +end + proc generate-wat ptr // file-path in @@ -490,9 +515,8 @@ in " i32 (i32.const " bfd bputs char-counter @int wat-string-section + bfd bputu "))\n" bfd bputs - char-counter @int str @Str drop - "\\" str @Str str-starts-with if 3 / + 1 end + - char-counter !int + char-counter str + inc-by-char-count end end i 1 + @@ -707,11 +731,11 @@ in " -r Run the program after successful compilation\n" fd fputs " -b Bundles the program to a linux executable. (If with -r, executes the bundle)\n" fd fputs " -opt Optimize the program with wasm-opt\n" fd fputs - " -wat Transforms the labels stripped program back from `.wasm` to `.wat` \n" fd fputs + " -wat Transforms the stripped program back from the final `.wasm` to `.wat` \n" fd fputs " -s Silent mode. Don't print any info about compilation phases\n" fd fputs - " dump Dump the ops of the program\n" fd fputs - " lex Produce lexical analysis of the file\n" fd fputs - " summary Print the summary of the program\n" fd fputs + // " dump Dump the ops of the program\n" fd fputs + // " lex Produce lexical analysis of the file\n" fd fputs + // " summary Print the summary of the program\n" fd fputs " png2src Uses w4 to convert a png file to Porth code and prints it\n" fd fputs " -s Silent mode. Only print the converted code\n" fd fputs " help Print this help to stdout and exit with 0 code\n" fd fputs diff --git a/4std.porth b/4std.porth index fad75a9..77482d4 100644 --- a/4std.porth +++ b/4std.porth @@ -2,6 +2,13 @@ include "std.porth" inline proc inc-Str int ptr -- int ptr in 1 ptr+ swap 1 - swap end +inline proc dec64-by ptr int in over @64 swap - swap !64 end + +inline proc str-chop-left-by ptr int in + over Str.count over dec64-by + swap Str.data swap inc64-by +end + proc @hex int -- int ptr in memory hex-char sizeof(ptr) end tmp-end hex-char !ptr diff --git a/README.md b/README.md index 13daf0c..7fd69a9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# 4orth is a Porth to Wasm compiler with Wasm-4 bindings +# 4orth +4orth is a [Porth](https://gitlab.com/tsoding/porth) compiler with a [WASM-4](https://wasm4.org/) target. + ## Quick Start You can download the latest [release](https://github.com/FrankWPA/4orth/releases) or [bootstrap](https://github.com/FrankWPA/4orth#bootstrapping) it yourself. @@ -37,28 +39,41 @@ Or you can use the subcommands `-b` and `-r` to bundle and run after the compila $ ./4orth com -b -r game.porth ``` -Use `-opt` to benefit from [binaryen](https://github.com/WebAssembly/binaryen) optimization. Use `4orth help` to view all other available subcommands. +### Running options and subcommands + +```console +$ ./4orth [OPTIONS] + +OPTIONS: + -unsafe Disable type checking. + -I Add to the include paths list. +SUBCOMMANDS: + com [OPTIONS] Compile the program + -r Run the program after successful compilation + -b Bundles the program to a linux executable. (If with -r, executes the bundle) + -opt Optimize the program with wasm-opt + -wat Transforms the stripped program back from the final `.wasm` to `.wat` + -s Silent mode. Don't print any info about compilation phases + png2src Uses w4 to convert a png file to Porth code and prints it + -s Silent mode. Only print the converted code + help Print this help to stdout and exit with 0 code +``` ### Status 4orth is not feature complete with the current open version of Porth and Wasm-4 yet. The current set of features not supported consists of: -- Int64 -- Floats +- Let-bindings (let and peek keywords) +- Int64 (all integers are treated as Int32, if an overflow occurs on const evaluation, the .wat file will contain an error) - Negative numbers - -Porth operations: -- OP_BIND_LET -- OP_BIND_PEEK -- OP_PUSH_BIND -- OP_UNBIND +- Floats ### Changes -4orth implements some features not available in Porth to facilitate Wasm and Wasm-4 integration: +4orth implements some features not available in Porth to facilitate Wasm-4 integration: - Xor Intrinsic -- Hexadecimal numbers +- Hexadecimal numbers (as `0x` format on numbers, and as `\\` plus 2 digits on strings) - Bit shift support in const evaluation - Null terminated string support in const evaluation - Baked in random number generator (will be removed once there is float and int64 support). @@ -74,4 +89,4 @@ import proc trace ptr in end This adds the ability to call the wasm-4 `trace` function via the defined proc contract. Imported procs must have an empty body and no return type. (Porth's `print` intrinsic calls this imported proc, you can use either of them to log to the console) -All available [functions](https://wasm4.org/docs/reference/functions), constants and the [memory map](https://wasm4.org/docs/reference/memory) from Wasm-4 are in the [wasm4.porth](./wasm4.porth) library. +All available [functions](https://wasm4.org/docs/reference/functions), constants and the [memory map](https://wasm4.org/docs/reference/memory) from Wasm-4 are in the [wasm4.porth](./wasm4.porth) library. \ No newline at end of file diff --git a/porth-4ork.porth b/porth-4ork.porth index d233531..7f484f4 100644 --- a/porth-4ork.porth +++ b/porth-4ork.porth @@ -406,8 +406,8 @@ in lexer @ptr Lexer.line ptr+ ?str-empty lnot if lexer @ptr Lexer.line ptr+ @Str.data @8 'c' = if lexer @ptr Lexer.line ptr+ str-chop-one-left - 0 strbuf-append-char - token @ptr Token.value ptr+ offsetof(Str.count) ptr+ inc64 + "\\00" strbuf-append-str + token @ptr Token.value ptr+ offsetof(Str.count) ptr+ 3 inc64-by TOKEN_CSTR token @ptr Token.type ptr+ !64 else TOKEN_STR token @ptr Token.type ptr+ !64