How to resolve the algorithm Extend your language step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Extend your language step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

Some programming languages allow you to extend the language. While this can be done to a certain degree in most languages (e.g. by using macros), other languages go much further. Most notably in the Forth and Lisp families, programming per se is done by extending the language without any formal distinction between built-in and user-defined elements. If your language supports it, show how to introduce a new flow control mechanism. A practical and useful example is a four-way branch: Occasionally, code must be written that depends on two conditions, resulting in up to four branches (depending on whether both, only the first, only the second, or none of the conditions are "true"). In a C-like language this could look like the following: Besides being rather cluttered, the statement(s) for 'condition2isTrue' must be written down twice. If 'condition2isTrue' were a lengthy and involved expression, it would be quite unreadable, and the code generated by the compiler might be unnecessarily large. This can be improved by introducing a new keyword if2. It is similar to if, but takes two conditional statements instead of one, and up to three 'else' statements. One proposal (in pseudo-C syntax) might be: Pick the syntax which suits your language. The keywords 'else1' and 'else2' are just examples. The new conditional expression should look, nest and behave analogously to the language's built-in 'if' statement.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Extend your language step by step in the UNIX Shell programming language

Source code in the unix programming language

if2() {
	if eval "$1"; then
		if eval "$2"; then eval "$3"; else eval "$4"; fi
	else
		if eval "$2"; then eval "$5"; else eval "$6"; fi
	fi
}


if2 'test 7 -lt 9' 'test 7 -gt 9' '
	echo both 1 and 2
' '
	echo 1 but not 2
' '
	echo 2 but not 1
' '
	echo neither 1 nor 2
'


fn if2 cond1 cond2 body11 body10 body01 body00 {
	# Must evaluate both conditions, and should do so in order.
	# Negation ensures a boolean result: a true condition becomes
        # 1 for false; a false condition becomes 0 for true.
	let (c1 = <={! $cond1}; c2 = <={! $cond2}) {
		# Use those values to pick the body to evaluate.
		$(body$c1$c2)
	}
}

if2 {test 1 -gt 0} {~ grill foo bar boo} {
	echo 1 and 2
} {
	echo 1 but not 2
} {
	echo 2 but not 1
} {
	echo neither 1 nor 2
}

  

You may also check:How to resolve the algorithm Pairs with common factors step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Sort stability step by step in the Phix programming language
You may also check:How to resolve the algorithm Random numbers step by step in the BASIC programming language
You may also check:How to resolve the algorithm Proper divisors step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the Logo programming language