Commits

Anonymous committed 2024aa4

[svn r525] use local everywhere and $() instead of ``

Comments (0)

Files changed (2)

-- Add tests for all,any
+- Test with quotes, etc, (escaping)
+- Investigate some form of let (__make_function)?
+- Avoid leakeage on __lambdaN
 	__check_optr $0 "$@"
 	shift $(($OPTIND - 1))
 
-	__f=$(__make_lambda 1 "$1")
+	local f=$(__make_lambda 1 "$1")
 	shift
 
 	__args2stdin "$@" | {
-		__redir=
-		[[ -n "$__rc" ]] && __redir=">/dev/null"
+		local redir=
+		[[ -n "$__rc" ]] && redir=">/dev/null"
 
-		while read __a ; do
-			eval "__lambda1=\"$__a\" ; $__f $__redir"
-			__r="$?"
-			[[ -n "$__rc" ]] && echo "$__r"
+		local a
+		while read a ; do
+			# escape a
+			eval "__lambda1=\"$a\" ; $f $redir"
+			local r="$?"
+			[[ -n "$__rc" ]] && echo "$r"
 		done
 	}
 
 # Filters the input according to the given predicate.
 #-- filter :: Command1 -> [String] -> [String]
 function filter {
-	__f=$(__make_lambda 1 "$1")
+	local f=$(__make_lambda 1 "$1")
 	shift
 
 	__args2stdin "$@" | {
-		while read __a ; do
-			if eval "__lambda1=\"$__a\" ; $__f >/dev/null" ; then
-				echo "$__a"
+		local a
+		while read a ; do
+			# escape a
+			if eval "__lambda1=\"$a\" ; $f >/dev/null" ; then
+				echo "$a"
 			fi
 		done
 	}
 #-- foldl :: Command2 -> String -> [String] -> String
 function foldl {
 	OPTIND=
-	__rc=
-	while getopts ":r" __opt ; do
-		case $__opt in
-			r) __rc=1 ;;
-			*) __die "$0: unrecognized option -$OPTARG" ;;
-		esac
-	done ; shift $(($OPTIND - 1))
+	__check_optr $0 "$@"
+	shift $(($OPTIND - 1))
 
-	__f=$(__make_lambda 2 "$1")
-	__acc="$2"
+	local f=$(__make_lambda 2 "$1")
+	local acc="$2"
+
 	shift ; shift
 
 	__args2stdin "$@" | {
-		__redir=
-		[[ -n "$__rc" ]] && __redir=">/dev/null"
+		local redir=
+		[[ -n "$__rc" ]] && redir=">/dev/null"
 
-		while read __a ; do
-			__acc=$(eval "__lambda1=\"$__acc\" ; __lambda2=\"$__a\" ; $__f $__redir")
-			__r="$?"
-			[[ -n "$__rc" ]] && __acc="$__r"
+		local a=
+		while read a ; do
+			# escape a, acc
+			acc=$(eval "__lambda1=\"$acc\" ; __lambda2=\"$a\" ; $f $redir")
+			local r="$?"
+			[[ -n "$__rc" ]] && acc="$r"
 		done
-		echo "$__acc"
+		echo "$acc"
 	}
 
 	return 0
 #-- foldr :: Command2 -> String -> [String] -> String
 function foldr {
 	OPTIND=
-	__rc=
-	while getopts ":r" __opt ; do
-		case $__opt in
-			r) __rc=1 ;;
-			*) __die "$0: unrecognized option -$OPTARG" ;;
-		esac
-	done ; shift $(($OPTIND - 1))
+	__check_optr $0 "$@"
+	shift $(($OPTIND - 1))
 
-	__fo="$1"
-	__f=$(__make_lambda 2 "$1")
-	__acc="$2"
+	local fo="$1"
+	local f=$(__make_lambda 2 "$1")
+	local acc="$2"
 	shift ; shift
 
 	__args2stdin "$@" | {
-		if read __a ; then
-			__acc=`foldr "$__fo" "$__acc"`
-			__acc=$(eval "__lambda1=\"$__a\" ; __lambda2=\"$__acc\" ; $__f")
-			__r="$?"
-			[[ -n "$__rc" ]] && __acc="$__r"
+		local redir=
+		[[ -n "$__rc" ]] && redir=">/dev/null"
+
+		local a=
+		if read a ; then
+			acc=$(foldr "$fo" "$acc")
+			# escape a, acc
+			acc=$(eval "__lambda1=\"$a\" ; __lambda2=\"$acc\" ; $f $redir")
+			local r="$?"
+			[[ -n "$__rc" ]] && acc="$r"
 		fi
-		echo "$__acc"
+		echo "$acc"
 	}
 
 	return 0
 	__check_optr $0 "$@"
 	shift $(($OPTIND - 1))
 
-	__out=`foldl -r '\acc x -> __return "$acc" && __return "$x"' 0 "$@"`
+	local out=$(foldl -r '\acc x -> __return "$acc" && __return "$x"' 0 "$@")
 	if [[ -z "$__rc" ]] ; then
-		echo $__out
+		echo $out
 	fi
 
-	return $__out
+	return $out
 }
 
 
 	__check_optr $0 "$@"
 	shift $(($OPTIND - 1))
 
-	__out=`foldl -r '\acc x -> __return "$acc" || __return "$x"' 1 "$@"`
+	local out=$(foldl -r '\acc x -> __return "$acc" || __return "$x"' 1 "$@")
 	if [[ -z "$__rc" ]] ; then
-		echo $__out
+		echo "$out"
 	fi
 
-	return $__out
+	return "$out"
 }
 
 
 	__check_optr $0 "$@"
 	shift $(($OPTIND - 1))
 
-	__out=`map -r "$@" | and`
+	local out=$(map -r "$@" | and)
 	if [[ -z "$__rc" ]] ; then
-		echo $__out
+		echo "$out"
 	fi
 
-	return $__out
+	return "$out"
 }
 
 
 	__check_optr $0 "$@"
 	shift $(($OPTIND - 1))
 
-	__out=`map -r "$@" | or`
+	local out=$(map -r "$@" | or)
 	if [[ -z "$__rc" ]] ; then
-		echo $__out
+		echo "$out"
 	fi
 
-	return $__out
+	return "$out"
 }
 
 
 }
 
 
+function __bash_escape {
+	local a="$1"
+	a=${a//\\/\\\\} ;
+	a=${a//\"/\\\"} ;
+	echo "$a"
+}
+
+
 # Places args on stdin, or else passes stdin through.
 function __args2stdin {
 	if [[ "$#" -gt 0 ]] ; then
 		if [[ "$1" = '$' ]] ; then
-			shift 
-			for __a in "$@" ; do 
-				echo "$__a" ; 
+			shift
+			local a=
+			for a in "$@" ; do 
+				echo "$a" ; 
 			done
 		else
 			__die "syntax error before '$1': $ expected"
 		fi
 	else
-		while read __a ; do
-			echo "$__a"
+		local a=
+		while read a ; do
+			echo "$a"
 		done
 	fi
 }
 		# We have a lambda
 		__n=$((__n-1))
 		__plus=
-		[ "$__n" = "0" ] || __plus='\([[:space:]]\+\w\+\)\{'$__n'\}'
+		[ "$__n" -gt 0 ] && __plus='\([[:space:]]\+\w\+\)\{'$__n'\}'
+		[ "$__n" -lt 0 ] && __plus='\([[:space:]]\+\w\+\)*'
 		__lambda_pattern='^[[:space:]]*\\[[:space:]]*\w\+'$__plus'[[:space:]]*->[[:space:]]*'
 		if echo "$__f" | grep -q "$__lambda_pattern" ; then
 			__lambdas=${__f%%->*}
-			__lambdas=$(echo $__lambdas)	# Loose leading an trailing spaces
+			__lambdas=$(echo $__lambdas)	# Loose leading and trailing spaces
 			__lambdas=${__lambdas#\\}
 			__f=${__f#*->}
 
 }
 
 
+function __make_function {
+	local func="$@"
+
+	local function_pattern='^\w\+[[:space:]]*=[[:space:]]*'
+	if echo "$func" | grep -q "$function_pattern" ; then
+		local name=${func%%=*}
+		local body=${func#*=}
+		body=$(__make_lambda 0 "$body")
+		
+		local code="\
+function $name { \
+	local i=1 ;\
+	local a= ;\
+	for a in \"\$@\" ; do\
+		a=\$(__bash_escape \"\$a\") ;\
+		eval \"__lambda\$i=\\\"\$a\\\"\" ;\
+		i=\$((i + 1)) ;\
+	done ;\
+	$body ;\
+}"
+		eval "$code"
+	else
+		__die "function syntax error near '$func'"
+	fi
+}
+
+
 function __check_optr {
-	__name="$1"
+	local name="$1"
 	shift 
 
 	__rc=
-	while getopts ":r" __opt ; do
-		case $__opt in
+	local opt
+	while getopts ":r" opt ; do
+		case $opt in
 			r) __rc=1 ;;
-			*) __die "$__name: unrecognized option -$OPTARG" ;;
+			*) __die "$name: unrecognized option -$OPTARG" ;;
 		esac
 	done ;
 }