Skip to content

Commit

Permalink
Short circuit debug output (#7)
Browse files Browse the repository at this point in the history
Don't compute debug messages unnecessarily
  • Loading branch information
chalcolith authored Nov 28, 2024
1 parent 649c957 commit 5871625
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 118 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.2
1.5.3
2 changes: 1 addition & 1 deletion corral.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"homepage": "https://github.com/chalcolith/kiuatan",
"documentation_url": "https://chalcolith.github.io/kiuatan/kiuatan--index/",
"license": "MIT",
"version": "1.5.2"
"version": "1.5.3"
},
"deps": []
}
12 changes: 10 additions & 2 deletions kiuatan/_dbg.pony
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ use "debug"
use "collections"

primitive _Dbg
fun out(depth: USize, msg: String) =>
fun apply(): Bool =>
false
// ifdef debug then
// true
// else
// false
// end

fun out(depth: USize, msg: String): Bool =>
// // Uncomment to get lots of debug output
// let indent =
// recover val
Expand All @@ -13,4 +21,4 @@ primitive _Dbg
// indent'
// end
// Debug.out(indent + msg)
None
false
11 changes: 4 additions & 7 deletions kiuatan/bind.pony
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@ class Bind[S, D: Any #share = None, V: Any #share = None]
fun val parse(parser: _ParseNamedRule[S, D, V], depth: USize, loc: Loc[S])
: Result[S, D, V]
=>
ifdef debug then
_Dbg.out(depth,
"BIND " + variable.name + " @" + loc.string())
end
_Dbg() and _Dbg.out(depth,"BIND " + variable.name + " @" + loc.string())

let result =
match _body.parse(parser, depth + 1, loc)
| let success: Success[S, D, V] =>
Success[S, D, V](this, success.start, success.next, [success])
| let failure: Failure[S, D, V] =>
failure
end
ifdef debug then
_Dbg.out(depth, " = " + variable.name + " := " + result.string())
end
_Dbg() and _Dbg.out(
depth, " = " + variable.name + " := " + result.string())
result

fun action(): (Action[S, D, V] | None) =>
Expand Down
8 changes: 2 additions & 6 deletions kiuatan/cond.pony
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ class Cond[S, D: Any #share = None, V: Any #share = None]
fun val parse(parser: _ParseNamedRule[S, D, V], depth: USize, loc: Loc[S])
: Result[S, D, V]
=>
ifdef debug then
_Dbg.out(depth, "COND @" + loc.string())
end
_Dbg() and _Dbg.out(depth, "COND @" + loc.string())
let result =
match _body.parse(parser, depth + 1, loc)
| let success: Success[S, D, V] =>
Expand All @@ -34,9 +32,7 @@ class Cond[S, D: Any #share = None, V: Any #share = None]
| let failure: Failure[S, D, V] =>
failure
end
ifdef debug then
_Dbg.out(depth, " = " + result.string())
end
_Dbg() and _Dbg.out(depth, " = " + result.string())
result

fun action(): (Action[S, D, V] | None) =>
Expand Down
12 changes: 3 additions & 9 deletions kiuatan/conj.pony
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ class Conj[S, D: Any #share = None, V: Any #share = None]
fun val parse(parser: _ParseNamedRule[S, D, V], depth: USize, loc: Loc[S])
: Result[S, D, V]
=>
ifdef debug then
_Dbg.out(depth, "CONJ @" + loc.string())
end
_Dbg() and _Dbg.out(depth, "CONJ @" + loc.string())

if _children.size() == 0 then
return Failure[S, D, V](this, loc, ErrorMsg.conjunction_empty())
Expand All @@ -39,17 +37,13 @@ class Conj[S, D: Any #share = None, V: Any #share = None]
next = success.next
| let failure: Failure[S, D, V] =>
let failure' = Failure[S, D, V](this, failure.start, None, failure)
ifdef debug then
_Dbg.out(depth, "= " + failure'.string())
end
_Dbg() and _Dbg.out(depth, "= " + failure'.string())
return failure'
end
end

let success = Success[S, D, V](this, loc, next, consume results)
ifdef debug then
_Dbg.out(depth, "= " + success.string())
end
_Dbg() and _Dbg.out(depth, "= " + success.string())
success

fun action(): (Action[S, D, V] | None) =>
Expand Down
12 changes: 3 additions & 9 deletions kiuatan/disj.pony
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ class Disj[S, D: Any #share = None, V: Any #share = None]
fun val parse(parser: _ParseNamedRule[S, D, V], depth: USize, loc: Loc[S])
: Result[S, D, V]
=>
ifdef debug then
_Dbg.out(depth, "DISJ @" + loc.string())
end
_Dbg() and _Dbg.out(depth, "DISJ @" + loc.string())

if _children.size() == 0 then
return Failure[S, D, V](this, loc, ErrorMsg.disjunction_empty())
Expand All @@ -36,9 +34,7 @@ class Disj[S, D: Any #share = None, V: Any #share = None]
match child.parse(parser, depth + 1, loc)
| let success: Success[S, D, V] =>
let result = Success[S, D, V](this, loc, success.next, [success])
ifdef debug then
_Dbg.out(depth, " = " + result.string())
end
_Dbg() and _Dbg.out(depth, " = " + result.string())
return result
| let failure: Failure[S, D, V] =>
if message.size() > 0 then
Expand All @@ -49,9 +45,7 @@ class Disj[S, D: Any #share = None, V: Any #share = None]
end

let result = Failure[S, D, V](this, loc, consume message)
ifdef debug then
_Dbg.out(depth, " = " + result.string())
end
_Dbg() and _Dbg.out(depth, " = " + result.string())
result

fun action(): (Action[S, D, V] | None) =>
Expand Down
4 changes: 1 addition & 3 deletions kiuatan/error.pony
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class Error[S, D: Any #share = None, V: Any #share = None]
: Result[S, D, V]
=>
let result = Failure[S, D, V](this, loc, _message)
ifdef debug then
_Dbg.out(depth, "ERROR " + result.string())
end
_Dbg() and _Dbg.out(depth, "ERROR " + result.string())
result

fun action(): (Action[S, D, V] | None) =>
Expand Down
8 changes: 2 additions & 6 deletions kiuatan/literal.pony
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ class Literal[
fun val parse(parser: _ParseNamedRule[S, D, V], depth: USize, loc: Loc[S])
: Result[S, D, V]
=>
ifdef debug then
_Dbg.out(depth, "LIT @" + loc.string())
end
_Dbg() and _Dbg.out(depth, "LIT @" + loc.string())

let result =
try
Expand All @@ -39,9 +37,7 @@ class Literal[
else
Failure[S, D, V](this, loc, ErrorMsg.literal_failed())
end
ifdef debug then
_Dbg.out(depth, " = " + result.string())
end
_Dbg() and _Dbg.out(depth, " = " + result.string())
result

fun action(): (Action[S, D, V] | None) =>
Expand Down
8 changes: 2 additions & 6 deletions kiuatan/look.pony
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ class Look[S, D: Any #share = None, V: Any #share = None]
fun val parse(parser: _ParseNamedRule[S, D, V], depth: USize, loc: Loc[S])
: Result[S, D, V]
=>
ifdef debug then
_Dbg.out(depth, "LOOK @" + loc.string())
end
_Dbg() and _Dbg.out(depth, "LOOK @" + loc.string())

let result =
match _body.parse(parser, depth + 1, loc)
Expand All @@ -33,9 +31,7 @@ class Look[S, D: Any #share = None, V: Any #share = None]
| let failure: Failure[S, D, V] =>
Failure[S, D, V](this, loc, "lookahead failed", failure)
end
ifdef debug then
_Dbg.out(depth, "= " + result.string())
end
_Dbg() and _Dbg.out(depth, "= " + result.string())
result

fun action(): (Action[S, D, V] | None) =>
Expand Down
8 changes: 2 additions & 6 deletions kiuatan/named_rule.pony
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ class NamedRule[S, D: Any #share = None, V: Any #share = None]
loc: Loc[S])
: Result[S, D, V]
=>
ifdef debug then
_Dbg.out(depth, "RULE " + name + " @" + loc.string())
end
_Dbg() and _Dbg.out(depth, "RULE " + name + " @" + loc.string())

let result =
match _body
Expand All @@ -60,9 +58,7 @@ class NamedRule[S, D: Any #share = None, V: Any #share = None]
else
Failure[S, D, V](this, loc, ErrorMsg.rule_empty(name))
end
ifdef debug then
_Dbg.out(depth, " " + name + " = " + result.string())
end
_Dbg() and _Dbg.out(depth, " " + name + " = " + result.string())
result

fun action(): (Action[S, D, V] | None) =>
Expand Down
8 changes: 2 additions & 6 deletions kiuatan/neg.pony
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ class Neg[S, D: Any #share = None, V: Any #share = None]
fun val parse(parser: _ParseNamedRule[S, D, V], depth: USize, loc: Loc[S])
: Result[S, D, V]
=>
ifdef debug then
_Dbg.out(depth, "NEG @" + loc.string())
end
_Dbg() and _Dbg.out(depth, "NEG @" + loc.string())

let result =
match _body.parse(parser, depth + 1, loc)
Expand All @@ -33,9 +31,7 @@ class Neg[S, D: Any #share = None, V: Any #share = None]
| let _: Failure[S, D, V] =>
Success[S, D, V](this, loc, loc)
end
ifdef debug then
_Dbg.out(depth, "= " + result.string())
end
_Dbg() and _Dbg.out(depth, "= " + result.string())
result

fun action(): (Action[S, D, V] | None) =>
Expand Down
58 changes: 22 additions & 36 deletions kiuatan/parser.pony
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ actor Parser[S, D: Any #share = None, V: Any #share = None]
// look in memo for a top-level memoized result
match _lookup(depth + 1, rule, loc)
| let result: Result[S, D, V] =>
ifdef debug then
_Dbg.out(depth + 1, rule.name + ": FOUND: " + result.string())
end
_Dbg() and _Dbg.out(depth + 1, rule.name + ": FOUND: " + result.string())
return result
end

Expand All @@ -220,10 +218,11 @@ actor Parser[S, D: Any #share = None, V: Any #share = None]
| (let result: Result[S, D, V], let first_lr: Bool) =>
ifdef debug then
if first_lr then
_Dbg.out(depth + 1, rule.name + ": LR DETECTED")
_Dbg() and _Dbg.out(depth + 1, rule.name + ": LR DETECTED")
end
let prev_exp = _current_expansion(rule, loc) - 1
_Dbg.out(depth + 1, "fnd_exp " + rule.name + "@" + loc.string() + " <" +
_Dbg() and _Dbg.out(
depth + 1, "fnd_exp " + rule.name + "@" + loc.string() + " <" +
prev_exp.string() + "> " + result.string())
end
return result
Expand All @@ -234,10 +233,9 @@ actor Parser[S, D: Any #share = None, V: Any #share = None]
let failure = Failure[S, D, V](rule, loc)
let topmost = _push_expansion(depth + 1, rule, loc, failure)

ifdef debug then
_Dbg.out(depth + 1, rule.name + "@" + loc.string() + " <" +
_current_expansion(rule, loc).string() + ">")
end
_Dbg() and _Dbg.out(
depth + 1, rule.name + "@" + loc.string() + " <" +
_current_expansion(rule, loc).string() + ">")
_try_expansion(depth, rule, body, loc, topmost)

fun ref _is_left_recursive(
Expand Down Expand Up @@ -313,10 +311,9 @@ actor Parser[S, D: Any #share = None, V: Any #share = None]
_remove_expansions_below(depth + 2, loc)
_push_expansion(depth + 2, rule, loc, success)

ifdef debug then
_Dbg.out(depth + 1, rule.name + "@" + loc.string() + " <" +
_current_expansion(rule, loc).string() + ">")
end
_Dbg() and _Dbg.out(
depth + 1, rule.name + "@" + loc.string() + " <" +
_current_expansion(rule, loc).string() + ">")
return _try_expansion(depth, rule, body, loc, topmost)
end
// fall through
Expand Down Expand Up @@ -367,11 +364,7 @@ actor Parser[S, D: Any #share = None, V: Any #share = None]
=>
try
let result = _memo(rule)?(loc)?

ifdef debug then
_Dbg.out(depth, "found " + rule.name + " " + result.string())
end

_Dbg() and _Dbg.out(depth, "found " + rule.name + " " + result.string())
result
end

Expand All @@ -381,9 +374,7 @@ actor Parser[S, D: Any #share = None, V: Any #share = None]
loc: Loc[S],
result: Result[S, D, V])
=>
ifdef debug then
_Dbg.out(depth, "memoize " + rule.name + " " + result.string())
end
_Dbg() and _Dbg.out(depth, "memoize " + rule.name + " " + result.string())

let by_loc =
try
Expand All @@ -405,9 +396,7 @@ actor Parser[S, D: Any #share = None, V: Any #share = None]
continue
end

ifdef debug then
_Dbg.out(depth, "memoize " + rule.name + " " + res.string())
end
_Dbg() and _Dbg.out(depth, "memoize " + rule.name + " " + res.string())

let by_loc =
try
Expand All @@ -430,18 +419,16 @@ actor Parser[S, D: Any #share = None, V: Any #share = None]
try
let lr_state = _lr_states((rule, loc))?
let cur_exp = lr_state.expansions.size()
ifdef debug then
_Dbg.out(depth, "mem_exp " + rule.name + "@" + loc.string() + " <" +
cur_exp.string() + "> " + result.string())
end
_Dbg() and _Dbg.out(
depth, "mem_exp " + rule.name + "@" + loc.string() + " <" +
cur_exp.string() + "> " + result.string())
lr_state.expansions.push(result)
else
let lr_state = _LRRuleState[S, D, V](depth, rule, loc)
let cur_exp = lr_state.expansions.size()
ifdef debug then
_Dbg.out(depth, "mem_exp " + rule.name + "@" + loc.string() + " <" +
cur_exp.string() + "> " + result.string())
end
_Dbg() and _Dbg.out(
depth, "mem_exp " + rule.name + "@" + loc.string() + " <" +
cur_exp.string() + "> " + result.string())
lr_state.expansions.push(result)
_lr_states((rule, loc)) = lr_state
end
Expand All @@ -456,10 +443,9 @@ actor Parser[S, D: Any #share = None, V: Any #share = None]
try
let lr_state = _lr_states((rule, loc))?
let prev_exp = lr_state.expansions.size() - 1
ifdef debug then
_Dbg.out(depth, "mem_upd " + rule.name + "@" + loc.string() + " <" +
prev_exp.string() + "> " + result.string())
end
_Dbg() and _Dbg.out(
depth, "mem_upd " + rule.name + "@" + loc.string() + " <" +
prev_exp.string() + "> " + result.string())
lr_state.expansions(prev_exp)? = result
end

Expand Down
8 changes: 2 additions & 6 deletions kiuatan/single.pony
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@ class Single[
fun val parse(parser: _ParseNamedRule[S, D, V], depth: USize, loc: Loc[S])
: Result[S, D, V]
=>
ifdef debug then
_Dbg.out(depth, "SING @" + loc.string())
end
_Dbg() and _Dbg.out(depth, "SING @" + loc.string())

let result = _parse_single(loc)
ifdef debug then
_Dbg.out(depth, "= " + result.string())
end
_Dbg() and _Dbg.out(depth, "= " + result.string())
result

fun val _parse_single(loc: Loc[S]): Result[S, D, V] =>
Expand Down
Loading

0 comments on commit 5871625

Please sign in to comment.