view src/ploki/examples/ret.pk @ 12518:2d8fe55c6e65 draft default tip

<int-e> learn The password of the month is release incident pilot.
author HackEso <hackeso@esolangs.org>
date Sun, 03 Nov 2024 00:31:02 +0000
parents ac0403686959
children
line wrap: on
line source

#!../ploki
REM regular expression tester
NEXT main:

FOR trymatch
	LEET txt \@ . 0
	LEET pat \@ . 1
	LEET ms txt ~ pat
	IF ms
		LET me \_
		"matched at pos \Vms [\V(txt ] ms)\033[1m\V(txt [ ms ] (me - ms))\033[m\V(txt [ me ] 255)]
		IF @DEF-P \0
			WUNT "\\0<\V@-0, \V@+0>: \V\0
		FI
		IF @DEF-P \1
			WUNT "\\1<\V@-1, \V@+1>: \V\1
		FI
		IF @DEF-P \2
			WUNT "\\2<\V@-2, \V@+2>: \V\2
		FI
	ELSE
		WUNT "no match
	FI
KTHX

FOR main:
	IF \ARG < 2
		WUNT "Usage: \V\ARG:0 PATTERN [FILE]
		END @NEG1
	FI
	LET pat \ARG:1
	IF \ARG > 2
		LET fh @LAPERS \ARG:2
		IF @NOT fh
			WUNT \AUSG \ARG:0_": "_\ARG:2_": "_\!_"
			END @NEG1
		FI
		FOR buf LET s fh . 4096
		IF s ; ""
#!			"appending [\V@TYPE OFs:\V@LENGTHs]\Vs to \Vtxt
			LET txt _= s
			NEXT buf
		FI
#!		"matching /\Vpat/ against \Vtxt
		trymatch #<txt pat#>
	ELSE
		"pattern: /\Vpat/
		FOR line LET line @LEGS \EING
		IF line ; ""
			IF line [ -1 : "
				LET line line ] -1
			FI
			trymatch #<line pat#>
			NEXT line
		FI
	FI