On 2022-03-28, Ed Morton <
morto...@gmail.com> wrote:
> On 3/28/2022 2:09 AM, Kaz Kylheku wrote:
>> GNU Awk has a switch statement.
>>
>> In cppawk I made a case statement which translates to GNU Awk switch,
>> but supports non-GNU Awk.
>
> If I want portable code why wouldn't I simply write portable code?
My use case is that what I *have* is nonportable code, and I want
GNU Awk to keep seeing more or less that same nonportable code while
I make it run on another implementation also.
Furthermore, I have reached a more or less final version of the macro
which is safer to use than switch: each clause must explicitly break,
fall-through or return from the surrounding function. This is now a
syntax error:
case (state) {
of (1, 2, 3)
state++
of (4)
return 0
}
it has to be:
case (state) {
of (1, 2, 3)
state++
cbreak # explicit cbreak, cfall or cret() required
of (4)
cret (0)
}
The syntax error isn't something nice (just unbalanced braces), but it's
better than silence.
> s = $0
> if (s == "foo") {
> print "** foo case: fallthrough!"
> }
> if ( (s == "bar") || (s == "xyzzy") ) {
> print "** bar/xyzzy case"
> }
This translation doesn't preserve the fallthrough; so while we have
portable code now, it has succumbed to human error.
I will make fewer conversion mistakes with a macro whose features
closely mimic switch, and which can be separately validated by its own
test cases.
> With "cppawk" you seem to be solving a problem that doesn't exist by
> creating a real problem - introducing a private tool that'd be required
I released this to the public, under a BSD license. It relies on other
commonly available and installed public tools.
> to parse the code and the necessity for people to have to look up ".h"
> files to figure out what the language constructs in the code mean and
That could be a problem in a project without documentation.
Oh, look; good thing I have that!
$ man -l cppawk-case.1
CPPAWK-CASE(1) Case Macro CPPAWK-CASE(1)
NAME
case - macro for portable switch statement
SYNOPSIS
#include <case.h>
[...]
> the potential for the tokens in the .h files to clash with variables or
> other language constructs used in the script.
While it's certainly possible to make a dog's breakfast of this, I'm
consciously following best practices for header file hygiene, because
that is super important in a tool like this. I'm hardly a beginner.
--
TXR Programming Language:
http://nongnu.org/txr
Cygnal: Cygwin Native Application Library:
http://kylheku.com/cygnal