DEV Community

starevan
starevan

Posted on

I imagined a programming language Silk

For a long time, I was curious about programming languages, and wanted to make a language that had many syntax sugars and was smooth and fun for developers to use. I think it's time to show you the grammar of the language I have in mind for you to comment on. I hope that the language, called Silk, will be refined and implemented in the comments of many dear members of the community.

So far, I've pretty much completed the first step of the compiler, the lexical analysis of the Silk language using Rust language development. I wasn't sure about the details of the syntax until I designed the parsing. But there is a general direction, I hope that users give valuable advice.
Silk on github If you have the insterest in Silk and ,If you want to develop Silk with me, you can contact me at Github ~ Thanks for the support for open source projects! email: zstarcc@gmail.com

concise variable definition

a = [1,2,3,4,5,6] //array
b = |"function","good"| //tuple
c = Set("good","bad","brilliant") //set
d = *{a:[1,3,4],b:*{c:"hello"}} //literal object(box in Silk)
Enter fullscreen mode Exit fullscreen mode

humanized function features:

  1. Type constraints in parameter parentheses do not require colons.
  2. Use the arrow (- >) instead of the return keyword.
  3. Return a value type constraint using a colon You can assign a function using the assignment notation
  4. You can not return more than one value, but you can return a tuple.
  5. Functions can be taken as arguments or returned. The function is a first-class citizen. In the same position as any variable
f add(a int,b int) :int { ->a+b }

f add(a int,b int) :int  ->a+b



f fib(n int) :int {
    dp:int[] = [-1]  
    for i each 1 to n {
        if i <= 2 {dp[i] = i} els {
            dp[i] = dp[i-1]+dp[i-2]
        }
    }
    -> dp[n]
}
Enter fullscreen mode Exit fullscreen mode

When Clause

//switch
j='x'
when j {
    'x'{
            log("j is x !")
    }
    'y'{
            log("j is y !")
    }
    _ {/*... other condition*/}
}
Enter fullscreen mode Exit fullscreen mode

For & Loop & while

animal_list = [ "tiger","duck","cat","dog","lion","flamingo" ]
// normal for
for i=0;i<animal_list.len();i++{
    log(e)
}
// for of
for e of animal_list{ //traverse element in the literator
    log(e)
}
//for in
for i in animal_list{ //traverse i in the literator
    log(i)
}
for each
for i,e each animal_list{
    log(i)
}

animal_list.for_each (i,e){
    log(e)
}
animal_list.for_each((i,e){
    log(e)
})
// Why is this possible? Because for_each has the ability "ParamWithoutParen"

Enter fullscreen mode Exit fullscreen mode

Support for a silky, composition-oriented programming experience: the box and ability block!

ability Drivable{
    f drive():str
}

box Car {
    make:str
    model:str,
    year:UInt32
}

box Engine {
    horsepower:UInt32
    fuel_type:str
}

box CarWithEngine :: Drivable {
    car:Car = Car()
    engine:Engine = Engine()
    drive_history: str[] = []

    f drive():str {
    -> "Driving a {car.year} {car.make} {car.model} with a {car.engine.horsepower} horsepower {car.engine.fuel_type} engine"
    }
}

box EngineCar mixin (car)Car+(engine)Engine {
    drive_history: str[] = []
    f drive():str {
    -> "Driving a {car.year} {car.make} {car.model} with a {car.engine.horsepower} horsepower {car.engine.fuel_type} engine"
    }
}
Enter fullscreen mode Exit fullscreen mode

box Type

box Bird {
    name = "Polly"
    animal_type = "parrot"
    eat = -> "Polly is eating."
  cry = -> "Polly is making a sound."
    fly = -> "Polly is flying."

}
bird1 = Bird()
log(is_of(bird1)) // Bird
bird.fly()
bird.cry()
Enter fullscreen mode Exit fullscreen mode

Can support duck model


ability Fly() {
    f fly()
}

box Duck :: Fly {
    f fly(){
        log("Duck is flying")
    }
}
box Penguin :: Fly {
    f fly(){
        log("Penguin is flying (with help from the wind)")
    }
}


f fly_then_swim(flyer :: Fly) { //// ability bond
    flyer.fly()
    log("Swimming!")
}
Enter fullscreen mode Exit fullscreen mode

First Class Function

// This is a scope, which you can also think of as a "block of code", or an immediate execution function
arr = {
    -> [1,2,3,"value"]
}
/* Similiar in Javascript:
arr = (()=>{
    return [1,2,3,"value"]
})()
*/
// equals to:
arr = (){
 -> [1,2,3,"value"]
}()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)