git @ Cat's Eye Technologies Turmac / e18380d
Refactor IR. Chris Pressey 2 months ago
3 changed file(s) with 19 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
9292 ]
9393 compileStep (CondSymbol branches) =
9494 compileBranches "self.tape.read_symbol()" branches
95 compileStep (WriteSymbol sym) =
96 "self.tape.write_symbol(" ++ show sym ++ ")"
97 compileStep (ChangeState state) =
98 "self.state = " ++ show state
99 compileStep MoveLeft = "self.tape.move_left()"
100 compileStep MoveRight = "self.tape.move_right()"
101 compileStep Halt = "self.halted = True"
95 compileStep (WriteMoveGoto sym dir state) = unlines [
96 "self.tape.write_symbol(" ++ show sym ++ ")",
97 if dir == -1 then "self.tape.move_left()" else "self.tape.move_right()",
98 if state == "H" then "self.halted = True" else "self.state = " ++ show state
99 ]
102100
103101 compileBranches :: String -> [(StateId, Prog)] -> String
104102 compileBranches cond branches = unlines $
1313 | Seq [Prog]
1414 | CondState [(StateId, Prog)]
1515 | CondSymbol [(Symbol, Prog)]
16 | WriteSymbol Symbol
17 | ChangeState StateId
18 | MoveLeft
19 | MoveRight
16 | WriteMoveGoto Symbol Int StateId
2017 | Halt
2118 deriving (Eq)
2219
2926 showProg n (Seq progs) = "Seq\n" ++ concatMap (\p -> indent (n+1) ++ showProg (n+1) p ++ "\n") progs
3027 showProg n (CondState branches) = "CondState\n" ++
3128 concatMap (\(state, p) -> indent (n+1) ++ show state ++ " ->\n" ++
32 indent (n+2) ++ showProg (n+2) p) branches
29 indent (n+2) ++ showProg (n+2) p) branches
3330 showProg n (CondSymbol branches) = "CondSymbol\n" ++
3431 concatMap (\(sym, p) -> indent (n+1) ++ show sym ++ " ->\n" ++
35 indent (n+2) ++ showProg (n+2) p) branches
36 showProg _ (WriteSymbol sym) = "Write " ++ show sym
37 showProg _ (ChangeState state) = "Goto " ++ show state
38 showProg _ MoveLeft = "Left"
39 showProg _ MoveRight = "Right"
40 showProg _ Halt = "Halt"
32 indent (n+2) ++ showProg (n+2) p) branches
33 showProg n (WriteMoveGoto sym dir state) = "Write " ++ show sym ++ "\n" ++
34 (indent n) ++ showGoto state ++ "\n" ++
35 (indent n) ++ showDir dir
36
37 showDir (-1) = "Left"
38 showDir 1 = "Right"
39 showDir other = "?ERROR(" ++ (show other) ++ ")"
40
41 showGoto "H" = "Halt"
42 showGoto state = "Goto " ++ show state
1717 | state <- states]
1818 where states = nub [s | (s,_,_,_,_) <- rules]
1919
20 -- Convert direction char to program
21 dirToProg :: Int -> Prog
22 dirToProg (-1) = MoveLeft
23 dirToProg (1) = MoveRight
24 dirToProg d = error $ "Invalid direction: " ++ (show d)
25
2620 -- Convert a single state's rules to CondSymbol
2721 stateRulesToProg :: [(Symbol, (Symbol, StateId, Direction))] -> Prog
2822 stateRulesToProg rules =
29 CondSymbol [(sym, Seq [WriteSymbol newSym,
30 changeState newState,
31 dirToProg dir])
23 CondSymbol [(sym, Seq [WriteMoveGoto newSym dir newState])
3224 | (sym, (newSym, newState, dir)) <- rules]
33 where
34 changeState "H" = Halt
35 changeState s = ChangeState s
3625
3726 -- Convert all rules to full program
3827 buildProgram :: [TMRule] -> Prog