Giter Site home page Giter Site logo

marethyu / peep Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 3.0 123 KB

A toy programming language, syntax based on C.

License: GNU General Public License v3.0

Python 100.00%
toy-programming-language python interpreter interpreted-programming-language cli

peep's Introduction

Peep

Overview

Peep is a toy programming language with small practical applications. This project helps me to learn more about complier and interpreter design and implementation. Peep's syntax is mostly based off C, with some minor modifications.

This repository consists of a complete compiler frontend (lexical analyzer, syntax analyzer, and semantic analyzer) and a working tree-walk interpreter.

Getting Started (for Windows users)

This project is built with Python 3.6.0 so make sure you have Python installed in your system with version 3.6.0 or greater. Also Python must be added in PATH environment variable.

After you satisfy the Python requirement, open a command prompt and type the following command to install Peep:

pip install git+https://github.com/Marethyu12/Peep.git

Then type peep, have fun!

Basic Usage

To run a Peep program, save the code in a file with .peep extension and then type the following command in the prompt (replace ____ with your file name):

peep -i ____.peep

Run with Python script:

run.py

from peep import Lexer, Parser, Interpreter

if __name__ == "__main__":
    Interpreter(Parser(Lexer("____.peep")).parse()).interpret()

To view the Peep program's abstract syntax tree (AST), run the following command:

peep -p ____.peep

It will generate a seperate file with .ast.xml extension. You can use your favourite text editor to view AST in the generated file.

Language Overview

Peep is derived from C based on its syntax. It's similar to C in most ways except that the Peep programs' entry point is a block (a code enclosed in curly braces {...}) whereas C programs have a main function as its entry point.

Built-in variable types and literals

  • int An unbounded number (ie. 1, -35, 134)

  • float An unbounded number with a decimal point (ie. 3.14, 2.71)

  • bool It's allowed to take either true or false.

  • string A sequence of characters enclosed with double quotes (ie. "foobarbaz", "#@$*", "123456")

[TODO]

See Grammar file for a complete description of Peep.

Example Programs

Hello world program

{
    print("Hello World!");
}

Generated .xml file for the program above

<?xml version="1.0" encoding="UTF-8"?>
<Program>
  <Block>
    <Block>
    </Block>
    <Print>
      <Constant type="Type.STRING" value="Hello World!"></Constant>
    </Print>
  </Block>
</Program>

Program to input and print your name

{
    string name;

    print("Enter your name:");
    scan(name);
    print("Hello...\t" + name + "!");
}

Program to calculate lowest commom multiple of two numbers

{
    int a;
    int b;
    int maxn;
    
    print("Input two numbers:");
    scan(a); scan(b);
    
    if (a > b) {
        maxn = a;
    } else {
        maxn = b;
    }
    
    int lcm = maxn;
    
    while (true) {
        if (lcm % a == 0 && lcm % b == 0) {
            break;
        }
        
        lcm += maxn;
    }
    
    print("Lowest commom multiple of two numbers are:");
    print(lcm);
}

Technical Details

[TODO]

Changes

  • 03/7/2020: Added new operators: *=, /=, %=
  • 03/7/2020: For technical reasons, do-while loop is removed from the language's grammar
  • 02/08/2020: Organized files; Packaged this repository to PyPI

Things to Add for Next Version (not necessary in order)

  • Ternary operator
  • Multiple assignment
  • Global variables
  • Command line arguments
  • Option to customize IO
  • Functions
  • More built-in functions
  • Variable references
  • Explicit type casting
  • Arrays
  • Modules
  • Classes
  • Debugger
  • Interactive mode
  • Code generation
  • Code optimization

Contributing

I appreciate any kind of contributions. It can be bug fixes, code improvements, recommendations, and etc...

License

GNU General Public License v3.0

Bugs or Feature Request?

Please email me or you can open a new issue for this repository.

peep's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.