works kinda

This commit is contained in:
2026-09-15 02:21:00 -06:00
parent fc931ca77c
commit daf21ce3f5
+29 -22
View File
@@ -20,7 +20,6 @@ import Data.Monoid (Ap(Ap, getAp))
import Gyehoek.Sexp.Grammar.Base ((:-)(..)) import Gyehoek.Sexp.Grammar.Base ((:-)(..))
import Control.Lens.Extras (is) import Control.Lens.Extras (is)
import Control.Applicative (Alternative(..)) import Control.Applicative (Alternative(..))
import Effectful.Writer.Static.Shared
import Gyehoek.GenSym import Gyehoek.GenSym
import Data.HashSet.Lens (setOf) import Data.HashSet.Lens (setOf)
import Data.List (sort) import Data.List (sort)
@@ -29,7 +28,7 @@ import Gyehoek.Jalmot
import Data.Maybe (isNothing) import Data.Maybe (isNothing)
import Data.Kind (Type) import Data.Kind (Type)
import Data.Traversable (for, mapAccumR) import Data.Traversable (for, mapAccumR)
import Effectful.State.Static.Shared import Effectful.State.Dynamic
import Witherable import Witherable
import Data.Semigroup (Arg(..)) import Data.Semigroup (Arg(..))
import Data.Ord (Down(..)) import Data.Ord (Down(..))
@@ -44,6 +43,7 @@ data Bind
newtype Scope = MkScope { identity :: Natural } newtype Scope = MkScope { identity :: Natural }
deriving stock (Show, Generic, Eq, Ord) deriving stock (Show, Generic, Eq, Ord)
deriving anyclass (Hashable) deriving anyclass (Hashable)
deriving newtype (Gen)
type ScopeSet = HashSet Scope type ScopeSet = HashSet Scope
@@ -227,9 +227,7 @@ instance S.DatumIso Tem where
affixEllipses [] = [] affixEllipses [] = []
instance S.DatumIso Scope where instance S.DatumIso Scope where
datumIso = S.with \g -> datumIso = S.with (S.datumIso >>>)
S.list (S.el S.datumIso >>> S.el S.datumIso)
>>> g
hashSetGrammar hashSetGrammar
:: forall a. (Hashable a, Ord a) :: forall a. (Hashable a, Ord a)
@@ -274,7 +272,7 @@ optEl g =
anykw :: Text -> S.G (Datum :- t) t anykw :: Text -> S.G (Datum :- t) t
anykw s = S.Flip $ S.PartialIso anykw s = S.Flip $ S.PartialIso
(\t -> (Symbol s & ann . #syntax .~ SynBuiltin) :- t) (\t -> adorn SynBuiltin (Symbol s) :- t)
\case \case
(Symbol _ :- t) -> Right t (Symbol _ :- t) -> Right t
_ -> Left $ S.expected "symbol" _ -> Left $ S.expected "symbol"
@@ -331,7 +329,7 @@ instance S.DatumIso Bind where
-- ... -- ...
match :: Pat -> Datum -> Maybe (HashMap Name Datum) match :: Pat -> Datum -> Maybe (HashMap Name Datum)
match p = getAp . match' p match p = getAp . match' p
match' :: Pat -> Datum -> Ap Maybe (HashMap Name Datum) match' :: Pat -> Datum -> Ap Maybe (HashMap Name Datum)
match' PatWildcard _ = pure mempty match' PatWildcard _ = pure mempty
@@ -395,8 +393,12 @@ instance Monoid SymTable where mempty = MkSymTable mempty
type Expand es = (State SymTable :> es, GenSym :> es) type Expand es = (State SymTable :> es, GenSym :> es)
runExpand :: Eff (State SymTable : GenSym : es) a -> Eff es (a, SymTable) runExpand :: SymTable -> Eff (State SymTable : GenSym : es) a -> Eff es (a, SymTable)
runExpand = runGenSym . runState mempty runExpand tab = runGenSym . runStateLocal tab
testResolve :: Datum -> IO (Datum, SymTable)
testResolve = runJalmotIO . runExpand (symTableOfEnv env_scheme_base)
. resolve env_scheme_base mempty
{- | {- |
>>> :{ >>> :{
@@ -451,6 +453,10 @@ env_scheme_base = MkEnv . fromList . fold $
, "let-syntax" , "let-syntax"
] ]
symTableOfEnv :: Env -> SymTable
symTableOfEnv = ifoldMapOf (#names . itraversed) \cases
b@(BindGlobal n) d -> binding n mempty b
type instance IxValue Env = Denot type instance IxValue Env = Denot
type instance Index Env = Bind type instance Index Env = Bind
@@ -474,19 +480,20 @@ resolve
:: (Expand es, Jalmot :> es) :: (Expand es, Jalmot :> es)
=> Env -> HashSet Scope -> Datum -> Eff es Datum => Env -> HashSet Scope -> Datum -> Eff es Datum
resolve g scopes (Symbol s) = case lookupSymbol g scopes s of resolve g scopes (Symbol s) = do
Just (bind,_) -> pure [S.sx|#{bind}|] (bind,_) <- lookupSymbol g scopes (MkName s)
Nothing -> err [i|심벌 #{s}가 정의되지 않다|] pure [S.sx|#{bind}|]
resolve g scopes e@(List xs) resolve g scopes e@(List xs)
| Symbol s : xs' <- xs | Symbol s : xs' <- xs
, Just (_,denot) <- lookupSymbol g scopes s = do
= case denot of (_,denot) <- lookupSymbol g scopes (MkName s)
DenotVar -> _ case denot of
DenotMacro t -> _ DenotVar -> _
DenotPrim primname -> case parsePrim @Datum primname e of DenotMacro t -> _
Just p -> resolvePrim g scopes p DenotPrim primname -> case parsePrim @Datum primname e of
Nothing -> err [i|prim에서 신택스는 잘못한다: #{e}|] Just p -> resolvePrim g scopes p
Nothing -> err [i|prim에서 신택스는 잘못한다: #{e}|]
| otherwise = List <$> traverse (resolve g scopes) xs | otherwise = List <$> traverse (resolve g scopes) xs
resolvePrim resolvePrim
@@ -504,7 +511,7 @@ resolvePrim g scopes (PrimLet (MkPrimLet n bs body)) = do
] ]
body' <- traverse (resolve g' scopes') body body' <- traverse (resolve g' scopes') body
let n_g = [ MkKey x scopes' | x <- foldMap (:[]) n ] let n_g = [ MkKey x scopes' | x <- foldMap (:[]) n ]
let bs_g = [ MkKey x scopes | x <- bs ^.. each . _1 ] let bs_g = [ (MkKey lhs scopes',rhs) | (lhs,rhs) <- bs ^.. each ]
pure [S.sx| pure [S.sx|
(let ##{n_g} #{bs_g} (let ##{n_g} #{bs_g}
##{body'}) ##{body'})
@@ -533,10 +540,10 @@ gensymLexical symbol = do
pure $ BindLexical {symbol,identity} pure $ BindLexical {symbol,identity}
lookupSymbol lookupSymbol
:: (State SymTable :> es, Jalmot :> es) :: forall es. (State SymTable :> es, Jalmot :> es)
=> Env -> HashSet Scope -> Name -> Eff es (Bind, Denot) => Env -> HashSet Scope -> Name -> Eff es (Bind, Denot)
lookupSymbol g scopes x = do lookupSymbol g scopes x = do
ss <- fmap fold . preuse @SymTable $ ix x ss <- fmap fold . preuse @SymTable @(Eff es) $ ix x
case nearest (MkKey x scopes) (H.keys ss) of case nearest (MkKey x scopes) (H.keys ss) of
[] -> err [i|심벌 #{x}는 정의되지 않다|] [] -> err [i|심벌 #{x}는 정의되지 않다|]
(_:_:_) -> err [i|심벌 #{x}는 모호하다|] (_:_:_) -> err [i|심벌 #{x}는 모호하다|]