Skip to content

Latest commit

 

History

History
118 lines (101 loc) · 2.23 KB

README.md

File metadata and controls

118 lines (101 loc) · 2.23 KB

DUSL

DUSL = Dynamic Utility And Scripting Language

(WIP) A simple scripting interpreted language written in C++, meant for utility scripts and other similar scripting needs, not meant for performance critical tasks.

Printing

println("Hello World")
print("Hello World\n")

Input

print("Enter your name: ")
name = readLine()

Data types and variable definition

a = "Hello, world" #string
b = 123 #int64
c = 1342.292 #float64
d = [1, 2, 3, "A", "B"] # list
e = {"Key1": "value1", "key2": "value2", 35: 3+5, "sub": {"sub": 1}} # dictionary

Functions

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

# Annonymous function
sub = fn(a, b) {
    return a - b
}
# Note: All functions return the last element by default same for annonymous functions

Using for-loop

# we have range its syntax is 0..100 this means 0 to 100
# we can use range for for-loop, like:

for item from 0..10 {
    println(item)
}

# or use an iterable object like string and list
value = "Hello world"
for c from value {
# loops over each charcer
    println("Character", c)
}

#list
val_list = [3, 4, 5, 6]
result = 0
for item from list {
    result = result + item
}

println("result", result)
# note "break" also exist for for-loop and while loop

Examples

# bubble sort
fn bubbleSort(list) {
    n = list.size
    swapped = true
    while swapped {
        swapped = false
        i = 0
        while i < n - 1 {
            if (list[i] > list[i + 1]) {
                temp = list[i]
                list[i] = list[i + 1]
                list[i + 1] =  temp
                swapped = true
            }
            i = i + 1
        }
    }
}

# binary search implementation
# expects a sorted list
fn binarySearch(list, target) {
    low = 0
    high = list.size - 1
    while low <= high {
        mid = floor((low + high) / 2)
        if (list[mid] == target) {
            return mid
        }
        elseif (list[mid] < target) {
            low = mid + 1
        }
        else {
            high = mid - 1
        }
    }
    
    return -1
}

More on examples directory

Build

mkdir build
cmake -DCMAKE_BUILD_TYPE=Release -S . -B build
cmake --build build --config Release