[fpc-devel] String cases development

Daniël Mantione daniel.mantione at freepascal.org
Sat Apr 18 10:02:29 CEST 2009

Op Sat, 18 Apr 2009, schreef Mike Denisenko:

> Hello everyone, I'm a student of FESU, Russia. I've started to develop a feature - string cases (Topic: Easiest way to "case" strings, http://www.lazarus.freepascal.org/pipermail/lazarus/2009-March/023369.html), and now some questions appear:
> Existing method of parsing and generating assembly code doesn't process any constants but ordinal type,
> so the class holding case data, TCaseNode, contains labelinfo as object of TCaseLabel record.
> And TCaseLabel contains two fields: _low and _high representing the low 
> and high bounds of interval signed as case label. These fields are of 
> TConstExprInt type, so if we want to process the string label we get a 
> problem: how it's better to hold its value? Should we simply add two 
> string-type fields to TCaseLabel, or it should be solved by creating a 
> base class with some virtual procedures and refactoring all modules 
> having procedures operating with this type?

I am fine converting Tcaselabel into a variant record that takes a Pstring 
field. A new class is not a good idea, since we already have classes for 
this purpose: Tnode and descendends, a string constant can be represented 
by a Tstringconstnode. Tnode should be the most flexible since it allows 
you to handle all types of contants (and non constants if necessary).

Probably the parser already creates a Tnode tree, does constant 
expression evalutation, and then creates a Tcaselabel with the evaluated 

I wrote this case-label code about 10 years ago, it has a fundamental 
flaw: There is a worst case with quadratic time blowup, if you create a 
specially crafted case statement in the code, you can keep the compiler 
busy for a very long time. On the other hand, this has never been a 
problem in the lifetime of the compiler.


More information about the fpc-devel mailing list