/
expr_eval.py
executable file
·819 lines (646 loc) · 24.1 KB
/
expr_eval.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
819
#!/usr/bin/env python2
# Copyright 2016 Andy Chu. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
"""
expr_eval.py -- Currently used for boolean and arithmetic expressions.
"""
import stat
from _devbuild.gen.id_kind_asdl import Id
from _devbuild.gen.runtime_asdl import (
lvalue, value, value_e, value_t, scope_e,
)
from _devbuild.gen.syntax_asdl import (
arith_expr_e, lhs_expr_e, lhs_expr_t, bool_expr_e,
)
from _devbuild.gen.types_asdl import bool_arg_type_e
from asdl import const
from core.meta import BOOL_ARG_TYPES
from core import util
from core.util import e_die
from osh import state
from osh import word
import posix_ as posix
try:
import libc # for fnmatch
except ImportError:
from benchmarks import fake_libc as libc # type: ignore
def _StringToInteger(s, span_id=const.NO_INTEGER):
"""Use bash-like rules to coerce a string to an integer.
Runtime parsing enables silly stuff like $(( $(echo 1)$(echo 2) + 1 )) => 13
0xAB -- hex constant
042 -- octal constant
42 -- decimal constant
64#z -- arbitary base constant
bare word: variable
quoted word: string (not done?)
"""
if s.startswith('0x'):
try:
integer = int(s, 16)
except ValueError:
e_die('Invalid hex constant %r', s, span_id=span_id)
return integer
if s.startswith('0'):
try:
integer = int(s, 8)
except ValueError:
e_die('Invalid octal constant %r', s, span_id=span_id)
return integer
if '#' in s:
b, digits = s.split('#', 1)
try:
base = int(b)
except ValueError:
e_die('Invalid base for numeric constant %r', b, span_id=span_id)
integer = 0
n = 1
for char in digits:
if 'a' <= char and char <= 'z':
digit = ord(char) - ord('a') + 10
elif 'A' <= char and char <= 'Z':
digit = ord(char) - ord('A') + 36
elif char == '@': # horrible syntax
digit = 62
elif char == '_':
digit = 63
elif char.isdigit():
digit = int(char)
else:
e_die('Invalid digits for numeric constant %r', digits, span_id=span_id)
if digit >= base:
e_die('Digits %r out of range for base %d', digits, base, span_id=span_id)
integer += digit * n
n *= base
return integer
# Normal base 10 integer
try:
integer = int(s)
except ValueError:
e_die("Invalid integer constant %r", s, span_id=span_id)
return integer
#
# Common logic for Arith and Command/Word variants of the same expression
#
# Calls EvalLhs()
# a[$key]=$val # osh/cmd_exec.py:814 (command_e.Assignment)
# (( a[key] = val )) # osh/expr_eval.py:326 (_EvalLhsArith)
#
# Calls EvalLhsAndLookup():
# a[$key]+=$val # osh/cmd_exec.py:795 (assign_op_e.PlusEqual)
# (( a[key] += val )) # osh/expr_eval.py:308 (_EvalLhsAndLookupArith)
#
# Uses Python's [] operator
# val=${a[$key]} # osh/word_eval.py:633 (bracket_op_e.ArrayIndex)
# (( val = a[key] )) # osh/expr_eval.py:490 (Id.Arith_LBracket)
#
def _LookupVar(name, mem, exec_opts):
val = mem.GetVar(name)
# By default, undefined variables are the ZERO value. TODO: Respect
# nounset and raise an exception.
if val.tag == value_e.Undef and exec_opts.nounset:
e_die('Undefined variable %r', name) # TODO: need token
return val
def EvalLhs(node, arith_ev, mem, spid, lookup_mode):
"""lhs_expr -> lvalue.
Used for a=b and a[x]=b
"""
assert isinstance(node, lhs_expr_t), node
if node.tag == lhs_expr_e.LhsName: # a=x
lval = lvalue.LhsName(node.name)
lval.spids.append(spid)
return lval
if node.tag == lhs_expr_e.LhsIndexedName: # a[1+2]=x
# The index of StrArray needs to be coerced to int, but not the index of
# an AssocArray.
int_coerce = not mem.IsAssocArray(node.name, lookup_mode)
index = arith_ev.Eval(node.index, int_coerce=int_coerce)
lval = lvalue.LhsIndexedName(node.name, index)
lval.spids.append(node.spids[0]) # copy left-most token over
return lval
raise AssertionError(node.tag)
def _EvalLhsArith(node, mem, arith_ev):
"""lhs_expr -> lvalue.
Very similar to EvalLhs above in core/cmd_exec.
"""
assert isinstance(node, lhs_expr_t), node
if node.tag == lhs_expr_e.LhsName: # (( i = 42 ))
lval = lvalue.LhsName(node.name)
# TODO: location info. Use the = token?
#lval.spids.append(spid)
return lval
if node.tag == lhs_expr_e.LhsIndexedName: # (( a[42] = 42 ))
# The index of StrArray needs to be coerced to int, but not the index of
# an AssocArray.
int_coerce = not mem.IsAssocArray(node.name, scope_e.Dynamic)
index = arith_ev.Eval(node.index, int_coerce=int_coerce)
lval = lvalue.LhsIndexedName(node.name, index)
# TODO: location info. Use the = token?
#lval.spids.append(node.spids[0])
return lval
raise AssertionError(node.tag)
def EvalLhsAndLookup(node, arith_ev, mem, exec_opts,
lookup_mode=scope_e.Dynamic):
"""Evaluate the operand for i++, a[0]++, i+=2, a[0]+=2 as an R-value.
Also used by the Executor for s+='x' and a[42]+='x'.
Args:
node: syntax_asdl.lhs_expr
Returns:
value_t, lvalue_t
"""
#log('lhs_expr NODE %s', node)
assert isinstance(node, lhs_expr_t), node
if node.tag == lhs_expr_e.LhsName: # a = b
# Problem: It can't be an array?
# a=(1 2)
# (( a++ ))
lval = lvalue.LhsName(node.name)
val = _LookupVar(node.name, mem, exec_opts)
elif node.tag == lhs_expr_e.LhsIndexedName: # a[1] = b
# See tdop.IsIndexable for valid values:
# - ArithVarRef (not LhsName): a[1]
# - FuncCall: f(x), 1
# - ArithBinary LBracket: f[1][1] -- no semantics for this?
int_coerce = not mem.IsAssocArray(node.name, lookup_mode)
index = arith_ev.Eval(node.index, int_coerce=int_coerce)
lval = lvalue.LhsIndexedName(node.name, index)
val = mem.GetVar(node.name)
if val.tag == value_e.Str:
e_die("Can't assign to characters of string %r", node.name)
elif val.tag == value_e.Undef:
# (( a[undefined_var]=1 ))
val = value.Str('')
elif val.tag == value_e.StrArray:
#log('ARRAY %s -> %s, index %d', node.name, array, index)
array = val.strs
# NOTE: Similar logic in RHS Arith_LBracket
try:
s = array[index]
except IndexError:
s = None
if s is None:
val = value.Str('') # NOTE: Other logic is value.Undef()? 0?
else:
assert isinstance(s, str), s
val = value.Str(s)
elif val.tag == value_e.AssocArray: # declare -A a; a['x']+=1
# TODO: Also need IsAssocArray() check?
index = arith_ev.Eval(node.index, int_coerce=False)
lval = lvalue.LhsIndexedName(node.name, index)
s = val.d.get(index)
if s is None:
val = value.Str('')
else:
val = value.Str(s)
else:
raise AssertionError(val.tag)
else:
raise AssertionError(node.tag)
return val, lval
class _ExprEvaluator(object):
"""Shared between arith and bool evaluators.
They both:
1. Convert strings to integers, respecting shopt -s strict_arith.
2. Look up variables and evaluate words.
"""
def __init__(self, mem, exec_opts, word_ev, errfmt):
self.mem = mem
self.exec_opts = exec_opts
self.word_ev = word_ev # type = word_eval.WordEvaluator
self.errfmt = errfmt
def _StringToIntegerOrError(self, s, blame_word=None,
span_id=const.NO_INTEGER):
"""Used by both [[ $x -gt 3 ]] and (( $x ))."""
if span_id == const.NO_INTEGER and blame_word:
span_id = word.LeftMostSpanForWord(blame_word)
try:
i = _StringToInteger(s, span_id=span_id)
except util.FatalRuntimeError as e:
if self.exec_opts.strict_arith:
raise
else:
self.errfmt.PrettyPrintError(e, prefix='warning: ')
i = 0
return i
class ArithEvaluator(_ExprEvaluator):
def _ValToArith(self, val, span_id, int_coerce=True):
"""Convert value_t to a Python int or list of strings."""
assert isinstance(val, value_t), '%r %r' % (val, type(val))
if int_coerce:
if val.tag == value_e.Undef: # 'nounset' already handled before got here
# Happens upon a[undefined]=42, which unfortunately turns into a[0]=42.
#log('blame_word %s arena %s', blame_word, self.arena)
e_die('Undefined value in arithmetic context '
'(0 if shopt -u strict-arith)', span_id=span_id)
return 0
if val.tag == value_e.Str:
return _StringToInteger(val.s, span_id=span_id) # calls e_die
if val.tag == value_e.StrArray: # array is valid on RHS, but not on left
return val.strs
if val.tag == value_e.AssocArray:
return val.d
raise AssertionError(val)
if val.tag == value_e.Undef: # 'nounset' already handled before got here
return '' # I think nounset is handled elsewhere
if val.tag == value_e.Str:
return val.s
if val.tag == value_e.StrArray: # array is valid on RHS, but not on left
return val.strs
if val.tag == value_e.AssocArray:
return val.d
def _ValToArithOrError(self, val, int_coerce=True, blame_word=None,
span_id=const.NO_INTEGER):
if span_id == const.NO_INTEGER and blame_word:
span_id = word.LeftMostSpanForWord(blame_word)
#log('_ValToArithOrError span=%s blame=%s', span_id, blame_word)
try:
i = self._ValToArith(val, span_id, int_coerce=int_coerce)
except util.FatalRuntimeError as e:
if self.exec_opts.strict_arith:
raise
else:
i = 0
span_id = word.SpanIdFromError(e)
self.errfmt.PrettyPrintError(e, prefix='warning: ')
return i
def _EvalLhsAndLookupArith(self, node):
"""
Args:
node: lhs_expr
Returns:
int or list of strings, lvalue_t
"""
val, lval = EvalLhsAndLookup(node, self, self.mem, self.exec_opts)
if val.tag == value_e.StrArray:
e_die("Can't use assignment like ++ or += on arrays")
# TODO: attribute a span ID here. There are a few cases, like UnaryAssign
# and BinaryAssign.
span_id = word.SpanForLhsExpr(node)
i = self._ValToArithOrError(val, span_id=span_id)
return i, lval
def _Store(self, lval, new_int):
val = value.Str(str(new_int))
self.mem.SetVar(lval, val, (), scope_e.Dynamic)
def Eval(self, node, int_coerce=True):
"""
Args:
node: osh_ast.arith_expr
Returns:
int or list of strings (or dict?)
"""
# OSH semantics: Variable NAMES cannot be formed dynamically; but INTEGERS
# can. ${foo:-3}4 is OK. $? will be a compound word too, so we don't have
# to handle that as a special case.
if node.tag == arith_expr_e.ArithVarRef: # $(( x )) (can be array)
tok = node.token
val = _LookupVar(tok.val, self.mem, self.exec_opts)
return self._ValToArithOrError(val, int_coerce=int_coerce,
span_id=tok.span_id)
if node.tag == arith_expr_e.ArithWord: # $(( $x )) $(( ${x}${y} )), etc.
val = self.word_ev.EvalWordToString(node.w)
return self._ValToArithOrError(val, int_coerce=int_coerce, blame_word=node.w)
if node.tag == arith_expr_e.UnaryAssign: # a++
op_id = node.op_id
old_int, lval = self._EvalLhsAndLookupArith(node.child)
if op_id == Id.Node_PostDPlus: # post-increment
new_int = old_int + 1
ret = old_int
elif op_id == Id.Node_PostDMinus: # post-decrement
new_int = old_int - 1
ret = old_int
elif op_id == Id.Arith_DPlus: # pre-increment
new_int = old_int + 1
ret = new_int
elif op_id == Id.Arith_DMinus: # pre-decrement
new_int = old_int - 1
ret = new_int
else:
raise AssertionError(op_id)
#log('old %d new %d ret %d', old_int, new_int, ret)
self._Store(lval, new_int)
return ret
if node.tag == arith_expr_e.BinaryAssign: # a=1, a+=5, a[1]+=5
op_id = node.op_id
if op_id == Id.Arith_Equal:
rhs = self.Eval(node.right)
lval = _EvalLhsArith(node.left, self.mem, self)
self._Store(lval, rhs)
return rhs
old_int, lval = self._EvalLhsAndLookupArith(node.left)
rhs = self.Eval(node.right)
if op_id == Id.Arith_PlusEqual:
new_int = old_int + rhs
elif op_id == Id.Arith_MinusEqual:
new_int = old_int - rhs
elif op_id == Id.Arith_StarEqual:
new_int = old_int * rhs
elif op_id == Id.Arith_SlashEqual:
try:
new_int = old_int / rhs
except ZeroDivisionError:
# TODO: location
e_die('Divide by zero')
elif op_id == Id.Arith_PercentEqual:
new_int = old_int % rhs
elif op_id == Id.Arith_DGreatEqual:
new_int = old_int >> rhs
elif op_id == Id.Arith_DLessEqual:
new_int = old_int << rhs
elif op_id == Id.Arith_AmpEqual:
new_int = old_int & rhs
elif op_id == Id.Arith_PipeEqual:
new_int = old_int | rhs
elif op_id == Id.Arith_CaretEqual:
new_int = old_int ^ rhs
else:
raise AssertionError(op_id) # shouldn't get here
self._Store(lval, new_int)
return new_int
if node.tag == arith_expr_e.ArithUnary:
op_id = node.op_id
if op_id == Id.Node_UnaryPlus:
return self.Eval(node.child)
if op_id == Id.Node_UnaryMinus:
return -self.Eval(node.child)
if op_id == Id.Arith_Bang: # logical negation
return int(not self.Eval(node.child))
if op_id == Id.Arith_Tilde: # bitwise complement
return ~self.Eval(node.child)
raise AssertionError(op_id)
if node.tag == arith_expr_e.ArithBinary:
op_id = node.op_id
lhs = self.Eval(node.left)
# Short-circuit evaluation for || and &&.
if op_id == Id.Arith_DPipe:
if lhs == 0:
rhs = self.Eval(node.right)
return int(rhs != 0)
else:
return 1 # true
if op_id == Id.Arith_DAmp:
if lhs == 0:
return 0 # false
else:
rhs = self.Eval(node.right)
return int(rhs != 0)
rhs = self.Eval(node.right) # eager evaluation for the rest
# TODO: Validate that in lhs + rhs, both are STRINGS and not [] or {}.
if op_id == Id.Arith_LBracket:
# StrArray or AssocArray
if not isinstance(lhs, (list, dict)):
# TODO: Add error context
e_die('Expected array in index expression, got %s', lhs)
try:
item = lhs[rhs]
except KeyError:
if self.exec_opts.nounset:
e_die('Invalid key %r' % rhs)
else:
return 0 # If not fatal, return 0
except IndexError:
if self.exec_opts.nounset:
e_die('Index out of bounds')
else:
return 0 # If not fatal, return 0
assert isinstance(item, str), item
return self._StringToIntegerOrError(item)
if op_id == Id.Arith_Comma:
return rhs
# Do additional type checking after indexing and comma.
if not isinstance(lhs, int):
e_die('LHS should be an integer, got %s', lhs)
if not isinstance(rhs, int):
e_die('RHS should be an integer, got %s', rhs)
if op_id == Id.Arith_Plus:
return lhs + rhs
if op_id == Id.Arith_Minus:
return lhs - rhs
if op_id == Id.Arith_Star:
return lhs * rhs
if op_id == Id.Arith_Slash:
try:
return lhs / rhs
except ZeroDivisionError:
# TODO: _ErrorWithLocation should also accept arith_expr ? I
# think I needed that for other stuff.
# Or I could blame the '/' token, instead of op_id.
error_expr = node.right # node is ArithBinary
if error_expr.tag == arith_expr_e.ArithVarRef:
# TODO: ArithVarRef should store a token instead of a string!
e_die('Divide by zero (name)')
elif error_expr.tag == arith_expr_e.ArithWord:
e_die('Divide by zero', word=node.right.w)
else:
e_die('Divide by zero')
if op_id == Id.Arith_Percent:
return lhs % rhs
if op_id == Id.Arith_DStar:
# OVM is stripped of certain functions that are somehow necessary for
# exponentiation.
# Python/ovm_stub_pystrtod.c:21: PyOS_double_to_string: Assertion `0'
# failed.
if rhs < 0:
e_die("Exponent can't be less than zero") # TODO: error location
result = 1
for i in xrange(rhs):
result *= lhs
return result
if op_id == Id.Arith_DEqual:
return int(lhs == rhs)
if op_id == Id.Arith_NEqual:
return int(lhs != rhs)
if op_id == Id.Arith_Great:
return int(lhs > rhs)
if op_id == Id.Arith_GreatEqual:
return int(lhs >= rhs)
if op_id == Id.Arith_Less:
return int(lhs < rhs)
if op_id == Id.Arith_LessEqual:
return int(lhs <= rhs)
if op_id == Id.Arith_Pipe:
return lhs | rhs
if op_id == Id.Arith_Amp:
return lhs & rhs
if op_id == Id.Arith_Caret:
return lhs ^ rhs
# Note: how to define shift of negative numbers?
if op_id == Id.Arith_DLess:
return lhs << rhs
if op_id == Id.Arith_DGreat:
return lhs >> rhs
raise NotImplementedError(op_id)
if node.tag == arith_expr_e.TernaryOp:
cond = self.Eval(node.cond)
if cond: # nonzero
return self.Eval(node.true_expr)
else:
return self.Eval(node.false_expr)
raise NotImplementedError("Unhandled node %r" % node.__class__.__name__)
class BoolEvaluator(_ExprEvaluator):
def _SetRegexMatches(self, matches):
"""For ~= to set the BASH_REMATCH array."""
state.SetGlobalArray(self.mem, 'BASH_REMATCH', matches)
def _EvalCompoundWord(self, word, do_fnmatch=False, do_ere=False):
"""
Args:
node: Id.Word_Compound
"""
val = self.word_ev.EvalWordToString(word, do_fnmatch=do_fnmatch,
do_ere=do_ere)
return val.s
def Eval(self, node):
#print('!!', node.tag)
if node.tag == bool_expr_e.WordTest:
s = self._EvalCompoundWord(node.w)
return bool(s)
if node.tag == bool_expr_e.LogicalNot:
b = self.Eval(node.child)
return not b
if node.tag == bool_expr_e.LogicalAnd:
# Short-circuit evaluation
if self.Eval(node.left):
return self.Eval(node.right)
else:
return False
if node.tag == bool_expr_e.LogicalOr:
if self.Eval(node.left):
return True
else:
return self.Eval(node.right)
if node.tag == bool_expr_e.BoolUnary:
op_id = node.op_id
s = self._EvalCompoundWord(node.child)
# Now dispatch on arg type
arg_type = BOOL_ARG_TYPES[op_id.enum_id] # could be static in the LST?
if arg_type == bool_arg_type_e.Path:
# Only use lstat if we're testing for a symlink.
if op_id in (Id.BoolUnary_h, Id.BoolUnary_L):
try:
mode = posix.lstat(s).st_mode
except OSError:
return False
return stat.S_ISLNK(mode)
try:
st = posix.stat(s)
except OSError:
# TODO: Signal extra debug information?
#log("Error from stat(%r): %s" % (s, e))
return False
mode = st.st_mode
if op_id in (Id.BoolUnary_e, Id.BoolUnary_a): # -a is alias for -e
return True
if op_id == Id.BoolUnary_f:
return stat.S_ISREG(mode)
if op_id == Id.BoolUnary_d:
return stat.S_ISDIR(mode)
if op_id == Id.BoolUnary_b:
return stat.S_ISBLK(mode)
if op_id == Id.BoolUnary_c:
return stat.S_ISCHR(mode)
if op_id == Id.BoolUnary_p:
return stat.S_ISFIFO(mode)
if op_id == Id.BoolUnary_S:
return stat.S_ISSOCK(mode)
if op_id == Id.BoolUnary_x:
return posix.access(s, posix.X_OK)
if op_id == Id.BoolUnary_r:
return posix.access(s, posix.R_OK)
if op_id == Id.BoolUnary_w:
return posix.access(s, posix.W_OK)
if op_id == Id.BoolUnary_s:
return st.st_size != 0
if op_id == Id.BoolUnary_O:
return st.st_uid == posix.geteuid()
if op_id == Id.BoolUnary_G:
return st.st_gid == posix.getegid()
e_die("%s isn't implemented", op_id) # implicit location
if arg_type == bool_arg_type_e.Str:
if op_id == Id.BoolUnary_z:
return not bool(s)
if op_id == Id.BoolUnary_n:
return bool(s)
raise AssertionError(op_id) # should never happen
if arg_type == bool_arg_type_e.Other:
if op_id == Id.BoolUnary_t:
try:
fd = int(s)
except ValueError:
# TODO: Need location information of [
e_die('Invalid file descriptor %r', s, word=node.child)
return posix.isatty(fd)
# See whether 'set -o' options have been set
if op_id == Id.BoolUnary_o:
b = getattr(self.exec_opts, s, None)
return False if b is None else b
e_die("%s isn't implemented", op_id) # implicit location
raise AssertionError(arg_type) # should never happen
if node.tag == bool_expr_e.BoolBinary:
op_id = node.op_id
s1 = self._EvalCompoundWord(node.left)
# Whether to glob escape
do_fnmatch = op_id in (Id.BoolBinary_GlobEqual, Id.BoolBinary_GlobDEqual,
Id.BoolBinary_GlobNEqual)
do_ere = (op_id == Id.BoolBinary_EqualTilde)
s2 = self._EvalCompoundWord(node.right, do_fnmatch=do_fnmatch,
do_ere=do_ere)
# Now dispatch on arg type
arg_type = BOOL_ARG_TYPES[op_id.enum_id]
if arg_type == bool_arg_type_e.Path:
st1 = posix.stat(s1)
st2 = posix.stat(s2)
# TODO: test newer than (mtime)
if op_id == Id.BoolBinary_nt:
return st1[stat.ST_MTIME] > st2[stat.ST_MTIME]
if op_id == Id.BoolBinary_ot:
return st1[stat.ST_MTIME] < st2[stat.ST_MTIME]
e_die("%s isn't implemented", op_id) # implicit location
if arg_type == bool_arg_type_e.Int:
# NOTE: We assume they are constants like [[ 3 -eq 3 ]].
# Bash also allows [[ 1+2 -eq 3 ]].
i1 = self._StringToIntegerOrError(s1, blame_word=node.left)
i2 = self._StringToIntegerOrError(s2, blame_word=node.right)
if op_id == Id.BoolBinary_eq:
return i1 == i2
if op_id == Id.BoolBinary_ne:
return i1 != i2
if op_id == Id.BoolBinary_gt:
return i1 > i2
if op_id == Id.BoolBinary_ge:
return i1 >= i2
if op_id == Id.BoolBinary_lt:
return i1 < i2
if op_id == Id.BoolBinary_le:
return i1 <= i2
raise AssertionError(op_id) # should never happen
if arg_type == bool_arg_type_e.Str:
if op_id in (Id.BoolBinary_GlobEqual, Id.BoolBinary_GlobDEqual):
#log('Matching %s against pattern %s', s1, s2)
return libc.fnmatch(s2, s1)
if op_id == Id.BoolBinary_GlobNEqual:
return not libc.fnmatch(s2, s1)
if op_id in (Id.BoolBinary_Equal, Id.BoolBinary_DEqual):
return s1 == s2
if op_id == Id.BoolBinary_NEqual:
return s1 != s2
if op_id == Id.BoolBinary_EqualTilde:
# TODO: This should go to --debug-file
#log('Matching %r against regex %r', s1, s2)
try:
matches = libc.regex_match(s2, s1)
except RuntimeError:
# Status 2 indicates a regex parse error. This is fatal in OSH but
# not in bash, which treats [[ like a command with an exit code.
e_die("Invalid regex %r", s2, word=node.right, status=2)
if matches is None:
return False
self._SetRegexMatches(matches)
return True
if op_id == Id.Redir_Less: # pun
return s1 < s2
if op_id == Id.Redir_Great: # pun
return s1 > s2
raise AssertionError(op_id) # should never happen
raise AssertionError(node.tag)