Skip to content

SyedZawwarAhmed/LavaScript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

LavaScript

LavaScript is a language inspired by JavaScript.

Group Members:

Sheikh Abdullah
Syed Zawwar Ahmed
Muhammad Annas Baig

Language Specification

Variables

Variables can be declared using dynamic and static. dynamic variables are changeable while static variable is unchangeable like const.

  dynamic a = 10;
  static b = "two";
  a = false;
  a = a + 3;

Operators

LavaScript has 2 types of operators.

Mathematical Operators:

+, -, *, /, ^, %

Logical Operators:

==, !=, >, <, >=, <=, &&, ||

Data Types

We have 3 datatypes in LavaScript. Namely: string, number and boolean.

  dynamic a = 10;
  dynamic b = 2 - 3;
  dynamic c = "Sheikh Abdullah";
  static d = true;
  dynamic e = false && true;

Built-ins

Use log to print anything to console.

  log "Hello World";
  static foo = "I'm a Ubitian";
  log foo;

Conditionals

LavaScript supports if-else construct, if block will execute if condition is true, else block will execute if the above conditions is false. if-else blocks can be nested.

  dynamic = 10;
  if (a < 20) {
    log "a is less than 20";
  } else {
    if (a > 20) {
      log "a is greater than 20";
    } else {
      log "a is equals to 20";
    }
  }

Loops

Statements inside until blocks are executed as long as a specified condition evaluates to true. If the condition becomes false, statement within the loop stops executing and control passes to the statement following the loop. Use exit to break the loop and skip to continue within loop.

  dynamic i = 0;
  until (i < 10) {
    log "Let's study Automata";
    if (i == 5) {
      exit;
    } else {
      skip;
    }
    i = i + 1;
  }

Functions

Function can be decalared using proc. A function can accept 0 to n parameters separated by comma. return 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

  proc isEven(a, b): string {
    static rem = a % 2;
    if (rem == 0) {
      return "Number is even";
    } else {
      return "Number is odd";
    }
  }

  log isEven(23);

Comments

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

? This is a variable
static f = 4;

@
  This function is used to calculate age
  from date of birth.
@
proc calcAge(dob) {
  ...
}

Data Structures

Arrays

LavaScript supports the array data structure. Arrays can be defined by using brackets [] with the collection of values inside of the brackets separated by commas. Arrays can be multi-dimensional as well.

dynamic myArray = [1, 3.8, "Hello"];

dynamic multiArray = [
  [
    ['a', 'b', 'c'],
    ['A', 'B', 'C']
  ],
  [1, 2, 3]
];

Object Oriented Programing (OOP)

LavaScript also supports Object Oriented Programming. One can create classes of their own and then create instances namely objects of the classes created.

Classes

Classes can be created using class keyword.

class Employee {
  ...
}

Public attributes can be added by writing their identifier in the class definition. Private attributes can also be added by writing a # followed by their identifier in the class definition.

class Employee {
  name;
  #SSN;
}

In the above code block, the Employee class has two attributes, name is a public attribute, SSN is a private attribute.

A constructor is a function which is called at the time of creation of an object of the class. The constructor method can be added by using the constructor keyword.

class Employee {
  name;
  #SSN;

  constructor(name, SSN) {
    this.name = name;
    this.SSN = SSN;
  }
}

The this keyword refers to the current instace of the class.

Furthermore, simple methods can also be introduced by defining a function in the class definition with the method keyword. Again, class methods can also be made private by adding a # before the identifier.

class Employee {
  name;
  #SSN;
  #salary;

  constructor(name, SSN, salary) {
    this.name = name;
    this.SSN = SSN;
    this.salary = salary;
  }

  method #fire() {
    log "Good bye! You've been promoted to a customer.";
  }

  method evalutate(performance) {
    if (performance == 'good') {
      this.salary = this.salary + 10000;
    } else {
      this.fire();
    }
  }
}

In this example, evaluate is a public method while fire is a private method. Note that private methods can only be called inside of a class.

Objects

Objects are instances of any LavaScript class. They can be created by using the init keyword followed by the name of the class, and passing the respective arguements as the parameters of the class constructor.

dynamic employee1 = init Employee("Sheikh Abdullah", 123456789, 250000);

In the above line of code, an instance of the class Employee is created and then assigned to a dynamic variable employee 1.

The object attributes and methods can be accessed using the dot(.) operator.

log employee1.name;

This line of code will dislay the name of the employee1 object in the terminal, in this case "Sheikh Abdullah".

employee1.evaluate("good");

This will call the evaluate method of the Employee class for this object.

Inheritance

In LavaScript, it is possible to inherit attributes and methods from one class to another. To inherit from a class, use the extends keyword.

class SoftwareEngineer extends Employee {
  level;

  constructor(level, name, SSN) {
    this.level = level;
    super(name, SSN, this.level * 75000);
  }
}

static emp = init SoftwareEngineer(3, "Sheikh Abdullah", 123456789, 74000);
log emp.name; // prints: Sheikh Abdullah

The super keyword is a reference variable which is used to refer immediate parent class object.

If you don't want a class to be extended, you can mark it with sealed keyword.

sealed class Employee {
  ...
}

Interfaces

Interface is a blueprint of a class. It is used to achieve abstraction in LavaScript. In interfaces you can only define the method headers. The body will be described by the class that implements it.

interface IShape {
    method GetArea(): number;
}

class Rectangle implements IShape {
    #length;
    #breadth;

    method GetArea(): number {
      return this.length * this.breadth;
    }
}

Multi-Inheritance is supported in LavaScript through the use of interfaces.

interface IShape {
    method GetArea(): number;
}

interface IColor {
    method GetColor(): string;
}

class Rectangle implements IShape, IColor {
    #length;
    #breadth;
    #color;

    method GetArea(): number {
      return this.length * this.breadth;
    }

    method GetColor(): string {
      return this.color;
    }
}

Classification of Keywords

The keywords can be classified as follows:

  • Initializers
    • static
    • dynamic
  • Special Identifiers
    • log
  • init
  • if
  • else
  • until
  • proc
  • return
  • class
  • method
  • this
  • interface
  • extends
  • implements
  • super
  • sealed