Skip to content

Samora Lang - A Simple Interpreted Programing Language just for Educational Purposes

Notifications You must be signed in to change notification settings

hc12r/Samora-LangX

 
 

Repository files navigation

Samora Lang

Samora Lang is a simple and expressive programming language designed just for fun, mostly for learning purposes.

Getting Started

To get started with Samora Lang, follow these steps:

  1. Install the Samora Lang compiler and interpreter from the binary corresponding to your OS Architecture.

Arch Linux

Using the Arch User Repository

$   yay -S samora-lang
  1. Write your Samora Lang code in a text editor or an integrated development environment (IDE).
  2. Save your Samora Lang code with a .sml extension.

Hello, World!

To familiarize yourself with Samora Lang, here's a "Hello, World!" example:

print("Hello, World!");

Read a file from the filesystem

let content = readFile("file.txt")
println(content)

Write data to a file

let data = "This is some content"
writeFile("file.txt", data)

Delete a file

deleteFile("file.txt")

Create Directory

makeDir("path")

Language Features

Samora Lang boasts the following key features:

Recursion

Samora Lang supports recursion, allowing functions to call themselves. This enables the solution of problems that can be divided into smaller, similar subproblems. Recursive algorithms and data structures can be implemented efficiently in Samora Lang.

Nested if Statements

Samora Lang allows for nested if statements, providing the ability to have conditional statements within other conditional statements. This grants developers more control over the flow of their programs by allowing multiple conditions to be evaluated in a structured and hierarchical manner.

Closures

Closures in Samora Lang enable functions to access and manipulate variables defined outside their own scope. They capture the environment in which they are created, retaining access to variables and their values. This feature allows for the creation of functions that can "remember" and operate on specific data, even after exiting their original scope.

Examples

Here are some examples to illustrate the usage of Samora Lang's features:

Recursive Factorial Function

let factorial = fn(n) {
  if (n == 0) {
    1;
  } else {
    n * factorial(n - 1);
  }
};

let result = factorial(5);
print("The factorial of 5 is: ", result);

In this example, the factorial function calculates the factorial of a given number using recursion.

Closure Example

let adder = fn(x) {
  fn(y) {
    x + y;
  };
};

let addTwo = adder(2);
let result = addTwo(3);
print("2 + 3 = ", result);

In this example, the adder function returns a closure that adds the provided value x to any value y passed to it. The closure addTwo adds 2 to its argument 3 and returns the result 5.

Single-line Comments Example

// This is a single-line comment.
# This is also a single-line comment.

Array Example