Skip to content

Commit ec4c747

Browse files
author
Joel Abrahams
committed
doc: proposal new syntax
Blocks are now a litertal 'list of statements'. This means we use square instead of curly brackets to denote them.
1 parent da991ff commit ec4c747

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ def factorial(x: Int) -> Int := match x
7979
n => n * factorial(n - 1)
8080
8181
def num := input("Compute factorial: ")
82-
if num.is_digit() then
82+
if num.is_digit() then [
8383
def result := factorial(Int(num))
8484
print("Factorial {num} is: {result}.")
85-
else
85+
] else
8686
print("Input was not an integer.")
8787
```
8888

@@ -94,10 +94,11 @@ _Note_ One could use [dynamic programming](https://en.wikipedia.org/wiki/Dynamic
9494
```mamba
9595
def factorial(x: Int) -> Int := match x
9696
0 => 1
97-
n =>
98-
def ans := 1
99-
for i in 1 ..= n do ans := ans * i
100-
ans
97+
n => [
98+
def ans := 1
99+
for i in 1 ..= n do ans := ans * i
100+
ans
101+
]
101102
```
102103

103104
### 📋 Types, Classes, and Mutability
@@ -120,9 +121,10 @@ class MyServer(def ip_address: IPv4Address)
120121
def last_sent(fin self) -> Str ! ServerError :=
121122
self._last_message
122123
123-
def connect(self) :=
124+
def connect(self) := [
124125
self.is_connected := True
125126
print(always_the_same_message)
127+
]
126128
127129
def send(self, message: Str) ! ServerError :=
128130
if self.is_connected then
@@ -271,11 +273,12 @@ Immutable variables and pure functions make it easier to write declarative progr
271273
def fin taylor := 7
272274
273275
# the sin function is pure, its output depends solely on the input
274-
def pure sin(x: Int) :=
276+
def pure sin(x: Int) := [
275277
def ans := x
276278
for i in 1 ..= taylor .. 2 do
277279
ans := ans + (x ^ (i + 2)) / (factorial (i + 2))
278280
ans
281+
]
279282
```
280283

281284
### ⚠ Error handling
@@ -311,12 +314,14 @@ This is shown below:
311314

312315
```mamba
313316
def a := function_may_throw_err()
314-
err: MyErr :=
317+
err: MyErr => [
315318
print("We have a problem: {err.message}.")
316319
return # we return, halting execution
317-
err: MyOtherErr :=
320+
]
321+
err: MyOtherErr => [
318322
print("We have another problem: {err.message}.")
319323
0 # ... or we assign default value 0 to a
324+
]
320325
321326
print("a has value {a}.")
322327
```

docs/spec/characters.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Symbol | Use
1515
`{` | Denote start of set, set constructor, or map
1616
`}` | Denote end of set, set constructor, or map
1717
`[` | Denote start of list, or opening bracket of generics of a class
18+
| Also denotes the start of a list of statements and/or expressions
1819
`]` | Denote end of list, or closing bracket of generics of a class
20+
| Also denotes the end of a list of statements and/or expressions
1921

2022
## Type
2123

docs/spec/grammar.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ The grammar of the language in Extended Backus-Naur Form (EBNF).
1111
- ```{ ... }``` = zero or more
1212

1313
```ebnf
14-
file ::= block
14+
file ::= { expr-or-stmt }
1515
import ::= [ "from" id ] "import" id { "," id } [ as id { "," id } ]
1616
17-
type-def ::= "type" type [ ":" type ] ( newline block | "when" [ conditions ] )
18-
conditions ::= ( newline indent { condition newline } dedent | condition )
17+
type-def ::= "type" type [ ":" type ] ( code-block | "when" [ conditions ] )
18+
conditions ::= "[" condition { newline condition } "]" | condition
1919
condition ::= expression [ "!" expression ]
2020
type-tuple ::= "(" [ type ] { "," type } ")"
2121
22-
class ::= "class" id [ fun-args ] [ ":" ( type | type-tuple ) ] ( newline block )
22+
class ::= "class" id [ fun-args ] [ ":" ( type | type-tuple ) ] ( code-block )
2323
generics ::= "[" id { "," id } "]"
2424
2525
id ::= { character }
@@ -28,8 +28,6 @@ The grammar of the language in Extended Backus-Naur Form (EBNF).
2828
type ::= ( id [ generics ] | type-tuple ) [ "->" type ]
2929
type-tuple ::= "(" [ type { "," type } ] ")"
3030
31-
block ::= indent { expr-or-stmt } dedent
32-
3331
expr-or-stmt ::= ( statement | expression ) [ comment ]
3432
statement ::= control-flow-stmt
3533
| definition
@@ -46,7 +44,7 @@ The grammar of the language in Extended Backus-Naur Form (EBNF).
4644
| "return" [ expression ]
4745
| expression "as" id
4846
| control-flow-expr
49-
| newline block
47+
| code-block
5048
| collection
5149
| index
5250
| key-value
@@ -74,10 +72,10 @@ The grammar of the language in Extended Backus-Naur Form (EBNF).
7472
7573
variable-def ::= [ "fin" ] ( id-maybe-type | collection ) [ ":=" expression ] [ forward ]
7674
operator-def ::= [ "pure" ] overridable-op [ "(" [ id-maybe-type ] ")" ] "->" type
77-
[ ":=" ( expr-or-stmt | newline block ) ]
75+
[ ":=" ( expr-or-stmt | code-block ) ]
7876
7977
fun-def ::= [ "pure" ] id fun-args [ "->" type ] [ raise ]
80-
[ ":=" ( expr-or-stmt | newline block ) ]
78+
[ ":=" ( expr-or-stmt | code-block ) ]
8179
fun-args ::= "(" [ fun-arg ] { "," fun-arg } ")"
8280
fun-arg ::= id-maybe-type [ ":=" expression ]
8381
forward ::= "forward" id { "," id }
@@ -108,18 +106,20 @@ The grammar of the language in Extended Backus-Naur Form (EBNF).
108106
e-notation ::= ( integer | real ) "E" [ "-" ] integer
109107
string ::= """ { character } """
110108
111-
newline-block ::= newline block | expr-or-stmt
109+
code-block ::= "[" expr-or-statement "]"
110+
| expr-or-stmt
111+
| "[" newline expr-or-statement { newline expr-or-statement } "]"
112112
one-or-more-expr ::= expression { "," expression }
113113
114114
control-flow-expr::= if | match
115-
if ::= "if" one-or-more-expr "then" newline-block [ "else" newline-block ]
115+
if ::= "if" one-or-more-expr "then" code-block [ "else" code-block ]
116116
match ::= "match" one-or-more-expr "with" newline match-cases
117-
match-cases ::= indent { match-case { newline } } dedent
117+
match-cases ::= match-case | "[" match-case { newline match-case } "]"
118118
match-case ::= expression "=>" expr-or-stmt
119119
120120
control-flow-stmt::= while | foreach | "break" | "continue"
121-
while ::= "while" one-or-more-expr "do" newline-block
122-
foreach ::= "for" one-or-more-expr "in" expression "do" newline-block
121+
while ::= "while" one-or-more-expr "do" code-block
122+
foreach ::= "for" one-or-more-expr "in" expression "do" code-block
123123
124124
newline ::= newline-char
125125
newline-char ::= \n | \r\n

0 commit comments

Comments
 (0)