Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #34021
| From | mhx@iae.nl (Marcel Hendrix) |
|---|---|
| Subject | Re: Tiny regular expressions code |
| Newsgroups | comp.lang.forth |
| Message-ID | <17931816938433@frunobulax.edu> (permalink) |
| Date | 2014-10-19 17:10 +0200 |
| References | <2014Oct18.174324@mips.complang.tuwien.ac.at> |
| Organization | SunSITE.dk - Supporting Open source |
anton@mips.complang.tuwien.ac.at (Anton Ertl) writes Re: Tiny regular expressions code
>> Hans Bezemer <the.beez.speaks@gmail.com> writes:
> Related to an adjacent discussion: I think that this serves nicely as
> a demonstration that lack of locals does not automatically lead to
> well-factored code (especially if you use 'the magical "seven, plus or
> minus two" words' *maximum* as the definition of "well-factored").
Removing the locals *and* removing the stack noise does not seem to
lead to a much easier algorithm (although the max. number of
lines / word is now 8). There is quite obvious similarity between
the words in the local-less variant, but I don't see how to exploit it.
A state machine and goto's may be a better idea in this case.
-marcel
-- -------------------------
ANEW -regexpr
0 [IF]
DEFER (matchhere) ( $1 $2 -- bool )
: (match*) LOCALS| c n2 a2 n1 a1 | ( $1 $2 char -- bool )
BEGIN
a1 n1 a2 n2 (matchhere) IF TRUE EXIT ENDIF
n1 0= IF FALSE EXIT ENDIF
a1 c@ '.' = a1 c@ c = OR 0= IF FALSE EXIT ENDIF
a1 n1 1 /string TO n1 TO a1
AGAIN ;
: (match?) LOCALS| c n2 a2 n1 a1 | ( $1 $2 char -- bool )
a1 n1 a2 n2 (matchhere) IF TRUE EXIT ENDIF
n1 0= IF FALSE EXIT ENDIF
a1 c@ '.' = a1 c@ c = OR 0= IF FALSE EXIT ENDIF
a1 n1 1 /string a2 n2 (matchhere) ;
:NONAME 0 LOCALS| c n2 a2 n1 a1 | ( $1 $2 -- bool )
n2 0= IF TRUE EXIT ENDIF
a2 c@ TO c a2 char+ c@ '*' = \ if not, does it equal a '*'
IF a2 n2 2 /string TO n2 TO a2
a1 n1 a2 n2 c (match*) EXIT
ENDIF \ otherwise, does it equal a '?'
a2 char+ c@ '?' =
IF a2 n2 2 /string TO n2 TO a2
a1 n1 a2 n2 c (match?) EXIT
ENDIF \ otherwise, does it equal a '$'
c '$' = n2 1 = AND IF n1 0= EXIT ENDIF \ and is it the last character?
n1 0= IF FALSE EXIT ENDIF
c '.' = c a1 c@ = OR 0= IF FALSE EXIT ENDIF
a1 n1 1 /string a2 n2 1 /string (matchhere) ; IS (matchhere)
: match LOCALS| n2 a2 n1 a1 | ( $1 $2 -- bool )
n2 IF a2 c@ '^' = IF a1 n1 a2 n2 1 /string (matchhere) ENDIF
ELSE BEGIN
a1 n1 a2 n2 (matchhere) IF TRUE EXIT ENDIF
n1 0= IF FALSE EXIT ENDIF
a1 n1 1 /string TO n1 TO a1
AGAIN
ENDIF ;
[ELSE]
CREATE $1$2 #256 5 * CELLS ALLOT $1$2 VALUE ptr
: +level ( char a1 n1 a2 n2 -- ) 5 CELLS +TO ptr ptr 2 CELL[] d! ptr d! ptr 4 CELL[] ! ;
: -level ( -- ) -5 CELLS +TO ptr ;
: reset ( $1 $2 -- ) $1$2 TO ptr -level 2>r 2>r 0 2r> 2r> +level ;
: a1 ( -- addr ) ptr @ ; : n1 ( -- n ) ptr CELL+ @ ; : $1 ( -- a1 n1 ) ptr d@ ;
: a2 ( -- addr ) ptr 2 CELL[] @ ; : n2 ( -- n ) ptr 3 CELL[] @ ; : $2 ( -- a2 n2 ) ptr 2 CELL[] d@ ;
: c ( -- char ) ptr 4 CELL[] @ ;
: shorten$1 ( u -- ) $1 ROT /string ptr d! ;
: shorten$2 ( u -- ) $2 ROT /string ptr 2 CELL[] d! ;
DEFER (matchhere) ( -- bool )
: (match*) ( char -- bool )
$1 $2 +level
BEGIN
(matchhere) IF -level TRUE EXIT ENDIF
n1 0= IF -level FALSE EXIT ENDIF
a1 c@ '.' = a1 c@ c = OR 0= IF -level FALSE EXIT ENDIF
1 shorten$1
AGAIN ;
: (match?) ( char -- bool )
$1 $2 +level
(matchhere) IF -level TRUE EXIT ENDIF
n1 0= IF -level FALSE EXIT ENDIF
a1 c@ '.' = a1 c@ c = OR 0= IF -level FALSE EXIT ENDIF
1 shorten$1 (matchhere) -level ;
:NONAME ( -- bool )
a2 c@ $1 $2 +level
n2 0= IF -level TRUE EXIT ENDIF
a2 char+ c@ '*' = IF 2 shorten$2 c (match*) -level EXIT ENDIF
a2 char+ c@ '?' = IF 2 shorten$2 c (match?) -level EXIT ENDIF
c '$' = n2 1 = AND IF -level n1 0= EXIT ENDIF
n1 0= IF -level FALSE EXIT ENDIF
c '.' = c a1 c@ = OR 0= IF -level FALSE EXIT ENDIF
1 shorten$1 1 shorten$2 (matchhere) -level ; IS (matchhere)
: match ( $1 $2 -- bool )
reset
n2 IF a2 c@ '^' = IF 1 shorten$2 (matchhere) ENDIF
ELSE BEGIN
(matchhere) IF -level TRUE EXIT ENDIF
n1 0= IF -level FALSE EXIT ENDIF
1 shorten$1
AGAIN
ENDIF ;
[THEN]
cr s" 0,9" s" ^0,?9$" match . .( -1)
cr s" 0:9" s" ^0,?9$" match . .( 0)
cr s" 09" s" ^0,?9$" match . .( -1)
cr s" 009" s" ^0,?9$" match . .( 0)
cr s" 0,,9" s" ^0,?9$" match . .( 0)
cr s" 0,,9" s" ^0,*9$" match . .( -1)
cr s" 0,,9" s" ^0*9$" match . .( 0)
cr
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-13 18:15 +0200
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-13 18:18 +0200
Re: Tiny regular expressions code Pablo Hugo Reda <pabloreda@gmail.com> - 2014-10-13 09:32 -0700
Re: Tiny regular expressions code Howerd <howerdo@yahoo.co.uk> - 2014-10-13 11:18 -0700
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-13 20:36 +0200
Re: Tiny regular expressions code Howerd <howerdo@yahoo.co.uk> - 2014-10-13 11:53 -0700
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-13 22:14 +0200
Re: Tiny regular expressions code mhx@iae.nl (Marcel Hendrix) - 2014-10-13 22:59 +0200
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-13 23:11 +0200
Re: Tiny regular expressions code albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-10-14 09:22 +0000
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-14 17:43 +0200
Re: Tiny regular expressions code albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-10-14 16:27 +0000
Re: Tiny regular expressions code albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-10-15 09:48 +0000
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-15 17:38 +0200
Re: Tiny regular expressions code albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-10-16 09:20 +0000
Re: Tiny regular expressions code josv@planet.nl - 2014-10-17 05:03 -0700
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-17 17:48 +0200
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-17 19:20 +0200
Re: Tiny regular expressions code anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-18 15:43 +0000
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-18 18:53 +0200
Re: Tiny regular expressions code hughaguilar96@yahoo.com - 2014-10-18 19:19 -0700
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-19 14:52 +0200
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-19 15:25 +0200
Re: Tiny regular expressions code Bernd Paysan <bernd.paysan@gmx.de> - 2014-10-19 21:14 +0200
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-20 00:17 +0200
Re: Tiny regular expressions code albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-10-19 17:10 +0000
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-19 19:59 +0200
Re: Tiny regular expressions code Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-19 09:31 -0500
The magical number seven (was: Tiny regular expressions code) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-20 09:10 +0000
Re: The magical number seven (was: Tiny regular expressions code) m.a.m.hendrix@tue.nl - 2014-10-20 03:13 -0700
Re: The magical number seven (was: Tiny regular expressions code) mhx@iae.nl (Marcel Hendrix) - 2014-10-20 20:19 +0200
Re: The magical number seven Lars Brinkhoff <lars.spam@nocrew.org> - 2014-10-21 08:56 +0200
Re: The magical number seven Bernd Paysan <bernd.paysan@gmx.de> - 2014-10-21 11:44 +0200
Re: The magical number seven (was: Tiny regular expressions code) Just Ian <superian@gmail.com> - 2014-10-21 07:06 -0700
Re: The magical number seven (was: Tiny regular expressions code) mhx@iae.nl (Marcel Hendrix) - 2014-10-21 20:11 +0200
Re: The magical number seven Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-26 04:27 -0500
Re: The magical number seven (was: Tiny regular expressions code) Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-20 17:28 +0200
Re: The magical number seven (was: Tiny regular expressions code) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-20 16:25 +0000
Re: The magical number seven (was: Tiny regular expressions code) "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-10-20 16:36 -0400
Re: The magical number seven "Elizabeth D. Rather" <erather@forth.com> - 2014-10-20 12:19 -1000
Re: The magical number seven "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-10-21 01:47 -0400
Re: The magical number seven "Elizabeth D. Rather" <erather@forth.com> - 2014-10-20 20:31 -1000
Re: The magical number seven albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-10-21 09:06 +0000
Re: The magical number seven "Elizabeth D. Rather" <erather@forth.com> - 2014-10-21 08:47 -1000
Re: The magical number seven "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-10-22 02:21 -0400
Re: The magical number seven (was: Tiny regular expressions code) Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-21 10:29 +0200
Re: The magical number seven (was: Tiny regular expressions code) Bernd Paysan <bernd.paysan@gmx.de> - 2014-10-21 11:34 +0200
Re: The magical number seven Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-26 04:41 -0500
Re: The magical number seven Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-26 12:07 +0100
Re: The magical number seven Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-26 13:58 -0500
Re: The magical number seven (was: Tiny regular expressions code) hughaguilar96@yahoo.com - 2014-10-20 16:34 -0700
Re: The magical number seven Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-26 04:33 -0500
Re: The magical number seven Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-26 12:12 +0100
Re: The magical number seven Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-26 04:24 -0500
Re: Tiny regular expressions code mhx@iae.nl (Marcel Hendrix) - 2014-10-19 17:10 +0200
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-18 16:02 +0200
Re: Tiny regular expressions code josv@planet.nl - 2014-10-18 07:50 -0700
Re: Tiny regular expressions code Hans Bezemer <the.beez.speaks@gmail.com> - 2014-10-18 17:27 +0200
Re: Tiny regular expressions code the_gavino_himself <visphatesjava@gmail.com> - 2014-11-08 19:24 -0800
Re: Tiny regular expressions code the_gavino_himself <visphatesjava@gmail.com> - 2014-11-08 20:42 -0800
csiph-web