xelis-project / xelis-vm

Interpreted language developed in Rust
7 stars 2 forks source link
interpreter language lexer parser vm

XELIS VM

XVM is a virtual machine with its own interpreted language for the XELIS network developed in Rust. It supports constants, functions, while/foreach loops, arrays and structures. The syntax is strongly inspired by Rust and Golang.

All the verifications are mainly made at the level of the Parser to check the conformity of the code to be interpreted.

The different primitive types are:

File extension is .xel

Documentation

the semicolon is optional, thus can be added if desired without any difference in the code.

Recursive functions are allowed, but limited to a configurable depth.

A environment system is completely customizable to set your own native functions. This helps to manage exactly what a program can interact with. Custom structs are also available.

Numbers

An error will be returned by the interpreter if an overflow is detected without causing a panic.

Rules

Examples

let my_byte: u8 = 10
let my_u16: u16 = 70
let my_u32: u32 = 999
let my_int: u64 = 25655
let my_u128: u128 = 100_000_000L

Variable

for constant variable, it must be declared outside a function, with const keyword.

Rules

Examples

const hello: string = "hello"
...
let world: string = "world"

Casting

Values of built-in types can be casted into other built-in types easily using the keyword as.

Rules

Examples

let id: u128 = 1337
let b: u8 = id as u8
let id_str: string = id as string 

Import

Instead of having one file with all your code, you can have multiple files that will be compiled into one final program.

Rules

Examples

math namespace

import "math.xel" as math;
...
math.sum(a, b)

no namespace:

sum(a, b)

Function

entry function is a "public callable" function and must return a u64 value.

Rules

Examples

entry foo() { ... }
func foo() { ... }
func foo(): u64 { ... }
func foo(a: u64, b: u64) { ... }
func (f Foo) bar() { ... }

Structure

A structure can contain other structures.

Rules

Examples

struct MyStruct {
    message: string,
    value: u64
}

Ternary

Rules

Examples

let score: u64 = is_winner() ? 20 : 0

Negate operator

Rules

Examples

let negative: bool = !condition

Array

Rules

Examples

let array: u64[] = [10, 20, 30, 40]
...
let dim: u64[][] = [[34, 17], [8, 14], [0, 69]]

If

Rules

Examples

if condition {
    ...
}

if (i > 20 && i != 25) || i == 0 {
    ...
}

Else

Rules

Examples

else {
    ...
}

Else if

Rules

Examples

else if condition {
    ...
}

else if my_struct != null {
    ...
}

While

Rules

Examples

while condition {
    ...
}

Foreach

Rules

Examples

foreach val in values {
    ...
}

For

Rules

Examples

for i: u64 = 0; i < 10; i += 1 {
    ...
}

Break

Rules

Examples

while condition {
    if i % 10 == 0 {
        break;
    }
    ...
}

Continue

Rules

Examples

while condition {
    if i % 10 == 0 {
        continue;
    }
    ...
}

Return

Rules

Examples

func foo(): string {
    return "Hello World!"
}

func bar() {
    if condition {
        return
    }
    foo()
}

Scope

Allows you to isolate a part of the code / variables created.

Rules

Examples

{
    ...
}