Skip to content

Daksh14/dyna-lang

Repository files navigation

Run

Install rust https://www.rust-lang.org/tools/install then clone this repository then run

cargo r

Dyna Language Documentation

Table of Contents

  1. Overview
  2. Variables and Immutability
  3. Control Flow and Pattern Matching
  4. Functions
  5. Arrays
  6. Type System and Type Checking
  7. Examples

1. Overview

Dyna is a language inspired by Rust, focusing on immutability, strict typing, and pattern matching. This documentation outlines the syntax, core concepts, and usage patterns of Dyna, guiding developers through its functionality.

2. Variables and Immutability

In Dyna, variables are immutable by default. Variables are declared using the let keyword. To make a variable mutable, add the mut keyword.

Syntax

let variable_name = String::new();       // Immutable variable
let mut variable_mutable = String::from("Hello"); // Mutable variable

Note

The final expression in a block does not require a semicolon:

let variable = if true {
    String::new()
} else {
    String::from("false")
};

3. Control Flow and Pattern Matching

Dyna includes pattern matching and supports user-defined types such as enumerations (enum) and structs.

Syntax

enum T {
    Some(String),
    None,
}

let var = T::None;

match var {
    T::Some(string) => println!(string),
    T::None => (),
}

4. Functions

Functions in Dyna are declared with the fn keyword. Each parameter requires an explicit type.

Syntax

fn name_of_function(mut arg: String) -> String {
    arg.push_str("value");
    arg
}

Return Types

Functions can specify a return type using ->. The default return type is () (unit type).

5. Arrays

Dyna supports compile-time arrays, requiring explicit types since type inference is not supported.

Syntax

let array: &[u8] = [1, 2, 3, 4];

6. Type System and Type Checking

Dyna performs compile-time type checking to prevent mismatched types. While Dyna does not yet support advanced type theories, concrete type matching ensures type consistency.

Example of Type Checking

fn name_of_function(mut arg: String) -> String {
    arg.push_str("value");
    arg
}

let array: &[u8] = [1, 2, 3, 4];
// Error: Expected `String`, found `&[u8]`
name_of_function(array);

7. Examples

Below are several examples demonstrating Dyna’s features.

Example 1: Immutable and Mutable Variables

let x = 5;       // Immutable
let mut y = 10;  // Mutable
y += x;

Example 2: Pattern Matching with Enums

enum Option {
    Some(i32),
    None,
}

let value = Option::Some(10);

match value {
    Option::Some(num) => println("Value"),
    Option::None => println("No value"),
}

Example 3: Function with Type Error

fn add_prefix(mut text: String) -> String {
    text.push_str("prefix_");
    text
}

let arr: &[u8] = [1, 2, 3];
// Error: Expected `String`, found `&[u8]`
add_prefix(arr);

About

This is the dyna lang repository

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published