module Development.Shake.ATS.Generate ( generateLinks
                                      ) where

import           Language.ATS
import           Lens.Micro

generateLinks :: String -> Either ATSError String
generateLinks :: String -> Either ATSError String
generateLinks = (ATS AlexPosn -> String)
-> Either ATSError (ATS AlexPosn) -> Either ATSError String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ATS AlexPosn -> String
forall a. Eq a => ATS a -> String
printATS (ATS AlexPosn -> String)
-> (ATS AlexPosn -> ATS AlexPosn) -> ATS AlexPosn -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ATS AlexPosn -> ATS AlexPosn
forall a. ATS a -> ATS a
generateLinks') (Either ATSError (ATS AlexPosn) -> Either ATSError String)
-> (String -> Either ATSError (ATS AlexPosn))
-> String
-> Either ATSError String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Either ATSError (ATS AlexPosn)
parseM

generateLinks' :: ATS a -> ATS a
generateLinks' :: ATS a -> ATS a
generateLinks' (ATS [Declaration a]
ds) = [Declaration a] -> ATS a
forall a. [Declaration a] -> ATS a
ATS ((Declaration a -> Declaration a)
-> [Declaration a] -> [Declaration a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Declaration a -> Declaration a
forall a. Declaration a -> Declaration a
g [Declaration a]
ds [Declaration a] -> [Declaration a] -> [Declaration a]
forall a. Semigroup a => a -> a -> a
<> [Declaration a
forall a. Declaration a
macDecl]) --  ATS [Local undefined (ATS (fmap g ds <> [macDecl])) (ATS mempty)]
    where g :: Declaration a -> Declaration a
g f :: Declaration a
f@Func{} = a -> Declaration a -> Declaration a
forall a. a -> Declaration a -> Declaration a
Extern a
forall a. HasCallStack => a
undefined (ASetter
  (Declaration a)
  (Declaration a)
  (Maybe (Expression a))
  (Maybe (Expression a))
-> Maybe (Expression a) -> Declaration a -> Declaration a
forall s t a b. ASetter s t a b -> b -> s -> t
set ((Function a -> Identity (Function a))
-> Declaration a -> Identity (Declaration a)
forall a. Traversal' (Declaration a) (Function a)
fun((Function a -> Identity (Function a))
 -> Declaration a -> Identity (Declaration a))
-> ((Maybe (Expression a) -> Identity (Maybe (Expression a)))
    -> Function a -> Identity (Function a))
-> ASetter
     (Declaration a)
     (Declaration a)
     (Maybe (Expression a))
     (Maybe (Expression a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(PreFunction Expression a -> Identity (PreFunction Expression a))
-> Function a -> Identity (Function a)
forall a. Traversal' (Function a) (PreFunction Expression a)
preF((PreFunction Expression a -> Identity (PreFunction Expression a))
 -> Function a -> Identity (Function a))
-> ((Maybe (Expression a) -> Identity (Maybe (Expression a)))
    -> PreFunction Expression a -> Identity (PreFunction Expression a))
-> (Maybe (Expression a) -> Identity (Maybe (Expression a)))
-> Function a
-> Identity (Function a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe (Expression a) -> Identity (Maybe (Expression a)))
-> PreFunction Expression a -> Identity (PreFunction Expression a)
forall (ek :: * -> *) a. Lens' (PreFunction ek a) (Maybe (ek a))
expression) Maybe (Expression a)
forall a. Maybe (Expression a)
expr Declaration a
f)
          g Declaration a
x        = Declaration a
x
          expr :: Maybe (Expression a)
expr = Expression a -> Maybe (Expression a)
forall a. a -> Maybe a
Just (String -> Expression a
forall a. String -> Expression a
StringLit String
"\"mac#\"")
          macDecl :: Declaration a
macDecl = String -> Declaration a
forall a. String -> Declaration a
Define String
"#define ATS_MAINATSFLAG 1"