From e0946200eaca8ebf1efb8493d204b7f59e99d9dd Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 4 May 2020 16:50:14 -0700 Subject: [PATCH] Add a basic Python code generator. --- src/Cdl/Codegen.hs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/Cdl/Codegen.hs diff --git a/src/Cdl/Codegen.hs b/src/Cdl/Codegen.hs new file mode 100644 index 0000000..258ae38 --- /dev/null +++ b/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])