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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Extend your language step by step in the Seed7 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 Seed7 programming language

Source code in the seed7 programming language

$ include "seed7_05.s7i";

$ syntax expr: .if.().().then.().else1.().else2.().else3.().end.if is -> 25;

const proc: if (in boolean: cond1) (in boolean: cond2) then
              (in proc: statements1)
            else1
              (in proc: statements2)
            else2
              (in proc: statements3)
            else3
              (in proc: statements4)
            end if                     is func
  begin
    if cond1 then
      if cond2 then
        statements1;
      else
        statements2;
      end if;
    elsif cond2 then
      statements3;
    else
      statements4;
    end if;
  end func;

const proc: main is func
  begin
    if TRUE FALSE then
      writeln("error TRUE TRUE");
    else1
      writeln("TRUE FALSE");
    else2
      writeln("error FALSE TRUE");
    else3
      writeln("error FALSE FALSE");
    end if;
  end func;

  

You may also check:How to resolve the algorithm Zero to the zero power step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Jaro-Winkler distance step by step in the Go programming language
You may also check:How to resolve the algorithm Parallel brute force step by step in the Sidef programming language
You may also check:How to resolve the algorithm Modular exponentiation step by step in the Perl programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the TI SR-56 programming language