Guest User

Untitled

a guest
Jun 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import IR
  2.  
  3. type C = String
  4.  
  5. genC x = helper "" "" 0 x
  6. where helper :: C -> C -> Int -> Statement -> C
  7.  
  8. helper defs code n (Sequence (x:[])) = helper (defs ++ genDefs n True x) (code ++ genCode n True x) (n + countDefs x) (Sequence [])
  9. helper defs code n (Sequence (x:xs)) = helper (defs ++ genDefs n False x) (code ++ genCode n False x) (n + countDefs x) (Sequence xs)
  10. helper defs code n (Sequence _ ) = "#include \"routines.h\"\nint main(){" ++ defs ++ code ++ "}"
  11.  
  12. helper defs code n (Command x xs) = "#include \"routines.h\"\nint main(){" ++ genDefs 0 True (Command x xs) ++ genCode 0 True (Command x xs) ++ "}"
  13.  
  14. countDefs :: Statement -> Int
  15. countDefs (Command _ _) = 1
  16. countDefs _ = 0
  17.  
  18. genDefs :: Int -> Bool -> Statement -> C
  19. genDefs n return (Command (ConcatA x) _) = "const char* cmd" ++ show n ++ "[] = { " ++ fromConcatA x ++ "NULL };" ++ (if return then "int retval;" else "")
  20. genDefs n return (Command x _) = "const char* cmd" ++ show n ++ "[] = { " ++ fromArray x ++ "NULL };" ++ (if return then "int retval;" else "")
  21. genDefs n return x = "genDefs: n=" ++ show n ++ ", x=" ++ show x ++ ""
  22.  
  23. fromConcatA :: [Array] -> C
  24. fromConcatA ((Field x):[]) = "\"" ++ parseExpression x ++ "\", "
  25. fromConcatA ((Field x):xs) = "\"" ++ parseExpression x ++ "\", " ++ fromConcatA xs
  26. fromConcatA _ = ""
  27.  
  28. fromArray :: Array -> C
  29. fromArray (Field x) = "\"" ++ parseExpression x ++ "\", "
  30. fromArray _ = ""
  31.  
  32. parseExpression :: Expression -> C
  33. parseExpression (Const x) = x
  34. parseExpression _ = ""
  35.  
  36. genCode :: Int -> Bool -> Statement -> C
  37. genCode n return (Command (ConcatA x) _) = (if return then "retval=" else "") ++ "exec_command (cmd" ++ show n ++ ");" ++ (if return then "return retval;" else "")
  38. genCode n return (Command x _) = (if return then "retval=" else "") ++ "exec_command (cmd" ++ show n ++ ");" ++ (if return then "return retval;" else "")
  39. genCode n return x = "genCode: n=" ++ show n ++ ", x=" ++ show x ++ ""
  40.  
  41.  
  42. main = putStr (genC ( Sequence [ Command (ConcatA [Field (Const "ping"),Field (Const "-c5"),Field (Const "google.com")]) [], Command (Field (Const "date")) [], Command (ConcatA [Field (Const "tuch"),Field (Const "example.file")]) [], Command (Field (Const "ncal")) [], Command (ConcatA [Field (Const "echo"),Field (Const "Hello, world!")]) [] ] ) )
  43. --main = putStr (genC (Command (Field (Const "date")) []))
  44. --main = putStr (genC (Sequence [(Command (ConcatA [Field (Const "ping"),Field (Const "google.com")]) [])]))
Add Comment
Please, Sign In to add comment