How to resolve the algorithm Inverted syntax step by step in the ALGOL 68 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Inverted syntax step by step in the ALGOL 68 programming language
Table of Contents
Problem Statement
Inverted syntax with conditional expressions In traditional syntax conditional expressions are usually shown before the action within a statement or code block: In inverted syntax, the action is listed before the conditional expression in the statement or code block: Inverted syntax with assignment In traditional syntax, assignments are usually expressed with the variable appearing before the expression: In inverted syntax, the expression appears before the variable: Task The task is to demonstrate support for inverted syntax forms within the language by showing both the traditional and inverted forms.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Inverted syntax step by step in the ALGOL 68 programming language
Source code in the algol programming language
# Inverted assignment #
# Assignment in Algol 68 is via ":=" which is automaically provided for all modes (types) #
# However we could define e.g. "=:" as an inverted assignment operator but we would need to #
# define a separate operator for each mode, e.g. for integers and strings: #
PRIO =: = 1;
OP =: = ( INT a, REF INT b )REF INT: b := a;
OP =: = ( STRING a, REF STRING b )REF STRING: b := a;
OP =: = ( CHAR a, REF STRING b )REF STRING: b := a;
INT a, b; STRING s;
1 =: a;
a + 1 =: b;
"?" =: s;
print( ( a, b, s, newline ) );
# There is one standard inverted assignment operator: +=: or PLUSTO which prepends a string #
# to another: #
"bc" =: s;
"b" +=: s;
print( ( s, newline ) );
# Inverted Conditional Expressions #
# We could define an operator called WHEN perhaps, that would execute its left operand if #
# the right operand was TRUE. However the left operand would need to be a PROC VOID so the #
# syntax would not be as convientent as the standard IF-THEN-FI construct. E.g.: #
PRIO WHEN = 1;
OP WHEN = ( PROC VOID code, BOOL test )VOID: IF test THEN code FI;
( VOID: print( ( "NO", newline ) ) ) WHEN a = b; # the anonymous PROC VOID is not called #
( VOID: print( ( "yes", newline ) ) ) WHEN a /= b # the anonymous PROC VOID is called #
You may also check:How to resolve the algorithm Exponentiation operator step by step in the Quackery programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the VBA programming language
You may also check:How to resolve the algorithm Middle three digits step by step in the Picat programming language
You may also check:How to resolve the algorithm Align columns step by step in the Factor programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the PL/I programming language