<commit_before>-- | Module to generate Javascript from a ModGuts structure
module CodeGen.Javascript.Generate (generate) where
import GhcPlugins as P
import Name
import OccName
import TypeRep

import CodeGen.Javascript.Monad
import CodeGen.Javascript.AST as AST
import CodeGen.Javascript.Bag as Bag hiding (concat)

import Debug.Trace

generate :: ModGuts -> [JSStmt]
generate = genAST

genAST :: ModGuts -> [JSStmt]
genAST = concat . map (toList . snd . genJS . genBind) . mg_binds

genBind :: CoreBind -> JSGen ()
genBind (NonRec v ex) = do
  let (retEx, body) = genJS (genEx ex)
  v' <- genVar v
  emit $ Assign (AST.Var v') (Thunk (toList $ body) retEx)

genEx :: Expr Var -> JSGen JSExp
genEx (P.Var v) =
  genVar v >>= return . AST.Var
genEx (P.Lit lit) =
  genLit lit
genEx (App exp arg) = do
  exp' <- genEx exp
  let (argEx, argBody) = genJS $ genEx arg
  return $ Call exp' [Thunk (toList argBody) argEx]
genEx (Lam v exp) =
  genFun [v] exp<commit_msg>Generate code for recursive bindings too.<commit_after>-- | Module to generate Javascript from a ModGuts structure
module CodeGen.Javascript.Generate (generate) where
import GhcPlugins as P
import Name
import OccName
import TypeRep

import CodeGen.Javascript.Monad
import CodeGen.Javascript.AST as AST
import CodeGen.Javascript.Bag as Bag hiding (concat)

import Debug.Trace

generate :: ModGuts -> [JSStmt]
generate = genAST

genAST :: ModGuts -> [JSStmt]
genAST = concat . map (toList . snd . genJS . genBind) . mg_binds

genBind :: CoreBind -> JSGen ()
genBind (NonRec v ex) = do
  let (retEx, body) = genJS (genEx ex)
  v' <- genVar v
  emit $ Assign (AST.Var v') (Thunk (toList $ body) retEx)
genBind (Rec bs) =
  mapM_ (\(v, ex) -> genBind (NonRec v ex)) bs

genEx :: Expr Var -> JSGen JSExp
genEx (P.Var v) =
  genVar v >>= return . AST.Var
genEx (P.Lit lit) =
  genLit lit
genEx (App exp arg) = do
  exp' <- genEx exp
  let (argEx, argBody) = genJS $ genEx arg
  return $ Call exp' [Thunk (toList argBody) argEx]
genEx (Lam v exp) =
  genFun [v] exp
{'fraction_non_alphanumeric': 0.11287128712871287, 'fraction_numerical': 0.0, 'mean_word_length': 3.6566820276497696, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, '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': '346198', 'n_tokens_mistral': 749, 'n_tokens_neox': 722, 'n_words': 261}