From 3f7adea56f361bca5fb7ae0a65589507c57c7e97 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Wed, 14 Sep 2022 20:08:40 -0700 Subject: [PATCH] Read defs.txt at compile-time. I'd like to build the dict datastructure at compile-time too but I'm not going to figure that out tonight. It's enough that the defs are embedded in the executable. (You can see them with the strings utility.) --- implementations/Nim/defs.txt | 1 + implementations/Nim/simplejoy.nim | 47 ++++++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/implementations/Nim/defs.txt b/implementations/Nim/defs.txt index 570293f..3b96c53 100644 --- a/implementations/Nim/defs.txt +++ b/implementations/Nim/defs.txt @@ -16,6 +16,7 @@ at drop first average [sum] [size] cleave / b [i] dip i binary unary popd +bool truthy ccccons ccons ccons ccons cons cons clear [] swaack pop diff --git a/implementations/Nim/simplejoy.nim b/implementations/Nim/simplejoy.nim index 4983b70..cd9152f 100644 --- a/implementations/Nim/simplejoy.nim +++ b/implementations/Nim/simplejoy.nim @@ -1,4 +1,4 @@ -import rdstdin, strutils +import rdstdin, streams, strutils import bigints, fp @@ -556,9 +556,10 @@ proc joy_eval(sym: string, stack: JoyListType, expression: JoyListType, return truthy(stack, expression, dictionary) else: - raise newException(UnknownWordError, "Unknown: " & sym) - - return (stack, expression, dictionary) + let def = dictionary.get(sym) + if def.isEmpty: + raise newException(UnknownWordError, "Unknown: " & sym) + return (stack, push_quote_list(def.get(), expression), dictionary) proc joy(stack: JoyListType, expression: JoyListType, dictionary: JoyMapType): ( @@ -581,16 +582,50 @@ proc joy(stack: JoyListType, expression: JoyListType, dictionary: JoyMapType): ( +proc add_def(def: string, dictionary: var JoyMapType) = + let d = text_to_expression(def) + let sym = d.head + case sym.kind: + of joySymbol: + dictionary = dictionary + (sym.symVal, d.tail) + else: + raise newException(ValueError, def) + + +proc defs_file2dict(defs_filename: string = "defs.txt"): JoyMapType = + var strm = newFileStream(defs_filename, fmRead) + var dictionary = newMap[string, JoyListType]() + var line = "" + if not isNil(strm): + while strm.readLine(line): + if line.isEmptyOrWhitespace: + continue + add_def(line, dictionary) + strm.close() + return dictionary + + #let exp = text_to_expression("2 3 add 23 mul 45 gt") #let exp = text_to_expression("2 3 false [add] [mul] branch") #let exp = text_to_expression("2 3 true [add] [mul] branch") #let exp = text_to_expression("[add] [mul] concat") #let (s,d) = joy(stack, exp, dict) - #echo print_stack(s) +#echo print_stack(s) -var s = empty_list.listVal + +#let dictionary = defs_file2dict() +#var d = dictionary + + +const defs_text = staticRead"defs.txt" var d = newMap[string, JoyListType]() +for line in defs_text.splitLines: + if line.isEmptyOrWhitespace: + continue + add_def(line, d) + +var s = empty_list.listVal var exp: JoyListType while true: try: -- 2.11.0