OILS / builtin / method_other.py View on Github | oils.pub

87 lines, 46 significant
1"""Methods on various types"""
2
3from __future__ import print_function
4
5from _devbuild.gen.value_asdl import (value, value_t, LiteralBlock, cmd_frag,
6 cmd_frag_e)
7
8from core import alloc
9from core import num
10from core import state
11from core import vm
12from display import ui
13from frontend import typed_args
14from mycpp.mylib import log, tagswitch, NewDict
15
16from typing import Dict, Optional, cast
17
18_ = log
19
20
21class SetValue(vm._Callable):
22
23 def __init__(self, mem):
24 # type: (state.Mem) -> None
25 self.mem = mem
26
27 def Call(self, rd):
28 # type: (typed_args.Reader) -> value_t
29
30 # This is guaranteed
31 place = rd.PosPlace()
32
33 val = rd.PosValue()
34 rd.Done()
35
36 self.mem.SetPlace(place, val, rd.LeftParenToken())
37
38 return value.Null
39
40
41class SourceCode(vm._Callable):
42
43 def __init__(self):
44 # type: () -> None
45 pass
46
47 def Call(self, rd):
48 # type: (typed_args.Reader) -> value_t
49
50 # This is guaranteed
51 cmd = rd.PosCommand()
52 rd.Done()
53
54 # For now, we only have lines if the block arg is literal like p { echo
55 # hi }
56 # As opposed to
57 # p (; ; myblock)
58
59 lit_block = None # type: Optional[LiteralBlock]
60 frag = cmd.frag
61 with tagswitch(frag) as case:
62 if case(cmd_frag_e.LiteralBlock):
63 lit_block = cast(LiteralBlock, frag)
64 elif case(cmd_frag_e.Expr):
65 c = cast(cmd_frag.Expr, frag).c
66 return value.Null
67 else:
68 raise AssertionError()
69
70 result = NewDict() # type: Dict[str, value_t]
71
72 brace_group = lit_block.brace_group
73 # BraceGroup has location for {
74 line = brace_group.left.line
75
76 # for the user to pass back to --location-str
77 result['location_str'] = value.Str(ui.GetLineSourceString(line))
78 result['location_start_line'] = num.ToBig(line.line_num)
79
80 #log('LINES %s', lit_block.lines)
81 # Between { and }
82 code_str = alloc.SnipCodeBlock(brace_group.left, brace_group.right,
83 lit_block.lines)
84
85 result['code_str'] = value.Str(code_str)
86
87 return value.Dict(result)