aboutsummaryrefslogtreecommitdiff
path: root/ABC/Assembler.icl
diff options
context:
space:
mode:
authorCamil Staps2016-07-06 12:45:03 +0200
committerCamil Staps2016-07-06 12:45:03 +0200
commitd13c8c39bde8c0cfea466a4cff5189b9532005ce (patch)
tree1347f3b67d14a8ae3df774d57d20b1130d3f0d80 /ABC/Assembler.icl
parentAdded Machine as module level (diff)
toString for Assembler (and Statement)
Diffstat (limited to 'ABC/Assembler.icl')
-rw-r--r--ABC/Assembler.icl43
1 files changed, 43 insertions, 0 deletions
diff --git a/ABC/Assembler.icl b/ABC/Assembler.icl
index e5e3a9a..656ea1a 100644
--- a/ABC/Assembler.icl
+++ b/ABC/Assembler.icl
@@ -1,10 +1,53 @@
implementation module ABC.Assembler
import StdEnv
+import StdGeneric
import ABC.Machine
import ABC.Misc
+instance toString Assembler
+where
+ toString [] = ""
+ toString [stm=:(Label l):r] = stm <+ "\n" <+ r
+ toString [stm=:(Descriptor _ _ _ _):r] = toString r
+ toString [stm :r] = "\t" <+ stm <+ "\n" <+ r
+
+generic gPrint a :: !a -> [Char]
+gPrint{|Int|} x = fromString (toString x)
+gPrint{|Bool|} x = map toLower (fromString (toString x))
+gPrint{|String|} x = fromString x
+gPrint{|UNIT|} x = []
+gPrint{|EITHER|} fl fr (LEFT x) = fl x
+gPrint{|EITHER|} fl fr (RIGHT x) = fr x
+gPrint{|PAIR|} fl fr (PAIR x y) = fl x ++ ['\t':fr y]
+gPrint{|OBJECT|} fx (OBJECT x) = fx x
+gPrint{|CONS of d|} fx (CONS x) = case d.gcd_name of
+ "Label" = fx x
+ "Descriptor" = []
+ "Dump" = ['dump\t"'] ++ quote (fx x) ++ ['"']
+ "Print" = ['print\t"'] ++ quote (fx x) ++ ['"']
+ name = tl (cons (fromString name)) ++ ['\t':fx x]
+where
+ cons :: ![Char] -> [Char]
+ cons [] = []
+ cons [c:cs]
+ | isUpper c = ['_':toLower c:cons cs]
+ | otherwise = [c :cons cs]
+
+derive gPrint Statement
+
+instance toString Statement
+where
+ toString stm = toString (gPrint{|*|} stm)
+
+quote :: ![Char] -> [Char]
+quote [] = []
+quote ['\\':cs] = ['\\':'\\':quote cs]
+quote ['\n':cs] = ['\\':'n' :quote cs]
+quote ['"' :cs] = ['\\':'"' :quote cs]
+quote [c :cs] = [c :quote cs]
+
assemble :: Assembler -> ([Instruction], [Desc])
assemble stms = (translate stms loc_counter syms, descTable stms syms)
where