Skip to content

BRCode4Fun/Python-Interpreter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Logo

Python Interpreter

A custom-built interpreter designed from the ground up to execute Python code. It also serves as an educational resource, promoting learning and encouraging contributions to the open-source Python ecosystem.

Project Overview

Welcome to this Python Interpreter in C++ project! This project aims to provide a robust and feature-rich interpreter for the Python programming language, built entirely from scratch in C++. Whether you're looking to explore the internals of how an interpreter works, experiment with language features, or simply explore the intersection of C++ and Python, this interpreter is a powerful tool designed with flexibility and extensibility in mind.

🔑 Key Features

🔧 Support for Python's Built-in Data Types

This interpreter already supports various built-in Python data types, enabling the manipulation of variables, strings, numbers, and more, similarly to the original Python.

🧠 Dynamic Typing

Just like Python, this interpreter supports dynamic typing, allowing variables to change types at runtime. This feature provides flexibility and mimics Python’s behavior, making it easier to write and execute dynamic code.

🗂️ Classes and Instances

Support for Object-Oriented Programming is implemented! You can define your own classes, create instances, and access attributes and methods just as you would in Python.

Operator Overloading

The interpreter supports operator overloading, allowing you to define specific behaviors for operators like +, -, *, /, and more, within your custom classes.

📜 Execution System and Parser

The architecture of the interpreter includes a robust parser and execution system that translates Python code into executable operations. This allows for dynamic interpretation and real-time code execution.

♻️ Garbage Collector

To manage memory efficiently, the project includes an integrated garbage collector, which helps prevent memory leaks by automatically freeing unused objects.

What This Interpreter Can Do

To illustrate the capabilities of this interpreter, consider the following Python code that demonstrates complex object-oriented features such as class definitions, method overloading, and recursive function calls. The interpreter is designed to handle these operations with precision, just as the standard Python interpreter would.

class Number:
    def __init__(self, n):
        self._n = n
        
    def __add__(self, other):
        return Number(self._n + other._n)
    
    def __sub__(self, other):
        return Number(self._n - other._n)
    
    def __eq__(self, other):
        return self._n == other._n
    
    def __le__(self, other):
        return self._n <= other._n
    
    def get_value(self):
        return self._n

def fib(n):
    if n <= Number(1):
        return Number(1)
    else:
        return fib(n - Number(1)) + fib(n - Number(2))

def main():
    
    print(fib(Number(5)).get_value()) # 8

main()

🛠️ Building

$ git clone https://github.com/BRCode4Fun/Python-Interpreter
$ cd Python-Interpreter
$ make

✅ Running Tests

  $ make test

🚀 Future steps:

  • List, Set, Dict.
  • BigInt class with Karatsuba algorithm.
  • REPL mode.
  • Virtual Machine.
  • Better type checking and error handling.
  • Modules.
  • Iterator and generator objects.
  • Inheritance.
  • etc.

🤝 Contributions

This project is under continuous development, with new features being added regularly. If you’re passionate about Python, C++, or just interested in contributing to an exciting open-source project, feel free to jump in!

How to Contribute

  • Fork this repository.
  • Create a new branch for your feature (git checkout -b feature/NewFeature).
  • Commit your changes (git commit -m 'Add NewFeature').
  • Push to the branch (git push origin feature/NewFeature).
  • Open a Pull Request.

Any form of contribution is welcome, whether it’s code, documentation, or testing. Additionally, feel free to share this project with friends, colleagues, or within developer communities.