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

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