update parser to be able to use data/effect decls it encounters

This commit is contained in:
Arya Irani 2018-06-04 14:23:44 -04:00
parent dcf6acb372
commit 58302ae563
7 changed files with 31 additions and 23 deletions

View File

@ -27,6 +27,13 @@ data DataDeclaration v = DataDeclaration {
constructors :: [(v, Type v)]
} deriving (Show)
newtype EffectDeclaration v = EffectDeclaration {
toDataDecl :: DataDeclaration v
} deriving (Show)
mkEffectDecl :: [v] -> [(v, Type v)] -> EffectDeclaration v
mkEffectDecl = (EffectDeclaration .) . DataDeclaration
-- type List a = Nil | Cons a (List a)
-- cycle (abs "List" (LetRec [abs "a" (cycle (absChain ["Nil","Cons"] (Constructors [List a, a -> List a -> List a])))] "List"))
-- absChain [a] (cycle ())"List")

View File

@ -1,11 +0,0 @@
module Unison.EffectDeclaration where
import Unison.Type (Type)
import Unison.DataDeclaration
newtype EffectDeclaration v = EffectDeclaration {
toDataDecl :: DataDeclaration v
} deriving (Show)
mkEffectDecl :: [v] -> [(v, Type v)] -> EffectDeclaration v
mkEffectDecl = (EffectDeclaration .) . DataDeclaration

View File

@ -1,3 +1,5 @@
{-# Language OverloadedStrings #-}
module Unison.FileParser where
-- import Text.Parsec.Prim (ParsecT)
@ -7,10 +9,11 @@ import Unison.Parser
import Control.Applicative
import Data.Either (partitionEithers)
import Data.Map (Map)
import Unison.DataDeclaration (DataDeclaration(..))
import Unison.EffectDeclaration (EffectDeclaration(..), mkEffectDecl)
import Unison.DataDeclaration (DataDeclaration(..), hashDecls,
EffectDeclaration(..), mkEffectDecl)
import Unison.Parser (PEnv, penv0)
import Unison.Parsers (unsafeGetRight)
import Unison.Reference (Reference)
import Unison.Symbol (Symbol)
import Unison.Term (Term)
import Unison.TypeParser (S)
@ -21,10 +24,12 @@ import qualified Unison.Parsers as Parsers
import qualified Unison.Type as Type
import qualified Unison.TermParser as TermParser
import qualified Unison.TypeParser as TypeParser
import qualified Unison.Var as Var
import Control.Monad.Reader
import Data.Text.IO (readFile)
import System.IO (FilePath)
import qualified Data.Text as Text
import Debug.Trace
data UnisonFile v = UnisonFile {
dataDeclarations :: Map v (DataDeclaration v),
@ -57,8 +62,15 @@ file = traced "file" $ do
term <- TermParser.block
pure $ UnisonFile dataDecls effectDecls term
environmentFor :: Map v (DataDeclaration v) -> Map v (EffectDeclaration v) -> PEnv
environmentFor _ _ = Map.empty -- todo
environmentFor :: Var v => Map v (DataDeclaration v) -> Map v (EffectDeclaration v) -> PEnv
environmentFor dataDecls effectDecls = -- todo: add effectDecls
trace "new env" $ traceShowId $ Map.fromList (constructors' =<< hashDecls (Map.union dataDecls (toDataDecl <$> effectDecls)))
constructors' :: Var v => (v, Reference, DataDeclaration v) -> [(String, (Reference, Int))]
constructors' (typeSymbol, r, (DataDeclaration _ constructors)) =
let qualCtorName ((ctor,_), i) =
(Text.unpack $ mconcat [Var.qualifiedName typeSymbol, ".", Var.qualifiedName ctor], (r, i))
in qualCtorName <$> constructors `zip` [0..]
declarations :: Var v => Parser (S v)
(Map v (DataDeclaration v),

View File

@ -18,7 +18,9 @@ instance Var Symbol where
name (Symbol _ n) = n
named n = Symbol 0 n
clear (Symbol id n) = Symbol id n
qualifiedName s = name s `Text.append` (Text.pack (show (freshId s)))
qualifiedName s =
if freshId s /= 0 then name s `Text.append` (Text.pack (show (freshId s)))
else name s
freshIn vs s | Set.null vs || Set.notMember s vs = s -- already fresh!
freshIn vs s@(Symbol i n) = case Set.elemAt (Set.size vs - 1) vs of
Symbol i2 _ -> if i > i2 then s else Symbol (i2+1) n

View File

@ -7,7 +7,6 @@ module Unison.Test.FileParser where
import Unison.FileParser
import Unison.Parser
import Unison.DataDeclaration
import Unison.EffectDeclaration
import qualified Unison.Parser as Parser
import qualified Unison.Parsers as Parsers
import Unison.Parsers (unsafeGetRight)
@ -16,7 +15,7 @@ module Unison.Test.FileParser where
import qualified Unison.Reference as R
import Unison.Symbol (Symbol)
test1 = scope "fileparser" . tests . map parses $
test1 = scope "fileparser.test1" . tests . map parses $
[
"type Pair a b = Pair a b\n()"
, "type Optional a = Just a | Nothing\n()"
@ -46,13 +45,13 @@ module Unison.Test.FileParser where
,"ping"]
]
test2 = scope "fileparser.test1" $ do
test2 = scope "fileparser.test2" $ do
file <- io $ unsafeReadAndParseFile' "unison-src/test1.u"
io $ putStrLn (show (file :: UnisonFile Symbol))
ok
test = test2
-- test1 <|> test2
test = --test2
test1 <|> test2
builtins = Map.fromList
[("Pair", (R.Builtin "Pair", 0)),

View File

@ -37,7 +37,6 @@ library
Unison.ABT
Unison.Builtin
Unison.DataDeclaration
Unison.EffectDeclaration
Unison.FileParser
Unison.Hash
Unison.Hashable

View File

@ -3,7 +3,7 @@ type Optional a = None | Some a
Optional.isEmpty : ∀ a . Optional a -> Boolean
Optional.isEmpty o = case o of
None -> True
Optional.None -> True
_ -> False
()