56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
from tango.vm import TangoVM
|
|
|
|
if __name__ == '__main__':
|
|
code = """
|
|
(
|
|
(def hello "world")
|
|
(def foo "bar")
|
|
(def dance (
|
|
(def other "world")
|
|
(print (if (eq hello other) "the world is ok" "the world is broken"))
|
|
))
|
|
(on "click" dance)
|
|
(dance)
|
|
(def hello "moon")
|
|
)
|
|
"""
|
|
|
|
event_handlers = []
|
|
|
|
def builtin_on(vm, scope, event, handler):
|
|
handler = vm.eval_in_scope(scope, handler)
|
|
event_handlers.append((event, handler, scope))
|
|
|
|
def trigger_event(vm, trigger_event):
|
|
results = []
|
|
for event, handler, scope in event_handlers:
|
|
if trigger_event == event:
|
|
results.append(vm.eval_in_scope(scope, handler))
|
|
return results
|
|
|
|
def builtin_def(vm, scope, ident, val):
|
|
scope.set_def(ident, val)
|
|
|
|
def builtin_print(vm, scope, *args):
|
|
print(*vm.multi_eval_in_scope(scope, args))
|
|
|
|
def builtin_if(vm, scope, predictate, truthy, falsy = None):
|
|
if vm.eval_in_scope(scope, predictate):
|
|
return vm.eval_in_scope(scope, truthy)
|
|
elif falsy:
|
|
return vm.eval_in_scope(scope, falsy)
|
|
else:
|
|
return None
|
|
|
|
def builtin_eq(vm, scope, a, b):
|
|
a, b = vm.multi_eval_in_scope(scope, (a, b))
|
|
return a == b
|
|
|
|
vm = TangoVM()
|
|
vm.def_builtin('def', builtin_def)
|
|
vm.def_builtin('print', builtin_print)
|
|
vm.def_builtin('eq', builtin_eq)
|
|
vm.def_builtin('if', builtin_if)
|
|
vm.def_builtin('on', builtin_on)
|
|
vm.eval_string(code)
|
|
trigger_event(vm, 'click') |