OILS / ysh / expr_eval.py View on Github | oilshell.org

1591 lines, 1055 significant
1#!/usr/bin/env python2
2"""expr_eval.py."""
3from __future__ import print_function
4
5from _devbuild.gen.id_kind_asdl import Id
6from _devbuild.gen.syntax_asdl import (
7 loc,
8 loc_t,
9 re,
10 re_e,
11 re_t,
12 Token,
13 SimpleVarSub,
14 word_part,
15 SingleQuoted,
16 DoubleQuoted,
17 BracedVarSub,
18 ShArrayLiteral,
19 CommandSub,
20 expr,
21 expr_e,
22 expr_t,
23 y_lhs_e,
24 y_lhs_t,
25 Attribute,
26 Subscript,
27 class_literal_term,
28 class_literal_term_e,
29 class_literal_term_t,
30 char_class_term_t,
31 PosixClass,
32 PerlClass,
33 CharCode,
34 CharRange,
35 ArgList,
36 Eggex,
37)
38from _devbuild.gen.runtime_asdl import (
39 coerced_e,
40 coerced_t,
41 scope_e,
42 scope_t,
43 part_value,
44 part_value_t,
45 Piece,
46)
47from _devbuild.gen.value_asdl import (value, value_e, value_t, y_lvalue,
48 y_lvalue_e, y_lvalue_t, IntBox, LeftName,
49 Obj, cmd_frag)
50from core import error
51from core.error import e_die, e_die_status
52from core import num
53from core import pyutil
54from core import state
55from display import ui
56from core import vm
57from data_lang import j8
58from frontend import lexer
59from frontend import match
60from frontend import typed_args
61from osh import braces
62from mycpp import mops
63from mycpp.mylib import log, NewDict, switch, tagswitch, print_stderr
64from ysh import func_proc
65from ysh import val_ops
66
67import libc
68
69from typing import cast, Optional, Dict, List, Tuple, TYPE_CHECKING
70
71if TYPE_CHECKING:
72 from osh import cmd_eval
73 from osh import word_eval
74 from osh import split
75
76_ = log
77
78
79def LookupVar(mem, var_name, which_scopes, var_loc):
80 # type: (state.Mem, str, scope_t, loc_t) -> value_t
81
82 # Lookup WITHOUT dynamic scope.
83 val = mem.GetValue(var_name, which_scopes=which_scopes)
84 if val.tag() == value_e.Undef:
85 e_die('Undefined variable %r' % var_name, var_loc)
86
87 return val
88
89
90def _ConvertToInt(val, msg, blame_loc):
91 # type: (value_t, str, loc_t) -> mops.BigInt
92 UP_val = val
93 with tagswitch(val) as case:
94 if case(value_e.Int):
95 val = cast(value.Int, UP_val)
96 return val.i
97
98 elif case(value_e.Str):
99 val = cast(value.Str, UP_val)
100 if match.LooksLikeYshInt(val.s):
101 s = val.s.replace('_', '')
102 ok, i = mops.FromStr2(s)
103 if not ok:
104 e_die("Integer too big: %s" % s, blame_loc)
105 return i
106
107 raise error.TypeErr(val, msg, blame_loc)
108
109
110def _ConvertToNumber(val):
111 # type: (value_t) -> Tuple[coerced_t, mops.BigInt, float]
112 UP_val = val
113 with tagswitch(val) as case:
114 if case(value_e.Int):
115 val = cast(value.Int, UP_val)
116 return coerced_e.Int, val.i, -1.0
117
118 elif case(value_e.Float):
119 val = cast(value.Float, UP_val)
120 return coerced_e.Float, mops.MINUS_ONE, val.f
121
122 elif case(value_e.Str):
123 val = cast(value.Str, UP_val)
124
125 if match.LooksLikeYshInt(val.s):
126 s = val.s.replace('_', '')
127 ok, i = mops.FromStr2(s)
128 if not ok:
129 e_die("Integer too big: %s" % s, loc.Missing)
130 return coerced_e.Int, i, -1.0
131
132 if match.LooksLikeYshFloat(val.s):
133 s = val.s.replace('_', '')
134 return coerced_e.Float, mops.MINUS_ONE, float(s)
135
136 return coerced_e.Neither, mops.MINUS_ONE, -1.0
137
138
139def _ConvertForBinaryOp(left, right):
140 # type: (value_t, value_t) -> Tuple[coerced_t, mops.BigInt, mops.BigInt, float, float]
141 """
142 Returns one of
143 value_e.Int or value_e.Float
144 2 ints or 2 floats
145
146 To indicate which values the operation should be done on
147 """
148 c1, i1, f1 = _ConvertToNumber(left)
149 c2, i2, f2 = _ConvertToNumber(right)
150
151 nope = mops.MINUS_ONE
152
153 if c1 == coerced_e.Int and c2 == coerced_e.Int:
154 return coerced_e.Int, i1, i2, -1.0, -1.0
155
156 elif c1 == coerced_e.Int and c2 == coerced_e.Float:
157 return coerced_e.Float, nope, nope, mops.ToFloat(i1), f2
158
159 elif c1 == coerced_e.Float and c2 == coerced_e.Int:
160 return coerced_e.Float, nope, nope, f1, mops.ToFloat(i2)
161
162 elif c1 == coerced_e.Float and c2 == coerced_e.Float:
163 return coerced_e.Float, nope, nope, f1, f2
164
165 else:
166 # No operation is valid
167 return coerced_e.Neither, nope, nope, -1.0, -1.0
168
169
170class ExprEvaluator(object):
171 """Shared between arith and bool evaluators.
172
173 They both:
174
175 1. Convert strings to integers, respecting shopt -s strict_arith.
176 2. Look up variables and evaluate words.
177 """
178
179 def __init__(
180 self,
181 mem, # type: state.Mem
182 mutable_opts, # type: state.MutableOpts
183 methods, # type: Dict[int, Dict[str, vm._Callable]]
184 splitter, # type: split.SplitContext
185 errfmt, # type: ui.ErrorFormatter
186 ):
187 # type: (...) -> None
188 self.shell_ex = None # type: vm._Executor
189 self.cmd_ev = None # type: cmd_eval.CommandEvaluator
190 self.word_ev = None # type: word_eval.AbstractWordEvaluator
191
192 self.mem = mem
193 self.mutable_opts = mutable_opts
194 self.methods = methods
195 self.splitter = splitter
196 self.errfmt = errfmt
197
198 def CheckCircularDeps(self):
199 # type: () -> None
200 assert self.shell_ex is not None
201 assert self.word_ev is not None
202
203 def _LookupVar(self, name, var_loc):
204 # type: (str, loc_t) -> value_t
205 return LookupVar(self.mem, name, scope_e.LocalOrGlobal, var_loc)
206
207 def EvalAugmented(self, lval, rhs_val, op, which_scopes):
208 # type: (y_lvalue_t, value_t, Token, scope_t) -> None
209 """ setvar x += 1, setvar L[0] -= 1
210
211 Called by CommandEvaluator
212 """
213 UP_lval = lval
214 with tagswitch(lval) as case:
215 if case(y_lvalue_e.Local): # setvar x += 1
216 lval = cast(LeftName, UP_lval)
217 lhs_val = self._LookupVar(lval.name, lval.blame_loc)
218 if op.id in (Id.Arith_PlusEqual, Id.Arith_MinusEqual,
219 Id.Arith_StarEqual, Id.Arith_SlashEqual):
220 new_val = self._ArithIntFloat(lhs_val, rhs_val, op)
221 else:
222 new_val = self._ArithIntOnly(lhs_val, rhs_val, op)
223
224 self.mem.SetNamed(lval, new_val, which_scopes)
225
226 elif case(y_lvalue_e.Container): # setvar d.key += 1
227 lval = cast(y_lvalue.Container, UP_lval)
228
229 obj = lval.obj
230 UP_obj = obj
231
232 lhs_val_ = None # type: value_t
233 # Similar to command_e.Mutation
234 with tagswitch(obj) as case:
235 if case(value_e.List):
236 obj = cast(value.List, UP_obj)
237 i1 = _ConvertToInt(lval.index,
238 'List index should be Int',
239 loc.Missing)
240 # TODO: don't truncate
241 index = mops.BigTruncate(i1)
242 try:
243 lhs_val_ = obj.items[index]
244 except IndexError:
245 raise error.Expr(
246 'List index out of range: %d' % index,
247 loc.Missing)
248
249 elif case(value_e.Dict):
250 obj = cast(value.Dict, UP_obj)
251 index = -1 # silence C++ warning
252 key = val_ops.ToStr(lval.index,
253 'Dict key should be Str',
254 loc.Missing)
255 try:
256 lhs_val_ = obj.d[key]
257 except KeyError:
258 raise error.Expr('Dict key not found: %r' % key,
259 loc.Missing)
260
261 elif case(value_e.Obj):
262 obj = cast(Obj, UP_obj)
263 index = -1 # silence C++ warning
264 key = val_ops.ToStr(lval.index,
265 'Obj attribute should be Str',
266 loc.Missing)
267 try:
268 lhs_val_ = obj.d[key]
269 except KeyError:
270 raise error.Expr(
271 'Obj attribute not found: %r' % key,
272 loc.Missing)
273
274 else:
275 raise error.TypeErr(
276 obj, "obj[index] expected List or Dict",
277 loc.Missing)
278
279 if op.id in (Id.Arith_PlusEqual, Id.Arith_MinusEqual,
280 Id.Arith_StarEqual, Id.Arith_SlashEqual):
281 new_val_ = self._ArithIntFloat(lhs_val_, rhs_val, op)
282 else:
283 new_val_ = self._ArithIntOnly(lhs_val_, rhs_val, op)
284
285 with tagswitch(obj) as case:
286 if case(value_e.List):
287 obj = cast(value.List, UP_obj)
288 assert index != -1, 'Should have been initialized'
289 obj.items[index] = new_val_
290
291 elif case(value_e.Dict):
292 obj = cast(value.Dict, UP_obj)
293 obj.d[key] = new_val_
294
295 elif case(value_e.Obj):
296 obj = cast(Obj, UP_obj)
297 obj.d[key] = new_val_
298
299 else:
300 raise AssertionError()
301
302 else:
303 raise AssertionError()
304
305 def _EvalLeftLocalOrGlobal(self, lhs, which_scopes):
306 # type: (expr_t, scope_t) -> value_t
307 """Evaluate the LEFT MOST part, respecting setvar/setglobal.
308
309 Consider this statement:
310
311 setglobal g[a[i]] = 42
312
313 - The g is always global, never local. It's the thing to be mutated.
314 - The a can be local or global
315 """
316 UP_lhs = lhs
317 with tagswitch(lhs) as case:
318 if case(expr_e.Var):
319 lhs = cast(expr.Var, UP_lhs)
320
321 # respect setvar/setglobal with which_scopes
322 return LookupVar(self.mem, lhs.name, which_scopes, lhs.left)
323
324 elif case(expr_e.Subscript):
325 lhs = cast(Subscript, UP_lhs)
326
327 # recursive call
328 obj = self._EvalLeftLocalOrGlobal(lhs.obj, which_scopes)
329 index = self._EvalExpr(lhs.index)
330
331 return self._EvalSubscript(obj, index, lhs.left)
332
333 elif case(expr_e.Attribute):
334 lhs = cast(Attribute, UP_lhs)
335 assert lhs.op.id == Id.Expr_Dot
336
337 # recursive call
338 obj = self._EvalLeftLocalOrGlobal(lhs.obj, which_scopes)
339 return self._EvalDot(lhs, obj)
340
341 else:
342 # Shouldn't happen because of Transformer._CheckLhs
343 raise AssertionError()
344
345 def _EvalLhsExpr(self, lhs, which_scopes):
346 # type: (y_lhs_t, scope_t) -> y_lvalue_t
347 """
348 Handle setvar x, setvar a[i], ... setglobal x, setglobal a[i]
349 """
350 UP_lhs = lhs
351 with tagswitch(lhs) as case:
352 if case(y_lhs_e.Var):
353 lhs = cast(Token, UP_lhs)
354 return LeftName(lexer.LazyStr(lhs), lhs)
355
356 elif case(y_lhs_e.Subscript):
357 lhs = cast(Subscript, UP_lhs)
358 # setvar mylist[0] = 42
359 # setvar mydict['key'] = 42
360
361 lval = self._EvalLeftLocalOrGlobal(lhs.obj, which_scopes)
362 index = self._EvalExpr(lhs.index)
363 return y_lvalue.Container(lval, index)
364
365 elif case(y_lhs_e.Attribute):
366 lhs = cast(Attribute, UP_lhs)
367 assert lhs.op.id == Id.Expr_Dot
368
369 # setvar mydict.key = 42
370 lval = self._EvalLeftLocalOrGlobal(lhs.obj, which_scopes)
371
372 attr = value.Str(lhs.attr_name)
373 return y_lvalue.Container(lval, attr)
374
375 else:
376 raise AssertionError()
377
378 def EvalExprClosure(self, expr_val, blame_loc):
379 # type: (value.Expr, loc_t) -> value_t
380 """
381 Used by user-facing APIs that take value.Expr closures:
382
383 var i = 42
384 var x = io->evalExpr(^[i + 1])
385 var x = s.replace(pat, ^"- $0 $i -")
386 """
387 with state.ctx_EnclosedFrame(self.mem, expr_val.captured_frame,
388 expr_val.module_frame, None):
389 return self.EvalExpr(expr_val.e, blame_loc)
390
391 def EvalExpr(self, node, blame_loc):
392 # type: (expr_t, loc_t) -> value_t
393 """Public API for _EvalExpr to ensure command_sub_errexit"""
394 self.mem.SetLocationForExpr(blame_loc)
395 # Pure C++ won't need to catch exceptions
396 with state.ctx_YshExpr(self.mutable_opts):
397 val = self._EvalExpr(node)
398 return val
399
400 def EvalLhsExpr(self, lhs, which_scopes):
401 # type: (y_lhs_t, scope_t) -> y_lvalue_t
402 """Public API for _EvalLhsExpr to ensure command_sub_errexit"""
403 with state.ctx_YshExpr(self.mutable_opts):
404 lval = self._EvalLhsExpr(lhs, which_scopes)
405 return lval
406
407 def EvalExprSub(self, part):
408 # type: (word_part.ExprSub) -> part_value_t
409
410 val = self.EvalExpr(part.child, part.left)
411
412 with switch(part.left.id) as case:
413 if case(Id.Left_DollarBracket): # $[join(x)]
414 s = val_ops.Stringify(val, loc.WordPart(part), 'Expr sub ')
415 return Piece(s, False, False)
416
417 elif case(Id.Lit_AtLBracket): # @[split(x)]
418 strs = val_ops.ToShellArray(val, loc.WordPart(part),
419 'Expr splice ')
420 return part_value.Array(strs)
421
422 else:
423 raise AssertionError(part.left)
424
425 def PluginCall(self, func_val, pos_args):
426 # type: (value.Func, List[value_t]) -> value_t
427 """For renderPrompt()
428
429 Similar to
430 - WordEvaluator.EvalForPlugin(), which evaluates $PS1 outside main loop
431 - ReadlineCallback.__call__, which executes shell outside main loop
432 """
433 with state.ctx_YshExpr(self.mutable_opts):
434 with state.ctx_Registers(self.mem): # to sandbox globals
435 named_args = {} # type: Dict[str, value_t]
436 arg_list = ArgList.CreateNull() # There's no call site
437 rd = typed_args.Reader(pos_args, named_args, None, arg_list)
438
439 try:
440 val = func_proc.CallUserFunc(func_val, rd, self.mem,
441 self.cmd_ev)
442 except error.FatalRuntime as e:
443 val = value.Str('<Runtime error: %s>' %
444 e.UserErrorString())
445
446 except (IOError, OSError) as e:
447 val = value.Str('<I/O error: %s>' % pyutil.strerror(e))
448
449 except KeyboardInterrupt:
450 val = value.Str('<Ctrl-C>')
451
452 return val
453
454 def CallConvertFunc(self, func_val, arg, convert_tok, call_loc):
455 # type: (value_t, value_t, Token, loc_t) -> value_t
456 """ For Eggex captures """
457 with state.ctx_YshExpr(self.mutable_opts):
458 pos_args = [arg]
459 named_args = {} # type: Dict[str, value_t]
460 arg_list = ArgList.CreateNull() # There's no call site
461 rd = typed_args.Reader(pos_args, named_args, None, arg_list)
462 rd.SetFallbackLocation(convert_tok)
463 try:
464 val = self._CallFunc(func_val, rd)
465 except error.FatalRuntime as e:
466 func_name = lexer.TokenVal(convert_tok)
467 self.errfmt.Print_(
468 'Fatal error calling Eggex conversion func %r from this Match accessor'
469 % func_name, call_loc)
470 print_stderr('')
471 raise
472
473 return val
474
475 def _CallMetaMethod(self, func_val, pos_args, blame_loc):
476 # type: (value_t, List[value_t], loc_t) -> value_t
477
478 named_args = {} # type: Dict[str, value_t]
479 arg_list = ArgList.CreateNull() # There's no call site
480 rd = typed_args.Reader(pos_args, named_args, None, arg_list)
481 rd.SetFallbackLocation(blame_loc)
482 # errors propagate
483 return self._CallFunc(func_val, rd)
484
485 def SpliceValue(self, val, part):
486 # type: (value_t, word_part.Splice) -> List[str]
487 """ write -- @myvar """
488 return val_ops.ToShellArray(val, loc.WordPart(part), prefix='Splice ')
489
490 def _EvalConst(self, node):
491 # type: (expr.Const) -> value_t
492 return node.val
493
494 def _EvalUnary(self, node):
495 # type: (expr.Unary) -> value_t
496
497 val = self._EvalExpr(node.child)
498
499 with switch(node.op.id) as case:
500 if case(Id.Arith_Minus):
501 c1, i1, f1 = _ConvertToNumber(val)
502 if c1 == coerced_e.Int:
503 return value.Int(mops.Negate(i1))
504 if c1 == coerced_e.Float:
505 return value.Float(-f1)
506 raise error.TypeErr(val, 'Negation expected Int or Float',
507 node.op)
508
509 elif case(Id.Arith_Tilde):
510 i = _ConvertToInt(val, '~ expected Int', node.op)
511 return value.Int(mops.BitNot(i))
512
513 elif case(Id.Expr_Not):
514 b = val_ops.ToBool(val)
515 return value.Bool(False if b else True)
516
517 # &s &a[0] &d.key &d.nested.other
518 elif case(Id.Arith_Amp):
519 # Only 3 possibilities:
520 # - expr.Var
521 # - expr.Attribute with `.` operator (d.key)
522 # - expr.SubScript
523 #
524 # See _EvalLhsExpr, which gives you y_lvalue
525
526 # TODO: &x, &a[0], &d.key, creates a value.Place?
527 # If it's Attribute or SubScript, you don't evaluate them.
528 # y_lvalue_t -> place_t
529
530 raise NotImplementedError(node.op)
531
532 else:
533 raise AssertionError(node.op)
534
535 raise AssertionError('for C++ compiler')
536
537 def _ArithIntFloat(self, left, right, op):
538 # type: (value_t, value_t, Token) -> value_t
539 """
540 Note: may be replaced with arithmetic on tagged integers, e.g. 60 bit
541 with overflow detection
542 """
543 c, i1, i2, f1, f2 = _ConvertForBinaryOp(left, right)
544
545 op_id = op.id
546
547 if c == coerced_e.Int:
548 with switch(op_id) as case:
549 if case(Id.Arith_Plus, Id.Arith_PlusEqual):
550 return value.Int(mops.Add(i1, i2))
551 elif case(Id.Arith_Minus, Id.Arith_MinusEqual):
552 return value.Int(mops.Sub(i1, i2))
553 elif case(Id.Arith_Star, Id.Arith_StarEqual):
554 return value.Int(mops.Mul(i1, i2))
555 elif case(Id.Arith_Slash, Id.Arith_SlashEqual):
556 if mops.Equal(i2, mops.ZERO):
557 raise error.Expr('Divide by zero', op)
558 return value.Float(mops.ToFloat(i1) / mops.ToFloat(i2))
559 else:
560 raise AssertionError()
561
562 elif c == coerced_e.Float:
563 with switch(op_id) as case:
564 if case(Id.Arith_Plus, Id.Arith_PlusEqual):
565 return value.Float(f1 + f2)
566 elif case(Id.Arith_Minus, Id.Arith_MinusEqual):
567 return value.Float(f1 - f2)
568 elif case(Id.Arith_Star, Id.Arith_StarEqual):
569 return value.Float(f1 * f2)
570 elif case(Id.Arith_Slash, Id.Arith_SlashEqual):
571 if f2 == 0.0:
572 raise error.Expr('Divide by zero', op)
573 return value.Float(f1 / f2)
574 else:
575 raise AssertionError()
576
577 else:
578 raise error.TypeErrVerbose(
579 'Binary operator expected numbers, got %s and %s (OILS-ERR-201)'
580 % (ui.ValType(left), ui.ValType(right)), op)
581
582 def _ArithIntOnly(self, left, right, op):
583 # type: (value_t, value_t, Token) -> value_t
584
585 i1 = _ConvertToInt(left, 'Left operand should be Int', op)
586 i2 = _ConvertToInt(right, 'Right operand should be Int', op)
587
588 with switch(op.id) as case:
589
590 # a % b setvar a %= b
591 if case(Id.Arith_Percent, Id.Arith_PercentEqual):
592 if mops.Equal(i2, mops.ZERO):
593 raise error.Expr('Divide by zero', op)
594 if mops.Greater(mops.ZERO, i2):
595 # Disallow this to remove confusion between modulus and remainder
596 raise error.Expr("Divisor can't be negative", op)
597
598 return value.Int(mops.Rem(i1, i2))
599
600 # a // b setvar a //= b
601 elif case(Id.Expr_DSlash, Id.Expr_DSlashEqual):
602 if mops.Equal(i2, mops.ZERO):
603 raise error.Expr('Divide by zero', op)
604 return value.Int(mops.Div(i1, i2))
605
606 # a ** b setvar a **= b (ysh only)
607 elif case(Id.Arith_DStar, Id.Expr_DStarEqual):
608 # Same as sh_expr_eval.py
609 if mops.Greater(mops.ZERO, i2):
610 raise error.Expr("Exponent can't be a negative number", op)
611 return value.Int(num.Exponent(i1, i2))
612
613 # Bitwise
614 elif case(Id.Arith_Amp, Id.Arith_AmpEqual): # &
615 return value.Int(mops.BitAnd(i1, i2))
616
617 elif case(Id.Arith_Pipe, Id.Arith_PipeEqual): # |
618 return value.Int(mops.BitOr(i1, i2))
619
620 elif case(Id.Arith_Caret, Id.Arith_CaretEqual): # ^
621 return value.Int(mops.BitXor(i1, i2))
622
623 elif case(Id.Arith_DGreat, Id.Arith_DGreatEqual): # >>
624 if mops.Greater(mops.ZERO, i2): # i2 < 0
625 raise error.Expr("Can't right shift by negative number",
626 op)
627 return value.Int(mops.RShift(i1, i2))
628
629 elif case(Id.Arith_DLess, Id.Arith_DLessEqual): # <<
630 if mops.Greater(mops.ZERO, i2): # i2 < 0
631 raise error.Expr("Can't left shift by negative number", op)
632 return value.Int(mops.LShift(i1, i2))
633
634 else:
635 raise AssertionError(op.id)
636
637 def _Concat(self, left, right, op):
638 # type: (value_t, value_t, Token) -> value_t
639 UP_left = left
640 UP_right = right
641
642 if left.tag() == value_e.Str and right.tag() == value_e.Str:
643 left = cast(value.Str, UP_left)
644 right = cast(value.Str, UP_right)
645
646 return value.Str(left.s + right.s)
647
648 elif left.tag() == value_e.List and right.tag() == value_e.List:
649 left = cast(value.List, UP_left)
650 right = cast(value.List, UP_right)
651
652 c = list(left.items) # mycpp rewrite of L1 + L2
653 c.extend(right.items)
654 return value.List(c)
655
656 else:
657 raise error.TypeErrVerbose(
658 'Expected Str ++ Str or List ++ List, got %s ++ %s' %
659 (ui.ValType(left), ui.ValType(right)), op)
660
661 def _EvalBinary(self, node):
662 # type: (expr.Binary) -> value_t
663
664 left = self._EvalExpr(node.left)
665
666 # Logical and/or lazily evaluate
667 with switch(node.op.id) as case:
668 if case(Id.Expr_And):
669 if val_ops.ToBool(left): # no errors
670 return self._EvalExpr(node.right)
671 else:
672 return left
673
674 elif case(Id.Expr_Or):
675 if val_ops.ToBool(left):
676 return left
677 else:
678 return self._EvalExpr(node.right)
679
680 # These operators all eagerly evaluate
681 right = self._EvalExpr(node.right)
682
683 with switch(node.op.id) as case:
684 if case(Id.Arith_DPlus): # a ++ b to concat Str or List
685 return self._Concat(left, right, node.op)
686
687 elif case(Id.Arith_Plus, Id.Arith_Minus, Id.Arith_Star,
688 Id.Arith_Slash):
689 return self._ArithIntFloat(left, right, node.op)
690
691 else:
692 return self._ArithIntOnly(left, right, node.op)
693
694 def _CompareNumeric(self, left, right, op):
695 # type: (value_t, value_t, Token) -> bool
696 c, i1, i2, f1, f2 = _ConvertForBinaryOp(left, right)
697
698 if c == coerced_e.Int:
699 with switch(op.id) as case:
700 if case(Id.Arith_Less):
701 return mops.Greater(i2, i1)
702 elif case(Id.Arith_Great):
703 return mops.Greater(i1, i2)
704 elif case(Id.Arith_LessEqual):
705 return mops.Greater(i2, i1) or mops.Equal(i1, i2)
706 elif case(Id.Arith_GreatEqual):
707 return mops.Greater(i1, i2) or mops.Equal(i1, i2)
708 else:
709 raise AssertionError()
710
711 elif c == coerced_e.Float:
712 with switch(op.id) as case:
713 if case(Id.Arith_Less):
714 return f1 < f2
715 elif case(Id.Arith_Great):
716 return f1 > f2
717 elif case(Id.Arith_LessEqual):
718 return f1 <= f2
719 elif case(Id.Arith_GreatEqual):
720 return f1 >= f2
721 else:
722 raise AssertionError()
723
724 else:
725 raise error.TypeErrVerbose(
726 'Comparison operator expected numbers, got %s and %s' %
727 (ui.ValType(left), ui.ValType(right)), op)
728
729 def _EvalCompare(self, node):
730 # type: (expr.Compare) -> value_t
731
732 left = self._EvalExpr(node.left)
733 result = True # Implicit and
734 for i, op in enumerate(node.ops):
735 right_expr = node.comparators[i]
736
737 right = self._EvalExpr(right_expr)
738
739 if op.id in (Id.Arith_Less, Id.Arith_Great, Id.Arith_LessEqual,
740 Id.Arith_GreatEqual):
741 result = self._CompareNumeric(left, right, op)
742
743 elif op.id == Id.Expr_TEqual:
744 result = val_ops.ExactlyEqual(left, right, op)
745 elif op.id == Id.Expr_NotDEqual:
746 result = not val_ops.ExactlyEqual(left, right, op)
747
748 elif op.id == Id.Expr_In:
749 result = val_ops.Contains(left, right)
750 elif op.id == Id.Node_NotIn:
751 result = not val_ops.Contains(left, right)
752
753 elif op.id == Id.Expr_Is:
754 result = left is right
755
756 elif op.id == Id.Node_IsNot:
757 result = left is not right
758
759 elif op.id == Id.Expr_DTilde:
760 # no extglob in YSH; use eggex
761 if left.tag() != value_e.Str:
762 raise error.TypeErrVerbose('LHS must be Str', op)
763
764 if right.tag() != value_e.Str:
765 raise error.TypeErrVerbose('RHS must be Str', op)
766
767 UP_left = left
768 UP_right = right
769 left = cast(value.Str, UP_left)
770 right = cast(value.Str, UP_right)
771 return value.Bool(libc.fnmatch(right.s, left.s))
772
773 elif op.id == Id.Expr_NotDTilde:
774 if left.tag() != value_e.Str:
775 raise error.TypeErrVerbose('LHS must be Str', op)
776
777 if right.tag() != value_e.Str:
778 raise error.TypeErrVerbose('RHS must be Str', op)
779
780 UP_left = left
781 UP_right = right
782 left = cast(value.Str, UP_left)
783 right = cast(value.Str, UP_right)
784 return value.Bool(not libc.fnmatch(right.s, left.s))
785
786 elif op.id == Id.Expr_TildeDEqual:
787 # Approximate equality
788 UP_left = left
789 if left.tag() != value_e.Str:
790 e_die('~== expects a string on the left', op)
791
792 left = cast(value.Str, UP_left)
793 left2 = left.s.strip()
794
795 UP_right = right
796 with tagswitch(right) as case:
797 if case(value_e.Str):
798 right = cast(value.Str, UP_right)
799 return value.Bool(left2 == right.s)
800
801 elif case(value_e.Bool):
802 right = cast(value.Bool, UP_right)
803 left2 = left2.lower()
804 lb = False
805 if left2 == 'true':
806 lb = True
807 elif left2 == 'false':
808 lb = False
809 else:
810 return value.Bool(False)
811
812 #log('left %r left2 %r', left, left2)
813 return value.Bool(lb == right.b)
814
815 elif case(value_e.Int):
816 right = cast(value.Int, UP_right)
817
818 # Note: this logic is similar to _ConvertToInt(left2)
819 if not match.LooksLikeYshInt(left2):
820 return value.Bool(False)
821
822 left2 = left2.replace('_', '')
823 ok, left_i = mops.FromStr2(left2)
824 if not ok:
825 e_die('Integer too big: %s' % left2, op)
826
827 eq = mops.Equal(left_i, right.i)
828 return value.Bool(eq)
829
830 e_die('~== expects Str, Int, or Bool on the right', op)
831
832 else:
833 try:
834 if op.id == Id.Arith_Tilde:
835 result = val_ops.MatchRegex(left, right, self.mem)
836
837 elif op.id == Id.Expr_NotTilde:
838 # don't pass self.mem to not set a match
839 result = not val_ops.MatchRegex(left, right, None)
840
841 else:
842 raise AssertionError(op)
843 except ValueError as e:
844 # Status 2 indicates a regex parse error, as with [[ in OSH
845 e_die_status(2, e.message, op)
846
847 if not result:
848 return value.Bool(result)
849
850 left = right
851
852 return value.Bool(result)
853
854 def _CallFunc(self, to_call, rd):
855 # type: (value_t, typed_args.Reader) -> value_t
856
857 # Now apply args to either builtin or user-defined function
858 UP_to_call = to_call
859 with tagswitch(to_call) as case:
860 if case(value_e.Func):
861 to_call = cast(value.Func, UP_to_call)
862
863 return func_proc.CallUserFunc(to_call, rd, self.mem,
864 self.cmd_ev)
865
866 elif case(value_e.BuiltinFunc):
867 to_call = cast(value.BuiltinFunc, UP_to_call)
868
869 # C++ cast to work around ASDL 'any'
870 f = cast(vm._Callable, to_call.callable)
871 return f.Call(rd)
872 else:
873 raise AssertionError("Shouldn't have been bound")
874
875 def _EvalFuncCall(self, node):
876 # type: (expr.FuncCall) -> value_t
877
878 func = self._EvalExpr(node.func)
879 UP_func = func
880
881 # The () operator has a 2x2 matrix of
882 # (free, bound) x (builtin, user-defined)
883
884 # Eval args first
885 with tagswitch(func) as case:
886 if case(value_e.Func, value_e.BuiltinFunc):
887 to_call = func
888 pos_args, named_args = func_proc._EvalArgList(self, node.args)
889 rd = typed_args.Reader(pos_args, named_args, None, node.args)
890
891 elif case(value_e.BoundFunc):
892 func = cast(value.BoundFunc, UP_func)
893
894 to_call = func.func
895 pos_args, named_args = func_proc._EvalArgList(self,
896 node.args,
897 self_val=func.me)
898 rd = typed_args.Reader(pos_args,
899 named_args,
900 None,
901 node.args,
902 is_bound=True)
903 else:
904 raise error.TypeErr(func, 'Expected a function or method',
905 node.args.left)
906
907 return self._CallFunc(to_call, rd)
908
909 def _EvalSubscript(self, obj, index, blame_loc):
910 # type: (value_t, value_t, loc_t) -> value_t
911
912 UP_obj = obj
913 UP_index = index
914
915 with tagswitch(obj) as case:
916 if case(value_e.Str):
917 # Note: s[i] and s[i:j] are like Go, on bytes. We may provide
918 # s->numBytes(), s->countRunes(), and iteration over runes.
919 obj = cast(value.Str, UP_obj)
920 with tagswitch(index) as case2:
921 if case2(value_e.Slice):
922 index = cast(value.Slice, UP_index)
923
924 lower = index.lower.i if index.lower else 0
925 upper = index.upper.i if index.upper else len(obj.s)
926 return value.Str(obj.s[lower:upper])
927
928 elif case2(value_e.Int):
929 index = cast(value.Int, UP_index)
930 i = mops.BigTruncate(index.i)
931 try:
932 return value.Str(obj.s[i])
933 except IndexError:
934 raise error.Expr('index out of range', blame_loc)
935
936 else:
937 raise error.TypeErr(index,
938 'Str index expected Int or Slice',
939 blame_loc)
940
941 elif case(value_e.List):
942 obj = cast(value.List, UP_obj)
943
944 big_i = mops.ZERO
945 with tagswitch(index) as case2:
946 if case2(value_e.Slice):
947 index = cast(value.Slice, UP_index)
948
949 lower = (index.lower.i if index.lower else 0)
950 upper = (index.upper.i
951 if index.upper else len(obj.items))
952 return value.List(obj.items[lower:upper])
953
954 elif case2(value_e.Int):
955 index = cast(value.Int, UP_index)
956 big_i = index.i
957
958 elif case2(value_e.Str):
959 index = cast(value.Str, UP_index)
960 big_i = _ConvertToInt(index, 'List index expected Int',
961 blame_loc)
962
963 else:
964 raise error.TypeErr(
965 index, 'List index expected Int, Str, or Slice',
966 blame_loc)
967
968 i = mops.BigTruncate(big_i) # TODO: don't truncate
969 try:
970 return obj.items[i]
971 except IndexError:
972 raise error.Expr('List index out of range: %d' % i,
973 blame_loc)
974
975 elif case(value_e.Dict):
976 obj = cast(value.Dict, UP_obj)
977 if index.tag() != value_e.Str:
978 raise error.TypeErr(index, 'Dict index expected Str',
979 blame_loc)
980
981 index = cast(value.Str, UP_index)
982 try:
983 return obj.d[index.s]
984 except KeyError:
985 # TODO: expr.Subscript has no error location
986 raise error.Expr('Dict entry not found: %r' % index.s,
987 blame_loc)
988
989 elif case(value_e.Obj):
990 obj = cast(Obj, UP_obj)
991
992 index_method = val_ops.IndexMetaMethod(obj)
993 if index_method is not None:
994 pos_args = [obj, index]
995 return self._CallMetaMethod(index_method, pos_args,
996 blame_loc)
997
998 raise error.TypeErr(
999 obj, 'Subscript expected one of (Str List Dict, indexable Obj)',
1000 blame_loc)
1001
1002 def _ChainedLookup(self, obj, current, attr_name):
1003 # type: (Obj, Obj, str) -> Optional[value_t]
1004 """Prototype chain lookup.
1005
1006 Args:
1007 obj: properties we might bind to
1008 current: our location in the prototype chain
1009 """
1010 val = current.d.get(attr_name)
1011 if val is not None:
1012 # Special bound method logic for objects, but NOT modules
1013 if val.tag() in (value_e.Func, value_e.BuiltinFunc):
1014 return value.BoundFunc(obj, val)
1015 else:
1016 return val
1017
1018 if current.prototype is not None:
1019 return self._ChainedLookup(obj, current.prototype, attr_name)
1020
1021 return None
1022
1023 def _EvalDot(self, node, val):
1024 # type: (Attribute, value_t) -> value_t
1025 """ foo.attr on RHS or LHS
1026
1027 setvar x = foo.attr
1028 setglobal g[foo.attr] = 42
1029 """
1030 UP_val = val
1031 with tagswitch(val) as case:
1032 if case(value_e.Dict):
1033 val = cast(value.Dict, UP_val)
1034 attr_name = node.attr_name
1035
1036 # Dict key / normal attribute lookup
1037 result = val.d.get(attr_name)
1038 if result is not None:
1039 return result
1040
1041 raise error.Expr('Dict entry %r not found' % attr_name,
1042 node.op)
1043
1044 elif case(value_e.Obj):
1045 obj = cast(Obj, UP_val)
1046 attr_name = node.attr_name
1047
1048 # Dict key / normal attribute lookup
1049 result = obj.d.get(attr_name)
1050 if result is not None:
1051 return result
1052
1053 # Prototype lookup - with special logic for BoundMethod
1054 if obj.prototype is not None:
1055 result = self._ChainedLookup(obj, obj.prototype, attr_name)
1056 if result is not None:
1057 return result
1058
1059 raise error.Expr('Attribute %r not found on Obj' % attr_name,
1060 node.op)
1061
1062 else:
1063 # Method lookup on builtin types.
1064 # They don't have attributes or prototype chains -- we only
1065 # have a flat dict.
1066 type_methods = self.methods.get(val.tag())
1067 name = node.attr_name
1068 vm_callable = (type_methods.get(name)
1069 if type_methods is not None else None)
1070 if vm_callable:
1071 func_val = value.BuiltinFunc(vm_callable)
1072 return value.BoundFunc(val, func_val)
1073
1074 raise error.TypeErrVerbose(
1075 "Method %r not found on builtin type %s" %
1076 (name, ui.ValType(val)), node.attr)
1077
1078 raise AssertionError()
1079
1080 def _EvalRArrow(self, node, val):
1081 # type: (Attribute, value_t) -> value_t
1082 mut_name = 'M/' + node.attr_name
1083
1084 UP_val = val
1085 with tagswitch(val) as case:
1086 if case(value_e.Obj):
1087 obj = cast(Obj, UP_val)
1088
1089 if obj.prototype is not None:
1090 result = self._ChainedLookup(obj, obj.prototype, mut_name)
1091 if result is not None:
1092 return result
1093
1094 # TODO: we could have different errors for:
1095 # - no prototype
1096 # - found in the properties, not in the prototype chain (not
1097 # sure if this error is common.)
1098 raise error.Expr(
1099 "Mutating method %r not found on Obj prototype chain" %
1100 mut_name, node.attr)
1101 else:
1102 # Look up methods on builtin types
1103 # TODO: These should also be called M/append, M/erase, etc.
1104
1105 type_methods = self.methods.get(val.tag())
1106 vm_callable = (type_methods.get(mut_name)
1107 if type_methods is not None else None)
1108 if vm_callable:
1109 func_val = value.BuiltinFunc(vm_callable)
1110 return value.BoundFunc(val, func_val)
1111
1112 raise error.TypeErrVerbose(
1113 "Mutating method %r not found on builtin type %s" %
1114 (mut_name, ui.ValType(val)), node.attr)
1115 raise AssertionError()
1116
1117 def _EvalAttribute(self, node):
1118 # type: (Attribute) -> value_t
1119
1120 val = self._EvalExpr(node.obj)
1121 with switch(node.op.id) as case:
1122 if case(Id.Expr_Dot): # d.key is like d['key']
1123 return self._EvalDot(node, val)
1124
1125 elif case(Id.Expr_RArrow): # e.g. mylist->append(42)
1126 return self._EvalRArrow(node, val)
1127
1128 elif case(Id.Expr_RDArrow): # chaining s => split()
1129 name = node.attr_name
1130
1131 # Look up builtin methods, e.g.
1132 # s => strip() is like s.strip()
1133 # Note:
1134 # m => group(1) is worse than m.group(1)
1135 # This is not a transformation, but more like an attribute
1136
1137 type_methods = self.methods.get(val.tag())
1138 vm_callable = (type_methods.get(name)
1139 if type_methods is not None else None)
1140 if vm_callable:
1141 func_val = value.BuiltinFunc(vm_callable)
1142 return value.BoundFunc(val, func_val)
1143
1144 # Operator is =>, so try function chaining.
1145
1146 # Instead of str(f()) => upper()
1147 # or str(f()).upper() as in Pythohn
1148 #
1149 # It's more natural to write
1150 # f() => str() => upper()
1151
1152 # Could improve error message: may give "Undefined variable"
1153 val2 = self._LookupVar(name, node.attr)
1154
1155 with tagswitch(val2) as case2:
1156 if case2(value_e.Func, value_e.BuiltinFunc):
1157 return value.BoundFunc(val, val2)
1158 else:
1159 raise error.TypeErr(
1160 val2, 'Fat arrow => expects method or function',
1161 node.attr)
1162
1163 else:
1164 raise AssertionError(node.op)
1165 raise AssertionError()
1166
1167 def _EvalExpr(self, node):
1168 # type: (expr_t) -> value_t
1169 """Turn an expression into a value."""
1170 if 0:
1171 print('_EvalExpr()')
1172 node.PrettyPrint()
1173 print('')
1174
1175 UP_node = node
1176 with tagswitch(node) as case:
1177 if case(expr_e.Const):
1178 node = cast(expr.Const, UP_node)
1179 return self._EvalConst(node)
1180
1181 elif case(expr_e.Var):
1182 node = cast(expr.Var, UP_node)
1183 return self._LookupVar(node.name, node.left)
1184
1185 elif case(expr_e.Place):
1186 node = cast(expr.Place, UP_node)
1187 frame = self.mem.CurrentFrame()
1188 return value.Place(LeftName(node.var_name, node.blame_tok),
1189 frame)
1190
1191 elif case(expr_e.CommandSub):
1192 node = cast(CommandSub, UP_node)
1193
1194 id_ = node.left_token.id
1195 if id_ == Id.Left_CaretParen: # ^(echo block literal)
1196 # TODO: Propagate location info with ^(
1197 return value.Command(cmd_frag.Expr(node.child),
1198 self.mem.CurrentFrame(),
1199 self.mem.GlobalFrame())
1200 else:
1201 stdout_str = self.shell_ex.RunCommandSub(node)
1202 if id_ == Id.Left_AtParen: # @(seq 3)
1203 # YSH splitting algorithm: does not depend on IFS
1204 try:
1205 strs = j8.SplitJ8Lines(stdout_str)
1206 except error.Decode as e:
1207 # status code 4 is special, for encode/decode errors.
1208 raise error.Structured(4, e.Message(),
1209 node.left_token)
1210
1211 #strs = self.splitter.SplitForWordEval(stdout_str)
1212
1213 items = [value.Str(s)
1214 for s in strs] # type: List[value_t]
1215 return value.List(items)
1216 else:
1217 return value.Str(stdout_str)
1218
1219 elif case(expr_e.ShArrayLiteral): # var x = :| foo *.py |
1220 node = cast(ShArrayLiteral, UP_node)
1221 words = braces.BraceExpandWords(node.words)
1222 strs = self.word_ev.EvalWordSequence(words)
1223 #log('ARRAY LITERAL EVALUATED TO -> %s', strs)
1224 #return value.BashArray(strs)
1225
1226 # It's equivalent to ['foo', 'bar']
1227 items = [value.Str(s) for s in strs]
1228 return value.List(items)
1229
1230 elif case(expr_e.DoubleQuoted):
1231 node = cast(DoubleQuoted, UP_node)
1232 # In an ideal world, YSH would *statically* disallow:
1233 #
1234 # - "$@" and "${array[@]}"
1235 # - backticks like `echo hi`
1236 # - $(( 1+2 )) and $[] -- although useful for refactoring
1237 # - not sure: ${x%%} -- could disallow this
1238 # - these enters the ArgDQ state: "${a:-foo bar}" ?
1239 #
1240 # But that would complicate the parser/evaluator. So just rely
1241 # on runtime strict_array to disallow the bad parts.
1242 return value.Str(self.word_ev.EvalDoubleQuotedToString(node))
1243
1244 elif case(expr_e.SingleQuoted):
1245 node = cast(SingleQuoted, UP_node)
1246 return value.Str(node.sval)
1247
1248 elif case(expr_e.BracedVarSub):
1249 node = cast(BracedVarSub, UP_node)
1250 return value.Str(self.word_ev.EvalBracedVarSubToString(node))
1251
1252 elif case(expr_e.SimpleVarSub):
1253 node = cast(SimpleVarSub, UP_node)
1254 return value.Str(self.word_ev.EvalSimpleVarSubToString(node))
1255
1256 elif case(expr_e.Unary):
1257 node = cast(expr.Unary, UP_node)
1258 return self._EvalUnary(node)
1259
1260 elif case(expr_e.Binary):
1261 node = cast(expr.Binary, UP_node)
1262 return self._EvalBinary(node)
1263
1264 elif case(expr_e.Slice): # a[:0]
1265 node = cast(expr.Slice, UP_node)
1266
1267 lower = None # type: Optional[IntBox]
1268 upper = None # type: Optional[IntBox]
1269
1270 if node.lower:
1271 i1 = _ConvertToInt(self._EvalExpr(node.lower),
1272 'Slice begin should be Int', node.op)
1273 # TODO: don't truncate
1274 lower = IntBox(mops.BigTruncate(i1))
1275
1276 if node.upper:
1277 i1 = _ConvertToInt(self._EvalExpr(node.upper),
1278 'Slice end should be Int', node.op)
1279 # TODO: don't truncate
1280 upper = IntBox(mops.BigTruncate(i1))
1281
1282 return value.Slice(lower, upper)
1283
1284 elif case(expr_e.Range):
1285 node = cast(expr.Range, UP_node)
1286
1287 assert node.lower is not None
1288 assert node.upper is not None
1289
1290 i1 = _ConvertToInt(self._EvalExpr(node.lower),
1291 'Range begin should be Int', node.op)
1292
1293 i2 = _ConvertToInt(self._EvalExpr(node.upper),
1294 'Range end should be Int', node.op)
1295
1296 if node.op.id == Id.Expr_DDotEqual: # Closed range
1297 i2 = mops.Add(i2, mops.ONE)
1298
1299 # TODO: Don't truncate
1300 return value.Range(mops.BigTruncate(i1), mops.BigTruncate(i2))
1301
1302 elif case(expr_e.Compare):
1303 node = cast(expr.Compare, UP_node)
1304 return self._EvalCompare(node)
1305
1306 elif case(expr_e.IfExp):
1307 node = cast(expr.IfExp, UP_node)
1308 b = val_ops.ToBool(self._EvalExpr(node.test))
1309 if b:
1310 return self._EvalExpr(node.body)
1311 else:
1312 return self._EvalExpr(node.orelse)
1313
1314 elif case(expr_e.List):
1315 node = cast(expr.List, UP_node)
1316 items = [self._EvalExpr(e) for e in node.elts]
1317 return value.List(items)
1318
1319 elif case(expr_e.Tuple):
1320 node = cast(expr.Tuple, UP_node)
1321 # YSH language: Tuple syntax evaluates to LIST !
1322 items = [self._EvalExpr(e) for e in node.elts]
1323 return value.List(items)
1324
1325 elif case(expr_e.Dict):
1326 node = cast(expr.Dict, UP_node)
1327
1328 kvals = [self._EvalExpr(e) for e in node.keys]
1329 values = [] # type: List[value_t]
1330
1331 for i, value_expr in enumerate(node.values):
1332 if value_expr.tag() == expr_e.Implicit: # {key}
1333 # Enforced by parser. Key is expr.Const
1334 assert kvals[i].tag() == value_e.Str, kvals[i]
1335 key = cast(value.Str, kvals[i])
1336 v = self._LookupVar(key.s, loc.Missing)
1337 else:
1338 v = self._EvalExpr(value_expr)
1339
1340 values.append(v)
1341
1342 d = NewDict() # type: Dict[str, value_t]
1343 for i, kval in enumerate(kvals):
1344 k = val_ops.ToStr(kval, 'Dict keys must be strings',
1345 loc.Missing)
1346 d[k] = values[i]
1347
1348 return value.Dict(d)
1349
1350 elif case(expr_e.ListComp):
1351 e_die_status(
1352 2, 'List comprehension reserved but not implemented')
1353
1354 elif case(expr_e.GeneratorExp):
1355 e_die_status(
1356 2, 'Generator expression reserved but not implemented')
1357
1358 elif case(expr_e.Literal): # ^[1 + 2]
1359 node = cast(expr.Literal, UP_node)
1360 return value.Expr(node.inner, self.mem.CurrentFrame(),
1361 self.mem.GlobalFrame())
1362
1363 elif case(expr_e.Lambda): # |x| x+1 syntax is reserved
1364 # TODO: Location information for |, or func
1365 # Note: anonymous functions also evaluate to a Lambda, but they shouldn't
1366 e_die_status(2, 'Lambda reserved but not implemented')
1367
1368 elif case(expr_e.FuncCall):
1369 node = cast(expr.FuncCall, UP_node)
1370 return self._EvalFuncCall(node)
1371
1372 elif case(expr_e.Subscript):
1373 node = cast(Subscript, UP_node)
1374 obj = self._EvalExpr(node.obj)
1375 index = self._EvalExpr(node.index)
1376 return self._EvalSubscript(obj, index, node.left)
1377
1378 elif case(expr_e.Attribute): # obj->method or mydict.key
1379 node = cast(Attribute, UP_node)
1380 return self._EvalAttribute(node)
1381
1382 elif case(expr_e.Eggex):
1383 node = cast(Eggex, UP_node)
1384 return self.EvalEggex(node)
1385
1386 else:
1387 raise NotImplementedError(node.__class__.__name__)
1388
1389 def EvalEggex(self, node):
1390 # type: (Eggex) -> value.Eggex
1391
1392 # Splice, check flags consistency, and accumulate convert_funcs indexed
1393 # by capture group
1394 ev = EggexEvaluator(self.mem, node.canonical_flags)
1395 spliced = ev.EvalE(node.regex)
1396
1397 # as_ere and capture_names filled by ~ operator or Str method
1398 return value.Eggex(spliced, node.canonical_flags, ev.convert_funcs,
1399 ev.convert_toks, None, [])
1400
1401
1402class EggexEvaluator(object):
1403
1404 def __init__(self, mem, canonical_flags):
1405 # type: (state.Mem, str) -> None
1406 self.mem = mem
1407 self.canonical_flags = canonical_flags
1408 self.convert_funcs = [] # type: List[Optional[value_t]]
1409 self.convert_toks = [] # type: List[Optional[Token]]
1410
1411 def _LookupVar(self, name, var_loc):
1412 # type: (str, loc_t) -> value_t
1413 """
1414 Duplicated from ExprEvaluator
1415 """
1416 return LookupVar(self.mem, name, scope_e.LocalOrGlobal, var_loc)
1417
1418 def _EvalClassLiteralTerm(self, term, out):
1419 # type: (class_literal_term_t, List[char_class_term_t]) -> None
1420 UP_term = term
1421
1422 # These 2 vars will be initialized if we don't return early
1423 s = None # type: str
1424 char_code_tok = None # type: Token
1425
1426 with tagswitch(term) as case:
1427
1428 if case(class_literal_term_e.CharCode):
1429 term = cast(CharCode, UP_term)
1430
1431 # What about \0? At runtime, ERE should disallow it. But we
1432 # can also disallow it here.
1433 out.append(term)
1434 return
1435
1436 elif case(class_literal_term_e.CharRange):
1437 term = cast(CharRange, UP_term)
1438 out.append(term)
1439 return
1440
1441 elif case(class_literal_term_e.PosixClass):
1442 term = cast(PosixClass, UP_term)
1443 out.append(term)
1444 return
1445
1446 elif case(class_literal_term_e.PerlClass):
1447 term = cast(PerlClass, UP_term)
1448 out.append(term)
1449 return
1450
1451 elif case(class_literal_term_e.SingleQuoted):
1452 term = cast(SingleQuoted, UP_term)
1453
1454 s = term.sval
1455 char_code_tok = term.left
1456
1457 elif case(class_literal_term_e.Splice):
1458 term = cast(class_literal_term.Splice, UP_term)
1459
1460 val = self._LookupVar(term.var_name, term.name)
1461 s = val_ops.ToStr(val, 'Eggex char class splice expected Str',
1462 term.name)
1463 char_code_tok = term.name
1464
1465 assert s is not None, term
1466 for ch in s:
1467 char_int = ord(ch)
1468 if char_int >= 128:
1469 # / [ '\x7f\xff' ] / is better written as / [ \x7f \xff ] /
1470 e_die(
1471 "Use unquoted char literal for byte %d, which is >= 128"
1472 " (avoid confusing a set of bytes with a sequence)" %
1473 char_int, char_code_tok)
1474 out.append(CharCode(char_code_tok, char_int, False))
1475
1476 def EvalE(self, node):
1477 # type: (re_t) -> re_t
1478 """Resolve references and eval constants in an Eggex
1479
1480 Rules:
1481 Splice => re_t # like Hex and @const in / Hex '.' @const /
1482 Speck/Token (syntax) => Primitive (logical)
1483 Chars and Strings => LiteralChars
1484 """
1485 UP_node = node
1486
1487 with tagswitch(node) as case:
1488 if case(re_e.Seq):
1489 node = cast(re.Seq, UP_node)
1490 new_children = [self.EvalE(child) for child in node.children]
1491 return re.Seq(new_children)
1492
1493 elif case(re_e.Alt):
1494 node = cast(re.Alt, UP_node)
1495 new_children = [self.EvalE(child) for child in node.children]
1496 return re.Alt(new_children)
1497
1498 elif case(re_e.Repeat):
1499 node = cast(re.Repeat, UP_node)
1500 return re.Repeat(self.EvalE(node.child), node.op)
1501
1502 elif case(re_e.Group):
1503 node = cast(re.Group, UP_node)
1504
1505 # placeholder for non-capturing group
1506 self.convert_funcs.append(None)
1507 self.convert_toks.append(None)
1508 return re.Group(self.EvalE(node.child))
1509
1510 elif case(re_e.Capture): # Identical to Group
1511 node = cast(re.Capture, UP_node)
1512 convert_func = None # type: Optional[value_t]
1513 convert_tok = None # type: Optional[Token]
1514 if node.func_name:
1515 func_name = lexer.LazyStr(node.func_name)
1516 func_val = self.mem.GetValue(func_name)
1517 with tagswitch(func_val) as case:
1518 if case(value_e.Func, value_e.BuiltinFunc):
1519 convert_func = func_val
1520 convert_tok = node.func_name
1521 else:
1522 raise error.TypeErr(
1523 func_val,
1524 "Expected %r to be a func" % func_name,
1525 node.func_name)
1526
1527 self.convert_funcs.append(convert_func)
1528 self.convert_toks.append(convert_tok)
1529 return re.Capture(self.EvalE(node.child), node.name,
1530 node.func_name)
1531
1532 elif case(re_e.CharClassLiteral):
1533 node = cast(re.CharClassLiteral, UP_node)
1534
1535 new_terms = [] # type: List[char_class_term_t]
1536 for t in node.terms:
1537 # can get multiple char_class_term.CharCode for a
1538 # class_literal_term_t
1539 self._EvalClassLiteralTerm(t, new_terms)
1540 return re.CharClass(node.negated, new_terms)
1541
1542 elif case(re_e.SingleQuoted):
1543 node = cast(SingleQuoted, UP_node)
1544
1545 s = node.sval
1546 return re.LiteralChars(node.left, s)
1547
1548 elif case(re_e.Splice):
1549 node = cast(re.Splice, UP_node)
1550
1551 val = self._LookupVar(node.var_name, node.name)
1552 UP_val = val
1553 with tagswitch(val) as case:
1554 if case(value_e.Str):
1555 val = cast(value.Str, UP_val)
1556 to_splice = re.LiteralChars(node.name,
1557 val.s) # type: re_t
1558
1559 elif case(value_e.Eggex):
1560 val = cast(value.Eggex, UP_val)
1561
1562 # Splicing means we get the conversion funcs too.
1563 self.convert_funcs.extend(val.convert_funcs)
1564 self.convert_toks.extend(val.convert_toks)
1565
1566 # Splicing requires flags to match. This check is
1567 # transitive.
1568 to_splice = val.spliced
1569
1570 if val.canonical_flags != self.canonical_flags:
1571 e_die(
1572 "Expected eggex flags %r, but got %r" %
1573 (self.canonical_flags, val.canonical_flags),
1574 node.name)
1575
1576 else:
1577 raise error.TypeErr(
1578 val, 'Eggex splice expected Str or Eggex',
1579 node.name)
1580 return to_splice
1581
1582 else:
1583 # These are evaluated at translation time
1584
1585 # case(re_e.Primitive)
1586 # case(re_e.PosixClass)
1587 # case(re_e.PerlClass)
1588 return node
1589
1590
1591# vim: sw=4