Skip to content

kmr-ankitt/S-lang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slangBanner

S-lang

S-lang is a Dynamically typed Language Object Oriented programming language designed for simplicity and ease of use.

Why

S-lang is designed in such a way that it is easy to learn and use for Gen-z newbies to programming world.

I know It's a weird descision to build a TypeScript based Interpreter. But ok.

Prerequisites

Installation

  1. Clone S-lang offical repository:
git clone https://github.com/kmr-ankitt/S-lang.git 
cd S-lang
  1. Install S-lang dependencies:
npm install
  1. Run S-lang REPL:
npm run slang

You can also run S-lang through Makefile:

make slang
  1. Compile and run .sx file :
npm run slang FILE_DIRECTORY/FILE_NAME.sx

S-lang Documentation

File extension

S-lang supports only .sx file extension. example: main.sx

Hello, world!

A simple hello world program in S-lang:

echo "Hello, World!";

Semi-colons at the end of every line is mandatory in S-lang.

Datatypes

S-lang has following datatypes

Numbers

There can be number literals which can be both integers and floating point numbers.

examples: 1, 2.5, 9

Strings

These are string literals defined inside "

examples: "S-lang", "Strings are easy"

Booleans

These are boolean literals which can be either real or noi.

examples: real, noi

Null Type

S-lang has null datatype. It can be defined using the void keyword. All uninitialized variables are given the value of void.

examples: void

Operators

S-lang has following operators:

Assignment

= - equals

Unary operators

- - Unary negation

Logical operators

nd - logical AND

or - logical OR

! - logical NOT

Arithmetic operators

+ - sum

- - difference

* - product

/ - division

% - mod

Comparison operators

== - is equals

!= - is not equals

> - is less than

>= - is less than or equals

> - is greater than

>= - is greater than or equals

Comments

S-lang support both single line and multi line comment.

  • Single line comment
// This is a single line comment.
// The Lexer completely ignores any line starting with // 
  • Multiline comment
/*This is a mulit line comment in S-lang similar to that of C.*/

Variables

let num = 15; // number
let name = "radiohead"; // string
let goat = real; // booleans
let noob = noi; // booleans
let nullable = void; // null type

Logical operators

!real;  // false.
!noi; // true.
real nd noi; // false.
real nd real;  // true.
noi or noi; // false.
real or noi;  // true.

Control flow

let num = 15;

iffy(num > 0){
    echo "num is positive";
}oof {
    echo "num is negative";
}

Loops

roll(let i = 0; i < 10; i = i + 1){
    echo i;
}

let num = 1;
loop(num > 0){
    echo num;
    num = num - 1;
}

Functions

fun add(a, b){
    return a + b;
}

echo add(5, 10);

Closures

fun addPair(a, b) {
return a + b;
}

fun identity(a) {
return a;
}

echo identity(addPair)(1, 2); // Prints "3".

fun makeCounter(){
    let c = 0;
    fun counter(){
        c = c + 1;
        echo c;
    }
    return counter;
}

let counter1 = makeCounter();
let counter2 = makeCounter();

counter1(); // 1
counter2(); // 1

Classes

S-lang has classes. It's a group of functions that can be used to create objects.

  • keywords: fam for class, here for current context or this keyword in other langs.
fam Bacon {
  eat() {
    echo "Crunch crunch crunch!";
  }
}

Bacon().eat(); // Prints "Crunch crunch crunch!".

fam Thing {
  getCallback() {
    fun localFunction() {
      echo this;
    }

    return localFunction;
  }
}

let callback = Thing().getCallback();
callback();