Skip to content

Commit

Permalink
Implement #9, rlwrap functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenhombre committed Jul 24, 2022
1 parent c7cfc03 commit d712bf1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
lark == 1.1.2
prompt_toolkit == 3.0.30
pycodestyle == 2.5.0
pytest
versioneer == 0.22
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
],
},
install_requires=[
'lark == 1.1.2'
'lark == 1.1.2',
'prompt_toolkit == 3.0.30',
],
classifiers=[
"Development Status :: 3 - Alpha",
Expand Down
3 changes: 2 additions & 1 deletion smallscheme/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import argparse
from smallscheme.env import Env
from smallscheme.scheme import evalu, repl, parse_str
from smallscheme.scheme import evalu, parse_str
from smallscheme.repl import repl

def run_file(filename):
env = Env()
Expand Down
30 changes: 30 additions & 0 deletions smallscheme/repl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# prompt_toolkit setup example taken from
# https://stackoverflow.com/questions/59627995:
from prompt_toolkit import prompt
from prompt_toolkit import PromptSession
from prompt_toolkit.history import FileHistory
from os.path import expanduser

from smallscheme.env import Env
from smallscheme.scheme import parse_str, evalu, printable_value


def repl():
session = PromptSession(history=FileHistory(
expanduser('~/.smallscheme_history')))

env = Env()
while True:
try:
x = session.prompt("scheme> ").strip()
except EOFError:
print()
break
if x:
try:
for parsed in parse_str(x):
pv = printable_value(evalu(parsed, env))
if pv:
print(pv)
except Exception as e:
print(e)
23 changes: 0 additions & 23 deletions smallscheme/scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,3 @@ def evalu(expr, env):
if typeof(expr) == 'list':
return eval_list(expr, env)
raise Exception('evaluation error: "%s"' % str(expr))

def inp():
if sys.version_info > (2, 9):
return input("scheme> ")
else:
return raw_input("scheme> ")

def repl():
env = Env()
while True:
try:
x = inp().strip()
except EOFError:
print()
break
if x:
try:
for parsed in parse_str(x):
pv = printable_value(evalu(parsed, env))
if pv:
print(pv)
except Exception as e:
print(e)

0 comments on commit d712bf1

Please sign in to comment.