🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #408

Re: Taming The Switch-Statement

#408
From: Dan Cross
Date: Thu, 15 Oct 2026 01:26
283 lines
6593 bytes
In article <11aoqa9$1rgda$1@dont-email.me>, bart  <bc@freeuk.com> wrote:
>On 14/10/2026 20:30, Dan Cross wrote:
>[snip more of the same already hashed and rehashed ad naseum]
>
>As for multiple nested loops, this will fix that too, that's great. We 
>just require some real examples of that being needed!

Here's a pretty terrible way to write part of a lexical
analyzer.  I just typed this in an hour or so ago.

#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static size_t lineno = 1;
static size_t colno = 0;

#define	MAXTOKLEN	1024

typedef enum TokenType TokenType;
enum TokenType {
	TkERROR = -1,
	TkEOF = 0,

	TkSTAR = '*',
	TkSLASH = '/',
	TkPLUS = '+',
	TkMINUS = '-',

	TkLT = '<',
	TkGT = '>',
	TkEQ = '=',
	TkBANG = '!',

	TkGE = 0x1000,
	TkLE,
	TkSTRING,
	TkIDENT,
	// etc....
};

typedef struct Token Token;
struct Token {
	TokenType type;
	char *token;
};

Token *
newtoken(TokenType tt, char *s, char *e)
{
	size_t len = sizeof(Token) + (e - s) + 1 + (e == s);
	Token *token = malloc(len);
	if (token == NULL) abort();
	memset(token, 0, len);
	token->type = tt;
	token->token = (char *)token + sizeof(*token);
	token->token[0] = tt;
	if (e != s)
		memcpy(token->token, s, e - s);
	return token;
}

static int
peek(FILE *fp)
{
	int ch = fgetc(fp);
	if (ch != EOF)
		ungetc(ch, fp);
	return ch;
}

Token *
nexttoken(FILE *fp)
{
	char tokstr[MAXTOKLEN], *tp = tokstr, *te = tokstr + MAXTOKLEN - 1;
	enum TokenType tt = TkEOF;
	Token *next = NULL;
	int ch;
	unsigned char c;

	loop:
	while ((ch = fgetc(fp)) != EOF) {
		tt = TkERROR;
		++colno;
		switch (ch) {
		case '\n':
			++lineno;
			colno = 0;
		case ' ':
		case '\t':
		case '\f':
		case '\v':
		case '\r':
			break;
		case '*':
			tt = TkSTAR;
			break loop;
		case '/':
			tt = TkSLASH;
			ch = peek(fp);
			switch (ch) {
			case EOF:
				break loop;
			case '/':
				while ((ch = fgetc(fp)) != '\n')
					if (ch == EOF)
						break loop;
				ungetc(ch, fp);
				continue;
			default:
				break;
			}
			break loop;
		case '+':
			tt = TkPLUS;
			break loop;
		case '-':
			tt = TkMINUS;
			break loop;
		case '<':
			tt = TkLT;
			*tp++ = '<'; 
			ch = peek(fp);
			switch (ch) {
			case EOF:
				break loop;
			case '=':
				tt = TkLE;
				*tp++ = '=';
				break;
			default:
				break;
			}
			*tp++ = '\0';
			break loop;
		case '>':
			tt = TkGT;
			*tp++ = '>';
			ch = peek(fp);
			switch (ch) {
			case EOF:
				break loop;
			case '=':
				tt = TkGE;
				*tp++ = '=';
				break;
			default:
				break;
			}
			*tp++ = '\0';
			break loop;
		case '=':
			tt = TkEQ;
			break loop;
		case '!':
			tt = TkBANG;
			break loop;
		case '"':
			tt = TkERROR;
			sloop:
			while (tp != te) {
				ch = fgetc(fp);
				switch (ch) {
				case EOF:
					break loop;
				case '"':
					break sloop;

				case '\\':
					ch = fgetc(fp);
					switch (ch) {
					case EOF:
						break loop;
					case 'n':
						ch = '\n';
						break;
					case 'r':
						ch = '\r';
						break;
					case 't':
						ch = '\t';
						break;
					default:
						break;
					}
				default:
					*tp++ = ch;
					break;
				}
			}
			*tp++ = '\0';
			tt = TkSTRING;
			break loop;
		default:
			c = ch;
			if (c != '_' && !isalnum(c))
				break loop;
			*tp++ = c;
			while (tp != te) {
				ch = fgetc(fp);
				if (ch == EOF)
					break loop;
				c = ch;
				if (c != '_' && !isalnum(c)) {
					ungetc(ch, fp);
					break;
				}
				*tp++ = c;
			}
			*tp++ = '\0';
			tt = TkIDENT;
			break loop;
		}
	}
	next = newtoken(tt, tokstr, tp);
	return next;
}

int
main(void)
{
	Token *tok;
	while ((tok = nexttoken(stdin)) != NULL) {
		if (tok->type == TkEOF)
			break;
		if (tok->type != TkERROR)
			printf("tok: tt = %x, token = '%s'\n", tok->type, tok->token);
		free(tok);
	}
	return 0;
}

>>> And yet I've solved all the problems in this thread and many others.
>
>> Congratulations!  And yet, despite you being obviously more
>> intelligent than all of Ritchie, McIlroy, Thompson, Johnson,
>> Kernighan, Plauger, and all of those who have and continue to
>> contribute to the development of C combined,
>
>Well, my personal C has 'breakloop'! Usually these micro-extensions are 
>shortlived, but I'll keep this one.

Consider that perhaps your obstinance about keeping this sort
of poorly thought-through extension is WHY no one but you uses
your stuff.

>  we don't see your
>> name on the list of contributors to that language, nor does
>> anyone other than you use your language.
>
>It's enough that I used it and benefited from it.
>
>This is that last example in my syntax (if the switch were the only 
>statement within the loop otherwise two more tokens needed):
>
>    doswitch x
>    when 3 then exit
>    end
>
>7 tokens vs. 20 tokens using N3355, and it's repeatable within the 
>function because 'exit' is relative.

Ok.  Don't really care.

>> But keep shooting hoops, bart.  Maybe some day a talent scout
>> will see you in your driveway and give you a starting position
>> on a playoff team.
>
>I really don't care. Most things in the world are not going the way I'd 
>like, and I'm not the only one who thinks that.
>
>This language stuff is small potatoes but it gives me kick to have long 
>ago come up with solutions that some languages take ... 54 years to 
>produce a compromise.
>
>Anyway, at least you've been a sport, unlike Mr. P.

*shrug*

	- Dan C.
Technical Details
Message-ID: <11apa4b$kv$1@reader2.panix.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail
References: <119qf6l$2rmg3$1@dont-email.me> <11ao8af$1k8p3$1@dont-email.me> <11aol8k$adq$1@reader2.panix.com> <11aoqa9$1rgda$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail
From: cross@spitfire.i.gajendra.net (Dan Cross)
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Thu, 15 Oct 2026 01:26:35 -0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Message-ID: <11apa4b$kv$1@reader2.panix.com>
References: <119qf6l$2rmg3$1@dont-email.me> <11ao8af$1k8p3$1@dont-email.me> <11aol8k$adq$1@reader2.panix.com> <11aoqa9$1rgda$1@dont-email.me>
Injection-Date: Thu, 15 Oct 2026 01:26:35 -0000 (UTC)
Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80";
	logging-data="671"; mail-complaints-to="abuse@panix.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: cross@spitfire.i.gajendra.net (Dan Cross)