Pnut: A C to POSIX shell compiler you can trust
pnut.sh193 pointsby feeley118 comments
> (define (f x) (/ 1 (* x x)))
> (f 5)
> (f "hello") ;; the * procedure will raise an exception and highlight "(* x x)" $ cat hello.scm
(display "hello!\n")
$ gsc hello.scm
$ gsi hello.o1
hello!
$ ls -l hello.o1 # this is generated by gcc
-rwxrwxr-x 1 feeley feeley 18152 Mar 13 17:16 hello.o1
$ rm hello.o1
$ gsc -cc "tcc -shared" hello.scm
$ gsi hello.o1
hello!
$ ls -l hello.o1 # this is generated by tcc
-rwxrwxr-x 1 feeley feeley 4432 Mar 13 17:17 hello.o1 > (module-whitelist-add! '(github.com/feeley))
> (import (github.com/feeley/roman demo))
This paper might be helpful if you want to know why it was designed that way: printf \\$((byte/64))$((byte/8%8))$((byte%8))
For getchar is is not possible to use the shell "read" command because it does not handle null bytes on stdin (and we want Ribbit to support binary I/O). This is admittedly somewhat of an artificial constraint given that Ribbit will probably be mostly used for text processing, like to write compilers that map source code text to target code text. So getchar is implemented with set `sed q | od -v -A n -t u1`
and then the next byte on stdin is available using $1 and "shift" is used to move to the next byte. The call to sed is to read each line separately (useful to implement a REPL where you enter a command which is executed before having to enter the next command). Both "sed" and "od" are standard unix tools. Moreover we checked that those set of options have existed for a long time (at least the early 1990's). It would be interesting to test many POSIX shells to verify Ribbit's portability (we used bash, dash, zsh and ksh for our testing). n=10000000; while [ $n -gt 0 ]; do n=$((n-1)); done
runs about 5000 times slower with bash than the equivalent JavaScript code run with nodejs, which is implemented with a JIT. Secondly, the POSIX shell does not have arrays so it is necessary to use the shell "eval" command to do indexing. For example a[i]=0 becomes eval a$i=0
As you can imagine the RVM implementation uses arrays all over the place for implementing access to the RVM's stack and Scheme objects (both of which are implemented using a garbage collected heap, which is conceptually an array of 3 field records and implemented in the shell as 3 "arrays").
Author: do you see issues with that use case?