Skip to content

Commit

Permalink
Update Calculator example
Browse files Browse the repository at this point in the history
  • Loading branch information
chalcolith committed Jan 3, 2025
1 parent e4eac28 commit 0a4d3af
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
56 changes: 25 additions & 31 deletions examples/calc/calc/grammar_builder.pony
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class GrammarBuilder
[ Bind(a, additive)
Bind(o, add_op)
Bind(b, multiplicative) ],
{(_, result, values, bindings) =>
{(_, result, _, bindings) =>
var first: F64 = 0.0
var op_is_add: Bool = true
var second: F64 = 0.0
Expand All @@ -97,13 +97,11 @@ class GrammarBuilder
second = bindings.values(b, result)?(0)?
end

let sum: (F64 | None) =
if op_is_add then
first + second
else
first - second
end
(sum, bindings)
if op_is_add then
first + second
else
first - second
end
})
Bind(b, multiplicative)
]))
Expand All @@ -119,7 +117,7 @@ class GrammarBuilder
[ Bind(a, multiplicative)
Bind(o, mul_op)
Bind(b, term) ],
{(_, result, values, bindings) =>
{(_, result, _, bindings) =>
var first: F64 = 1.0
var op_is_mul: Bool = true
var second: F64 = 1.0
Expand All @@ -138,17 +136,15 @@ class GrammarBuilder
second = bindings.values(b, result)?(0)?
end

let prod: (F64 | None) =
if op_is_mul then
first * second
if op_is_mul then
first * second
else
if second > 0.0 then
first / second
else
if second > 0.0 then
first / second
else
0.0
end
0.0
end
(prod, bindings)
end
})
Bind(b, term)
]))
Expand All @@ -171,7 +167,7 @@ class GrammarBuilder
Bind(f, Star(fraction where min' = 0, max' = 1))
Bind(e, Star(exponent where min' = 0, max' = 1))
space ],
{(_, result, values, bindings) =>
{(_, result, _, bindings) =>
var int_num: F64 = 0.0
var frac_num: F64 = 0.0
var exp_num: F64 = 1.0
Expand All @@ -196,9 +192,9 @@ class GrammarBuilder
end

if exp_num != 1.0 then
(n * F64(10).pow(exp_num), bindings)
n * F64(10).pow(exp_num)
else
(n, bindings)
n
end
}))

Expand All @@ -207,10 +203,10 @@ class GrammarBuilder
Conj(
[ Star(Sing("-+") where min' = 0, max' = 1)
Star(Sing("0123456789"), 1) ],
{(_,r,_,b) =>
{(_, result, _, _) =>
var n: F64 = 0.0
try
var start = r.start
var start = result.start
var ss = start()?

var sign: F64 = 1.0
Expand All @@ -221,12 +217,12 @@ class GrammarBuilder
start = start.next()
end

for ch in start.values(r.next) do
for ch in start.values(result.next) do
n = (n * 10.0) + (ch - '0').f64()
end
n = n * sign
end
(n, b)
n
}))

fun ref _gen_fraction() =>
Expand All @@ -238,20 +234,18 @@ class GrammarBuilder
Star(
Bind(i, integer),
0,
{(_,r,_,b) =>
{(_, result, _, bindings) =>
var f: F64 = 0.0

try
match b(i, r)?
| (let s: Success, let ns: ReadSeq[F64] val) =>
match bindings(i, result)?
| (let s: Success, let ns: ReadSeq[F64]) =>
f = ns(0)?
for _ in s.start.values(s.next) do
f = f / 10.0
end
end
end

(f, b)
f
},
1)
]))
Expand Down
20 changes: 15 additions & 5 deletions examples/calc/calc/test/test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ type Failure is k.Failure[U8, None, F64]


primitive _Test
fun should_succeed(h: TestHelper, expected: F64, result: Result,
values: ReadSeq[F64] val)
fun should_succeed(
h: TestHelper,
expected: F64,
result: Result,
values: ReadSeq[F64])
=>
match result
| let success: Success =>
Expand All @@ -55,8 +58,11 @@ primitive _Test
h.complete(false)
end

fun should_span(h: TestHelper, len: USize, result: Result,
values: ReadSeq[F64] val)
fun should_span(
h: TestHelper,
len: USize,
result: Result,
values: ReadSeq[F64])
=>
match result
| let success: Success =>
Expand All @@ -67,7 +73,11 @@ primitive _Test
h.fail("should have succeeded")
end

fun should_fail(h: TestHelper, result: Result, values: ReadSeq[F64] val) =>
fun should_fail(
h: TestHelper,
result: Result,
values: ReadSeq[F64])
=>
match result
| let success: Success =>
h.fail("expected failure; returned a value")
Expand Down

0 comments on commit 0a4d3af

Please sign in to comment.