Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split module parsing and types out better #1041

Merged
merged 4 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions smol-backend/test/Test/Helpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import qualified Data.Set.NonEmpty as NES
import Data.Text (Text)
import Smol.Core
import Smol.Core.Modules.FromParts
import Smol.Core.Modules.Parser
import Smol.Core.Modules.Types.Module
import Smol.Core.Typecheck.FromParsedExpr

Expand Down
2 changes: 1 addition & 1 deletion smol-backend/test/Test/IR/IRSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import qualified Smol.Backend.Compile.RunLLVM as Run
import Smol.Backend.IR.FromExpr.Expr
import Smol.Backend.IR.ToLLVM.ToLLVM
import Smol.Core.Modules.FromParts
import Smol.Core.Modules.Parser (parseModuleAndFormatError)
import Smol.Core.Modules.ResolveDeps
import Smol.Core.Modules.Typecheck
import Smol.Core.Modules.Types
import Smol.Core.Modules.Types.ModuleError
import Smol.Core.Parser (parseModuleAndFormatError)
import Smol.Core.Typecheck.Typeclass.Types
import Smol.Core.Types
import Test.BuiltInTypes
Expand Down
6 changes: 3 additions & 3 deletions smol-core/smol-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ library
Smol.Core.Modules.FromParts
Smol.Core.Modules.Helpers
Smol.Core.Modules.Interpret
Smol.Core.Modules.Parser
Smol.Core.Modules.PrettyPrint
Smol.Core.Modules.ResolveDeps
Smol.Core.Modules.RunTests
Expand All @@ -86,7 +87,6 @@ library
Smol.Core.Modules.Types.Module
Smol.Core.Modules.Types.ModuleError
Smol.Core.Modules.Types.ModuleItem
Smol.Core.Modules.Types.ModuleName
Smol.Core.Modules.Types.Test
Smol.Core.Modules.Types.TestName
Smol.Core.Modules.Types.TopLevelExpression
Expand All @@ -95,7 +95,6 @@ library
Smol.Core.Parser.DataType
Smol.Core.Parser.Expr
Smol.Core.Parser.Identifiers
Smol.Core.Parser.Module
Smol.Core.Parser.Op
Smol.Core.Parser.Pattern
Smol.Core.Parser.Primitives
Expand Down Expand Up @@ -148,6 +147,7 @@ library
Smol.Core.Types.DataType
Smol.Core.Types.Expr
Smol.Core.Types.Identifier
Smol.Core.Types.ModuleName
Smol.Core.Types.Op
Smol.Core.Types.ParseDep
Smol.Core.Types.Pattern
Expand All @@ -173,9 +173,9 @@ test-suite smol-core-tests
other-modules:
Test.BuiltInTypes
Test.Helpers
Test.Interpreter.InterpreterSpec
Test.Modules.FromPartsSpec
Test.Modules.InterpreterSpec
Test.Modules.ParserSpec
Test.Modules.ResolveDepsSpec
Test.Modules.RunTestsSpec
Test.Modules.TypecheckSpec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE OverloadedStrings #-}

module Smol.Core.Parser.Module
module Smol.Core.Modules.Parser
( moduleParser,
parseModule,
parseModuleAndFormatError,
)
where

import Data.Bifunctor (first)
import qualified Data.List.NonEmpty as NE
import Data.Text (Text)
import qualified Data.Text as T
import Data.Void
import Smol.Core.Modules.Types.ModuleItem
import Smol.Core.Modules.Types.TestName
import Smol.Core.Parser.DataType (dataTypeParser)
import Smol.Core.Parser.Expr
import Smol.Core.Parser.Identifiers
import Smol.Core.Parser.Primitives (textPrim)
import Smol.Core.Parser.Shared
import Smol.Core.Parser.Type
import Smol.Core.Parser.Typeclass
import Smol.Core.Typecheck.Typeclass.Types
import Smol.Core.Types
import Text.Megaparsec hiding (parseTest)
import Text.Megaparsec.Char

type Parser = Parsec Void Text

type ParseErrorType = ParseErrorBundle Text Void

parseAndFormat :: Parser a -> Text -> Either Text a
parseAndFormat p = first (T.pack . errorBundlePretty) . parse (p <* eof) "repl"

parseModule :: Text -> Either ParseErrorType [ModuleItem Annotation]
parseModule = parse (space *> moduleParser <* eof) "repl"

parseModuleAndFormatError :: Text -> Either Text [ModuleItem Annotation]
parseModuleAndFormatError = parseAndFormat (space *> moduleParser <* eof)

-------

-- currently fails at the first hurdle
-- since we can parse each thing separately, maybe
-- we should be making each throw errors for later, but returning `mempty` so
Expand Down Expand Up @@ -181,3 +201,6 @@ parseClass = do
tcFuncType = ty
}
)

testNameParser :: Parser TestName
testNameParser = myLexeme $ TestName <$> textPrim
2 changes: 0 additions & 2 deletions smol-core/src/Smol/Core/Modules/Types.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Smol.Core.Modules.Types
( module Smol.Core.Modules.Types.Entity,
module Smol.Core.Modules.Types.Module,
module Smol.Core.Modules.Types.ModuleName,
module Smol.Core.Modules.Types.ModuleItem,
module Smol.Core.Modules.Types.TestName,
module Smol.Core.Modules.Types.DefIdentifier,
Expand All @@ -14,7 +13,6 @@ import Smol.Core.Modules.Types.DefIdentifier
import Smol.Core.Modules.Types.Entity
import Smol.Core.Modules.Types.Module
import Smol.Core.Modules.Types.ModuleItem
import Smol.Core.Modules.Types.ModuleName
import Smol.Core.Modules.Types.Test
import Smol.Core.Modules.Types.TestName
import Smol.Core.Modules.Types.TopLevelExpression
2 changes: 1 addition & 1 deletion smol-core/src/Smol/Core/Modules/Types/Entity.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module Smol.Core.Modules.Types.Entity where
-- terrible, pls improve
import Data.Aeson (FromJSON, FromJSONKey, ToJSON, ToJSONKey)
import GHC.Generics (Generic)
import Smol.Core.Modules.Types.ModuleName
import Smol.Core.Printer
import Smol.Core.Types.Constructor
import Smol.Core.Types.Identifier
import Smol.Core.Types.ModuleName
import Smol.Core.Types.TypeName

data Entity
Expand Down
10 changes: 0 additions & 10 deletions smol-core/src/Smol/Core/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ module Smol.Core.Parser
parseExprAndFormatError,
parseTypeAndFormatError,
parseType,
parseModule,
parseConstraint,
parseModuleAndFormatError,
parseDataTypeAndFormatError,
parseConstraintAndFormatError,
ParseErrorType,
Expand All @@ -19,10 +17,8 @@ import Data.Bifunctor (first)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Void
import Smol.Core.Modules.Types
import Smol.Core.Parser.DataType (dataTypeParser)
import Smol.Core.Parser.Expr
import Smol.Core.Parser.Module
import Smol.Core.Parser.Type
import Smol.Core.Parser.Typeclass
import Smol.Core.Typecheck.Types
Expand All @@ -46,15 +42,9 @@ parseExpr = parse (space *> expressionParser <* eof) "repl"
parseExprAndFormatError :: Text -> Either Text ParserExpr
parseExprAndFormatError = parseAndFormat (space *> expressionParser <* eof)

parseModule :: Text -> Either ParseErrorType [ModuleItem Annotation]
parseModule = parse (space *> moduleParser <* eof) "repl"

parseConstraint :: Text -> Either ParseErrorType (Constraint ParseDep Annotation)
parseConstraint = parse (space *> constraintParser <* eof) "repl"

parseModuleAndFormatError :: Text -> Either Text [ModuleItem Annotation]
parseModuleAndFormatError = parseAndFormat (space *> moduleParser <* eof)

parseDataTypeAndFormatError :: Text -> Either Text (DataType ParseDep Annotation)
parseDataTypeAndFormatError = parseAndFormat (space *> dataTypeParser <* eof)

Expand Down
9 changes: 0 additions & 9 deletions smol-core/src/Smol/Core/Parser/Identifiers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module Smol.Core.Parser.Identifiers
typeNameParser,
plainTypeNameParser,
typeclassNameParser,
testNameParser,
)
where

Expand All @@ -20,9 +19,6 @@ import Data.Set (Set)
import qualified Data.Set as S
import Data.Text (Text)
import Data.Void
import Smol.Core.Modules.Types.ModuleName
import Smol.Core.Modules.Types.TestName
import Smol.Core.Parser.Primitives (textPrim)
import Smol.Core.Parser.Shared
import Smol.Core.Typecheck.Typeclass.Types
import Smol.Core.Types
Expand Down Expand Up @@ -190,8 +186,3 @@ withNamespace p = do
myString "."
a <- p
pure (a, mName)

-----

testNameParser :: Parser TestName
testNameParser = myLexeme $ TestName <$> textPrim
2 changes: 2 additions & 0 deletions smol-core/src/Smol/Core/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Smol.Core.Types
module Smol.Core.Types.Op,
module Smol.Core.Types.Expr,
module Smol.Core.Types.Identifier,
module Smol.Core.Types.ModuleName,
module Smol.Core.Types.ParseDep,
module Smol.Core.Types.Pattern,
module Smol.Core.Types.Prim,
Expand All @@ -22,6 +23,7 @@ import Smol.Core.Types.Constructor
import Smol.Core.Types.DataType
import Smol.Core.Types.Expr
import Smol.Core.Types.Identifier
import Smol.Core.Types.ModuleName
import Smol.Core.Types.Op
import Smol.Core.Types.ParseDep
import Smol.Core.Types.Pattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}

module Smol.Core.Modules.Types.ModuleName
module Smol.Core.Types.ModuleName
( ModuleName (..),
getModuleName,
validModuleName,
Expand Down
2 changes: 1 addition & 1 deletion smol-core/src/Smol/Core/Types/ParseDep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ where
import Data.Aeson (FromJSON, ToJSON)
import Data.String
import GHC.Generics (Generic)
import Smol.Core.Modules.Types.ModuleName
import Smol.Core.Printer
import Smol.Core.Types.ModuleName

---------------------------

Expand Down
4 changes: 3 additions & 1 deletion smol-core/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import qualified Test.Interpreter.InterpreterSpec
import qualified Test.Modules.CheckSpec
import qualified Test.Modules.FromPartsSpec
import qualified Test.Modules.InterpreterSpec
import qualified Test.Modules.ParserSpec
import qualified Test.Modules.PrettyPrintSpec
import qualified Test.Modules.ResolveDepsSpec
import qualified Test.Modules.RunTestsSpec
Expand All @@ -29,12 +30,13 @@ main = hspec $ parallel $ do
Test.Typecheck.TypeclassSpec.spec
Test.Typecheck.ToDictionaryPassingSpec.spec
Test.ParserSpec.spec
Test.Interpreter.InterpreterSpec.spec
Test.Modules.CheckSpec.spec
Test.Modules.FromPartsSpec.spec
Test.Modules.InterpreterSpec.spec
Test.Modules.PrettyPrintSpec.spec
Test.Modules.ResolveDepsSpec.spec
Test.Modules.RunTestsSpec.spec
Test.Modules.TypecheckSpec.spec
Test.Modules.ParserSpec.spec
Test.TransformSpec.spec
Test.Interpreter.InterpreterSpec.spec
1 change: 1 addition & 0 deletions smol-core/test/Test/Helpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import Data.Text (Text)
import qualified Data.Text as T
import Smol.Core
import Smol.Core.Modules.FromParts
import Smol.Core.Modules.Parser
import Smol.Core.Modules.ResolveDeps
import Smol.Core.Modules.Typecheck
import Smol.Core.Modules.Types.Module
Expand Down
45 changes: 0 additions & 45 deletions smol-core/test/Test/Interpreter/InterpreterSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@

module Test.Interpreter.InterpreterSpec (spec) where

import Control.Monad (void)
import Control.Monad.Reader
import Data.Foldable (traverse_)
import Data.Text (Text)
import Smol.Core
import Smol.Core.Interpreter.Types.Stack
import Smol.Core.Modules.Types.Module
import Smol.Core.Typecheck.FromParsedExpr
import Smol.Core.Typecheck.Typecheck (typecheck)
import Smol.Core.Typecheck.Typeclass
import Test.Helpers
import Test.Hspec

Expand All @@ -29,31 +24,6 @@ discardLeft :: (Show e) => Either e a -> a
discardLeft (Left e) = error (show e)
discardLeft (Right a) = a

runDictEnv :: ReaderT PassDictEnv m a -> m a
runDictEnv = flip runReaderT emptyPassDictEnv

-- | typecheck, resolve typeclasses, interpret, profit
doInterpret :: Text -> Expr ResolvedDep ()
danieljharvey marked this conversation as resolved.
Show resolved Hide resolved
doInterpret input =
let dictEnv =
ToDictEnv
{ tdeClasses = tceClasses typecheckEnv,
tdeInstances = fmap void <$> moInstances testModule,
tdeVars = mempty
}
in case typecheck typecheckEnv (fromParsedExpr (unsafeParseExpr input)) of
Right (_constraints, typedExpr) ->
fmap edAnnotation
. discardLeft
. interpret mempty
. addEmptyStackFrames
. void
. discardLeft
. runDictEnv
. passDictionaries dictEnv mempty
$ typedExpr
Left e -> error (show e)

spec :: Spec
spec = do
describe "InterpreterSpec" $ do
Expand Down Expand Up @@ -81,18 +51,3 @@ spec = do
`shouldBe` fromParsedExpr (unsafeParseExpr expect)
)
cases

-- not sure this is the way
describe "interpret with typeclasses" $ do
let cases =
[ ("equals (1 : Int) (1 : Int)", "True"), -- use Eq Int
("equals (2 : Int) (1 : Int)", "False"), -- use Eq Int
("equals ((1 : Int),(1 : Int)) ((1 : Int), (2 : Int))", "False") -- use Eq (a,b) and Eq Int
]
traverse_
( \(input, expect) ->
it (show input <> " = " <> show expect) $ do
doInterpret input
`shouldBe` fromParsedExpr (unsafeParseExpr expect)
)
cases
1 change: 1 addition & 0 deletions smol-core/test/Test/Modules/CheckSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Data.Text (Text)
import qualified Data.Text.Encoding as T
import Smol.Core
import Smol.Core.Modules.Check
import Smol.Core.Modules.Parser
import Smol.Core.Modules.RunTests
import Smol.Core.Modules.Types.ModuleError
import Test.Hspec
Expand Down
1 change: 1 addition & 0 deletions smol-core/test/Test/Modules/InterpreterSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import qualified Error.Diagnose as Diag
import Smol.Core
import Smol.Core.Modules.Check
import Smol.Core.Modules.Interpret
import Smol.Core.Modules.Parser
import Smol.Core.Modules.Types.DefIdentifier
import Smol.Core.Modules.Types.ModuleError
import Smol.Core.Typecheck.FromParsedExpr
Expand Down
Loading
Loading