Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Monadish" refactoring of interpreter #721

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

"Monadish" refactoring of interpreter #721

wants to merge 1 commit into from

Commits on Aug 23, 2024

  1. This commit does not introduce new functionality to the interpreter. …

    …It only refactors the interpreter to be able to change the semantics or add additional functionality to it.
    
    At first, I was trying to do a monad-style interpreter, but TypeScript does not have do-notation, which can quickly produce unreadable code because of the bind functions.
    Although, there are some suggestions over the internet on how to emulate do-notation using generators, it looks ad-hoc, hacky, and unnatural.
    So, I decided to take the idea of monad and adapt it into an imperative flavor.
    The idea is to define the semantics of an interpreter as an abstract class parametric over two generic types: "V" (the type of expressions' results) and
    "S" (the type of statements' results).
    At the moment, module level declarations do not have an associated generic type, but it will probably be added.
    
    Specific semantics can be represented by implementing the abstract class.
    For example, to create an interpreter with the standard semantics, just write:
    
    const interpreter = new Interpreter(new StandardSemantics(ctx));
    
    If we want to change the behavior, for example, by collecting special values when statements execute, just pass another semantics:
    
    const interpreter = new Interpreter(new YourCustomSemantics(ctx));
    
    This is a first version of the "monadish" interpreter. As such, it probably will suffer changes as more semantics are added since
    they will probably force changes to the interface in the abstract class.
    jeshecdom committed Aug 23, 2024
    Configuration menu
    Copy the full SHA
    114aa88 View commit details
    Browse the repository at this point in the history