Skip to content

Commit dee7d4f

Browse files
committed
Add some example programs
1 parent 2cf8321 commit dee7d4f

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ npm run build
1212
echo '{@runtime context => :context.program.start_time}' | ./please --output-format=json
1313
```
1414

15+
There are more example programs in [`./examples`](./examples).
16+
1517
## What This Repository Is
1618

1719
**This implementation of Please is a proof of concept**. There are bugs and

examples/fibonacci.plz

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
fibonacci: n => {
3+
@if :integer.less_than(2)(:n)
4+
then: :n
5+
else: :integer.add(
6+
:fibonacci(:integer.subtract(2)(:n))
7+
)(
8+
:fibonacci(:integer.subtract(1)(:n))
9+
)
10+
}
11+
12+
input: { @runtime context => :context.arguments.lookup(input) }
13+
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+
)
24+
}.output

examples/kitchen-sink.plz

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
// a comment
3+
foo: bar
4+
bar: :foo
5+
sky_is_blue: :boolean.not(false)
6+
colors: { red green blue }
7+
two: :integer.add(1)(1)
8+
add_one: :integer.add(1)
9+
three: :add_one(:two)
10+
function: x => { value: :x }
11+
conditional_value: :function({ @if :sky_is_blue :two :three })
12+
side_effect: { @runtime context => :context.log("this goes to stderr") }
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{@runtime context =>
2+
:flow({
3+
:context.arguments.lookup
4+
:match({
5+
none: {}
6+
some: :flow({
7+
:context.environment.lookup
8+
:match({
9+
none: {}
10+
some: :identity
11+
})
12+
})
13+
})
14+
})(variable)
15+
}

src/examples.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { exec } from 'node:child_process'
2+
import { promises as fs } from 'node:fs'
3+
import path from 'node:path'
4+
import test, { suite } from 'node:test'
5+
6+
const exampleDirectoryPath = path.join(import.meta.dirname, '..', 'examples')
7+
const pleasePath = path.join(
8+
import.meta.dirname,
9+
'language',
10+
'cli',
11+
'please.js',
12+
)
13+
14+
suite('examples', async () => {
15+
for (const exampleFileName of await fs.readdir(exampleDirectoryPath)) {
16+
const exampleFilePath = path.join(exampleDirectoryPath, exampleFileName)
17+
// TODO: Use snapshot testing instead of merely checking for errors.
18+
test(
19+
exampleFileName,
20+
() =>
21+
new Promise((resolve, reject) => {
22+
const _childProcess = exec(
23+
`cat "${exampleFilePath}" | node "${pleasePath}" --output-format=plz`,
24+
(error, _stdout, _stderr) => {
25+
// `error` is an `ExecError` when exit status is nonzero.
26+
if (error !== null) {
27+
reject(error)
28+
} else {
29+
resolve(undefined)
30+
}
31+
},
32+
)
33+
}),
34+
)
35+
}
36+
})

0 commit comments

Comments
 (0)