Skip to content

Commit

Permalink
Improve hex/utf8 string parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaAmora committed Apr 12, 2022
1 parent 934074d commit 2702207
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
38 changes: 31 additions & 7 deletions 4orth.porth
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 +
Expand Down Expand Up @@ -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 <file> Dump the ops of the program\n" fd fputs
" lex <file> Produce lexical analysis of the file\n" fd fputs
" summary <file> Print the summary of the program\n" fd fputs
// " dump <file> Dump the ops of the program\n" fd fputs
// " lex <file> Produce lexical analysis of the file\n" fd fputs
// " summary <file> Print the summary of the program\n" fd fputs
" png2src <file> 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
Expand Down
7 changes: 7 additions & 0 deletions 4std.porth
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 28 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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] <SUBCOMMAND>

OPTIONS:
-unsafe Disable type checking.
-I <path> Add <path> to the include paths list.
SUBCOMMANDS:
com [OPTIONS] <file> 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 <file> 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).
Expand All @@ -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.
4 changes: 2 additions & 2 deletions porth-4ork.porth
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2702207

Please sign in to comment.