Skip to content

Commit

Permalink
Merge pull request #108 from udem-dlteam/laurent/fix-typo-runtime
Browse files Browse the repository at this point in the history
Fix typo and slighly shorter runtime
  • Loading branch information
laurenthuberdeau authored Oct 20, 2024
2 parents 885bbb4 + 76cea41 commit db6a8b1
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 84 deletions.
19 changes: 7 additions & 12 deletions examples/compiled/c4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1626,11 +1626,8 @@ refill_buffer() { # $1: fd
__buffer=$((__buffer_fd$__fd))
IFS=
if read -r __temp_buf <&$__fd ; then # read next line into $__temp_buf
__ends_with_eof=0
else
__ends_with_eof=1
fi
__ends_with_eof=0
read -r __temp_buf <&$__fd || __ends_with_eof=1
# Check that the buffer is large enough to unpack the line
__buflen=$((__buflen_fd$__fd - 2)) # Minus 2 to account for newline and \0
Expand Down Expand Up @@ -1698,9 +1695,9 @@ _open() { # $2: filename, $3: flags, $4: mode
# is read and then unpacked in the buffer.
_malloc __addr 1000 # Allocate buffer
: $((_$__addr = 0)) # Init buffer to ""
: $((__buffer_fd$__fd = __addr)) # Save buffer address
: $((__cursor_fd$__fd = 0)) # Make buffer empty
: $((__buflen_fd$__fd = 1000)) # Init buffer length
: $((__buffer_fd$__fd = __addr)) # Buffer address
: $((__cursor_fd$__fd = 0)) # Buffer cursor
: $((__buflen_fd$__fd = 1000)) # Buffer length
: $((__state_fd$__fd = $3)) # Mark the fd as opened
__res=$(_put_pstr __ $2)
if [ $3 = 0 ] ; then
Expand All @@ -1725,7 +1722,7 @@ _open() { # $2: filename, $3: flags, $4: mode
9) exec 9>> "$__res" ;;
esac
else
echo "Unknow file mode" ; exit 1
echo "Unknown file mode" ; exit 1
fi
fi
: $(($1 = __fd))
Expand All @@ -1735,9 +1732,7 @@ _read() { : $((__fd = $2)) $((__buf = $3)) $((__count = $4))
: $((__i = 0))
while [ $__i -lt $__count ] ; do
read_byte __byte $__fd
if [ $__byte -lt 0 ] ; then
break
fi
if [ $__byte -lt 0 ] ; then break; fi
: $((_$((__buf + __i)) = __byte))
: $((__i += 1))
done
Expand Down
19 changes: 7 additions & 12 deletions examples/compiled/cat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,8 @@ refill_buffer() { # $1: fd
__buffer=$((__buffer_fd$__fd))

IFS=
if read -r __temp_buf <&$__fd ; then # read next line into $__temp_buf
__ends_with_eof=0
else
__ends_with_eof=1
fi
__ends_with_eof=0
read -r __temp_buf <&$__fd || __ends_with_eof=1

# Check that the buffer is large enough to unpack the line
__buflen=$((__buflen_fd$__fd - 2)) # Minus 2 to account for newline and \0
Expand Down Expand Up @@ -178,9 +175,9 @@ _open() { # $2: filename, $3: flags, $4: mode
# is read and then unpacked in the buffer.
_malloc __addr 1000 # Allocate buffer
: $((_$__addr = 0)) # Init buffer to ""
: $((__buffer_fd$__fd = __addr)) # Save buffer address
: $((__cursor_fd$__fd = 0)) # Make buffer empty
: $((__buflen_fd$__fd = 1000)) # Init buffer length
: $((__buffer_fd$__fd = __addr)) # Buffer address
: $((__cursor_fd$__fd = 0)) # Buffer cursor
: $((__buflen_fd$__fd = 1000)) # Buffer length
: $((__state_fd$__fd = $3)) # Mark the fd as opened
__res=$(_put_pstr __ $2)
if [ $3 = 0 ] ; then
Expand All @@ -205,7 +202,7 @@ _open() { # $2: filename, $3: flags, $4: mode
9) exec 9>> "$__res" ;;
esac
else
echo "Unknow file mode" ; exit 1
echo "Unknown file mode" ; exit 1
fi
fi
: $(($1 = __fd))
Expand All @@ -215,9 +212,7 @@ _read() { : $((__fd = $2)) $((__buf = $3)) $((__count = $4))
: $((__i = 0))
while [ $__i -lt $__count ] ; do
read_byte __byte $__fd
if [ $__byte -lt 0 ] ; then
break
fi
if [ $__byte -lt 0 ] ; then break; fi
: $((_$((__buf + __i)) = __byte))
: $((__i += 1))
done
Expand Down
19 changes: 7 additions & 12 deletions examples/compiled/cp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,8 @@ refill_buffer() { # $1: fd
__buffer=$((__buffer_fd$__fd))

IFS=
if read -r __temp_buf <&$__fd ; then # read next line into $__temp_buf
__ends_with_eof=0
else
__ends_with_eof=1
fi
__ends_with_eof=0
read -r __temp_buf <&$__fd || __ends_with_eof=1

# Check that the buffer is large enough to unpack the line
__buflen=$((__buflen_fd$__fd - 2)) # Minus 2 to account for newline and \0
Expand Down Expand Up @@ -162,9 +159,9 @@ _open() { # $2: filename, $3: flags, $4: mode
# is read and then unpacked in the buffer.
_malloc __addr 1000 # Allocate buffer
: $((_$__addr = 0)) # Init buffer to ""
: $((__buffer_fd$__fd = __addr)) # Save buffer address
: $((__cursor_fd$__fd = 0)) # Make buffer empty
: $((__buflen_fd$__fd = 1000)) # Init buffer length
: $((__buffer_fd$__fd = __addr)) # Buffer address
: $((__cursor_fd$__fd = 0)) # Buffer cursor
: $((__buflen_fd$__fd = 1000)) # Buffer length
: $((__state_fd$__fd = $3)) # Mark the fd as opened
__res=$(_put_pstr __ $2)
if [ $3 = 0 ] ; then
Expand All @@ -189,7 +186,7 @@ _open() { # $2: filename, $3: flags, $4: mode
9) exec 9>> "$__res" ;;
esac
else
echo "Unknow file mode" ; exit 1
echo "Unknown file mode" ; exit 1
fi
fi
: $(($1 = __fd))
Expand All @@ -199,9 +196,7 @@ _read() { : $((__fd = $2)) $((__buf = $3)) $((__count = $4))
: $((__i = 0))
while [ $__i -lt $__count ] ; do
read_byte __byte $__fd
if [ $__byte -lt 0 ] ; then
break
fi
if [ $__byte -lt 0 ] ; then break; fi
: $((_$((__buf + __i)) = __byte))
: $((__i += 1))
done
Expand Down
19 changes: 7 additions & 12 deletions examples/compiled/repl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -990,11 +990,8 @@ refill_buffer() { # $1: fd
__buffer=$((__buffer_fd$__fd))
IFS=
if read -r __temp_buf <&$__fd ; then # read next line into $__temp_buf
__ends_with_eof=0
else
__ends_with_eof=1
fi
__ends_with_eof=0
read -r __temp_buf <&$__fd || __ends_with_eof=1
# Check that the buffer is large enough to unpack the line
__buflen=$((__buflen_fd$__fd - 2)) # Minus 2 to account for newline and \0
Expand Down Expand Up @@ -1071,9 +1068,9 @@ _open() { # $2: filename, $3: flags, $4: mode
# is read and then unpacked in the buffer.
_malloc __addr 1000 # Allocate buffer
: $((_$__addr = 0)) # Init buffer to ""
: $((__buffer_fd$__fd = __addr)) # Save buffer address
: $((__cursor_fd$__fd = 0)) # Make buffer empty
: $((__buflen_fd$__fd = 1000)) # Init buffer length
: $((__buffer_fd$__fd = __addr)) # Buffer address
: $((__cursor_fd$__fd = 0)) # Buffer cursor
: $((__buflen_fd$__fd = 1000)) # Buffer length
: $((__state_fd$__fd = $3)) # Mark the fd as opened
__res=$(_put_pstr __ $2)
if [ $3 = 0 ] ; then
Expand All @@ -1098,7 +1095,7 @@ _open() { # $2: filename, $3: flags, $4: mode
9) exec 9>> "$__res" ;;
esac
else
echo "Unknow file mode" ; exit 1
echo "Unknown file mode" ; exit 1
fi
fi
: $(($1 = __fd))
Expand All @@ -1108,9 +1105,7 @@ _read() { : $((__fd = $2)) $((__buf = $3)) $((__count = $4))
: $((__i = 0))
while [ $__i -lt $__count ] ; do
read_byte __byte $__fd
if [ $__byte -lt 0 ] ; then
break
fi
if [ $__byte -lt 0 ] ; then break; fi
: $((_$((__buf + __i)) = __byte))
: $((__i += 1))
done
Expand Down
19 changes: 7 additions & 12 deletions examples/compiled/sha256sum.sh
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,8 @@ refill_buffer() { # $1: fd
__buffer=$((__buffer_fd$__fd))
IFS=
if read -r __temp_buf <&$__fd ; then # read next line into $__temp_buf
__ends_with_eof=0
else
__ends_with_eof=1
fi
__ends_with_eof=0
read -r __temp_buf <&$__fd || __ends_with_eof=1
# Check that the buffer is large enough to unpack the line
__buflen=$((__buflen_fd$__fd - 2)) # Minus 2 to account for newline and \0
Expand Down Expand Up @@ -401,9 +398,9 @@ _open() { # $2: filename, $3: flags, $4: mode
# is read and then unpacked in the buffer.
_malloc __addr 1000 # Allocate buffer
: $((_$__addr = 0)) # Init buffer to ""
: $((__buffer_fd$__fd = __addr)) # Save buffer address
: $((__cursor_fd$__fd = 0)) # Make buffer empty
: $((__buflen_fd$__fd = 1000)) # Init buffer length
: $((__buffer_fd$__fd = __addr)) # Buffer address
: $((__cursor_fd$__fd = 0)) # Buffer cursor
: $((__buflen_fd$__fd = 1000)) # Buffer length
: $((__state_fd$__fd = $3)) # Mark the fd as opened
__res=$(_put_pstr __ $2)
if [ $3 = 0 ] ; then
Expand All @@ -428,7 +425,7 @@ _open() { # $2: filename, $3: flags, $4: mode
9) exec 9>> "$__res" ;;
esac
else
echo "Unknow file mode" ; exit 1
echo "Unknown file mode" ; exit 1
fi
fi
: $(($1 = __fd))
Expand All @@ -438,9 +435,7 @@ _read() { : $((__fd = $2)) $((__buf = $3)) $((__count = $4))
: $((__i = 0))
while [ $__i -lt $__count ] ; do
read_byte __byte $__fd
if [ $__byte -lt 0 ] ; then
break
fi
if [ $__byte -lt 0 ] ; then break; fi
: $((_$((__buf + __i)) = __byte))
: $((__i += 1))
done
Expand Down
19 changes: 7 additions & 12 deletions examples/compiled/wc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,8 @@ refill_buffer() { # $1: fd
__buffer=$((__buffer_fd$__fd))

IFS=
if read -r __temp_buf <&$__fd ; then # read next line into $__temp_buf
__ends_with_eof=0
else
__ends_with_eof=1
fi
__ends_with_eof=0
read -r __temp_buf <&$__fd || __ends_with_eof=1

# Check that the buffer is large enough to unpack the line
__buflen=$((__buflen_fd$__fd - 2)) # Minus 2 to account for newline and \0
Expand Down Expand Up @@ -203,9 +200,9 @@ _open() { # $2: filename, $3: flags, $4: mode
# is read and then unpacked in the buffer.
_malloc __addr 1000 # Allocate buffer
: $((_$__addr = 0)) # Init buffer to ""
: $((__buffer_fd$__fd = __addr)) # Save buffer address
: $((__cursor_fd$__fd = 0)) # Make buffer empty
: $((__buflen_fd$__fd = 1000)) # Init buffer length
: $((__buffer_fd$__fd = __addr)) # Buffer address
: $((__cursor_fd$__fd = 0)) # Buffer cursor
: $((__buflen_fd$__fd = 1000)) # Buffer length
: $((__state_fd$__fd = $3)) # Mark the fd as opened
__res=$(_put_pstr __ $2)
if [ $3 = 0 ] ; then
Expand All @@ -230,7 +227,7 @@ _open() { # $2: filename, $3: flags, $4: mode
9) exec 9>> "$__res" ;;
esac
else
echo "Unknow file mode" ; exit 1
echo "Unknown file mode" ; exit 1
fi
fi
: $(($1 = __fd))
Expand All @@ -240,9 +237,7 @@ _read() { : $((__fd = $2)) $((__buf = $3)) $((__count = $4))
: $((__i = 0))
while [ $__i -lt $__count ] ; do
read_byte __byte $__fd
if [ $__byte -lt 0 ] ; then
break
fi
if [ $__byte -lt 0 ] ; then break; fi
: $((_$((__buf + __i)) = __byte))
: $((__i += 1))
done
Expand Down
19 changes: 7 additions & 12 deletions sh-runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -716,9 +716,9 @@ DEPENDS_ON(put_pstr)
putstr(" # is read and then unpacked in the buffer.\n");
putstr(" _malloc __addr 1000 # Allocate buffer\n");
putstr(" : $((_$__addr = 0)) # Init buffer to \"\"\n");
putstr(" : $((__buffer_fd$__fd = __addr)) # Save buffer address\n");
putstr(" : $((__cursor_fd$__fd = 0)) # Make buffer empty\n");
putstr(" : $((__buflen_fd$__fd = 1000)) # Init buffer length\n");
putstr(" : $((__buffer_fd$__fd = __addr)) # Buffer address\n");
putstr(" : $((__cursor_fd$__fd = 0)) # Buffer cursor\n");
putstr(" : $((__buflen_fd$__fd = 1000)) # Buffer length\n");
putstr(" : $((__state_fd$__fd = $3)) # Mark the fd as opened\n");
putstr(" __res=$(_put_pstr __ $2)\n");
putstr(" if [ $3 = 0 ] ; then\n");
Expand All @@ -743,7 +743,7 @@ DEPENDS_ON(put_pstr)
putstr(" 9) exec 9>> \"$__res\" ;;\n");
putstr(" esac\n");
putstr(" else\n");
putstr(" echo \"Unknow file mode\" ; exit 1\n");
putstr(" echo \"Unknown file mode\" ; exit 1\n");
putstr(" fi\n");
putstr(" fi\n");
putstr(" : $(($1 = __fd))\n");
Expand Down Expand Up @@ -791,11 +791,8 @@ DEPENDS_ON(char_to_int)
putstr(" __buffer=$((__buffer_fd$__fd))\n");
putstr("\n");
putstr(" IFS=\n");
putstr(" if read -r __temp_buf <&$__fd ; then # read next line into $__temp_buf\n");
putstr(" __ends_with_eof=0\n");
putstr(" else\n");
putstr(" __ends_with_eof=1\n");
putstr(" fi\n");
putstr(" __ends_with_eof=0\n");
putstr(" read -r __temp_buf <&$__fd || __ends_with_eof=1\n");
putstr("\n");
putstr(" # Check that the buffer is large enough to unpack the line\n");
putstr(" __buflen=$((__buflen_fd$__fd - 2)) # Minus 2 to account for newline and \\0\n");
Expand Down Expand Up @@ -838,9 +835,7 @@ DEPENDS_ON(open)
putstr(" : $((__i = 0))\n");
putstr(" while [ $__i -lt $__count ] ; do\n");
putstr(" read_byte __byte $__fd\n");
putstr(" if [ $__byte -lt 0 ] ; then\n");
putstr(" break\n");
putstr(" fi\n");
putstr(" if [ $__byte -lt 0 ] ; then break; fi\n");
putstr(" : $((_$((__buf + __i)) = __byte))\n");
putstr(" : $((__i += 1))\n");
putstr(" done\n");
Expand Down

0 comments on commit db6a8b1

Please sign in to comment.