OILS / builtin / meta_ysh.py View on Github | oilshell.org

101 lines, 63 significant
1#!/usr/bin/env python2
2"""
3meta_ysh.py - Builtins for introspection
4"""
5from __future__ import print_function
6
7from _devbuild.gen.runtime_asdl import cmd_value
8from core import error
9from core.error import e_usage
10from core import vm
11from frontend import flag_spec
12from frontend import match
13from frontend import typed_args
14from mycpp import mylib
15from mycpp.mylib import log
16
17_ = log
18
19from typing import TYPE_CHECKING
20if TYPE_CHECKING:
21 from core import state
22 from core import ui
23
24
25class Shvm(vm._Builtin):
26 """
27 shvm cell x - move pp cell x here
28 shvm gc-stats - like OILS_GC_STATS
29 shvm guts (x+y) - ASDL pretty printing
30 - similar to = x+y, but not stable
31
32 Related:
33 _vm->heapId(obj) - a heap ID that can be used to detect cycles for
34 serialization
35 """
36
37 def __init__(
38 self,
39 mem, # type: state.Mem
40 errfmt, # type: ui.ErrorFormatter
41 ):
42 # type: (...) -> None
43 self.mem = mem
44 self.errfmt = errfmt
45 self.stdout_ = mylib.Stdout()
46
47 def Run(self, cmd_val):
48 # type: (cmd_value.Argv) -> int
49
50 arg, arg_r = flag_spec.ParseCmdVal('shvm', cmd_val)
51
52 action, action_loc = arg_r.ReadRequired2(
53 'expected an action (cell, gc-stats, guts)')
54
55 if action == 'cell':
56 argv, locs = arg_r.Rest2()
57
58 status = 0
59 for i, name in enumerate(argv):
60 if name.startswith(':'):
61 name = name[1:]
62
63 if not match.IsValidVarName(name):
64 raise error.Usage('got invalid variable name %r' % name,
65 locs[i])
66
67 cell = self.mem.GetCell(name)
68 if cell is None:
69 self.errfmt.Print_("Couldn't find a variable named %r" %
70 name,
71 blame_loc=locs[i])
72 status = 1
73 else:
74 self.stdout_.write('%s = ' % name)
75 if mylib.PYTHON:
76 cell.PrettyPrint() # may be color
77
78 self.stdout_.write('\n')
79
80 elif action == 'gc-stats':
81 # mylib.PrintGcStats()
82 print('TODO')
83 status = 0
84
85 elif action == 'guts':
86 # Print the value
87 print('TODO')
88
89 if cmd_val.typed_args: # eval (myblock)
90 rd = typed_args.ReaderForProc(cmd_val)
91 val = rd.PosValue()
92 rd.Done()
93 if mylib.PYTHON:
94 print(val)
95
96 status = 0
97
98 else:
99 e_usage('got invalid action %r' % action, action_loc)
100
101 return status