module Language.Atom.Example.External where
import Language.Atom
main :: IO ()
main = do
let atomCfg = defaults { cCode = prePostCode , cRuleCoverage = False }
(sched, _, _, _, _) <- compile "extern_example" atomCfg extern
putStrLn $ reportSchedule sched
extern :: Atom ()
extern = do
let rng = word16' "g_random"
atom "update" $ do
call "new_random"
printIntegralE "g_random" $ value rng
prePostCode :: [Name] -> [Name] -> [(Name, Type)] -> (String, String)
prePostCode _ _ _ =
( unlines [ "// ---- This source is automatically generated by Atom ----"
, "#include <stdio.h>"
, "#include <stdlib.h>"
, "#include <unistd.h>"
, ""
, "static uint16_t g_random = 0;"
, "void new_random(void);"
]
, unlines [ "int main(void) {"
, " while (true) {"
, " extern_example();"
, " usleep(1000);"
, " }"
, " return 0;"
, "}"
, ""
, "void new_random(void) {"
, " g_random = rand();"
, "}"
, "// ---- End automatically-generated source ----"
])