Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. doc_type :: Type -> Doc
  2. doc_type TVoid         = text "void"
  3. doc_type TInt          = text "int"
  4. doc_type TFloat        = text "float"
  5. doc_type TChar         = text "char"
  6. doc_type (TPtr tp)     = (doc_type tp) <> text "*"
  7. doc_type (TStruct str) = text "struct" <+> text str
  8.  
  9. exp_doc :: Exp -> Doc
  10. exp_doc (EIdent str)                = text str
  11. exp_doc (ELit either)               = if isLeft either
  12.                                                 then text (show (head (lefts [either])))
  13.                                                 else text (rights [either])
  14. exp_doc (EBinOp str exp1 exp2)      = exp_doc exp1 <+> text str <+> exp_doc exp2
  15. exp_doc (EUnOpPre str exp)          = text str <> exp_doc exp
  16. exp_doc (EUnOpPost str exp)         = exp_doc exp <> text str
  17. exp_doc (EFunCall exp exps)        = exp_doc exp <> text "("
  18.                                                   <> exp_list_concat_doc exps
  19.                                                   <> text ")"
  20.  
  21. case_doc :: Case -> Doc
  22. case_doc CDefault   = text "default:""
  23. case_doc (CInt int) = text "case" <+> text (show int) <> text ":"
  24. case_doc (CChar c)  = text "case" <+> text c <> text ":"
  25.  
  26. stmt_doc :: Stmt -> Doc
  27. stmt_doc SBreak                           = text "break;"
  28. stmt_doc SContinue                        = text "continue;"
  29. stmt_doc (SLoc tp str maybe_exp)          = doc_type tp <+> text str <+> doc
  30.                                                                     where doc = if isNothing maybe_exp
  31.                                                                                             then text ";"
  32.                                                                                             else text " = "
  33.                                                                   <> exp_doc (fromJust maybe_exp)
  34.                                                                  <> text ";"
  35. stmt_doc (SExp exp)                       = exp_doc exp <> text ";"
  36. stmt_doc (SReturn exp)                    = text "return" <+> exp_doc exp <> text ";"
  37. stmt_doc (SBlock stmts)                   = text "{" <> nest 2 (statements stmts) <!> text "}"
  38. stmt_doc (SIf exp stmt m_stmt)              = text "if (" <> exp_doc exp <> text ")"
  39.                                                                 <+> stmt_doc stmt <> text "" <> doc
  40.                                                                       where doc = if isNothing m_stmt
  41.                                                                                               then text ""
  42.                                                                                               else line
  43.                                                                     <> text "else "
  44.                                                                     <> stmt_doc(fromJust m_stmt)
  45. stmt_doc (SSwitch Exp [Either Case Stmt]) =
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement