From 8b14a005ab0ef8d629d7aaaa528a66e7e524c9b5 Mon Sep 17 00:00:00 2001 From: lilac Date: Tue, 22 Oct 2024 23:45:17 +0800 Subject: [PATCH] Add some ideas of language syntax --- notes.md | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/notes.md b/notes.md index 6f3b4a2..133101a 100644 --- a/notes.md +++ b/notes.md @@ -69,6 +69,78 @@ That's why we chose Golang as the target platform. - Extension method - Elegant exception/error handling +## Syntax + +- Expression as Statement (do statement) + - ~~Add a semicolon `;` to the end of an expression, to make it a statement~~. + - Example + ```ocaml + do println "ab" + (* equivalent to this statement. *) + val _ = println "ab" + ``` +- Let-end expression + - Make the `in` clause of `let-in-end` expression optional. When the `in` clause is skipped, the result of the expression is `()`. + - Example + ```ocaml + let + val a = 1 + do println a + val b = "done" + do println b + end + ``` +- For expression + - The `for` expression is like the [for comprehension of Elixir](https://elixir-lang.org/getting-started/comprehensions.html). + - It supports 2 forms. + - `for in yield ` + - `for in ` + - Examples + ```ocaml + val lines = for x in [1, 2, 3] yield string x + (* A complex comprehension *) + val nums = for i in [1, 2, 3] do + if i mod 2 = 0 then yield i + else yield i * 2 + end + ``` +- Block expression + - In procedural programming style, block (of statements) is the building blocks of program. Though many functional programming languages do not support it, we think it's essential for practical programming. + - A block contains a sequence of expressions, that are executed one by one. + - Depending on the context, a block may contain control functions like `break`, `continue`, or `yield`. + - Example + ```ocaml + fun log x y = do + x := 1; + println "x = $x"; + println "y = $y" + end + ``` +- While expression + - A procedural style loop. + - Example + ```ocaml + let + val a = 3 + in + while i < 3 do + println i + end; + a + end + ``` +- For loop + - The procedural version of for expression + - Example + ```ocaml + for i in [1, 2, 3] do + if i mod 2 = 0 then println i + end + ``` +- Try expression +- Defer expression +- Raise/throw expression + ## Thoughts - Should we implement break/continue/return statements via escape continuation?