Skip to content

chompaa/dom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

93 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dom

A scripting language written in Rust

Playground

playground

You can try Dom for yourself using the playground here.

Features

  • Comments
  • Types
    • Booleans
    • Integers
    • Floats
    • Strings
    • Lists
  • Variables
    • Mutable
    • Constant
  • Comparisons
  • Unary expressions
  • Binary expressions
  • Scope
  • Functions
    • Defined
    • Built-in
  • Loops
  • Control flow
    • Conditional statements
      • Single conditions
      • Multiple conditions
    • Return
    • Continue
    • Break

Syntax

Comments

Commenting is done with the // characters:

// Hello, world!

They are not parsed in any manner.

Comparison

Numerical comparison uses the usual operators:

let foo = 1
let bar = 2

print(foo <= bar) // true
print(foo >= bar) // false
print(foo != bar) // true
print(foo == bar) // false

Likewise, for binary comparisons:

let foo = true
let bar = false

print(foo && bar) // false
print(foo || bar) // true

Arithmetic

Arithmetic can be performed as you would expect. For example:

(2 + 2) * (2 / 2) - 2

Outputs 2. Operations follow the usual order of operations.

Currently supported operations are:

  • Addition +
  • Subtraction -
  • Multiplication *
  • Division /

Variables

Variables can be set using the let keyword as follows:

let foo = 1

They are always mutable.

Conditionals

Conditional statements can be formed using the if keyword:

let foo = 1
let bar = 2

if foo < bar {
    print("`foo` is less than `bar`")
}

Lists

Lists can be created using brackets [..]:

let list = [0, "foo", 1, "bar"]
print(get(list, 1)) // "foo"

There are built-in functions for working with lists: get, set, push, pop, and len.

Functions

Functions are defined using the fn keyword as follows:

fn sum(a, b) {
    a + b
}

Unless the return keyword is used, they return the last evaluated expression. They are called as you may expect:

sum(1, 1)

Arguments are always passed by value, for now.

Dom has support for pipes, which let you pass the result of one function onto the next. For example:

["foo"]
|> push("bar")
|> print()

will output ["foo", "bar"].

Dom also contains some built-in functions, which can be seen below:

Note

These functions don't produce errors right now, i.e. for incorrect arguments or runtime errors.

Function Arguments Description
print Any Outputs a literal to the console
input None Requests and returns input from the console. Not supported in dom_wasm
get List, Int Gets an item at a specified index from a List
set List, Int, Any Sets an item at a specified index in a List
push List, Any Pushes an item to the end of a List
pop List, Int Pops an item at a specified index in a List
len List, Int Returns the length of a List

Loops

Loops are defined using the loop keyword, and use break and continue for control flow:

let foo = 0
let bar = 2

loop {
    foo = foo + 1
    if foo == bar {
        // Exit this iteration 
        continue
    }
    print(foo)
    if foo > 10 {
        // Exit the loop
        break
    }
}

Running locally

Make sure you have the Rust toolchain installed.

  • Clone this repository and navigate to it:
git clone https://github.com/chompaa/dom && cd dom
  • To start the interactive shell:
cargo run -p dom_cli
  • To interpret a file:
cargo run -p dom_cli <file>