This commit is contained in:
@@ -53,6 +53,9 @@ blah' = go [] where
|
|||||||
go acc [] k = k (reverse acc)
|
go acc [] k = k (reverse acc)
|
||||||
go acc (f:fs) k = f \a -> go (a:acc) fs k
|
go acc (f:fs) k = f \a -> go (a:acc) fs k
|
||||||
|
|
||||||
|
telescope :: Traversable t => t ((a -> r) -> r) -> (t a -> r) -> r
|
||||||
|
telescope = runCont . traverse cont
|
||||||
|
|
||||||
-- 뻘짓이어라
|
-- 뻘짓이어라
|
||||||
blah :: forall t a r. Foldable t => t ((a -> r) -> r) -> (List a -> r) -> r
|
blah :: forall t a r. Foldable t => t ((a -> r) -> r) -> (List a -> r) -> r
|
||||||
blah xs k =
|
blah xs k =
|
||||||
@@ -67,14 +70,9 @@ toANF'
|
|||||||
toANF' (Lam.ExpVal v) k = k v
|
toANF' (Lam.ExpVal v) k = k v
|
||||||
|
|
||||||
toANF' (Lam.ExpApply f xs) k =
|
toANF' (Lam.ExpApply f xs) k =
|
||||||
blah (toANF' <$> (f:|xs)) \(f':xs') -> do
|
telescope (toANF' <$> (f:|xs)) \(f':|xs') -> do
|
||||||
r <- gensym
|
r <- gensym
|
||||||
ExpLetApply r f' xs' <$> k (ValVar r)
|
ExpLetApply r f' xs' <$> k (ValVar r)
|
||||||
where
|
|
||||||
allToANF' es k = traverse (\e -> toANF' e k) es
|
|
||||||
-- blah = traverse g (f :| xs)
|
|
||||||
-- g :: Lam.Exp -> Eff es Val
|
|
||||||
-- g = ContT . toANF'
|
|
||||||
|
|
||||||
toANF' e k = _
|
toANF' e k = _
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,22 @@
|
|||||||
|
{-# LANGUAGE DeriveGeneric #-}
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE TypeOperators #-}
|
||||||
|
{-# LANGUAGE PartialTypeSignatures #-}
|
||||||
module Gyehoek.Syntax where
|
module Gyehoek.Syntax where
|
||||||
|
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import Data.List (List)
|
import Data.List (List)
|
||||||
|
import Language.SexpGrammar as Sexp hiding (List)
|
||||||
|
import Language.SexpGrammar.Generic
|
||||||
|
import GHC.Generics
|
||||||
|
import Prelude hiding ((.), id)
|
||||||
|
import Control.Category
|
||||||
|
|
||||||
|
|
||||||
type Name = Text
|
type Name = Text
|
||||||
|
|
||||||
data Prim = PrimAdd | PrimSub | PrimMul | PrimDiv
|
data Prim = PrimAdd | PrimSub | PrimMul | PrimDiv
|
||||||
deriving (Show)
|
deriving (Show, Generic)
|
||||||
|
|
||||||
data Val
|
data Val
|
||||||
= ValInt Int
|
= ValInt Int
|
||||||
@@ -15,11 +24,55 @@ data Val
|
|||||||
| ValPrim Prim
|
| ValPrim Prim
|
||||||
| ValLambda (List Name) Exp
|
| ValLambda (List Name) Exp
|
||||||
| ValVar Name
|
| ValVar Name
|
||||||
deriving (Show)
|
deriving (Show, Generic)
|
||||||
|
|
||||||
data Exp
|
data Exp
|
||||||
= ExpLet (List (Name, Exp)) Exp
|
= ExpLet (List (Name, Exp)) Exp
|
||||||
| ExpApply Exp (List Exp)
|
| ExpApply Exp (List Exp)
|
||||||
| ExpProgn (List Exp)
|
| ExpBegin (List Exp)
|
||||||
| ExpVal Val
|
| ExpVal Val
|
||||||
deriving (Show)
|
deriving (Show, Generic)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
letIso
|
||||||
|
:: (forall t. Grammar Position (Sexp :- t) (a :- t))
|
||||||
|
-> (forall t. Grammar Position (Sexp :- t) (b :- t))
|
||||||
|
-> Grammar Position (Sexp :- (List (a, b) :- t1)) t2
|
||||||
|
-> Grammar Position (Sexp :- t1) t2
|
||||||
|
letIso name rhs e = list (el (sym "let") >>> el bindings >>> el e)
|
||||||
|
where
|
||||||
|
-- bindings :: Grammar Position (Sexp :- _) (List (_, _) :- _)
|
||||||
|
bindings = list $ rest binding
|
||||||
|
binding :: forall t. Grammar Position (Sexp :- t) ((_, _) :- t)
|
||||||
|
binding = list (el name >>> el rhs) >>> pair
|
||||||
|
|
||||||
|
instance SexpIso Prim where
|
||||||
|
sexpIso = match
|
||||||
|
$ With (. sym "+")
|
||||||
|
$ With (. sym "-")
|
||||||
|
$ With (. sym "*")
|
||||||
|
$ With (. sym "/")
|
||||||
|
$ End
|
||||||
|
|
||||||
|
instance SexpIso Val where
|
||||||
|
sexpIso = match
|
||||||
|
$ With (. sexpIso)
|
||||||
|
$ With (. sym "nil")
|
||||||
|
$ With (. sexpIso)
|
||||||
|
$ With lam
|
||||||
|
$ With (\var -> var . symbol)
|
||||||
|
$ End
|
||||||
|
where
|
||||||
|
lam q = q . list
|
||||||
|
( el (sym "lambda")
|
||||||
|
>>> el (sexpIso @(List Name))
|
||||||
|
>>> el sexpIso )
|
||||||
|
|
||||||
|
instance SexpIso Exp where
|
||||||
|
sexpIso = match
|
||||||
|
$ With (. letIso symbol sexpIso sexpIso)
|
||||||
|
$ With (\app -> app . list (el sexpIso >>> rest sexpIso))
|
||||||
|
$ With (\bgn -> bgn . list (el (sym "begin") >>> rest sexpIso))
|
||||||
|
$ With (<<< sexpIso)
|
||||||
|
$ End
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ executable gyehoek
|
|||||||
, text
|
, text
|
||||||
, vector
|
, vector
|
||||||
, generic-lens
|
, generic-lens
|
||||||
|
, sexp-grammar
|
||||||
|
|
||||||
hs-source-dirs: app
|
hs-source-dirs: app
|
||||||
default-language: GHC2024
|
default-language: GHC2024
|
||||||
|
|||||||
Reference in New Issue
Block a user