while I'm at it
while I'm at it
let me tell you about another embarrassing bug I found in my custom Lisp.
(function _testWhile [] (let [max 8 &mut current 0 &mut iterations 0] (while (>= max (+= current 1)) (+= iterations 1)) (assertEquals 8 iterations)))
Outputs:
Somehow I've been working with while loops that evaluate their condition twice per iteration!!!!
This is a pretty good opportunity to delve into the language internals.
My language is just a very complicated Haxe macro.
Haxe macros are basically functions written in Haxe, which return a data structure representing haxe expressions, which haxe compiles into your program.
Here is the code where my language is generating a data structure for the while loop:
function whileForm(invert:Bool, wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) { var funcName = if (invert) "until" else "while"; var b = wholeExp.expBuilder(); var cond = k.convert(b.callSymbol("Prelude.truthy", [args[0])); if (invert) { cond = macro !$cond; cond = b.haxeExpr(cond); } return EWhile(cond, k.convert(b.begin(args)), true).withMacroPosOf(wholeExp); } k.doc("while", 2, null, '(while <condition> <body...>)'); map["while"] = whileForm.bind(false); k.doc("until", 2, null, '(until <condition> <body...>)'); map["until"] = whileForm.bind(true);
This registers two variations of while loops into my custom lisp: one for normal while loops, and one for inverted while loops: "until" loops where the body is repeated UNTIL the condition becomes true.
The problem, which took me a few minutes to pinpoint, is that I'm accessing the condition expression, args[0], but LEAVING it in the args list, which then gets passed into the data structure as part of the BODY of the while loop. So effectively, (while (checkCond) (doThing)) would turn into (while (checkCond) (checkCond) (doThing)). If any side-effects take place in the condition expression, kaboom!!
Get FLIES FLIES FLIES
FLIES FLIES FLIES
queer sci-fi comedy
Status | In development |
Author | Nat Quayle Nelson |
Genre | Visual Novel |
Tags | Comedy, Dark, Queer, Sci-fi |
Languages | English |
Accessibility | Subtitles, One button |
More posts
- Buy FLIES & 8 other indie games for $15!38 days ago
- Episode 3 is now available!54 days ago
- "I Collect Butterflies"Mar 10, 2024
- The results are in!Feb 10, 2024
- I try to do marketingJan 31, 2024
- Win some, lose someJan 14, 2024
- 10 Reviews QuestJan 04, 2024
- Can't predict 'em all!Dec 13, 2023
- Launch WeekendDec 04, 2023
- It's out!!Dec 01, 2023
Leave a comment
Log in with itch.io to leave a comment.