How to resolve the algorithm Extend your language step by step in the ALGOL W programming language
How to resolve the algorithm Extend your language step by step in the ALGOL W 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 ALGOL W programming language
Source code in the algol programming language
begin
% executes pBoth, p1, p2 or pNeither %
% depending on whether c1 and c2 are true, c1 is true, c2 is true %
% neither c1 nor c2 are true %
procedure if2 ( logical value c1, c2
; procedure pBoth, p1, p2, pNeither
);
if c1 and c2 then pBoth
else if c1 then p1
else if c2 then p2
else pNeither
;
begin
logical a, b;
a := true;
b := false;
if2( a, b
, write( "both: a and b are TRUE" )
, write( "first: only a is TRUE" )
, write( "second: only b is TRUE" )
, write( "neither: a and b are FALSE" )
)
end
end.
You may also check:How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the Java programming language
You may also check:How to resolve the algorithm Guess the number/With feedback (player) step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Loops/Wrong ranges step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Go programming language