Giter Club home page Giter Club logo

losos's Introduction

Losos

Version Python 3.11 Mypy strict Code style black

Losos is implementation of Lox language in Python 3.

Goals

  • Python implementation should be as close as possible to Java version in book (see below)

  • Losos 1.0 - working Python implementation of tree-walk interpreter

  • Losos 1.x - extend Losos so next goals are possible

  • Losos 2.0 - bytecode compiler written in Lox itself

  • Use v1.x to run v2.0 to compile v2.0 source to bytecode

  • Losos VM written in C++

Differences

  • No tool for generating syntax tree classes (tool/GenerateAst.java).
  • [expr.py] Visitor abstract class and all expression classes are not nested inside Expr: BinaryExpr instead of Expr.Binary, ExprVisitor instead of Expr.Visitor, etc.
  • [parser.py] _ParseError class is not nested inside Parser class.
  • [helpers.py] Whole new file with handy helpers.
  • [losos.py] Losos class is instantiable and all methods are non-static.
  • [losos.py] Reporter instance instead of _had_error property (see below).
  • [losos.py] main responsibility is to parse args and call run_* methods, so I made them public and moved main to __main__.py.
  • [losos.py] run_prompt method prints welcome message and will exit after CTRL+Z followed by RETURN.
  • [losos.py] error, _report, runtimeError, etc moved to Reporter (see below).
  • [reporter.py] In book, there was hadError static boolean flag inside top-level Lox class. We have instantiable Losos class, and we just cannot call static error method from it. This is minimal error reporter class. It's instance will be passed around, so classes used inside Losos (like Parser and Scanner) can report errors back. Later it may be turned into interface for swapping different error-reporting implementations. For now it's just a way to pass around basic information about encountered errors (just like mentioned flag in book).
  • Java's char type: I added class Char (in helpers.py) for type-hinting single character (see #2 for reasoning).
  • [runtimeerror.py] RuntimeError in Losos is named LososRuntimeError to avoid name collision with Python's built-in exception.
  • Possibly other minor differences.

losos's People

Contributors

acinis avatar

Watchers

 avatar

losos's Issues

Remove sys.exit() calls from losos.py

There are two places where Losos.run_file() method will call sys.exit().

Control should be passed back to main() function which in turn should return exit status code.

Only single place where sys.exit() can be used is __main__.py file, around main() function call.

scanner.py: Decide what to do with char type

Near comments in form: # TODO CHAR
In files: scanner.py, helpers.py

Python do not have separate type for single character. In original Java implementation, some methods are expecting/returning char type. Goal for Losos 1.x is to be as close to original implementation as possible.

Creating char type is not for blindly keeping implementations similar, but can allow mypy for catching more bugs.

Following class enables mypy to check if we pass around actual character (empty string as null char):

class Char(str):
    def __new__(cls, s: str) -> 'Char':
        if len(s) > 1:
            raise ValueError("Only one character or empty string")
        return super(Char, cls).__new__(cls, s)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.