Skip to content

Commit

Permalink
Merge pull request #102 from udem-dlteam/laurent/simplify-sh-switch
Browse files Browse the repository at this point in the history
Simplify shell switch code
  • Loading branch information
laurenthuberdeau authored Oct 1, 2024
2 parents 85efec8 + 8673510 commit 176d87a
Show file tree
Hide file tree
Showing 21 changed files with 332 additions and 298 deletions.
2 changes: 1 addition & 1 deletion examples/compiled/all-chars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LC_ALL=C
_main() {
let c
c=0
while [ $c -lt 128 ] ; do
while [ $c -lt 128 ]; do
printf \\$((c/64))$((c/8%8))$((c%8))
: $((c += 1))
done
Expand Down
10 changes: 5 additions & 5 deletions examples/compiled/base64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ _codes=$__str_0
: $((b3 = b2 = b1 = 0))
_encode() {
let b1; let b2; let b3
while [ 1 != 0 ] ; do
while [ 1 != 0 ]; do
_getchar b1
if [ $b1 -lt 0 ] ; then
break
Expand Down Expand Up @@ -107,7 +107,7 @@ defarr _lut 256
: $((__t1 = c = 0))
_get() {
let c; let __t1
while _getchar __t1; [ $((c = __t1)) -ge 0 ] ; do
while _getchar __t1; [ $((c = __t1)) -ge 0 ]; do
if [ $((c = _$((_lut + c)))) -ge 0 ] ; then
break
fi
Expand All @@ -120,16 +120,16 @@ _get() {
_decode() {
let i; let c1; let c2; let c3; let c4; let __t1
i=0
while [ $i -lt 256 ] ; do
while [ $i -lt 256 ]; do
: $((_$((_lut + i)) = -1))
: $((i += 1))
done
i=0
while [ $i -lt 64 ] ; do
while [ $i -lt 64 ]; do
: $((_$((_lut + 255 & _$((_codes + i)))) = i))
: $((i += 1))
done
while _get __t1; [ $((c1 = __t1)) -ge 0 ] ; do
while _get __t1; [ $((c1 = __t1)) -ge 0 ]; do
if _get __t1; [ $((c2 = __t1)) -lt 0 ] ; then
exit 1
fi
Expand Down
64 changes: 32 additions & 32 deletions examples/compiled/c4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _memset() { # b: $2, c: $3, len: $4
c=$3
len=$4
p=$b
while [ $(((len -= 1) + 1)) != 0 ] ; do
while [ $(((len -= 1) + 1)) != 0 ]; do
: $((_$(((p += 1) - 1)) = c))
done
: $(($1 = b))
Expand All @@ -24,7 +24,7 @@ _memcmp() { # vl: $2, vr: $3, n: $4
n=$4
l=$vl
r=$vr
while [ $n != 0 ] && [ $((_$l)) = $((_$r)) ] ; do
while [ $n != 0 ] && [ $((_$l)) = $((_$r)) ]; do
: $((n -= 1))
: $((l += 1))
: $((r += 1))
Expand Down Expand Up @@ -143,13 +143,13 @@ readonly _Idsz=9
: $((__t1 = pp = 0))
_next() {
set $@ $pp $__t1
while [ $((_tk = _$_p)) != 0 ] ; do
while [ $((_tk = _$_p)) != 0 ]; do
: $((_p += 1))
if [ $_tk = $__NEWLINE__ ] ; then
if [ $_src != 0 ] ; then
printf "%d: (%d - %d = %d) %.*s" $_line $_p $_lp $((_p - _lp)) $((_p - _lp)) "$(_put_pstr __ $_lp)"
_lp=$_p
while [ $_le -lt $_e ] ; do
while [ $_le -lt $_e ]; do
defstr __str_0 "LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,OPEN,READ,CLOS,PRTF,MALC,FREE,MSET,MCMP,EXIT,"
printf "%8.4s" "$(_put_pstr __ $((__str_0 + _$((_le += 1)) * 5)))"
if [ $((_$_le)) -le $_ADJ ] ; then
Expand All @@ -161,17 +161,17 @@ _next() {
fi
: $((_line += 1))
elif [ $_tk = $__SHARP__ ] ; then
while [ $((_$_p)) != 0 ] && [ $((_$_p)) != $__NEWLINE__ ] ; do
while [ $((_$_p)) != 0 ] && [ $((_$_p)) != $__NEWLINE__ ]; do
: $((_p += 1))
done
elif { [ $_tk -ge $__a__ ] && [ $_tk -le $__z__ ]; } || { [ $_tk -ge $__A__ ] && [ $_tk -le $__Z__ ]; } || [ $_tk = $__UNDERSCORE__ ] ; then
pp=$((_p - 1))
while { [ $((_$_p)) -ge $__a__ ] && [ $((_$_p)) -le $__z__ ]; } || { [ $((_$_p)) -ge $__A__ ] && [ $((_$_p)) -le $__Z__ ]; } || { [ $((_$_p)) -ge $__0__ ] && [ $((_$_p)) -le $__9__ ]; } || [ $((_$_p)) = $__UNDERSCORE__ ] ; do
while { [ $((_$_p)) -ge $__a__ ] && [ $((_$_p)) -le $__z__ ]; } || { [ $((_$_p)) -ge $__A__ ] && [ $((_$_p)) -le $__Z__ ]; } || { [ $((_$_p)) -ge $__0__ ] && [ $((_$_p)) -le $__9__ ]; } || [ $((_$_p)) = $__UNDERSCORE__ ]; do
_tk=$(((_tk * 147) + _$(((_p += 1) - 1))))
done
_tk=$(((_tk << 6) + (_p - pp)))
_id=$_sym
while [ $((_$((_id + _Tk)))) != 0 ] ; do
while [ $((_$((_id + _Tk)))) != 0 ]; do
if [ $_tk = $((_$((_id + _Hash)))) ] && { _memcmp __t1 $((_$((_id + _Name)))) $pp $((_p - pp)); [ $((!__t1)) != 0 ]; } ; then
_tk=$((_$((_id + _Tk))))
: $((__tmp = $1)) $((pp = $2)) $((__t1 = $3)) $(($1 = __tmp))
Expand All @@ -185,15 +185,15 @@ _next() {
break
elif [ $_tk -ge $__0__ ] && [ $_tk -le $__9__ ] ; then
if [ $((_ival = _tk - __0__)) != 0 ] ; then
while [ $((_$_p)) -ge $__0__ ] && [ $((_$_p)) -le $__9__ ] ; do
while [ $((_$_p)) -ge $__0__ ] && [ $((_$_p)) -le $__9__ ]; do
_ival=$((((_ival * 10) + _$(((_p += 1) - 1))) - __0__))
done
elif [ $((_$_p)) = $__x__ ] || [ $((_$_p)) = $__X__ ] ; then
while [ $((_tk = _$((_p += 1)))) != 0 ] && { { [ $_tk -ge $__0__ ] && [ $_tk -le $__9__ ]; } || { [ $_tk -ge $__a__ ] && [ $_tk -le $__f__ ]; } || { [ $_tk -ge $__A__ ] && [ $_tk -le $__F__ ]; }; } ; do
while [ $((_tk = _$((_p += 1)))) != 0 ] && { { [ $_tk -ge $__0__ ] && [ $_tk -le $__9__ ]; } || { [ $_tk -ge $__a__ ] && [ $_tk -le $__f__ ]; } || { [ $_tk -ge $__A__ ] && [ $_tk -le $__F__ ]; }; }; do
_ival=$(((_ival * 16) + (_tk & 15) + ((_tk >= __A__) ? 9: 0)))
done
else
while [ $((_$_p)) -ge $__0__ ] && [ $((_$_p)) -le $__7__ ] ; do
while [ $((_$_p)) -ge $__0__ ] && [ $((_$_p)) -le $__7__ ]; do
_ival=$((((_ival * 8) + _$(((_p += 1) - 1))) - __0__))
done
fi
Expand All @@ -202,7 +202,7 @@ _next() {
elif [ $_tk = $__SLASH__ ] ; then
if [ $((_$_p)) = $__SLASH__ ] ; then
: $((_p += 1))
while [ $((_$_p)) != 0 ] && [ $((_$_p)) != $__NEWLINE__ ] ; do
while [ $((_$_p)) != 0 ] && [ $((_$_p)) != $__NEWLINE__ ]; do
: $((_p += 1))
done
else
Expand All @@ -211,7 +211,7 @@ _next() {
fi
elif [ $_tk = $__QUOTE__ ] || [ $_tk = $__DQUOTE__ ] ; then
pp=$_data
while [ $((_$_p)) != 0 ] && [ $((_$_p)) != $_tk ] ; do
while [ $((_$_p)) != 0 ] && [ $((_$_p)) != $_tk ]; do
if [ $((_ival = _$(((_p += 1) - 1)))) = $__BACKSLASH__ ] ; then
if [ $((_ival = _$(((_p += 1) - 1)))) = $__n__ ] ; then
_ival=$__NEWLINE__
Expand Down Expand Up @@ -334,7 +334,7 @@ _expr() { # lev: $2
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = _ival))
_next __
while [ $_tk = $__DQUOTE__ ] ; do
while [ $_tk = $__DQUOTE__ ]; do
_next __
done
_data=$(((_data + 1) & -(1)))
Expand All @@ -354,7 +354,7 @@ _expr() { # lev: $2
_next __
_ty=$_CHAR
fi
while [ $_tk = $_Mul ] ; do
while [ $_tk = $_Mul ]; do
_next __
_ty=$((_ty + _PTR))
done
Expand All @@ -373,7 +373,7 @@ _expr() { # lev: $2
if [ $_tk = $__LPAREN__ ] ; then
_next __
t=0
while [ $_tk != $__RPAREN__ ] ; do
while [ $_tk != $__RPAREN__ ]; do
_expr __ $_Assign
: $((_$((_e += 1)) = _PSH))
: $((t += 1))
Expand Down Expand Up @@ -418,7 +418,7 @@ _expr() { # lev: $2
if [ $_tk = $_Int ] || [ $_tk = $_Char ] ; then
t=$(((_tk == _Int) ? _INT: _CHAR))
_next __
while [ $_tk = $_Mul ] ; do
while [ $_tk = $_Mul ]; do
_next __
t=$((t + _PTR))
done
Expand Down Expand Up @@ -515,7 +515,7 @@ _expr() { # lev: $2
printf "%d: bad expression\n" $_line
exit -1
fi
while [ $_tk -ge $lev ] ; do
while [ $_tk -ge $lev ]; do
t=$_ty
if [ $_tk = $_Assign ] ; then
_next __
Expand Down Expand Up @@ -787,7 +787,7 @@ _stmt() {
fi
elif [ $_tk = $__LBRACE__ ] ; then
_next __
while [ $_tk != $__RBRACE__ ] ; do
while [ $_tk != $__RBRACE__ ]; do
_stmt __
done
_next __
Expand Down Expand Up @@ -864,12 +864,12 @@ _main() { # argc: $2, argv: $3
defstr __str_1 "char else enum if int return sizeof while open read close printf malloc free memset memcmp exit void main"
_p=$__str_1
i=$_Char
while [ $i -le $_While ] ; do
while [ $i -le $_While ]; do
_next __
: $((_$((_id + _Tk)) = (i += 1) - 1))
done
i=$_OPEN
while [ $i -le $_EXIT ] ; do
while [ $i -le $_EXIT ]; do
_next __
: $((_$((_id + _Class)) = _Sys))
: $((_$((_id + _Type)) = _INT))
Expand All @@ -895,7 +895,7 @@ _main() { # argc: $2, argv: $3
_close __ $fd
_line=1
_next __
while [ $_tk != 0 ] ; do
while [ $_tk != 0 ]; do
bt=$_INT
if [ $_tk = $_Int ] ; then
_next __
Expand All @@ -910,7 +910,7 @@ _main() { # argc: $2, argv: $3
if [ $_tk = $__LBRACE__ ] ; then
_next __
i=0
while [ $_tk != $__RBRACE__ ] ; do
while [ $_tk != $__RBRACE__ ]; do
if [ $_tk != $_Id ] ; then
printf "%d: bad enum identifier %d\n" $_line $_tk
: $(($1 = -1))
Expand Down Expand Up @@ -939,9 +939,9 @@ _main() { # argc: $2, argv: $3
_next __
fi
fi
while [ $_tk != $__SEMICOLON__ ] && [ $_tk != $__RBRACE__ ] ; do
while [ $_tk != $__SEMICOLON__ ] && [ $_tk != $__RBRACE__ ]; do
ty=$bt
while [ $_tk = $_Mul ] ; do
while [ $_tk = $_Mul ]; do
_next __
ty=$((ty + _PTR))
done
Expand All @@ -964,15 +964,15 @@ _main() { # argc: $2, argv: $3
: $((_$((_id + _Val)) = (_e + 1)))
_next __
i=0
while [ $_tk != $__RPAREN__ ] ; do
while [ $_tk != $__RPAREN__ ]; do
ty=$_INT
if [ $_tk = $_Int ] ; then
_next __
elif [ $_tk = $_Char ] ; then
_next __
ty=$_CHAR
fi
while [ $_tk = $_Mul ] ; do
while [ $_tk = $_Mul ]; do
_next __
ty=$((ty + _PTR))
done
Expand Down Expand Up @@ -1008,12 +1008,12 @@ _main() { # argc: $2, argv: $3
fi
_loc=$((i += 1))
_next __
while [ $_tk = $_Int ] || [ $_tk = $_Char ] ; do
while [ $_tk = $_Int ] || [ $_tk = $_Char ]; do
bt=$(((_tk == _Int) ? _INT: _CHAR))
_next __
while [ $_tk != $__SEMICOLON__ ] ; do
while [ $_tk != $__SEMICOLON__ ]; do
ty=$bt
while [ $_tk = $_Mul ] ; do
while [ $_tk = $_Mul ]; do
_next __
ty=$((ty + _PTR))
done
Expand Down Expand Up @@ -1044,12 +1044,12 @@ _main() { # argc: $2, argv: $3
done
: $((_$((_e += 1)) = _ENT))
: $((_$((_e += 1)) = i - _loc))
while [ $_tk != $__RBRACE__ ] ; do
while [ $_tk != $__RBRACE__ ]; do
_stmt __
done
: $((_$((_e += 1)) = _LEV))
_id=$_sym
while [ $((_$((_id + _Tk)))) != 0 ] ; do
while [ $((_$((_id + _Tk)))) != 0 ]; do
if [ $((_$((_id + _Class)))) = $_Loc ] ; then
: $((_$((_id + _Class)) = _$((_id + _HClass))))
: $((_$((_id + _Type)) = _$((_id + _HType))))
Expand Down Expand Up @@ -1087,7 +1087,7 @@ _main() { # argc: $2, argv: $3
: $((_$((sp -= 1)) = argv_))
: $((_$((sp -= 1)) = t))
cycle=0
while [ 1 != 0 ] ; do
while [ 1 != 0 ]; do
i=$((_$(((pc += 1) - 1))))
: $((cycle += 1))
if [ $_debug != 0 ] ; then
Expand Down
4 changes: 2 additions & 2 deletions examples/compiled/cat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defarr _buf 1024
_cat_fd() { let fd $2
let n; let __t1
n=1024
while [ $n = 1024 ] ; do
while [ $n = 1024 ]; do
_read n $fd $_buf 1024
if [ $n -lt 0 ] || { _write __t1 1 $_buf $n; [ $__t1 != $n ]; } ; then
exit 1
Expand All @@ -43,7 +43,7 @@ _main() { let argc $2; let myargv $3
let i
if [ $argc -ge 2 ] ; then
i=1
while [ $i -lt $argc ] ; do
while [ $i -lt $argc ]; do
if [ $((_$((_$((myargv + i)) + 0)))) = $__MINUS__ ] && [ $((_$((_$((myargv + i)) + 1)))) = $__NUL__ ] ; then
_cat_fd __ 0
else
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/cp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ _main() { let argc $2; let args $3
if [ $dst = 0 ] ; then
_file_error __ $((_$((args + 2))))
fi
while _read __t1 $src $_buffer 1024; [ $((len = __t1)) != 0 ] ; do
while _read __t1 $src $_buffer 1024; [ $((len = __t1)) != 0 ]; do
_write __ $dst $_buffer $len
done
endlet $1 __t1 len c dst src args argc
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/echo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LC_ALL=C
_main() { let argc $2; let argv_ $3
let i
i=1
while [ $i -lt $argc ] ; do
while [ $i -lt $argc ]; do
_put_pstr __ $((_$((argv_ + i))))
printf " "
: $((i += 1))
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/fib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ _fib() { let n $2
_main() {
let n; let i
i=0
while [ $i -lt 20 ] ; do
while [ $i -lt 20 ]; do
_fib n $i
printf "fib(%d) = %d\n" $i $n
: $((i += 1))
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/non_zero.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _main() {
while :; do
printf "Enter a non-zero single-digit number:\r\n"
_getchar n
while _getchar __t1; [ $__NEWLINE__ != $__t1 ] ; do
while _getchar __t1; [ $__NEWLINE__ != $__t1 ]; do
:
done
[ $n = $__0__ ] || [ $((!((n >= __0__) && (n <= __9__)))) != 0 ]|| break
Expand Down
6 changes: 3 additions & 3 deletions examples/compiled/print-reverse.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ _reverse_str() { let str $2
let end; let tmp; let len; let i
end=$str
i=0
while [ $((_$(((end += 1) - 1)))) != 0 ] ; do
while [ $((_$(((end += 1) - 1)))) != 0 ]; do
:
done
len=$(((end - str) - 1))
while [ $i -lt $((len / 2)) ] ; do
while [ $i -lt $((len / 2)) ]; do
tmp=$((_$((str + i))))
: $((_$((str + i)) = _$((str + ((len - 1) - i)))))
: $((_$((str + (len - 1) - i)) = tmp))
Expand All @@ -24,7 +24,7 @@ _reverse_str() { let str $2
_main() { let argc $2; let argv_ $3
let i
i=1
while [ $i -lt $argc ] ; do
while [ $i -lt $argc ]; do
_reverse_str __ $((_$((argv_ + i))))
_put_pstr __ $i
printf "\n"
Expand Down
Loading

0 comments on commit 176d87a

Please sign in to comment.