Add a basic Python code generator.

This commit is contained in:
Danila Fedorin 2020-05-04 16:50:14 -07:00
parent 52001a23e5
commit e0946200ea

35
src/Cdl/Codegen.hs Normal file
View File

@ -0,0 +1,35 @@
module Cdl.Codegen where
import Cdl.Cow hiding (dataPoints)
import Data.Fixed
import Data.List
standard =
[ "from server.fgs.model import *"
, "from server.fgs import db"
, "import datetime"
]
dataPoint :: DataPoint -> String
dataPoint (id, Coord{longitude=lo,latitude=la}) =
"DataPoint(longitude=" ++ show lo ++
", latitude=" ++ show la ++
", datetime=datetime.now())"
dataPointLine :: DataPoint -> String
dataPointLine dp = "db.session.add(" ++ dataPoint dp ++ ")"
commitLine :: String
commitLine = "db.session.commit()"
sleepLine :: Float -> String
sleepLine f = "sleep(" ++ show f ++ ")"
dataPoints :: [DataPoint] -> [String]
dataPoints dps = map dataPointLine dps ++ [commitLine]
dataBatches :: Float -> [[DataPoint]] -> [String]
dataBatches f dbs = intercalate [sleepLine f] $ map dataPoints dbs
outputCow :: String -> Float -> Collar -> IO ()
outputCow filename f c =
writeFile filename $ intercalate "\n" $ standard ++ (dataBatches f $ runCows f [c])