git @ Cat's Eye Technologies Burro / 7312a26
Adapt genCond to convert Kondey to Burro. Chris Pressey 6 months ago
3 changed file(s) with 43 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
460460 -> Functionality "Compile Kondey Program" is implemented by
461461 -> shell command "bin/burro compile-kondey %(test-body-file)"
462462
463 A Kondey program (for now) consists of a sequence of Burro program fragments,
464 one on each line. It generates a conditional structure following this pattern:
463 Kondey is a proper superset of Burro. Every Burro program is a Kondey program, but not
464 vice versa.
465
466 Kondey adds a conditional structure to Burro, which looks like this:
467
468 {3.../.../...}
469
470 where each `...` is a Kondey program. (FIXME the hardcoded 3 to make 3 branches is an
471 awful hack and will be replaced by it supporting an arbitrary number of branches.)
472 This form maps to a Burro conditional structure following this pattern:
465473
466474 * if the current cell is greater than 0, then execute the 1st fragment
467475 * if the current cell is greater than 2, then undo the 1st fragment and execute the 2nd fragment
468476 * if the current cell is greater than 4, then undo the 2nd fragment and execute the 3rd fragment
469477
470 etc.
471
472478 So given these three lines
473479
474 +++++++++
475 +++++++++++++
476 +++++++
477
478 we will obtain the conditional structure shown in the "Notes" section above. (It omits
480 {3+++++++++/+++++++++++++/+++++++}
481
482 we will obtain the conditional structure shown in the "Notes" section above (omitting
479483 the prelude where we set up "5" as the value to be tested, and the postlude where we
480 tear down "5" again.)
481
482 +++++++++
483 +++++++++++++
484 +++++++
484 tear down "5" again).
485
486 {3+++++++++/+++++++++++++/+++++++}
485487 ===> (+++++++++>/>)(/)--(<---------+++++++++++++>>/>)--(/)----(<<-------------+++++++>>>/>)----(/)
44 module Language.Kondey.Compiler where
55
66 import qualified Language.Kondey.Definition as Kondey
7 import Language.Kondey.Definition (Kondey)
78 import Language.Burro.Definition
89
910
1011 compile :: String -> Burro
11 compile kondeyText =
12 let
13 kondey = Kondey.parse kondeyText
14 in
15 -- genBurro kondey
16 genCond $ map (parse) (lines kondeyText)
12 compile kondeyText = genBurro $ Kondey.parse kondeyText
1713
18 genBurro :: Kondey.Kondey -> Burro
14 genBurro :: Kondey -> Burro
1915 genBurro Kondey.Null = Null
2016 genBurro Kondey.ToggleHalt = ToggleHalt
2117 genBurro Kondey.Inc = Inc
2420 genBurro Kondey.GoRight = GoRight
2521 genBurro (Kondey.Test t f) = Test (genBurro t) (genBurro f)
2622 genBurro (Kondey.Seq k1 k2) = Seq (genBurro k1) (genBurro k2)
27 genBurro (Kondey.Cond ks) = error "need to refactor genCond"
23 genBurro (Kondey.Cond ks) = genCond ks
2824
29 genCond :: [Burro] -> Burro
30 genCond (b:bs) =
25 genCond :: [Kondey] -> Burro
26 genCond (kbranch:kbranches) =
3127 let
3228 counter = 0
33 fore = Seq (makeBranch counter b) (Test Null Null)
34 rest = genCondRest bs b (counter + 1)
29 fore = Seq (makeBranch counter (genBurro kbranch)) (Test Null Null)
30 rest = genCondRest kbranches kbranch (counter + 1)
3531 in
3632 Seq fore rest
3733
38 genCondRest :: [Burro] -> Burro -> Int -> Burro
34 genCondRest :: [Kondey] -> Kondey -> Int -> Burro
3935 genCondRest [] _prev _counter = Null
40 genCondRest (b:bs) prev counter =
36 genCondRest (kbranch:kbranches) prev counter =
4137 let
42 invB = inverse prev
38 b = genBurro kbranch
39 invB = inverse (genBurro prev)
4340 branch = makeBranch counter (catBurros invB b)
4441 coda = repl (counter * 2) Dec (Test Null Null)
4542 fore = repl (counter * 2) Dec (Seq branch coda)
46 rest = genCondRest bs b (counter + 1)
43 rest = genCondRest kbranches kbranch (counter + 1)
4744 in
4845 Seq fore rest
4946
3636 parseProgram rest (Seq acc GoRight)
3737 parseProgram ('!':rest) acc =
3838 parseProgram rest (Seq acc ToggleHalt)
39
3940 parseProgram ('(':rest) acc =
4041 let
4142 (rest', thenprog) = parseProgram rest Null
4748 (rest, acc)
4849 parseProgram (')':rest) acc =
4950 (rest, acc)
50 --TODO: parse Cond structure
51
52 -- FIXME: an awful hack
53 parseProgram ('{':'3':rest) acc =
54 let
55 (rest', prog1) = parseProgram rest Null
56 (rest'', prog2) = parseProgram rest' Null
57 (rest''', prog3) = parseProgram rest'' Null
58 test = Cond [prog1, prog2, prog3]
59 in
60 parseProgram rest''' (Seq acc test)
61 parseProgram ('}':rest) acc =
62 (rest, acc)
63
5164 parseProgram (_:rest) acc =
5265 parseProgram rest acc
5366