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 com…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by starevan

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)

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]
}

When Clause

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

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"

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"
    }
}

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()

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!")
}

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"]
}()


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by starevan


Print Share Comment Cite Upload Translate Updates
APA

starevan | Sciencx (2023-01-05T19:49:44+00:00) I imagined a programming language Silk. Retrieved from https://www.scien.cx/2023/01/05/i-imagined-a-programming-language-silk/

MLA
" » I imagined a programming language Silk." starevan | Sciencx - Thursday January 5, 2023, https://www.scien.cx/2023/01/05/i-imagined-a-programming-language-silk/
HARVARD
starevan | Sciencx Thursday January 5, 2023 » I imagined a programming language Silk., viewed ,<https://www.scien.cx/2023/01/05/i-imagined-a-programming-language-silk/>
VANCOUVER
starevan | Sciencx - » I imagined a programming language Silk. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/01/05/i-imagined-a-programming-language-silk/
CHICAGO
" » I imagined a programming language Silk." starevan | Sciencx - Accessed . https://www.scien.cx/2023/01/05/i-imagined-a-programming-language-silk/
IEEE
" » I imagined a programming language Silk." starevan | Sciencx [Online]. Available: https://www.scien.cx/2023/01/05/i-imagined-a-programming-language-silk/. [Accessed: ]
rf:citation
» I imagined a programming language Silk | starevan | Sciencx | https://www.scien.cx/2023/01/05/i-imagined-a-programming-language-silk/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.