Skip to content

harishsg99/Scoop-lang

Repository files navigation

Scoop-lang

A security focused programing language

Proposed features of scooplang

  1. variable bindings
  2. integers and booleans
  3. arithmetic expressions
  4. built-in functions
  5. first-class and higher-order functions
  6. closures
  7. a string data structure
  8. an array data structure
  9. a hash data structure
  10. VM for Scoop lang
  11. IDE for Scoop lang

Currently implemented

  1. extendable lexer
  2. variable bindings
  3. extentable parser

TODOs

  1. evaluation
  2. Adding more Features like loops , functions and data structures
  3. VM for Scoop lang
  4. IDE for Scoop lang

To Get Started

  1. Install go binary for macos , linux or windows
  2. excute go run main.go

Example code of ScoopLang

let fibonacci = fn(x) 
{ 
if (x == 0) {
return 0;
} else
{
if (x == 1) 
{
return 1;
} 
else {
fibonacci(x - 1) + fibonacci(x - 2);
} 
}
}; 
fibonacci(15);

Arithmetic expressions

You can do usual arithmetic operations against numbers, such as +, -, * and /.

>> let a = 10;
>> let b = a * 2;
>> (a + b) / 2 - 3;
12
>> let c = 2.5;
>> b + c
22.5

If expressions

You can use if and else keywords for conditional expressions. The last value in an executed block are returned from the expression.

>> let a = 10;
>> let b = a * 2;
>> let c = if (b > a) { 99 } else { 100 };
>> c
99

Functions and closures

You can define functions using fn keyword. All functions are closures in Scooplang and you must use let along with fn to bind a closure to a variable. Closures enclose an environment where they are defined, and are evaluated in the environment when called. The last value in an executed function body are returned as a return value.

>> let multiply = fn(x, y) { x * y };
>> multiply(50 / 2, 1 * 2)
50
>> fn(x) { x + 10 }(10)
20
>> let newAdder = fn(x) { fn(y) { x + y }; };
>> let addTwo = newAdder(2);
>> addTwo(3);
5
>> let sub = fn(a, b) { a - b };
>> let applyFunc = fn(a, b, func) { func(a, b) };
>> applyFunc(10, 2, sub);
8

Strings

You can build strings using a pair of double quotes "". Strings are immutable values just like numbers. You can concatenate strings with + operator.

>> let makeGreeter = fn(greeting) { fn(name) { greeting + " " + name + "!" } };
>> let hello = makeGreeter("Hello");
>> hello("John");
Hello John!

Arrays

You can build arrays using square brackets []. Arrays can contain any type of values, such as integers, strings, even arrays and functions (closures). To get an element at an index from an array, use array[index] syntax.

>> let myArray = ["Thorsten", "Ball", 28, fn(x) { x * x }];
>> myArray[0]
Thorsten
>> myArray[4 - 2]
28
>> myArray[3](2);
4

Hash tables

You can build hash tables using curly brackets {}. Hash literals are {key1: value1, key2: value2, ...}. You can use numbers, strings and booleans as keys, and any type of objects as values. To get a value of a key from a hash table, use hash[key] syntax.

>> let myHash = {"name": "Jimmy", "age": 72, true: "yes, a boolean", 99: "correct, an integer"};
>> myHash["name"]
Jimmy
>> myHash["age"]
72
>> myHash[true]
yes, a boolean
>> myHash[99]
correct, an integer

Built-in functions

There are many built-in functions in Monkey, for example len(), first() and last(). Special function, quote, returns an unevaluated code block (think it as an AST). Opposite function to quote, unquote, evaluates code inside quote.

>> len("hello");
5
>> len("");
3
>> let myArray = ["one", "two", "three"];
>> len(myArray)
3
>> first(myArray)
one
>> rest(myArray)
[two, three]
>> last(myArray)
three
>> push(myArray, "four")
[one, two, three, four]
>> puts("Hello World")
Hello World
nil
>> quote(2 + 2)
Quote((2 + 2)) # Unevaluated code
>> quote(unquote(1 + 2))
Quote(3)

Macros

You can define macros using macro keyword. Note that macro definitions must return Quote objects generated from quote function.

# Define `unless` macro which does the opposite to `if`
>> let unless = macro(condition, consequence, alternative) {
     quote(
       if (!(unquote(condition))) {
         unquote(consequence);
       } else {
         unquote(alternative);
       }
     );
   };
>> unless(10 > 5, puts("not greater"), puts("greater"));
greater
nil

Currently This Language has support for only Mac OS(Catalina , Mojave) and linux

For Windows support will be added soon

About

A security focused programming language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published