rpgsheet

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 7e6900cc5e0e65f24f0066caa9d1bd9474224a05
parent 0e093c28e583abc029d9903501d72351fe0ae177
Author: Skylar Hill <stellarskylark@posteo.net>
Date:   Sun,  5 Jun 2022 07:11:48 -0500

Error handling for calculating nonexistent expressions

Diffstat:
Msrc/verb.nim | 15++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/verb.nim b/src/verb.nim @@ -18,12 +18,14 @@ type View Tui + proc expressionAsString(node: YamlNode): string = result = fmt"""Dice: {node.getContent("dice")}""" & "\n" result = result & fmt"""Modifier: {node.getContent("modifier")}""" & "\n" result = result & fmt"""Tab: {node.getContent("tab")}""" & "\n" result = result & fmt"""Window: {node.getContent("window")}""" + proc calculateModifier(exp: string, sheet: YamlDocument): string = if exp == "": return "0" @@ -44,9 +46,12 @@ proc calculateModifier(exp: string, sheet: YamlDocument): string = return working for match in exp.findAll reRawExp: + if match == exp: + raise newException(KeyError, "The expression " & exp & " is not defined.") working = working.replace(match, calculateModifier(match, sheet)) return calculateModifier(working, sheet) + proc roll(action: string, sheet: YamlDocument): string = if action.contains(reDice): let (output, code) = execCmdEx("rolldice -s", input = action) @@ -60,7 +65,12 @@ proc roll(action: string, sheet: YamlDocument): string = except KeyError: return action & " is not a dice expression or defined expression" - var modifier = calculateModifier(exp.getContent("modifier"), sheet) + var modifier: string + try: + modifier = calculateModifier(exp.getContent("modifier"), sheet) + except KeyError: + return "ERROR: " & getCurrentExceptionMsg() + if not exp.contains "dice": return modifier let dice = exp.getContent("dice") @@ -79,16 +89,19 @@ proc roll(action: string, sheet: YamlDocument): string = # rolldice returns its input; the actual roll is the second line result = output.splitLines[1] + proc view(action: string, sheet: YamlDocument): string = let node = sheet.getExpression(action) echo action & ":" echo expressionAsString(node) + proc write*(doc: YamlDocument, file: string) = let s = newfileStream(file, fmWrite) dumpDom(doc, s, options = defineOptions(style = psBlockOnly)) s.close() + proc doVerb*(action: string, verb: Verb, sheet: YamlDocument): string = case verb of Roll: