-
Notifications
You must be signed in to change notification settings - Fork 5
/
ptypes.py
819 lines (682 loc) · 26.6 KB
/
ptypes.py
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
"""Classes for Pip data types."""
import re
import itertools
import sys
import types
import tokens
import parsing
zeroRgx = re.compile(r"^(0+(\.0*)?|0*\.0+)$")
exp = r"(?:e[+-]\d+)"
pyFloatRgx = re.compile(rf"-?(\d+\.\d*{exp}?|\d+{exp}|\.\d+{exp}?)")
pipFloatRgx = re.compile(r"-?\d+\.\d+")
intRgx = re.compile(r"-?\d+")
class PipType:
"""Base class for all Pip types."""
def __hash__(self):
return hash(repr(self))
class PipIterable(PipType):
"""Base class for all iterable Pip types."""
pass
class Scalar(PipIterable):
"""Represents a string or number."""
def __init__(self, value=""):
# Store the value as a Python string
if isinstance(value, bool):
# Convert to an integer first
value = int(value)
elif isinstance(value, float) and str(value).endswith(".0"):
# Convert float with no fractional part to integer
value = int(value)
self._value = str(value)
def copy(self):
return Scalar(self._value)
def isEmpty(self):
return self._value == ""
def isFinite(self):
return True
def __str__(self):
return self._value
def __repr__(self):
m = pipFloatRgx.match(self._value) or intRgx.match(self._value)
if m and m.end() == len(self._value):
# Scalars that match the format of Pip numeric literals
# can be displayed without quotes
return self._value
else:
# Other Scalars, even those that can evaluate to a number,
# must have quotes
if '"' in self._value:
# Use escaped-string format
return r'\"' + self._value.replace("\\", r"\\") + r'\"'
else:
# Use normal string format
return f'"{self._value}"'
def __int__(self):
if m := intRgx.match(self._value.lstrip()):
return int(m.group())
else:
return 0
def __bool__(self):
"""A Scalar is false iff it is empty string or some form of 0."""
return not self.isEmpty() and not zeroRgx.match(self._value)
def __eq__(self, rhs):
return type(self) is type(rhs) and self._value == rhs._value
__hash__ = PipIterable.__hash__
def __len__(self):
return len(self._value)
def toNumber(self):
"""Convert to a Python float or int for math purposes."""
if m := pyFloatRgx.match(self._value.lstrip()):
return float(m.group())
elif m := intRgx.match(self._value.lstrip()):
return int(m.group())
else:
# If it doesn't match a float or an int, its numeric value is 0
return 0
def __contains__(self, item):
if isinstance(item, (str, Scalar)):
return str(item) in self._value
else:
return False
def __getitem__(self, index):
if isinstance(index, List):
return List(self[i] for i in index)
elif isinstance(index, Scalar):
index = int(index)
elif isinstance(index, Range):
index = index.toSlice()
if isinstance(index, int):
if self._value == "":
raise IndexError("Cannot index into empty string.")
else:
index %= len(self._value)
return Scalar(self._value[index])
elif isinstance(index, slice):
if self._value == "":
# Slicing the empty string gives empty string
return self
length = len(self._value)
repString = self._value
start = index.start if index.start is not None else 0
stop = index.stop if index.stop is not None else length
if start < 0:
start += length
if stop < 0:
stop += length
if start >= stop:
return Scalar("")
while start < 0:
repString = self._value + repString
start += length
stop += length
while stop > len(repString):
repString += self._value
return Scalar(repString[start:stop])
else:
raise TypeError(f"Cannot use {type(index)} to index Scalar")
def __setitem__(self, index, item):
# Behold! Mutable strings!
if isinstance(index, int):
index %= len(self._value)
value = list(self._value)
value.__setitem__(index, str(item))
self._value = ''.join(value)
def __iter__(self):
for char in self._value:
yield Scalar(char)
def count(self, substring):
if isinstance(substring, Scalar):
return self._value.count(substring._value)
else:
return nil
def index(self, searchItem, startIndex=0):
if not isinstance(searchItem, Scalar):
raise TypeError("Scalar.index requires Scalar argument, "
f"not {type(searchItem)}")
else:
try:
return self._value.index(searchItem._value, startIndex)
except ValueError:
raise ValueError(f"{searchItem} is not in Scalar at index "
f">= {startIndex}")
class Pattern(PipType):
"""Represents a regular expression or substitution pattern."""
def __init__(self, value="", flags=0):
if isinstance(value, Pattern):
self._raw = value._raw
# Copy flags from the other pattern, then toggle any flags
# specified by the argument
self._flags = value._flags ^ flags
else:
self._raw = str(value)
self._flags = flags
self._compiled = None
self._separator = None
def asRegex(self):
if not self._compiled:
# Add an extra <all> capture group around the whole thing
pyRegex = f"(?P<all>{self._raw})"
# Increment the numbers of all back-references
pyRegex = re.sub(r"(?<!\\)((?:\\{2})*)\\([1-9]\d?)",
lambda m: (m.group(1)
+ "\\"
+ str(int(m.group(2))+1)),
pyRegex)
self._compiled = re.compile(pyRegex, self._flags)
return self._compiled
def asSeparator(self):
if not self._separator:
self._separator = re.compile(self._raw, self._flags)
return self._separator
def asReplacement(self):
# Increment all back-references
pyReplace = re.sub(r"(?<!\\)((?:\\{2})*)\\([1-9]\d?)",
lambda m: (m.group(1)
+ "\\"
+ str(int(m.group(2))+1)),
self._raw)
# Turn unescaped ampersands into back-references to the whole match
pyReplace = re.sub(r"(?<!\\)((?:\\{2})*)&",
lambda m: m.group(1) + r"\g<all>",
pyReplace)
# Turn escaped ampersands into literal ampersands
pyReplace = pyReplace.replace("\\&", "&")
return pyReplace
def copy(self):
copy = Pattern(self)
copy._compiled = self._compiled
copy._separator = self._separator
return copy
def wrap(self):
if len(self._raw) > 1:
return Pattern(f"(?:{self._raw})", self._flags)
else:
# A length-1 regex doesn't need to be wrapped
return self
def group(self):
return Pattern(f"({self._raw})", self._flags)
def kleeneStar(self):
return Pattern(f"{self._raw}*", self._flags)
def plus(self):
return Pattern(f"{self._raw}+", self._flags)
def repeat(self, count):
if isinstance(count, int):
return Pattern(f"{self._raw}{{{count}}}", self._flags)
else:
raise TypeError(f"Cannot repeat Pattern by {type(count)}")
def concat(self, other):
return Pattern(f"{self._raw}{other._raw}",
self._flags | other._flags)
def alternate(self, other):
return Pattern(f"{self._raw}|{other._raw}",
self._flags | other._flags)
def isEmpty(self):
return self._raw == ""
def isFinite(self):
return True
def __str__(self):
return self._raw
def __repr__(self):
flags = ""
if self._flags & re.ASCII:
flags += "A"
if self._flags & re.IGNORECASE:
flags += "-"
if self._flags & re.MULTILINE:
flags += ","
if self._flags & re.DOTALL:
flags += "."
return flags + "`" + self._raw.replace("`", r"\`") + "`"
def __bool__(self):
"""A Pattern is false iff it is empty."""
return not self.isEmpty()
def __eq__(self, rhs):
return (type(rhs) == type(self)
and self._raw == rhs._raw
and self._flags == rhs._flags)
__hash__ = PipType.__hash__
def __len__(self):
return len(self._raw)
def toNumber(self):
return 0
def __iter__(self):
for char in self._raw:
yield Scalar(char)
class List(PipIterable):
"""Represents a list of objects."""
# How to format a list when outputting it
# Options are the same as the associated command-line flags:
# None: Join on empty string
# n: Join on newline
# p: Pretty-print: use the repr instead (useful for debugging)
# s: Join on space
# l: Print as multiple lines, with each line joined on empty string
# P: Print as multiple lines, with each line repr'd
# S: Print as multiple lines, with each line joined on space
outFormat = None
def __init__(self, value=None):
# TODO: make this more robust to handle infinite Range or range
if isinstance(value, (Range,
tuple,
set,
types.GeneratorType,
map,
zip,
itertools.starmap,
)):
self._value = [item for item in value]
elif isinstance(value, (List, list)):
self._value = [item.copy() for item in value]
elif isinstance(value, range):
self._value = [Scalar(item) for item in value]
elif value is None:
self._value = []
else:
self._value = []
raise TypeError(f"Cannot convert {type(value)} to List")
def copy(self):
return List(item.copy() for item in self._value)
def isEmpty(self):
return self._value == []
def isFinite(self):
return True
def __str__(self):
# How a List is formatted depends on the command-line flags
if not self.outFormat:
# Default: concatenate all items together
return "".join(str(i) for i in self._value)
elif self.outFormat == "p":
return repr(self)
elif self.outFormat == "n":
return "\n".join(str(i) for i in self._value)
elif self.outFormat == "s":
return " ".join(str(i) for i in self._value)
elif self.outFormat == "l":
# Each item in the list is a line, which in turn is joined on
# empty string
return "\n".join(i.joined("") if isinstance(i, List) else str(i)
for i in self._value)
elif self.outFormat == "P":
# Each item in the list is a line, which in turn is repr'd
return "\n".join(repr(i) for i in self._value)
elif self.outFormat == "S":
# Each item in the list is a line, which in turn is joined on
# space
return "\n".join(i.joined(" ") if isinstance(i, List) else str(i)
for i in self._value)
def joined(self, separator):
return separator.join(i.joined(separator)
if isinstance(i, List)
else str(i)
for i in self._value)
def __repr__(self):
return "[" + ";".join(repr(i) for i in self._value) + "]"
def __bool__(self):
return not self.isEmpty()
def __eq__(self, rhs):
return type(rhs) == type(self) and self._value == rhs._value
__hash__ = PipIterable.__hash__
def __len__(self):
return len(self._value)
def toNumber(self):
# Returns a Python list containing Python number types
return [item.toNumber() for item in self]
def __contains__(self, item):
return item in self._value
def __getitem__(self, index):
if isinstance(index, List):
return List(self[i] for i in index)
elif isinstance(index, Scalar):
index = int(index)
elif isinstance(index, Range):
index = index.toSlice()
if isinstance(index, int):
if self.isEmpty():
raise IndexError("Cannot index into empty list.")
else:
index %= len(self._value)
return self._value[index]
elif isinstance(index, slice):
if self._value == []:
# Slicing the empty list gives empty list
return self
length = len(self._value)
repList = self._value[:] # Shallow copy so as not to change _value
start = index.start if index.start is not None else 0
stop = index.stop if index.stop is not None else length
if start < 0:
start += length
if stop < 0:
stop += length
if start >= stop:
return List([])
while start < 0:
repList = self._value + repList
start += length
stop += length
while stop > len(repList):
repList += self._value
return List(repList[start:stop])
else:
raise TypeError(f"Cannot use {type(index)} to index List")
def __setitem__(self, index, item):
if isinstance(index, int):
index %= len(self._value)
self._value.__setitem__(index, item)
def __iter__(self):
return iter(self._value)
def count(self, item):
return self._value.count(item)
def append(self, item):
if isinstance(item, PipType):
self._value.append(item)
else:
raise TypeError(f"Cannot append {type(item)} to Pip List")
def extend(self, iterable):
if isinstance(iterable, Range) and not iterable.isFinite():
raise ValueError("Cannot extend List with infinite Range "
f"{iterable!r}")
elif (isinstance(iterable, PipType)
and not isinstance(iterable, PipIterable)):
raise TypeError("Cannot extend List with non-iterable "
f"{iterable!r}")
else:
try:
values = list(iterable)
except TypeError:
raise TypeError("Cannot extend List with non-iterable "
f"{iterable!r}")
for value in values:
if not isinstance(value, PipType):
raise TypeError("Cannot extend Pip List with iterable "
f"containing {type(value)}")
self._value.extend(values)
def index(self, searchItem, startIndex=0):
try:
return self._value.index(searchItem, startIndex)
except ValueError:
raise ValueError(f"{searchItem} is not in List at index "
f">= {startIndex}")
class Range(PipIterable):
"""Represents a range of integer values."""
# TODO: add a step parameter
def __init__(self, value, upperVal=None):
if isinstance(value, Scalar):
value = int(value)
elif isinstance(value, (range, slice)):
# Convert from Python range or slice object
upperVal = value.stop
value = value.start
elif value is nil:
value = None
if isinstance(upperVal, Scalar):
upperVal = int(upperVal)
if isinstance(value, int) or value is None:
if upperVal is None:
# A single argument is actually the upper value
self._lower = None
self._upper = value
elif isinstance(upperVal, (int, Nil)):
self._lower = value
self._upper = upperVal if upperVal is not nil else None
else:
raise TypeError(f"Cannot convert {type(upperVal)} to Range")
else:
raise TypeError(f"Cannot convert {type(value)} to Range")
def copy(self):
return Range(self._lower,
self._upper if self._upper is not None else nil)
def getLower(self):
return self._lower
def getUpper(self):
return self._upper
def isEmpty(self):
lower = self._lower or 0
return self.isFinite() and self._upper <= lower
def isFinite(self):
return self._upper is not None
def __str__(self):
if self.isFinite():
# Treat finite Ranges like Lists
return str(List(self))
else:
# Infinite Ranges have to use the repr
return repr(self)
def __repr__(self):
lower = self._lower if self._lower is not None else "()"
upper = self._upper if self._upper is not None else "()"
return f"({lower},{upper})"
def __bool__(self):
return not self.isEmpty()
def __eq__(self, rhs):
return (type(self) is type(rhs)
and self._lower == rhs._lower
and self._upper == rhs._upper)
__hash__ = PipIterable.__hash__
def __len__(self):
lower = self._lower or 0
if self.isFinite():
return max(0, self._upper - lower)
else:
# A Range with no upper bound has an infinite length
raise ValueError("Cannot take len() of infinite Range")
def toNumber(self):
# Returns a Python list containing Python numbers (probably ints)
if self.isFinite():
return [item.toNumber() for item in self]
else:
# TBD: possibly return a generator instead? Check contexts where
# this is used
return []
def __contains__(self, item):
# TBD: Should this return true only for ints, or for any number
# between lower and upper?
if isinstance(item, Scalar):
lower = self._lower or 0
if self.isFinite():
return lower <= item.toNumber() < self._upper
else:
return lower <= item.toNumber()
else:
return False
def toSlice(self):
return slice(self._lower, self._upper)
def toRange(self):
if self.isFinite():
# Treat lower value of None as 0
lower = self._lower or 0
return range(lower, self._upper)
else:
# Can't return an infinite range
raise ValueError("Cannot convert infinite Range to Python range")
def __iter__(self):
isInfinite = not self.isFinite()
if isInfinite:
# TODO: use warning mechanism instead of print()?
print("Iterating over an infinite Range", file=sys.stderr)
i = self._lower or 0
while isInfinite or i < self._upper:
yield Scalar(i)
i += 1
def __getitem__(self, index):
if isinstance(index, List):
return List(self[i] for i in index)
lower = self._lower or 0
if isinstance(index, (Scalar, int)):
# Get a single element of the Range
index = int(index)
if self.isEmpty():
raise IndexError("Cannot index into empty range.")
elif not self.isFinite() and index < 0:
# Can't count from the end of an infinite Range
# TODO: IndexError instead
return nil
if self.isFinite():
# Indices wrap around for finite Ranges
index %= len(self)
return Scalar(lower + index)
elif isinstance(index, (Range, slice)):
# Get a slice of the Range
if isinstance(index, Range):
start, stop = index._lower, index._upper
else:
start, stop = index.start, index.stop
if self.isEmpty():
# Any slice of an empty Range will be empty
return self
elif self.isFinite():
length = len(self)
start = start if start is not None else 0
stop = stop if stop is not None else length
if start >= -length and stop <= length:
# Convert to Python range, slice it, and convert back
result = range(lower, self._upper)[start:stop]
return Range(result)
else:
# One or both slice bounds are outside the size of the
# Range; convert to a List to do extended slicing
result = List(self)
return result[start:stop]
else:
if (start is not None and start < 0
or stop is not None and stop < 0):
# One of the indices is negative; can't count from the end
# of an infinite Range
# TODO: IndexError instead
return nil
if start is None:
# Keep the bottom end of the Range the same
newLower = self._lower
else:
# Move the bottom end of the Range up
newLower = lower + start
if stop is None:
# Leave the Range unbounded above
newUpper = nil
else:
# Bound the Range above
newUpper = lower + stop
return Range(newLower, newUpper)
def count(self, number):
# 1 if number is in the Range, 0 otherwise
return int(number in self)
def index(self, searchItem, startIndex=0):
if not isinstance(searchItem, Scalar):
raise TypeError("Range.index requires Scalar argument, "
f"not {type(searchItem)}")
elif searchItem in self[startIndex:]:
# __contains__ returns true for floats, but index shouldn't
if int(searchItem) == searchItem.toNumber():
index = int(searchItem) - (self._lower or 0)
return index
else:
raise ValueError("Range.index requires integer argument, "
f"not {searchItem}")
else:
raise ValueError(f"{searchItem} is not in Range at index "
f">= {startIndex}")
class Block(PipType):
"""Represents a Pip function object."""
def __init__(self, statements, returnExpr=None):
self._statements = statements
if returnExpr is None:
self._returnExpr = nil
else:
self._returnExpr = returnExpr
def copy(self):
return Block(self._statements, self._returnExpr)
def getStatements(self):
return self._statements
def getReturnExpr(self):
return self._returnExpr
def isExpr(self):
return not self._statements
def isEmpty(self):
return not self._statements and self._returnExpr is nil
def isFinite(self):
return True
def __str__(self):
return repr(self)
def __repr__(self):
statements = self._statements + [self._returnExpr]
return "{" + parsing.unparse(statements) + "}"
def __bool__(self):
return not self.isEmpty()
def __eq__(self, rhs):
return (isinstance(rhs, Block)
and self._statements == rhs._statements
and self._returnExpr == rhs._returnExpr)
__hash__ = PipType.__hash__
def toNumber(self):
return 0
class Nil(PipType):
"""Represents the nil object."""
instance = None
def __new__(cls):
# The __new__ function is used here so there's only ever one instance
# of Nil
if cls.instance is None:
cls.instance = super().__new__(cls)
return cls.instance
def copy(self):
# Not really a copy, but implemented for completeness
return self
def isEmpty(self):
return True
def isFinite(self):
return True
def __str__(self):
return ""
def __repr__(self):
return "()"
def __bool__(self):
return False
def __iter__(self):
return iter([])
def __eq__(self, rhs):
return self is rhs
__hash__ = PipType.__hash__
def toNumber(self):
return 0
def __getitem__(self, index):
return self
nil = Nil()
def toPipType(pyObj):
if isinstance(pyObj, PipType):
# The argument is already a Pip type, no need for conversion
return pyObj
elif isinstance(pyObj, (str, int, float, bool)):
return Scalar(pyObj)
elif isinstance(pyObj, (list, tuple, set, types.GeneratorType, map)):
return List(pyObj)
elif isinstance(pyObj, (range, slice)):
return Range(pyObj)
elif pyObj is None:
return nil
elif isinstance(pyObj, tokens.Token):
token = pyObj
tokenString = str(token)
if isinstance(token, tokens.Number):
# Numbers and nil need no further processing
return Scalar(tokenString)
elif isinstance(token, tokens.String):
# Strip the double-quotes off a literal string
return Scalar(tokenString[1:-1])
elif isinstance(token, tokens.Pattern):
# Strip off backticks and simplify \` inside
# `\1\\\`2\\` -> \1\\`2\\
rawPattern = tokenString[1:-1].replace("\\`", "`")
return Pattern(rawPattern)
elif isinstance(token, tokens.Char):
# Single-quoted character
return Scalar(tokenString[1])
elif isinstance(token, tokens.EscapedString):
# This will be an escaped string without any interpolations
# Strip off \" delimiters and simplify backslash escapes
return Scalar(tokenString[2:-2].replace(r"\\", "\\"))
elif isinstance(token, tokens.Nil):
return nil
else:
raise TypeError(f"Cannot convert {token!r} to Pip type")
else:
raise TypeError(f"Cannot convert {type(pyObj)} to Pip type")