Skip to content

Commit

Permalink
Add simple eval example, base builtin on exec, gets `functools.part…
Browse files Browse the repository at this point in the history
…ial` working.
  • Loading branch information
cdleary committed May 31, 2024
1 parent 4861c66 commit 0166dac
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
5 changes: 4 additions & 1 deletion bin/echo_vm
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ def _handle_result(result: Result[Any]) -> int:
tb = result.get_exception().traceback
assert isinstance(tb, etraceback.ETraceback)
for filename, lineno in etraceback.walk(tb):
_print_surrounding(filename, lineno)
if os.path.exists(filename):
_print_surrounding(filename, lineno)
else:
print(f' {filename}:{lineno}', file=sys.stderr)
termcolor.cprint(str(result), color='red', file=sys.stderr)
error = True
else:
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions py_samples/simple_eval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert eval('x+x', {'x': 42}) == 42+42
6 changes: 5 additions & 1 deletion src/echo/ebuiltins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

from echo.eobjects import get_guest_builtin

# These are all the builtins that we implement in a virtualized fashion.
BUILTINS = tuple("""len str int bool super type object list dict tuple
bytearray
property staticmethod classmethod sum
map iter next enumerate any all exec hash vars
map iter next enumerate any all exec eval hash vars
hasattr getattr setattr isinstance issubclass repr callable min max dir
BaseException Exception
""".split())

# These are the builtins that we expose directly from the underlying Python
# implementation. Eventually one would want all of these to be virtualized.
PASSTHRU = tuple("""range slice float reversed set frozenset zip sorted
memoryview bytes complex id
compile
Expand Down
18 changes: 18 additions & 0 deletions src/echo/interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ def import_path(path: Text, module_name: Text, fully_qualified_name: Text,
def _do_exec(args: Tuple[Any, ...],
kwargs: Dict[Text, Any],
ictx: ICtx) -> Result[None]:
"""Implements the `exec` builtin."""
assert 1 <= len(args) <= 3 and not kwargs, (args, kwargs)
source, globals_, locals_ = none_filler(args, 3)
if isinstance(source, types.CodeType):
Expand All @@ -277,3 +278,20 @@ def _do_exec(args: Tuple[Any, ...],
if res.is_exception():
return res
return Result(None)


@check_result
@register_builtin('eval')
def _do_eval(args: Tuple[Any, ...],
kwargs: Dict[Text, Any],
ictx: ICtx) -> Result[None]:
"""Implements the `eval` builtin."""
source, globals_, locals_ = none_filler(args, 3)
if isinstance(source, types.CodeType):
code = source
else:
assert isinstance(source, str), type(source)
code = compile(source, '<string>', 'eval')
res = interp(code, globals_=globals_, ictx=ictx, name='eval',
locals_dict=locals_, in_function=False)
return res

0 comments on commit 0166dac

Please sign in to comment.