Browse Source

Add a basic Python code generator.

master
Danila Fedorin 2 years ago
parent
commit
e0946200ea
  1. 35
      src/Cdl/Codegen.hs

35
src/Cdl/Codegen.hs

@ -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])
Loading…
Cancel
Save