ShravScript

Introduction to ShravScript

ShravScript is a custom programming language implemented in Python that combines the best features of Python and JavaScript. It uses curly braces for blocks (like JavaScript) while maintaining a clean and readable syntax inspired by Python.

Key features include:

  • Clean and consistent syntax
  • First-class functions and lambdas
  • Built-in modules for common tasks
  • Interactive REPL for experimenting

Installation

To install ShravScript, follow these steps:

  1. Ensure you have Python 3.6+ installed on your system
  2. Clone the ShravScript repository from GitHub or download the zip file from the download page
  3. Install the required dependencies:
    pip install requests

To test your installation, try running an example:

python main.py examples/hello_world.shs

Basic Syntax

ShravScript uses curly braces to define blocks of code:


# This is a comment
let name = "World"
if name == "World" {
    print("Hello, World!")
} else {
    print("Hello, " + name)
}
                

Variables

Variables are declared using the let keyword:


let age = 25
let name = "John"
let is_active = true
                

Operators

ShravScript supports the standard arithmetic, comparison, and logical operators:


# Arithmetic
let sum = 5 + 3
let difference = 10 - 4
let product = 6 * 7
let quotient = 15 / 3
let exponent = 2 ** 3  # 8

# Comparison
let is_equal = x == y
let is_not_equal = x != y
let is_greater = x > y
let is_less_or_equal = x <= y

# Logical
let both_true = condition1 and condition2
let either_true = condition1 or condition2
let is_not_true = not condition
                

Data Types

ShravScript supports several built-in data types:

Primitive Types

  • Number (both integers and floating-point)
  • String
  • Boolean
  • Null

Collections


# Lists
let numbers = [1, 2, 3, 4, 5]
let first_number = numbers[0]

# Dictionaries
let person = {
    name: "Alice",
    age: 30,
    city: "New York"
}
let person_name = person.name
                

Control Flow

Conditionals


if x > 10 {
    print("x is greater than 10")
} elif x > 5 {
    print("x is greater than 5 but not greater than 10")
} else {
    print("x is 5 or less")
}
                

Loops


# While loop
let i = 0
while i < 5 {
    print(i)
    i = i + 1
}

# For loop
for j in 0..5 {
    print(j)
}
                

Functions

Functions are defined using the fn keyword:


fn greet(name) {
    return "Hello, " + name
}

print(greet("Alice"))  # Outputs: Hello, Alice
                

Returning Values

Functions can return values using the return statement:


fn add(a, b) {
    return a + b
}

let sum = add(5, 3)  # 8
                

Modules & Libraries

ShravScript includes several built-in modules:

fileio

For file operations:


import "fileio"

# Write to a file
fileio.write("output.txt", "Hello, World!")

# Read from a file
let content = fileio.read("output.txt")
print(content)
                

netgear

For network operations:


import "netgear"

# Make a GET request
let response = netgear.get("https://example.com")
print(response)
                

mathex

For mathematical operations:


import "mathex"

let root = mathex.sqrt(16)    # 4
let power = mathex.pow(2, 3)  # 8
let random = mathex.random()  # Random value between 0 and 1
                

sysops

For system operations:


import "sysops"

let files = sysops.listdir(".")
print(files)

let home_dir = sysops.getenv("HOME")
print(home_dir)
                

Using the REPL

ShravScript includes an interactive REPL (Read-Eval-Print Loop) that allows you to experiment with the language:

python main.py --repl

In the REPL, you can enter ShravScript code and see the results immediately:


shs> let x = 10
shs> let y = 20
shs> x + y
30
shs> exit()