1 unstable release

0.1.0 Oct 29, 2023

#607 in Programming languages

GPL-3.0-only

15KB

Dynamik

A fast, powerful, expressive and typesafe language.

Note: The language is still WIP but its pretty functional now.

Installation:

To install dyc (dynamik compiler) make sure cargo is installed and run the following command:

$ cargo install --git https://github.com/dynamik-lang/Dynamik

And there you go!

Quick start

Printing your first hello world in Dynamik:

There's no standard library right now, so we have to use printf from libc.

File: hello_world.dy

extern "C" let printf(string, ...) -> int;

printf("Hello, World!\n");

Running your first code:

Compiling the code:

$ dyc compile hello_world.dy    # to compile
$ ./hello_world                 # to run the executable
Hello, World!

Jit executing the code:

$ dyc run hello_world.dy
Hello, World!

Language Reference

This is what the language supports so far. Note: Since the language is work in progress everything in this section is the subject to change.

Comments

In Dynamik, like any other language, comments are used to add notes or describe functionality in the code. They are not executed as part of the program, but provide valuable context and explanation to developers reading the code.

Dynamik uses single-line comments which start with --. Here's an example:

-- This is a comment in Dynamik
let a: int = 1; -- You can also write comments after code

Data Types

Dynamik supports the following data types:

  • string: A sequence of characters.
  • int: A 64-bit signed integer.
  • float: A 64-bit floating-point number.

Let

To define a variable use the let name: type = value; syntax. For example:

let a: int = 1;
let b: int = a + 10;

Basic Math Operators

Dynamik supports the following basic math operators:

Operator Description Example
+ Addition: This operator adds two numbers together. let a: int = 1 + 2; sets a to 3.
- Subtraction: This operator subtracts the second number from the first. let a: int = 5 - 2; sets a to 3.
* Multiplication: This operator multiplies two numbers together. let a: int = 2 * 3; sets a to 6.
/ Division: This operator divides the first number by the second. let a: int = 6 / 2; sets a to 3.
% Modulus: This operator returns the remainder of the first number divided by the second. let a: int = 5 % 2; sets a to 1.
! Logical NOT: This operator inverts the value of a boolean. let a: bool = !true; sets a to false.
- Negation: This operator changes the sign of a number. let a: int = -3; sets a to -3.

Functions

You can define functions in Dynamik using the following syntax:

fn print_hello() {
    printf("Hello World");
}

Functions can also accept arguments:

fn add(v1: int, v2: int) -> int {
    return v1 + v2;
}

In the above example, the add function takes two int arguments and returns an int value.

External Functions

Dynamik allows you to use external functions from the C library. To use an external function, you can declare it as follows:

extern "C" fn puts(string) -> int;
extern "C" fn printf(string, ...) -> int;

The ... at the end indicates that the function can accept a variable number of arguments, making it variadic.

If-Else Statements

Dynamik supports if-else statements for controlling program flow. For example: Note: Dynamik doesn't supports

let a: int = 3;
let b: int = a + 1;

if a != b {
    printf("A is not equal to B");
} else {
    printf("A is equal to B");
}

In the above code, the if-else statement checks whether a is equal to b or not, in each case, it prints a message.

While Statements

Dynamik also supports while loops. For example:

let x: int = 0;
while x < 5 {
    printf("%d\n", x);
    x = x + 1;
}

In the above code, the while loop prints the value of x while it is less than 5.

Modules

Dynamik allows you to create modules to organize your code and create namespaces. For example:

mod math {
    fn add(v1: int, v2: int) -> int {
        return v1 + v2;
    }
}

In this example, we create a module named math. The module contains a function add adds two values and returns the result. Using the math module, you can call the add function as follows:

printf("1 + 2 = %d\n", math::add(1, 2));

TODO

  • Add more types
  • Add pointers
  • Add fixed size arrays
  • Add structs
  • Add traits and impl blocks
  • Add dependent typing
  • Create std library
  • Work on the package manager

No runtime deps