repl.py

#

Simple REPL functionality for pylisp.

import traceback
import sys
from pylisp.environment import standard_env, Environment, Expr
from pylisp.eval import (
    eval_sexp,
)
from pylisp.parse import parse_string
#

The main method. Input is any string and an environment to run in.

def main(inp: str, env: Environment) -> Expr:
#
    try:
        parsed_string = parse_string(inp)
        res = eval_sexp(parsed_string, env=env)
        return res
    except Exception as exception:
        print(traceback.format_exc())
        return str(exception)
#

Run a simple REPL.

def repl() -> None:
#
    import cmd
#

Subclass of cmd.Cmd. Makes a simple read-eval loop.

    class CmdL(cmd.Cmd):
#
        intro = "Welcome to pylisp."
        prompt = ">"

        env = standard_env()

        should_exit = False
#

Print the current environment.

        def do_env(self, _: str) -> None:
#
            print(self.env)
#
        def default(self, line: str) -> None:
            res = main(line, self.env)
            print(res)
#
        def postcmd(self, _: bool, line: str) -> bool:
            return line == ":quit"

    CmdL().cmdloop()
#
def plsp() -> None:
    if sys.stdin.isatty():
        repl()
    else:
        input_string = sys.stdin.read()
        res = main(input_string, standard_env())
        print(res)


if __name__ == "__main__":
    repl()