forked from Beuchey/OCL_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oclrepl.py
70 lines (60 loc) · 1.81 KB
/
oclrepl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from pyecoreocl.compiler import dummy_compiler, OCLTuple
from pyecore.resources import ResourceSet
import pyecore
import cmd
class OCLShell(cmd.Cmd):
intro = 'OCL Interpreter'
prompt = '(ocl) '
file = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.rset = ResourceSet()
self.vars = {
'rset': self.rset,
'ecore': pyecore.ecore
}
self.gvars = {
'OCLTuple': OCLTuple
}
self.vars['metavars'] = self.vars
def do_load(self, arg):
try:
print(f'!! Loading {arg}')
res = self.rset.get_resource(arg)
self.vars['self'] = res.contents[0]
except Exception as e:
print(e)
def do_register(self, arg):
try:
print(f'!! Registering {arg}')
res = self.rset.get_resource(arg)
root = res.contents[0]
self.rset.metamodel_registry[root.nsURI] = root
except Exception as e:
print(e)
def default(self, line):
if ':=' in line:
self.assign(line)
return
try:
print(f"{self.compile_execute(line)!r}")
except Exception as e:
print(e)
def assign(self, arg):
try:
var, expression = arg.split(':=')
res = self.compile_execute(expression)
self.vars[var.strip()] = res
except Exception as e:
print(e)
def compile_execute(self, line):
cmd = dummy_compiler(line)
print(f"!! expr: {cmd}")
code = compile(f"{cmd}", '<ocl_expr>', 'eval')
return eval(code, self.gvars, self.vars)
def do_quit(self, arg):
print("\n!! Exiting")
return True
do_q = do_quit
do_EOF = do_quit
OCLShell().cmdloop()