Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve printf and simplify runtime #72

Merged
merged 16 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/compiled/all-chars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ _main() {
# Local variables
__=0
__SP=0
let() { # $1: variable name, $2: value (optional)
let() { # $1: variable name, $2: value (optional)
: $((__SP += 1)) $((__$__SP=$1)) # Push
: $(($1=${2-0})) # Init
}
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/base64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ make_argv() {
# Local variables
__=0
__SP=0
let() { # $1: variable name, $2: value (optional)
let() { # $1: variable name, $2: value (optional)
: $((__SP += 1)) $((__$__SP=$1)) # Push
: $(($1=${2-0})) # Init
}
Expand Down
30 changes: 8 additions & 22 deletions examples/compiled/cat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,12 @@ read_byte() { # $2: fd
: $((__cursor_fd$__fd = __cursor + 1)) # Increment cursor
}


# Convert a pointer to a C string to a Shell string.
# $__res is set to the result, and $__len is set to the length of the string.
pack_string() { # $1 = string address, $2 = end of string delimiter (default to null), $3 = max length (default to 100000000)
__addr=$1;
__max_len=100000000
__delim=0
__len=0
__res=""
if [ $# -ge 2 ] ; then __delim=$2 ; fi # Optional end of string delimiter
if [ $# -ge 3 ] ; then __max_len=$3 ; fi # Optional max length
while [ $((_$__addr)) != $__delim ] && [ $__max_len -gt $__len ] ; do
__char=$((_$__addr))
__addr=$((__addr + 1))
__len=$((__len + 1))
case $__char in
10) __res="$__res\n" ;; # 10 == '\n'
*) __char=$(printf "\\$(($__char/64))$(($__char/8%8))$(($__char%8))")
__res="$__res$__char" ;;
esac
_put_pstr() {
: $(($1 = 0)); shift # Return 0
__addr=$1; shift
while [ $((_$__addr)) != 0 ]; do
printf \\$((_$__addr/64))$((_$__addr/8%8))$((_$__addr%8))
: $((__addr += 1))
done
}

Expand Down Expand Up @@ -196,7 +182,7 @@ _open() { # $2: filename, $3: flags, $4: mode
: $((__cursor_fd$__fd = 0)) # Make buffer empty
: $((__buflen_fd$__fd = 1000)) # Init buffer length
: $((__state_fd$__fd = $3)) # Mark the fd as opened
pack_string $2
__res=$(_put_pstr __ $2)
if [ $3 = 0 ] ; then
case $__fd in
0) exec 0< "$__res" ;; 1) exec 1< "$__res" ;; 2) exec 2< "$__res" ;;
Expand Down Expand Up @@ -298,7 +284,7 @@ make_argv() {
# Local variables
__=0
__SP=0
let() { # $1: variable name, $2: value (optional)
let() { # $1: variable name, $2: value (optional)
: $((__SP += 1)) $((__$__SP=$1)) # Push
: $(($1=${2-0})) # Init
}
Expand Down
29 changes: 3 additions & 26 deletions examples/compiled/cp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defarr _buffer 1024
_main() { let argc $2; let args $3
let src; let dst; let c; let len; let __t1
if [ $argc != 3 ] ; then
printf "Usage: cp <source> <destination>\n"
printf "Usage: cp <source> <destination>\n"
: $(($1 = 1))
endlet $1 __t1 len c dst src args argc
return
Expand Down Expand Up @@ -129,29 +129,6 @@ read_byte() { # $2: fd
: $((__cursor_fd$__fd = __cursor + 1)) # Increment cursor
}


# Convert a pointer to a C string to a Shell string.
# $__res is set to the result, and $__len is set to the length of the string.
pack_string() { # $1 = string address, $2 = end of string delimiter (default to null), $3 = max length (default to 100000000)
__addr=$1;
__max_len=100000000
__delim=0
__len=0
__res=""
if [ $# -ge 2 ] ; then __delim=$2 ; fi # Optional end of string delimiter
if [ $# -ge 3 ] ; then __max_len=$3 ; fi # Optional max length
while [ $((_$__addr)) != $__delim ] && [ $__max_len -gt $__len ] ; do
__char=$((_$__addr))
__addr=$((__addr + 1))
__len=$((__len + 1))
case $__char in
10) __res="$__res\n" ;; # 10 == '\n'
*) __char=$(printf "\\$(($__char/64))$(($__char/8%8))$(($__char%8))")
__res="$__res$__char" ;;
esac
done
}

__state_fd0=0;
_malloc __buffer_fd0 1000 # Allocate buffer
: $((_$__buffer_fd0 = 0)) # Init buffer to ""
Expand Down Expand Up @@ -189,7 +166,7 @@ _open() { # $2: filename, $3: flags, $4: mode
: $((__cursor_fd$__fd = 0)) # Make buffer empty
: $((__buflen_fd$__fd = 1000)) # Init buffer length
: $((__state_fd$__fd = $3)) # Mark the fd as opened
pack_string $2
__res=$(_put_pstr __ $2)
if [ $3 = 0 ] ; then
case $__fd in
0) exec 0< "$__res" ;; 1) exec 1< "$__res" ;; 2) exec 2< "$__res" ;;
Expand Down Expand Up @@ -277,7 +254,7 @@ make_argv() {
# Local variables
__=0
__SP=0
let() { # $1: variable name, $2: value (optional)
let() { # $1: variable name, $2: value (optional)
: $((__SP += 1)) $((__$__SP=$1)) # Push
: $(($1=${2-0})) # Init
}
Expand Down
4 changes: 2 additions & 2 deletions examples/compiled/echo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ _main() { let argc $2; let argv_ $3
printf " "
: $((i += 1))
done
printf "\n"
printf "\n"
endlet $1 i argv_ argc
}

Expand Down Expand Up @@ -70,7 +70,7 @@ make_argv() {
# Local variables
__=0
__SP=0
let() { # $1: variable name, $2: value (optional)
let() { # $1: variable name, $2: value (optional)
: $((__SP += 1)) $((__$__SP=$1)) # Push
: $(($1=${2-0})) # Init
}
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/fib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ _main() {
# Local variables
__=0
__SP=0
let() { # $1: variable name, $2: value (optional)
let() { # $1: variable name, $2: value (optional)
: $((__SP += 1)) $((__$__SP=$1)) # Push
: $(($1=${2-0})) # Init
}
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/hello.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e -u
LC_ALL=C

_main() {
printf "Hello, world\n"
printf "Hello, world\n"
}

# Runtime library
Expand Down
4 changes: 2 additions & 2 deletions examples/compiled/print-reverse.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ _main() { let argc $2; let argv_ $3
while [ $i -lt $argc ] ; do
_reverse_str __ $((_$((argv_ + i))))
_put_pstr __ $i
printf "\n" $((_$((argv_ + i))))
printf "\n"
: $((i += 1))
done
endlet $1 i argv_ argc
Expand Down Expand Up @@ -88,7 +88,7 @@ make_argv() {
# Local variables
__=0
__SP=0
let() { # $1: variable name, $2: value (optional)
let() { # $1: variable name, $2: value (optional)
: $((__SP += 1)) $((__$__SP=$1)) # Push
: $(($1=${2-0})) # Init
}
Expand Down
28 changes: 7 additions & 21 deletions examples/compiled/repl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -839,26 +839,12 @@ read_byte() { # $2: fd
: $((__cursor_fd$__fd = __cursor + 1)) # Increment cursor
}


# Convert a pointer to a C string to a Shell string.
# $__res is set to the result, and $__len is set to the length of the string.
pack_string() { # $1 = string address, $2 = end of string delimiter (default to null), $3 = max length (default to 100000000)
__addr=$1;
__max_len=100000000
__delim=0
__len=0
__res=""
if [ $# -ge 2 ] ; then __delim=$2 ; fi # Optional end of string delimiter
if [ $# -ge 3 ] ; then __max_len=$3 ; fi # Optional max length
while [ $((_$__addr)) != $__delim ] && [ $__max_len -gt $__len ] ; do
__char=$((_$__addr))
__addr=$((__addr + 1))
__len=$((__len + 1))
case $__char in
10) __res="$__res\n" ;; # 10 == '\n'
*) __char=$(printf "\\$(($__char/64))$(($__char/8%8))$(($__char%8))")
__res="$__res$__char" ;;
esac
_put_pstr() {
: $(($1 = 0)); shift # Return 0
__addr=$1; shift
while [ $((_$__addr)) != 0 ]; do
printf \\$((_$__addr/64))$((_$__addr/8%8))$((_$__addr%8))
: $((__addr += 1))
done
}

Expand Down Expand Up @@ -899,7 +885,7 @@ _open() { # $2: filename, $3: flags, $4: mode
: $((__cursor_fd$__fd = 0)) # Make buffer empty
: $((__buflen_fd$__fd = 1000)) # Init buffer length
: $((__state_fd$__fd = $3)) # Mark the fd as opened
pack_string $2
__res=$(_put_pstr __ $2)
if [ $3 = 0 ] ; then
case $__fd in
0) exec 0< "$__res" ;; 1) exec 1< "$__res" ;; 2) exec 2< "$__res" ;;
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/reverse.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ make_argv() {
# Local variables
__=0
__SP=0
let() { # $1: variable name, $2: value (optional)
let() { # $1: variable name, $2: value (optional)
: $((__SP += 1)) $((__$__SP=$1)) # Push
: $(($1=${2-0})) # Init
}
Expand Down
44 changes: 17 additions & 27 deletions examples/compiled/select-file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ LC_ALL=C
# functions that return multiple strings, the `unpack_lines` function can be
# used to unpack them into an array of strings.
#
# By default, the `unpack_string` function from the runtime library is included.
# This is because the implementation of `unpack_string` depends on the
# compilation options of pnut and may be optimized for different use cases.
#
# Similarly, the `pack_string` function calls the `_put_pstr` function from the
# runtime library so it doesn't have to be included twice (one for the runtime
# library and one for this script).
#
# ############################# Memory allocation ##############################
#
# Memory can be dynamically allocated using the `_malloc` function, and freed
Expand All @@ -50,6 +58,11 @@ LC_ALL=C
#
################################################################################

# Convert a C string to a shell string. The result is stored in $__res.
pack_string() { # $1 = string address
__res=$(_put_pstr __ $1)
}

# Like `unpack_string` but for multiple strings separated by newlines.
# Returns a pointer to the first string in the array, and null terminates the
# array.
Expand Down Expand Up @@ -140,7 +153,7 @@ _print_array() { let arr $2
while [ $((_$((arr + i)))) != 0 ] ; do
printf "%d: " $i
_put_pstr __ $((_$((arr + i))))
printf "\n"
printf "\n" $i
: $((i += 1))
done
endlet $1 i arr
Expand Down Expand Up @@ -184,12 +197,12 @@ _main() {
_array_len len $files
_print_array __ $files
while [ 1 != 0 ] ; do
printf "Select a file to print: "
printf "Select a file to print: "
_read_int ix
if [ 0 -le $ix ] && [ $ix -lt $len ] ; then
break
fi
printf "Invalid index.\n"
printf "Invalid index.\n"
done
_cat __ $((_$((files + ix))))
endlet $1 __t1 len ix files
Expand Down Expand Up @@ -242,7 +255,7 @@ _put_pstr() {
# Local variables
__=0
__SP=0
let() { # $1: variable name, $2: value (optional)
let() { # $1: variable name, $2: value (optional)
: $((__SP += 1)) $((__$__SP=$1)) # Push
: $(($1=${2-0})) # Init
}
Expand All @@ -257,29 +270,6 @@ endlet() { # $1: return variable
: $(($__ret=__tmp)) # Restore return value
}


# Convert a pointer to a C string to a Shell string.
# $__res is set to the result, and $__len is set to the length of the string.
pack_string() { # $1 = string address, $2 = end of string delimiter (default to null), $3 = max length (default to 100000000)
__addr=$1;
__max_len=100000000
__delim=0
__len=0
__res=""
if [ $# -ge 2 ] ; then __delim=$2 ; fi # Optional end of string delimiter
if [ $# -ge 3 ] ; then __max_len=$3 ; fi # Optional max length
while [ $((_$__addr)) != $__delim ] && [ $__max_len -gt $__len ] ; do
__char=$((_$__addr))
__addr=$((__addr + 1))
__len=$((__len + 1))
case $__char in
10) __res="$__res\n" ;; # 10 == '\n'
*) __char=$(printf "\\$(($__char/64))$(($__char/8%8))$(($__char%8))")
__res="$__res$__char" ;;
esac
done
}

__ALLOC=1 # Starting heap at 1 because 0 is the null pointer.

_malloc() { # $2 = object size
Expand Down
30 changes: 8 additions & 22 deletions examples/compiled/sha256sum.sh
Original file line number Diff line number Diff line change
Expand Up @@ -358,26 +358,12 @@ read_byte() { # $2: fd
: $((__cursor_fd$__fd = __cursor + 1)) # Increment cursor
}


# Convert a pointer to a C string to a Shell string.
# $__res is set to the result, and $__len is set to the length of the string.
pack_string() { # $1 = string address, $2 = end of string delimiter (default to null), $3 = max length (default to 100000000)
__addr=$1;
__max_len=100000000
__delim=0
__len=0
__res=""
if [ $# -ge 2 ] ; then __delim=$2 ; fi # Optional end of string delimiter
if [ $# -ge 3 ] ; then __max_len=$3 ; fi # Optional max length
while [ $((_$__addr)) != $__delim ] && [ $__max_len -gt $__len ] ; do
__char=$((_$__addr))
__addr=$((__addr + 1))
__len=$((__len + 1))
case $__char in
10) __res="$__res\n" ;; # 10 == '\n'
*) __char=$(printf "\\$(($__char/64))$(($__char/8%8))$(($__char%8))")
__res="$__res$__char" ;;
esac
_put_pstr() {
: $(($1 = 0)); shift # Return 0
__addr=$1; shift
while [ $((_$__addr)) != 0 ]; do
printf \\$((_$__addr/64))$((_$__addr/8%8))$((_$__addr%8))
: $((__addr += 1))
done
}

Expand Down Expand Up @@ -418,7 +404,7 @@ _open() { # $2: filename, $3: flags, $4: mode
: $((__cursor_fd$__fd = 0)) # Make buffer empty
: $((__buflen_fd$__fd = 1000)) # Init buffer length
: $((__state_fd$__fd = $3)) # Mark the fd as opened
pack_string $2
__res=$(_put_pstr __ $2)
if [ $3 = 0 ] ; then
case $__fd in
0) exec 0< "$__res" ;; 1) exec 1< "$__res" ;; 2) exec 2< "$__res" ;;
Expand Down Expand Up @@ -510,7 +496,7 @@ make_argv() {
# Local variables
__=0
__SP=0
let() { # $1: variable name, $2: value (optional)
let() { # $1: variable name, $2: value (optional)
: $((__SP += 1)) $((__$__SP=$1)) # Push
: $(($1=${2-0})) # Init
}
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/sum-array.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ _malloc() { # $2 = object size
# Local variables
__=0
__SP=0
let() { # $1: variable name, $2: value (optional)
let() { # $1: variable name, $2: value (optional)
: $((__SP += 1)) $((__$__SP=$1)) # Push
: $(($1=${2-0})) # Init
}
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/wc-stdin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ _getchar() {
# Local variables
__=0
__SP=0
let() { # $1: variable name, $2: value (optional)
let() { # $1: variable name, $2: value (optional)
: $((__SP += 1)) $((__$__SP=$1)) # Push
: $(($1=${2-0})) # Init
}
Expand Down
Loading
Loading