<commit_before>{-# LANGUAGE OverloadedStrings #-}
module CodeGen where
import Gen
import AST
import Control.Monad.State
import Control.Applicative
import qualified Data.Map as M
import Language.C.DSL
import Data.String

type CodeGenM = StateT (M.Map Var String) Gen

codegen :: [SDec ClosPrim] -> [CExtDecl]
codegen = runGen . flip evalStateT (M.empty) . mapM generateSDec

mangle :: Var -> CodeGenM String
mangle v = do
  res <- M.lookup v <$> get
  case res of
    Nothing -> makeLegal >>= modify . M.insert v >> (M.! v) <$> get
    Just s  -> return s
  where makeLegal = ("__" ++) . show <$> gen

generate :: SExp ClosPrim -> CodeGenM CExpr
generate (Var v) = fromString <$> mangle v
generate (App f args) = (#) <$> generate f <*> mapM generate args
generate (If test true false) = ternary <$> generate test <*> generate true <*> generate false
generate (Prim (NewClos v)) = do
  name <- mangle v
  return . fromString $ "<<primitive: NewClos " ++ name ++ ">>"
generate (Prim p) = return . fromString $ "<<primitive: " ++ show p ++ ">>"
generate (Lit (SInt i))  = return $ "mkInt"#[fromInteger . toInteger $ i]
generate (Lit (SSym s))  = return $ "mkSym"#[fromString $ s]
generate (Set v e) = ("set"#) . (:[]) <$> generate e
generate Lam{} = error "Hey you've found a lambda in a bad spot. CRY TEARS OF BLOOD"

generateSDec :: SDec ClosPrim -> CodeGenM CExtDecl
generateSDec (Def v (Lam args exps)) = do
  funName <- fromString <$> mangle v
  vars <- map (int . fromString) <$> mapM mangle args
  body <- mapM generate exps
  return . export $ fun [voidTy] funName vars (hBlock body)
generateSDec (Def v (App _ [e])) = do<commit_msg>More updates to codegen, NewClos now generates correctish code<commit_after>{-# LANGUAGE OverloadedStrings #-}
module CodeGen where
import Gen
import AST
import Control.Monad.State
import Control.Applicative
import qualified Data.Map as M
import Language.C.DSL
import Data.String

type CodeGenM = StateT (M.Map Var String) Gen

codegen :: [SDec ClosPrim] -> [CExtDecl]
codegen = runGen . flip evalStateT (M.empty) . mapM generateSDec

mangle :: Var -> CodeGenM String
mangle v = do
  res <- M.lookup v <$> get
  case res of
    Nothing -> makeLegal >>= modify . M.insert v >> (M.! v) <$> get
    Just s  -> return s
  where makeLegal = ("__" ++) . show <$> gen

generate :: SExp ClosPrim -> CodeGenM CExpr
generate (Var v) = fromString <$> mangle v
generate (If test true false) = ternary <$> generate test <*> generate true <*> generate false
generate (App (Prim (NewClos v)) args) = do
  name <- fromString <$> mangle v
  escaping <- mapM generate args
  return $ name <-- "mkClos"#escaping
generate (Prim p) = return . fromString $ "<<primitive: " ++ show p ++ ">>"
generate (Lit (SInt i))  = return $ "mkInt"#[fromInteger . toInteger $ i]
generate (Lit (SSym s))  = return $ "mkSym"#[fromString $ s]
generate (App f args) = (#) <$> generate f <*> mapM generate args
generate (Set v e) = ("set"#) . (:[]) <$> generate e
generate Lam{} = error "Hey you've found a lambda in a bad spot. CRY TEARS OF BLOOD"

generateSDec :: SDec ClosPrim -> CodeGenM CExtDecl
generateSDec (Def v (Lam args exps)) = do
  funName <- fromString <$> mangle v
  vars <- map (int . fromString) <$> mapM mangle args
  body <- mapM generate exps
  return . export $ fun [voidTy] funName vars (hBlock body)
generateSDec (Def v (App _ [e])) = do
{'fraction_non_alphanumeric': 0.1394517282479142, 'fraction_numerical': 0.0, 'mean_word_length': 3.728169014084507, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1147502', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1138, 'n_words': 418}