reuelrds / SchemeInterpreter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Scheme4101 Interpreter

Scheme4101 is a Scheme interpreter implemented in Python. This interpreter provides functionality to process Scheme code, breaking it down into tokens, parsing these tokens to form an abstract syntax tree (AST), and then evaluating this tree according to the semantics of the Scheme language.

Run the project

To run the interpreter, execute:

python3 Scheme4101.py

For debugging purposes, especially related to the Scanner, use:

python3 Scheme4101.py -d

Features

Core Functionalities:

  • Tokenization: The interpreter can break down raw Scheme input into individual tokens, distinguishing between numbers, identifiers, special characters, and other lexical elements.

  • Parsing: Based on a defined grammar, the interpreter constructs an abstract syntax tree (AST) that represents the structure and semantics of the input Scheme code.

  • Evaluation: The interpreter can evaluate the constructed AST according to the semantics of the Scheme language. This includes performing arithmetic operations, function calls, and evaluating special forms.

  • Environment Management: Variables and functions in Scheme can be defined, updated, and looked up. The interpreter manages an environment to handle these bindings.

Special Forms Handling:

  • Define: Users can define new variables or functions.
  • Lambda: Supports the creation of anonymous functions.
  • Quote: Allows users to quote expressions, preventing their evaluation.
  • Built-in Functions: The interpreter supports several built-in functions of Scheme, providing native functionalities without requiring external definitions.

Advanced Features:

  • Closures: The interpreter can create and manage closures, allowing for functionalities like higher-order functions and maintaining local state within functions.

  • Error Handling: While not explicitly mentioned in the provided code snippets, a robust interpreter typically has mechanisms to handle syntactic and semantic errors, providing meaningful feedback to the user.

Output and Debugging:

  • Structured Output: The interpreter can print the evaluated output in a structured and readable format, making it user-friendly.

  • Debugging Mode: A debugging mode is available to get insights into the tokenization process, which can be invaluable for understanding how the interpreter processes input.

Initialization and Bootstrapping:

  • Initialization File: The interpreter uses an initialization file (ini.scm) to preload certain definitions, ensuring that built-in functions and other essential elements are available right from the start making it easier to develop the interpreter.

Project Notes

Followed the suggestion to split implementation of BuiltIn.appy method into three separate methods: apply method with no parameter, apply method with a single parameter, and apply method with two parameters. Also implemented helper methods for checking the Node types and arithmetic operations.

For the display function, the project used redirect_stdout from the contextlib library to capture the stdout buffer before it is flushed to the terminal and used regular expression to remove the double quotes from the string.

Also implemented the Unspecific Node type and the Void Node type to print #{Unspecific} and nothing respectively as suggested.

Main Components and Functionalities

Parsing and Lexical Analysis

  • Parser (Parse/Parser.py):
    This module is responsible for reading Scheme input and breaking it down according to a set grammar defined using BNF notation.

  • Scanner (Parse/Scanner.py):
    The Scanner is used to read the Scheme input character by character. It identifies different lexical elements like numbers, identifiers, and special characters.

Tokenization

  • Token (Tokens/Token.py):
    Each instance of this class represents a token identified by the Scanner.

  • TokenType (Tokens/TokenType.py):
    This module defines an enumeration of all possible token types that the Scanner can identify.

Abstract Syntax Tree (AST)

  • Node (Tree/Node.py):
    This is a generic class that represents a node in the AST.

  • TreeBuilder (Tree/TreeBuilder.py):
    This module provides functionality to construct specific types of nodes for the AST.

  • Environment (Tree/Environment.py):
    In Scheme, variables can be bound to values. The Environment module provides functionality to manage these bindings.

  • BuiltIn (Tree/BuiltIn.py):
    Scheme has several built-in functions. This module is responsible for representing these functions within the interpreter.

Special Forms

  • Special (Special/Special.py):
    Base class for all special forms in Scheme.

  • Define (Special/Define.py):
    This module is specifically for the define special form in Scheme.

  • Lambda (Special/Lambda.py):
    The lambda special form in Scheme is used to create anonymous functions.

  • Quote (Special/Quote.py):
    The quote special form in Scheme is used to prevent evaluation of an expression.

Printing Mechanism

  • Printer (Print/Printer.py):
    The Printer module provides methods to print the AST, taking care of formatting details like indentation and line breaks.

About


Languages

Language:Python 95.2%Language:Scheme 4.5%Language:Makefile 0.4%