Skip to content

mdanishharoon/luma

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Programming Language Syntax Overview

Introduction

This document serves as a reference guide for the syntax and semantics of the language. The language is statically typed, functional with strong support for immutability, traits, function pointers, and heap allocation. It prioritizes readability and performance while providing a clear model for memory management.


1. Keywords

Variable Declaration

  • var - Declares a mutable variable.
  • <TYPE> - Declares an immutable variable (default).
  • h<TYPE> - Declares a heap-allocated variable (e.g., hint, hfloat).

Functions

  • func - Declares a normal stack-allocated function.
  • hfunc - Declares a heap-allocated function.

Control Flow

  • if, else
  • match, case
  • while, do while
  • for, for each

Data Structures

  • struct - Defines a structure.
  • trait - Defines an interface-like trait.
  • atom - Defines an immutable unique constant.

Miscellaneous

  • return - Returns a value from a function.
  • import - Imports external modules.

2. Operators

Arithmetic Operators

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo

Comparison Operators

Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Logical Operators

Operator Description
&& Logical AND
|| Logical OR
! Logical NOT

Pipe Operator

Operator Description
|> Passes the result of one function into another

3. Variables and Data Types

Immutable Variables (Default)

int x = 10;
str name = "Alice";

Mutable Variables

var int y = 20;
var str message = "Hello";

Heap-Allocated Variables

hint z = 50;
hstr heap_message = "Stored in heap";

4. Arrays

Array Declaration

int[] numbers = [1, 2, 3, 4, 5];

Mutable Arrays

var int[] mutableNumbers = [1, 2, 3];
mutableNumbers.append(4);

Heap-Allocated Arrays

hint[] heapNumbers = [10, 20, 30];

Array Operations

int length = numbers.len();
int first = numbers[0];
numbers.map((x) => x * 2);

5. Functions

Basic Function Syntax

func add (int x, int y) : int {
    return x + y;
}

Lambda Functions

var function double = (int x) : int => x * 2;
print(double(5)); // Outputs: 10

Returning Functions

hfunc make_multiplier (int factor) : function {
    hfunc multiplier (int x) : int {
        return x * factor;
    }
    return &multiplier;
}

6. Function Pointers

Function Pointer Syntax

hfunc square (int x) : int {
    return x * x;
}

func apply (function f, int x) : int {
    return f(x);
}

func main () {
    function ptr = &square;
    print(apply(ptr, 4)); // Outputs: 16
}

Heap Allocation for Function Pointers

hfunc higher_order () : function {
    hfunc inner (int x) : int {
        return x * 10;
    }
    return &inner;
}

7. Control Flow

If-Else Statements

if (x > 10) {
    print("Greater than 10");
} else {
    print("Less than or equal to 10");
}

Pattern Matching

match x {
    1 => print("One"),
    2 => print("Two"),
    _ => print("Other")
}

8. Loops

For Loop

for (int i in 0..10) {
    print(i);
}

While Loop

while (x < 10) {
    x = x + 1;
}

Do-While Loop

do {
    x = x + 1;
} while (x < 10);

For Each Loop

for each item in array {
    print(item);
}

9. Atoms

Defining Atoms

atom SUCCESS = :success;
atom FAILURE = :failure;

Using Atoms

match result {
    :success => print("Operation succeeded"),
    :failure => print("Operation failed"),
    _ => print("Unknown state")
}

10. Structures and Traits

Defining a Struct

struct Person {
    str name;
    int age;
}

Using a Struct

var Person p = Person("Alice", 30);
print(p.name);

Traits (Interfaces with Default Behavior)

trait Greeter {
    func greet() : str {
        return "Hello!";
    }
}

Implementing a Trait in a Struct

struct Person : Greeter {
    str name;
    int age;
}

About

Language and Compiler for Compiler Construction Project

Resources

License

Stars

1 star

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors