Skip to content

Commit 70a3ea9

Browse files
authored
Merge pull request #60 from mkantor/infix-notation
Infix notation
2 parents aebee21 + 9e98523 commit 70a3ea9

File tree

12 files changed

+890
-390
lines changed

12 files changed

+890
-390
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,34 @@ Functions can be applied:
149149
}
150150
```
151151

152+
Infix notation can be used to apply binary functions (those which look like
153+
`b => a => …`). For example, the expression `x f y` desugars to `:f(y)(x)`.
154+
Here's another example:
155+
156+
```
157+
{
158+
cons: b => a => { :a, :b }
159+
list: 1 cons (2 cons 3) // evaluates to `{ 1, { 2, 3 } }`
160+
}
161+
```
162+
163+
The standard library contains symbolically-named functions for arithmetic and
164+
other familiar binary operations. For example, `1 + 2 - 3` is `0`. Also included
165+
in the standard library are the functions`|>` (pipe) and `>>` (flow):
166+
167+
```
168+
{
169+
// `>>` composes functions
170+
append_bc: :atom.append(b) >> :atom.append(c)
171+
172+
// `|>` pipes an argument into a function
173+
abc: a |> :append_bc
174+
}
175+
```
176+
177+
All binary operations are currently left-associative and there is no operator
178+
precedence. Use of parentheses is encouraged.
179+
152180
#### Keywords
153181

154182
The functions and lookups shown above are syntax sugar for _keyword
@@ -197,7 +225,7 @@ possible. For example, this program compiles to the literal value `2` (no
197225
computation will occur at runtime):
198226

199227
```
200-
:integer.add(1)(1)
228+
1 + 1
201229
```
202230

203231
There's currently no module system and all Please programs are single files, but

examples/fibonacci.plz

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
{
22
fibonacci: n => {
3-
@if, :integer.less_than(2)(:n)
3+
@if
4+
condition: :n < 2
45
then: :n
5-
else: :integer.add(
6-
:fibonacci(:integer.subtract(2)(:n))
7-
)(
8-
:fibonacci(:integer.subtract(1)(:n))
9-
)
6+
else: :fibonacci(:n - 1) + :fibonacci(:n - 2)
107
}
118

12-
input: { @runtime, context => :context.arguments.lookup(input) }
9+
input: {
10+
@runtime
11+
context => :context.arguments.lookup(input)
12+
}
1313

14-
output: :apply(:input)(
15-
:match({
16-
none: _ => "missing input argument"
17-
some: input => {
18-
@if, :natural_number.is(:input)
19-
then: :fibonacci(:input)
20-
else: "input must be a natural number"
21-
}
22-
})
23-
)
14+
output: :input match {
15+
none: _ => "missing input argument"
16+
some: input => {
17+
@if
18+
condition: :natural_number.is(:input)
19+
then: :fibonacci(:input)
20+
else: "input must be a natural number"
21+
}
22+
}
2423
}.output

examples/kitchen-sink.plz

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
bar: :foo
55
sky_is_blue: :boolean.not(false)
66
colors: { red, green, blue }
7-
two: :integer.add(1)(1)
7+
two: 1 + 1
88
add_one: :integer.add(1)
99
three: :add_one(:two)
1010
function: x => { value: :x }
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
{@runtime, context =>
2-
:flow(
3-
:match({
1+
/**
2+
* Given CLI arguments like `--variable=FOO`, looks up the environment
3+
* variable named `FOO`.
4+
*/
5+
{
6+
@runtime
7+
context =>
8+
:context.arguments.lookup(variable) match {
49
none: {}
5-
some: :flow(
6-
:match({
7-
none: {}
8-
some: :identity
9-
})
10-
)(
11-
:context.environment.lookup
12-
)
13-
})
14-
)(
15-
:context.arguments.lookup
16-
)(variable)
10+
some: :context.environment.lookup >> :match({
11+
none: {}
12+
some: :identity
13+
})
14+
}
1715
}

0 commit comments

Comments
 (0)