aboutsummaryrefslogtreecommitdiff
path: root/challenge-099/duncan-c-white/opt2-separateRunX/mkTypes
blob: 1831538577630f6a90c58907c7a93c6b6a942261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/perl

use strict;
use warnings;
use lib qw(/homes/dcw/lib/perl5/DCW);
use Datadec;

my $posexpr = q(
PosExpr = I( int n )	"$1"
	or N( string name ) "$1"
	or NO( string name, char op, int n ) "$1$2$3"
);

my $extra = q!
use Data::Dumper;

my $debug = 0;
sub setdebug { my($d) = @_; $debug = $d; }

#
# my($pe,$leftover) = parse($input);
#	Parse $input, a string starting with a position expression,
#	delivering the position expression in $pe and the leftover
#	input in $leftover.  Die screaming if no well formed pos expr
#	can be found at the start of $input.
#	A position expression is either \d+ or name or name '+'|'-' \d+
#	The internal form of a posexpr is ['I', int const] or ['N', name]
#	or ['NO', name, +-, offset]
#
sub parse ($)
{
	my( $input ) = @_;
	if( $input =~ s/^(\d+)\s*// )
	{
		return ( PosExpr->I($1), $input );
	}
	$input =~ s/^(\w+)\s*// ||
		die "bad input in pos expr $input, name expected\n";
	my $name = $1;
	if( $input =~ s/^([\+-])\s*(\d+)\s*// )
	{
		if( $2 == 0 )
		{
			return ( PosExpr->N($name), $input );
		} else
		{
			return ( PosExpr->NO($name,$1,$2), $input );
		}
	}
	return ( PosExpr->N($name), $input );
}

#
# my $pe = simpleparse($str);
#	Parse a position expression out of $str.  Fail if there's
#	anything left over.
#
sub simpleparse ($)
{
	my( $str ) = @_;
	my($pe,$leftover) = parse($str);
	die "PosExpr->simpleparse($str): left over $leftover after pe $pe\n"
		if $leftover;
	return $pe;
}


#
# my $newpe = $pe->add($n);
#	Arithmetic on PosExprs: add $n (an integer)
#	to $pe, giving $newpe.
#
sub add
{
	my( $self, $n ) = @_;
	return $self if $n==0;
	if( $self->kind eq 'I' )
	{
		my $x = $self->I;
		return PosExpr->I( $x+$n );
	} elsif( $self->kind eq "N" )
	{
		my $name = $self->N;
		my $op = $n>0?'+':'-';
		$n = abs($n);
		return PosExpr->NO( $name, $op, $n );
	} else
	{
		# .. NO( string name, char op, int n )
		my( $name, $op, $x ) = $self->NO;
		$x = -$x if $op eq '-';
		$op = '+';
		$x += $n;
		return PosExpr->N( $name ) if $x==0;
		$op = '-' if $x<0;
		$x = abs($x);
		return PosExpr->NO( $name, $op, $x );
	}
}


#
# my $pos = $pe->eval($poshash);
#	Evaluate the given PosExpr $pe, using %$poshash
#	for name lookup.  Returns the actual position (a number).
#
sub eval ($$)
{
	my( $self, $poshash ) = @_;
	if( $self->kind eq 'I' )
	{
		return $self->I;
	} elsif( $self->kind eq 'N' )
	{
		my $name = $self->N;
		die "error in PosExpr::eval($self): ".
		    "no such name $name in poshash ". Dumper($poshash)
			unless exists $poshash->{$name};
		return $poshash->{$name};
	} else    # if( $self->kind eq 'NO' )
	{
		my ($name, $op, $n) = $self->NO;
		die "error in PosExpr::eval($self): ".
		    "no such name $name in poshash ". Dumper($poshash)
			unless exists $poshash->{$name};
		die "error in PosExpr::eval($self): bad op $op\n" unless
			$op eq '+' || $op eq '-';
		$n = - $n if $op eq '-';
		return $poshash->{$name} + $n;
	}
}



!;

open( my $fh, '>', "PosExpr.pm" ) || die;
say $fh gen_datatype( $posexpr, $extra );

close($fh);

my $api = q(
API = A( string lit, posexpr at, string name ) 	"A'$1' $2->$3"
    or M( string lit, posexpr atorafter, string name )	"M'$1' $2->$3"
    or T( posexpr pe1, string op, posexpr pe2 )		"T$1$2$3"
    or C( posexpr pe1, posexpr pe2)			"C $1 $2"
);

$extra = q%

my $debug = 0;
sub setdebug { my($d) = @_; $debug = $d; }

#
# my @api = parse( $input );
#	Parse $input, a string containing a comma-separated sequence
#	of Abstract Pattern Instructions, return the list of apis.
#	Die screaming if parsing fails.
#	For example, the pattern a*e is represented by the
#	following api string:
#		A'a' 0->a,A'e' slen-1->e,Te>a-1,C a+1 e-1
#	(where slen is a runtime variable representing the length of
#	the target string these APIs would eventually operate on).
#
#	The individual api forms are:
#		M'str' posexpr->name
#		A'str' posexpr->name
#		Tposexpr ('>'|'='|'>=') posexpr
#		C posexpr [posexpr]
#	where posexpr = \d+ or name or name '+'|'-' \d+ (see module PosExpr)
#
sub parse ($)
{
	my( $input ) = @_;
	my @result;
	while( $input )
	{
		# M'str' posexpr->name
		if( $input =~ s/^M\s*// )
		{
			$input =~ s/^'([^']+)'\s+// ||
				die "bad input $input in M..\n";
			my $str = $1;
			(my $pe,$input) = PosExpr::parse($input);
			die "bad input $input in M$str $pe...\n"
				unless $input =~ s/^\s*->\s*(\w+)//;
			my $pname = $1;
			my $api = API->M($str, $pe, $pname);
			say "debug: parsed api $api, rest input $input"
				if $debug;
			push @result, $api;
		}
		# A'str' posexpr->name
		elsif( $input =~ s/^A\s*// )
		{
			$input =~ s/^'([^']+)'\s+// ||
				die "bad input $input in A..\n";
			my $str = $1;
			(my $pe,$input) = PosExpr::parse($input);
			die "bad input $input in A$str $pe...\n"
				unless $input =~ s/^\s*->\s*(\w+)//;
			my $pname = $1;
			my $api = API->A($str, $pe, $pname);
			say "debug: parsed api $api, rest input $input"
				if $debug;
			push @result, $api;
		}
		# Tposexpr ('>'|'='|'>=') posexpr
		elsif( $input =~ s/^T\s*// )
		{
			(my $pe,$input) = PosExpr::parse($input);
			$input =~ s/^(>=|>|=)// ||
				die "bad input $input in T$pe, ".
				    "'>','=' or '>=' expected\n";
			my $op = $1;
			(my $pe2,$input) = PosExpr::parse($input);
			my $api = API->T($pe, $op, $pe2);
			say "debug: parsed api $api, rest input $input"
				if $debug;
			push @result, $api;
		}
		# C posexpr [posexpr]
		elsif( $input =~ s/^C\s*// )
		{
			(my $pe,$input) = PosExpr::parse($input);
			$input =~ s/^\s*//;

			# second posexpr is optional:
			# present if next ch is not ','
			my $pe2 = $pe;
			if( $input ne '' && $input !~ /^,/ )
			{
				($pe2,$input) = PosExpr::parse($input);
			}
			my $api = API->C($pe, $pe2);
			say "debug: parsed api $api, rest input $input"
				if $debug;
			push @result, $api;
		}
		$input =~ s/^\s*,\s*//;
	}
	die "bad input $input, non empty but not M|T|C..\n" if $input;
	return @result;
}
%;


open( $fh, '>', "API.pm" ) || die;
say $fh gen_datatype( $api, $extra );
close($fh);