Skip to content

Latest commit

 

History

History
116 lines (91 loc) · 4.01 KB

README.md

File metadata and controls

116 lines (91 loc) · 4.01 KB

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.