diff options
Diffstat (limited to 'ast.hs')
-rw-r--r-- | ast.hs | 21 |
1 files changed, 12 insertions, 9 deletions
@@ -14,7 +14,7 @@ newtype Program = Program [Instruction] data Instruction = IAdd Byte Offset | ISet Byte Offset - | IMove Offset [(Offset, Byte)] -- IMove from [(to, multiplier)] + | ICopy Offset Offset Byte -- ICopy from to multiplier | ISlide Offset | ILoop [Instruction] Offset | IInput Offset @@ -30,18 +30,22 @@ isISet :: Instruction -> Bool isISet (ISet _ _) = True isISet _ = False -isIMove :: Instruction -> Bool -isIMove (IMove _ _) = True -isIMove _ = False +isICopy :: Instruction -> Bool +isICopy (ICopy _ _ _) = True +isICopy _ = False isISlide :: Instruction -> Bool isISlide (ISlide _) = True isISlide _ = False +isIStart :: Instruction -> Bool +isIStart IStart = True +isIStart _ = False + offsetOf :: Instruction -> Offset offsetOf (IAdd _ o) = o offsetOf (ISet _ o) = o -offsetOf (IMove o _) = o +offsetOf (ICopy o _ _) = o offsetOf (ISlide _) = undefined offsetOf (ILoop _ _) = undefined offsetOf (IInput o) = o @@ -49,17 +53,16 @@ offsetOf (IOutput o) = o offsetOf IStart = 0 astSuccinct :: Program -> String -astSuccinct (Program inss) = concatMap insSuccinct inss +astSuccinct (Program inss) = intercalate " " $ map insSuccinct inss where insSuccinct :: Instruction -> String insSuccinct (IAdd v o) = let sv = signedByte v in (if sv >= 0 then "+" else "") ++ show (signedByte v) ++ ',' : show o insSuccinct (ISet v o) = '=' : show (signedByte v) ++ ',' : show o - insSuccinct (IMove from tos) = - 'M' : show from ++ '(' : intercalate "," (map (\(o,m) -> show o ++ '*' : show m) tos) ++ ")" + insSuccinct (ICopy from to v) = 'C' : show from ++ ',' : show to ++ ',' : show v insSuccinct (ISlide o) = '>' : show o - insSuccinct (ILoop inss' off) = "[(" ++ show off ++ ')' : concatMap insSuccinct inss' ++ "]" + insSuccinct (ILoop inss' off) = "[(" ++ show off ++ ')' : intercalate " " (map insSuccinct inss') ++ "]" insSuccinct (IInput o) = ',' : show o insSuccinct (IOutput o) = '.' : show o insSuccinct IStart = "$" |