{"slug": "lithp-py-2008", "title": "Lithp.py (~2008)", "summary": "Lithp.py, a Lisp interpreter for John McCarthy's original Lisp, was released around 2008 by developer Fogus. The interpreter, written in Python 2.6.1+, includes core functions like car, cdr, cons, and lambda, and provides a REPL environment. It is available at fogus.me/fun/lithp/.", "body_md": "## lithp.py |\n|\n|---|---|\n|\nLithp - A interpreter for John McCarthy's original Lisp. The heavily documented code for It wasn't enough to write the Lisp interpreter -- I also wanted to share what I learned with\nThe Lithp interpreter requires Python 2.6.1+ to function.\nplease add comments, report errors, annecdotes, etc. to the |\n\n``` python\nimport pdb\nimport getopt, sys, io\nfrom env import Environment\nfrom fun import Function\nfrom atom import TRUE\nfrom atom import FALSE\nfrom lisp import Lisp\nfrom reader import Reader\nfrom error import Error\nfrom fun import Lambda\nfrom fun import Closure\n\nNAME = \"Lithp\"\nVERSION = \"v1.1\"\nWWW = \"http://fogus.me/fun/lithp/\"\nPROMPT = \"lithp\"\nDEPTH_MARK = \".\"\n```\n\n |\n|\nThe Lithper class is the interpreter driver. It does the following: |\n\n```\nclass Lithp(Lisp):\n```\n\n |\n|\n\n``` python\n    def __init__( self):\n        iostreams=(sys.stdin, sys.stdout, sys.stderr)\n        (self.stdin, self.stdout, self.stderr) = iostreams\n\n        self.debug = False\n        self.verbose = True\n        self.core = True\n        self.closures = True\n\n        self.rdr = Reader()\n        self.environment = Environment()\n\n        self.init()\n```\n\n |\n|\n\n``` python\n    def init(self):\n```\n\n |\n|\n|\nDefine core functions |\n\n```\n        self.environment.set(\"eq\",     Function(self.eq))\n        self.environment.set(\"quote\",  Function(self.quote))\n        self.environment.set(\"car\",    Function(self.car))\n        self.environment.set(\"cdr\",    Function(self.cdr))\n        self.environment.set(\"cons\",   Function(self.cons))\n        self.environment.set(\"atom\",   Function(self.atom))\n        self.environment.set(\"cond\",   Function(self.cond))\n```\n\n |\n|\nDefine utility function |\n\n```\n        self.environment.set(\"print\",  Function( self.println))\n```\n\n |\n|\nSpecial forms |\n\n```\n        self.environment.set(\"lambda\", Function(self.lambda_))\n        self.environment.set(\"label\",  Function(self.label))\n```\n\n |\n|\nDefine core symbols |\n\n```\n        self.environment.set(\"t\", TRUE)\n```\n\n |\n|\nThere is one empty list, and it's named |\n\n```\n        self.environment.set(\"nil\", FALSE)\n```\n\n |\n|\nDefine meta-elements |\n\n```\n        self.environment.set(\"__lithp__\",  self)\n        self.environment.set(\"__global__\", self.environment)\n```\n\n |\n|\n\n``` python\n    def usage(self):\n        self.print_banner()\n        print\n        print NAME.lower(), \" <options> [lithp files]\\n\"\n```\n\n |\n|\n|\n\n``` python\n    def print_banner(self):\n        print \"The\", NAME, \"programming shell\", VERSION\n        print \"   by Fogus,\", WWW\n        print \"   Type :help for more information\"\n        print\n```\n\n |\n|\n|\n\n``` python\n    def print_help(self):\n        print \"Help for Lithp v\", VERSION\n        print \"  Type :help for more information\"\n        print \"  Type :env to see the bindings in the current environment\"\n        print \"  Type :load followed by one or more filenames to load source files\"\n        print \"  Type :quit to exit the interpreter\"\n```\n\n |\n|\n|\n\n``` python\n    def push(self, env=None):\n        if env:\n            self.environment = self.environment.push(env)\n        else:\n            self.environment = self.environment.push()\n```\n\n |\n|\n|\n\n``` python\n    def pop(self):\n        self.environment = self.environment.pop()\n```\n\n |\n|\n|\n\n``` python\n    def repl(self):\n        while True:\n```\n\n |\n|\n|\nStealing the s-expression parsing approach from |\n\n```\n            source = self.get_complete_command()\n```\n\n |\n|\nCheck for any REPL directives |\n\n```\n            if source in [\":quit\"]:\n                break\n            elif source in [\":help\"]:\n                self.print_help()\n            elif source.startswith(\":load\"):\n                files = source.split(\" \")[1:]\n                self.process_files(files)\n            elif source in [\":env\"]:\n                print(self.environment)\n            else:\n                self.process(source)\n```\n\n |\n|\nSource is processed one s-expression at a time. |\n\n``` python\n    def process(self, source):\n        sexpr = self.rdr.get_sexpr(source)\n\n        while sexpr:\n            result = None\n\n            try:\n                result = self.eval(sexpr)\n            except Error as err:\n                print(err)\n\n            if self.verbose:\n                self.stdout.write(\"    %s\\n\" % result)\n\n            sexpr = self.rdr.get_sexpr()\n```\n\n |\n|\nIn the process of living my life I had always heard that closures and dynamic scope cannot co-exist. As a thought-experiment I can visualize why this is the case. That is, while a closure captures the contextual binding of a variable, lookups in dynamic scoping occur on the dynamic stack. This means that you may be able to close over a variable as long as it's unique, but the moment someone else defines a variable of the same name and attempt to look up the closed variable will resolve to the top-most binding on the dynamic stack. This assumes the the lookup occurs before the variable of the same name is popped. While this is conceptually easy to grasp, I still wanted to see what would happen in practice -- and it wasn't pretty. |\n\n``` python\n    def lambda_(self, env, args):\n        if self.environment != env.get(\"__global__\") and self.closures:\n            return Closure(env, args[0], args[1:])\n        else:\n            return Lambda(args[0], args[1:])\n```\n\n |\n|\nDelegate evaluation to the form. |\n\n``` python\n    def eval(self, sexpr):\n        try:\n            return sexpr.eval(self.environment)\n        except ValueError as err:\n            print(err)\n            return FALSE\n```\n\n |\n|\nA complete command is defined as a complete s-expression. Simply put, this would be any atom or any list with a balanced set of parentheses. |\n\n``` python\n    def get_complete_command(self, line=\"\", depth=0):\n        if line != \"\":\n            line = line + \" \"\n\n        if self.environment.level != 0:\n            prompt = PROMPT + \" %i%s \" % (self.environment.level, DEPTH_MARK * (depth+1))\n        else:\n            if depth == 0:\n                prompt = PROMPT + \"> \"\n            else:\n                prompt = PROMPT + \"%s \" % (DEPTH_MARK * (depth+1))\n\n            line = line + self.read_line(prompt)\n```\n\n |\n|\nUsed to balance the parens |\n\n```\n            balance = 0\n            for ch in line:\n                if ch == \"(\":\n```\n\n |\n|\nThis is not perfect, but will do for now |\n\n```\n                    balance = balance + 1\n                elif ch == \")\":\n```\n\n |\n|\nToo many right parens is a problem |\n\n```\n                    balance = balance - 1\n            if balance > 0:\n```\n\n |\n|\nBalanced parens gives zero |\n\n```\n                return self.get_complete_command( line, depth+1)\n            elif balance < 0:\n                raise ValueError(\"Invalid paren pattern\")\n            else:\n                return line\n```\n\n |\n|\n\n``` python\n    def read_line( self, prompt) :\n        if prompt and self.verbose:\n            self.stdout.write(\"%s\" % prompt)\n            self.stdout.flush()\n\n        line = self.stdin.readline()\n\n        if(len(line) == 0):\n            return \"EOF\"\n\n        if line[-1] == \"\\n\":\n            line = line[:-1]\n\n        return line\n```\n\n |\n|\n|\nLithp also processes files using the reader plumbing. |\n\n``` python\n    def process_files(self, files):\n        self.verbose = False\n\n        for filename in files:\n            infile = open( filename, 'r')\n            self.stdin = infile\n\n            source = self.get_complete_command()\n            while(source not in [\"EOF\"]):\n                self.process(source)\n\n                source = self.get_complete_command()\n\n            infile.close()\n        self.stdin = sys.stdin\n\n        self.verbose = True\n\nif __name__ == '__main__':\n    lithp = Lithp()\n\n    try:\n        opts, files = getopt.getopt(sys.argv[1:], \"hd\", [\"help\", \"debug\", \"no-core\", \"no-closures\"])\n    except getopt.GetoptError as err:\n```\n\n |\n|\nPrint help information and exit: |\n\n```\n        print(str( err)) # will print something like \"option -a not recognized\"\n        lithp.usage()\n        sys.exit(1)\n\n    for opt,arg in opts:\n        if opt in (\"--help\", \"-h\"):\n            lithp.usage()\n            sys.exit(0)\n        elif opt in (\"--debug\", \"-d\"):\n            lithp.verbose = True\n        elif opt in (\"--no-core\"):\n            lithp.core = False\n        elif opt in (\"--no-closures\"):\n            lithp.closures = False\n        else:\n            print(\"unknown option \" + opt)\n```\n\n |\n|\nProcess the core lisp functions, if applicable |\n\n```\n    if lithp.core:\n        lithp.process_files([\"../core.lisp\"])\n\n    if len(files) > 0:\n        lithp.process_files(files)\n\n    lithp.print_banner()\n    lithp.repl()\n```\n\n |\n## References |\n|\n|\n|", "url": "https://wpnews.pro/news/lithp-py-2008", "canonical_source": "https://fogus.me/fun/lithp/", "published_at": "2026-06-21 23:23:07+00:00", "updated_at": "2026-06-24 06:43:06.099919+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "developer-tools"], "entities": ["John McCarthy", "Fogus", "Lithp", "Python"], "alternates": {"html": "https://wpnews.pro/news/lithp-py-2008", "markdown": "https://wpnews.pro/news/lithp-py-2008.md", "text": "https://wpnews.pro/news/lithp-py-2008.txt", "jsonld": "https://wpnews.pro/news/lithp-py-2008.jsonld"}}