Skip to content

Commit cc77386

Browse files
committed
Finished all syntax file
1 parent 46e6284 commit cc77386

File tree

2 files changed

+74
-33
lines changed

2 files changed

+74
-33
lines changed

examples/AllSyntax/main.roc

Lines changed: 71 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
app [main!] { cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.19.0/Hj-J_zxz7V9YurCSTFcFdu6cQJie4guzsPMUi5kBYUk.tar.br" }
1+
app [main!] { cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.20.0/X73hGh05nNTkDHU06FHC0YfFaQB1pimX7gncRcao5mU.tar.br" }
22

33
import cli.Stdout
44
import cli.Arg exposing [Arg]
@@ -27,18 +27,18 @@ number_operators = |a, b|
2727
gt: a > b,
2828
gteq: a >= b,
2929
# unary operators
30-
neg: -a, # the last item can have a comma too
30+
neg: -a,
31+
# the last item can have a comma too
3132
}
3233

3334
boolean_operators : Bool, Bool -> _
34-
boolean_operators = |a, b|
35-
{
36-
bool_and: a && b,
37-
bool_and_keyword: a and b,
38-
bool_or: a || b,
39-
bool_or_keyword: a or b,
40-
not_a: !a
41-
}
35+
boolean_operators = |a, b| {
36+
bool_and: a && b,
37+
bool_and_keyword: a and b,
38+
bool_or: a || b,
39+
bool_or_keyword: a or b,
40+
not_a: !a,
41+
}
4242

4343
pizza_operator : Str, Str -> Str
4444
pizza_operator = |str_a, str_b|
@@ -49,52 +49,58 @@ patterns = |lst|
4949
when lst is
5050
[1, 2, ..] ->
5151
42
52+
5253
[2, .., 1] ->
5354
24
55+
5456
[] ->
5557
0
56-
[_head, .. as tail] ->
58+
59+
[_head, .. as tail] if List.len(tail) > 7 ->
5760
List.len(tail)
58-
# Note: avoid using `_` in a when branch, in general you should try to match all cases explicitly.
61+
62+
# Note: avoid using `_` in a when branch, in general you should try to match all cases explicitly.
5963
_ ->
6064
100
6165

6266
string_stuff : Str
63-
string_stuff =
67+
string_stuff =
6468
planet = "Venus"
6569

6670
Str.concat(
6771
"Hello, ${planet}!",
6872
"""
6973
This is a multiline string.
70-
You can call functions inside $... too: ${Num.to_str(1+1)}
74+
You can call functions inside $... too: ${Num.to_str(1 + 1)}
7175
Unicode escape sequence: \u(00A0)
72-
"""
76+
""",
7377
)
7478

75-
pattern_match_tag_union : Result {} _ -> Str
79+
pattern_match_tag_union : Result {} [StdoutErr(Str), Other] -> Str
7680
pattern_match_tag_union = |result|
7781
# `Result a b` is the tag union `[Ok a, Err b]` under the hood.
7882
when result is
7983
Ok(_) ->
8084
"Success"
85+
8186
Err(StdoutErr(err)) ->
82-
"StdoutErr: ${Inspect.to_str(err)}"
87+
"StdoutErr: ${Inspect.to_str(err)}"
88+
8389
Err(_) ->
8490
"Unknown error"
8591

86-
# TODO when branch with tag union, strings, tuples...
87-
8892
# end name with `!` for effectful functions
89-
# `=>` shows effectfulness in the type signature
90-
effect_demo! : Str => Result {} [ StdoutErr(_), StdoutLineFailed [StdoutErr _] ]
93+
# `=>` shows effectfulness in the type signature
94+
effect_demo! : Str => Result {} [StdoutErr _, StdoutLineFailed [StdoutErr _]]
9195
effect_demo! = |msg|
9296

9397
# `?` to return the error if there is one
9498
Stdout.line!(msg)?
95-
99+
96100
# ` ? ` for map_err
97101
Stdout.line!(msg) ? |err| StdoutLineFailed(err)
102+
# this also works:
103+
Stdout.line!(msg) ? StdoutLineFailed
98104

99105
# ?? to provide default value
100106
Stdout.line!(msg) ?? {}
@@ -105,19 +111,19 @@ effect_demo! = |msg|
105111

106112
Ok({})
107113

108-
dbg_expect_crash : {} -> {}
109-
dbg_expect_crash = |{}|
114+
dbg_expect : {} -> {}
115+
dbg_expect = |{}|
110116
a = 42
111117

112118
dbg a
113119

114120
# dbg can forward what it receives
115121
b = dbg 43
116-
122+
117123
# inline expects get removed in optimized builds!
118124
expect b == 43
119125

120-
crash "Avoid using crash in production software!"
126+
{}
121127

122128
# Top level expect
123129
expect 0 == 0
@@ -140,7 +146,6 @@ tuple_demo = |{}|
140146
# they are allocated on the stack
141147
("Roc", 1)
142148

143-
144149
tag_union_demo : Str -> [Red, Green, Yellow]
145150
tag_union_demo = |string|
146151
when string is
@@ -149,8 +154,35 @@ tag_union_demo = |string|
149154
# We can't list all possible strings, so we use `_` to match all other cases.
150155
_ -> Yellow
151156

152-
# TODO: destructuring tuples, records
153-
# TODO: default value records
157+
type_var_star : List * -> List _
158+
type_var_star = |lst| lst
159+
160+
TypeWithTypeVar a : [
161+
TagOne,
162+
TagTwo Str,
163+
]a
164+
165+
tag_union_advanced : Str -> TypeWithTypeVar [TagThree, TagFour U64]
166+
tag_union_advanced = |string|
167+
when string is
168+
"one" -> TagOne
169+
"two" -> TagTwo("hello")
170+
"three" -> TagThree
171+
# We can't list all possible strings, so we use `_` to match all other cases.
172+
_ -> TagFour(42)
173+
174+
default_val_record : { a ?? Str } -> Str
175+
default_val_record = |{ a ?? "default" }|
176+
a
177+
178+
destructuring =
179+
tup = ("Roc", 1)
180+
(str, num) = tup
181+
182+
rec = { x: 1, y: 2 }
183+
{ x, y } = rec
184+
185+
(str, num, x, y)
154186

155187
main! : List Arg => Result {} _
156188
main! = |_args|
@@ -167,5 +199,14 @@ main! = |_args|
167199
Stdout.line!("${Inspect.to_str(if_demo(Bool.true))}")?
168200
Stdout.line!("${Inspect.to_str(tuple_demo({}))}")?
169201
Stdout.line!("${Inspect.to_str(tag_union_demo("red"))}")?
170-
Stdout.line!("${Inspect.to_str(dbg_expect_crash({}))}")
202+
Stdout.line!("${Inspect.to_str(type_var_star([1, 2]))}")?
203+
Stdout.line!("${Inspect.to_str(tag_union_advanced("four"))}")?
204+
Stdout.line!("${default_val_record({})}")?
205+
Stdout.line!("${Inspect.to_str(destructuring)}")?
206+
Stdout.line!("${Inspect.to_str(dbg_expect({}))}")?
207+
208+
# Commented out so CI tests can pass
209+
# crash "Avoid using crash in production software!"
210+
211+
Ok({})
171212

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)