GithubHelp home page GithubHelp logo

gotoc / hython Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mattgreen/hython

0.0 1.0 0.0 523 KB

Haskell-powered Python 3 interpreter

License: GNU General Public License v3.0

Makefile 0.22% Python 19.95% Haskell 63.13% Yacc 16.70%

hython's Introduction

#hython

A toy Python 3 interpreter implemented in Haskell.

Introduction

I wanted to learn Haskell, and I wanted a big project, so I decided to write a Python 3 interpreter. The result was extremely educational and easily the coolest project I've ever worked on. Because it's implemented in a naive fashion, it won't ever be a replacement for real Python implementations.

Note: Hython only implements most of the Python3 language. It doesn't contain much of a standard library, which is a big part of what makes Python pleasant to use. Adding all of the necessary machinery needed for the existing Python 3 standard library to function is an enormous undertaking that I'm not interested in.

Status

It's finally done! Or at least, I'm declaring it that way.

Features

  • Lexer
  • Parser
  • Most built-in data types, including int, bool, string, list and dict
  • Common unary and binary operators on common data types
  • A few built-in functions, including print
  • Variable assignment and lookup, with support for nonlocal and global keywords
  • Conditional expressions with if and else
  • All loop constructs: for and while with support for break and continue within them
  • Support for the with statement
  • Destructuring ((a,b) = [1,2])
  • Functions, including nested functions, default parameters, and keyword parameters
  • Splat (* and **) operators in caller argument lists
  • Lambda expressions, with proper environment capture
  • Classes, including inheritance and proper method resolution order
  • Objects
  • Exception handling via try, with support for handlers, frame unwinding, finally handlers, and else, along with some built-in exception classes
  • Basic support for loading modules with the import statement
  • Simple REPL
  • Support for the is operator
  • Support for generators and yield
  • Support for decorators / metaclasses

Code Metrics

sloccount output as of 2/17/16:

Totals grouped by language (dominant language first):
haskell:       2084 (78.02%)
yacc:           587 (21.98%) # Parser

Examples

See the test directory for example code that works

Building and running

  1. Install Stack

  2. Clone the repository:

     $ git clone https://github.com/mattgreen/hython.git
     $ cd hython
    
  3. Build:

     $ make
    
  4. Run a file:

     $ ./hython test/fib.py
    

REPL

Hython includes a simple REPL, which you can play around with:

$ ./hython

Test Suite

Hython's test suite is rather simple: it ensures the output of Hython matches that of the system's python3 for each test file.

To run the automated test suite:

$ make test

Reference Information

hython's People

Contributors

mattgreen avatar penberg avatar

Watchers

 avatar

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.