Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/syedzawwarahmed/lavascript
Our very own programming language.
https://github.com/syedzawwarahmed/lavascript
compiler intermediate-code-generation lexical-analysis parsing python semantic-analysis
Last synced: about 2 months ago
JSON representation
Our very own programming language.
- Host: GitHub
- URL: https://github.com/syedzawwarahmed/lavascript
- Owner: SyedZawwarAhmed
- Created: 2023-07-25T16:49:06.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-12T16:24:14.000Z (about 1 year ago)
- Last Synced: 2023-12-12T18:41:37.400Z (about 1 year ago)
- Topics: compiler, intermediate-code-generation, lexical-analysis, parsing, python, semantic-analysis
- Language: Python
- Homepage:
- Size: 169 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
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
andstatic
.
dynamic variables are changeable while static variable is unchangeable likeconst
.```
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 istrue
,else
block will execute if the above conditions isfalse
. 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 becomesfalse
, statement within the loop stops executing and control passes to the statement following the loop. Useexit
to break the loop andskip
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 whilefire
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 theEmployee
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 theextends
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