Coder Social home page Coder Social logo

zunainaliazam / langx-compiler Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 14 KB

It is a compiler of our custom language LangX which is inspired from Python and C++.

License: MIT License

Python 97.99% Logos 2.01%
compiler-construction lexical-analyzer parser-generator semantic-analyzer syntax-analyzer

langx-compiler's Introduction

Compiler-Construction

Python and C++ inspire LangX.

Data Types and Variable Declaration

Variables can be declared using num dec char text bool just like in C++ we use int float char string bool respectively.

  num a = 10;
  text b = "two";
  bool = false;
  char = 'a';

Built-ins

Use show to print anything to console.

  print ("Hello World");

Conditionals

LangX has when-check-otherwise block, when block will execute if the condition is True, check block will execute if the above condition is False and check block will execute if the condition is True, otherwise block will execute if the above conditions is false.

  num a = 10;
  when (a < 20) {
    show ("a is less than 20");
  } check (a > 15) {
      print ("a is greater than 15");
  } otherwise {
      print ("a is equals to 10");
  }

Loops

Loop syntax we are using is the same as C++. iterate is used for for loop identification. After iterate we have (init; cond; update;). The block will execute as long as a specified condition evaluates to true. If the condition becomes false, the statement within the loop stops executing and control passes to the statement following the loop.

  iterate(num i=0; i < 10; i++){
    show("We are doing CC");
  }

Functions

Function can be declared using define. A function can accept 0 to n parameters separated by comma. yield keyword can be used in the body to exit from function with or without a value. You can optionally specify the return type of a function

  define isSum(a, b){
    num sum = a + b;
    when (sum == 0) {
      return "Sum is equal to Zero";
    } otherwise {
      return "Sum is greater then zero";
    }
  }
  show isSum(2,2);

Comments

While code is for computer to understand, the comments are for humans. LangX has two types of comments i.e single-line comment, starts with ? and multi-line comment, wrapped by ??...??.

  ? This is a variable
  num f = 4;
  
  ??
    This function is used to calculate age
    from date of birth.
  ??
  define calculate(dob) {
    ...
  }

Data Structures

Arrays

The array data structure is vital in any language and LangX supports 1D array. Arrays can be defined by using array keyword with the dimension and collection of values inside of the brackets separated by commas. Arrays can be multi-dimensional as well.

  array myArray[1] = [1, 2.3, 3.9, "Hellow World"];

Dictionary

The dictionary data structure is vital in any language and LangX supports dict. Dict can be defined in the same way we do it in Python.

  dict myDict = {1: "Naseem", 2: "King"}

Configuration

Make .env file in the main directory of the project and give the following paths.

INPUT_FILE_PATH = ''
OUTPUT_FILE_PATH = ''

Install the dotenv package.

$ pip install dotenv

Collaborators

The following collaborators developed this project:

Contributions

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

langx-compiler's People

Contributors

zunainaliazam avatar

Stargazers

 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.