I imagined a programming language Silk
Tags: javascript, github, opensource, programming
Published: 2023-01-05 19:49:44

> 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](https://github.com/kunieone/silk) 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

```java
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

```java
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

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

## For & Loop & while

```java
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!

```java
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

```java
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

```java

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

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