diff --git a/docs/syntax_cheat_sheet.md b/docs/syntax_cheat_sheet.md index 1f3b118..5f7cf3d 100644 --- a/docs/syntax_cheat_sheet.md +++ b/docs/syntax_cheat_sheet.md @@ -62,6 +62,9 @@ * this is why `& = ` is the way it is. + `*** = ` * same as before, but performs dereferences: `&&&&int` becomes `&int` (minus 3 references because 3 `*`s), so a value of type `int` can be assigned to it. +- destructuring + + values can be destructured into tuples or lists (as of right now): + + `[a, b] := [10, "some text"]` is effectively the same as `a := 10, b := "some text"` ## Statements @@ -89,26 +92,26 @@ + if the statement returns a value that matches, the loop will end and return the matched value + the loop's return type is the match of the return type of the statement - for loop - + `for ` + + `for ` + in each iteration, the variable will be initialized with a value from the iterator. + iterators can be lists, tuples, or functions. + for function iterators, as long as the returned value matches, the matched value will be used. if the value doesn't match, the loop ends. + if the statement returns a value that matches, the loop will end and return the matched value + the loop's return type is the match of the return type of the statement or `[]` (if the loop ends because the iterator ended) - switch - + `switch { <...> }` - * where `` is ` ` - * if the variable's value is of type ``, `` will be executed. + + `switch { <...> }` + * where `` is ` ` + * if the variable's value is of type ``, `` will be executed with `` assigned to ``. * if the variables type is included multiple times, only the first match will be executed. * within the statement of an arm, the variables type is that specified in ``, and not its original type (which may be too broad to work with) + `switch! { <...> }` * same as above, but all types the variable could have must be covered * the additional `[]` in the return type isn't added here since it is impossible not to run the statement of one of the arms. - match - + `match { <...> }` - * where `` is ` ` - * each arm has a condition statement an an action statement. - * if the value returned by the condition statement matches, the matched value is assigned to the variable and the action statement is executed. + + `match { <...> }` + * where `` is `<(condition) statement> <(action) statement>` + * each arm has a condition statement, something that the matched value will be assigned to, and an action statement. + * if the value returned by the condition statement matches, the matched value is assigned to `` and the action statement is executed. * only the first matching arm will be executed. if no arm was executed, `[]` is returned. - fixed-indexing + `.n` where `n` is a fixed number (not a variable, just `0`, `1`, `2`, ...) diff --git a/index.html b/index.html index 9f69743..e470ff2 100644 --- a/index.html +++ b/index.html @@ -9,7 +9,7 @@

-fn get_number_input(question string) {
println(question)
input := read_line()
// try to parse to an int, then a float.
in := match input {
input.parse_int() input
input.parse_float() input
}
// 'in' has type int/float/[] because of the match statement
switch! in {
int/float in
// replace [] with an appropriate error before returning
[] Err: "input was not a number."
}
// return type is int/float/Err(string)
}

answer := get_number_input("What is your favorite number?")

answer.debug() // type: int/float/Err(string)
// switch can be used to branch based on a variables type.
// switch! indicates that every possible type must be handled.
switch! answer {
int {
println("Entered an integer")
answer.debug() // type: int
}
float {
println("Entered a decimal number")
answer.debug() // type: float
}
Err(string) println("Input was not a number!")
}

// wait one second
sleep(1)


// function that returns an anonymous function (function object).
// anonymous functions can be used as iterators in for-loops.
fn square_numbers() {
i := 0
() {
&i = i + 1
i * i
}
}

for num square_numbers() {
println(num.to_string())
// once num is greater than 60, the loop stops.
num.gt(50)
}
+fn get_number_input(question string) {
println(question)
input := read_line()
// try to parse to an int, then a float.
in := match {
input.parse_int() n n
input.parse_float() n n
}
// 'in' has type int/float/[] because of the match statement
switch! in {
int/float in in
// replace [] with an appropriate error before returning
[] [] Err: "input was not a number."
}
// return type is int/float/Err(string)
}

answer := get_number_input("What is your favorite number?")

answer.debug() // type: int/float/Err(string)
// switch can be used to branch based on a variables type.
// switch! indicates that every possible type must be handled.
switch! answer {
int num {
println("Entered an integer")
num.debug() // type: int
}
float num {
println("Entered a decimal number")
num.debug() // type: float
}
Err(string) [] println("Input was not a number!")
}

// wait one second
sleep(1)


// function that returns an anonymous function (function object).
// anonymous functions can be used as iterators in for-loops.
fn square_numbers() {
i := 0
() {
&i = i + 1
i * i
}
}

for num square_numbers() {
println(num.to_string())
// once num is greater than 60, the loop stops.
num.gt(50)
}