GithubHelp home page GithubHelp logo

plc-calculator's Introduction

Compiler Starter Project

This is the starter project for the Programming Language and Compiler course @ AIT. Since 2024, we have used Python.

Dependencies

  • Python version 3.9.18
  • sly as a submodule link
  • PyQt6 for GUI development

To use

I designed this project to run on a Docker container. However, if you are not a Docker enthusiast like me, you can still run this project locally.

Run on Docker (preferred)

Once you install Docker in your system, you can do the following.

  1. (Optional) Click Use this template on the top right of this page to clone this to your repository.
  2. Clone the project to your local machine.
  3. Clone submodule with this git submodule update --init --recursive.
  4. (Optional) Install X11Client if you want to use GUI in docker.
    • For Windows: X410 is the best but you will need to pay.
    • For Mac: XQuartz is the one I used.
    • For Linux/Ubuntu: You can simply map DISPLAY. No additional app is needed.
  5. Run docker compose up -d --build to build and run the container.
  6. Use VSCode to dev the project remotely.

Run on Local Machine

This might not work because I have never tried this on my machine. But, generally, it should be as follows.

  1. (Optional) Click Use this template on the top right of this page to clone this to your repository.
  2. Clone the project to your local machine.
  3. Clone submodule with this git submodule update --init --recursive.
  4. Set PYTHONPATH to include /path/to/sly.
  5. Run pipenv install inside src/.
  6. You might also need to install PyQt6 separately.

Code Explain

Inside src/ folder is all the code developed.

src/
  |- components/
      |- ast/
          |- statement.py
      |- lexica.py
      |- main.ui
      |- memory.py
      |- parsers.py
  |- main.py
  |- Pipfile
  |- Pipfile.lock

Since the project is done just to showcase libraries and techniques, here we divided it into subsections to explain the code.

components/lexica.py

This file showcases the Lexica analyzer component. It has a MyLexer class that extends sly.Lexer. It will translate a code/string into token stream/generator that feeds to a Parser. This file has a main just for testing the class.

components/parsers.py

There are two parsers. (1) MyParser and (2) ASTParser.

MyParser class

This class is what I call immediate evaluation which each of the semantics, once reduced, evaluates/calculates right away. This type of parser is fine for calculator projects or simple parsing. This parser also implements Memory and Variable assignment.

ASTParser class

This is a more complex but flexible way of parsing. AST (Abstract Syntax Trees) is actually a parse tree. This will allow you to control when to run a subsection of code like if-else statement. You can see that the semantic part is only creating an object inside components.ast. All the logic (in this case, addition and subtraction) is in the AST object. The Parser is there is create a parse tree that once ready will execute .run(). I only add the essentials to demonstrate this technique.

components/memory.py

This contains Memory class which is a singleton. Inside is just a simple dictionary where variable_name is a key and {'value':value,'data_type':<type>} as a value. Whether this solution is appropriate or not is your judgment.

main.py and components/main.ui

Finally, the main.py is the main file to run the entire project. It will render a GUI from components/main.ui that was designed from PyQt6. This shows how to bind a function with a button and how to display the result back to the GUI.

Design a GUI

We use PyQt6 and qt designer 6 for GUI. You can start to learn this tool from here.

To launch QT designer, use pipenv run pyqt6-tools designer and to open the existing UI use pipenv run pyqt6-tools designer <path/to/file.ui>

plc-calculator's People

Contributors

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