PistonDevelopers / dyon

A rusty dynamically typed scripting language
Apache License 2.0
1.77k stars 56 forks source link

Import functions as operators to `sym` blocks #435

Open bvssvni opened 7 years ago

bvssvni commented 7 years ago

An idea to import functions as operators, but only allowed inside a sym block. This makes it possible to experiment with syntax and create an ad-hoc DSL, without colliding with other operators or affecting how it is used in other projects.

The order of import defines the precedence.

With optional namespaces this could look like this:

use math::{add as ."+"., mul_scalar as ."*"., mul_comp as ."**".}

sym {[2] + [4, 5] * 3 + [6, 7] ** [8, 9]}

Unary and list arguments could be supported. No newline is needed after an expression.

use io::println as ">>".
use math::min as ."◁".. // Expand as array

a := 2
b := 3
c := 4
d := 5
// println(min([2, b, c]))
// println(d)
sym {>> 2 ◁ b ◁ c >> d}

One could bind variables in scope and function calls to arguments of an operator.

a := []
use std::push as (mut a)"<<"some(.)
sym {
    << 1 << 2 << 3
}
// prints `[some(1), some(2), some(3)]`
println(a)

Operators works as language sugar that rewrites meta data before passing it to lifetime/type checker and constructing the AST.

bvssvni commented 7 years ago

One problem with this approach is that you can't parse a source file with the meta syntax.

bvssvni commented 6 years ago

An alternative:

// Use `add` and `mul` from shared alias `matrix`.
sym matrix { a + b * c }