OILS / ysh / expr_eval.py View on Github | oils.pub

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