Skip to content

Commit

Permalink
Lovely
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljharvey committed Nov 18, 2023
1 parent 84ee58e commit 197cbde
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 29 deletions.
4 changes: 2 additions & 2 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 Down
16 changes: 4 additions & 12 deletions smol-core/src/Smol/Core/Modules/Parser.hs
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE OverloadedStrings #-}


module Smol.Core.Modules.Parser
(
moduleParser,


( 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 Smol.Core.Parser.Primitives (textPrim)
import Data.Bifunctor (first)
import qualified Data.Text as T
import Text.Megaparsec.Char

type Parser = Parsec Void Text
Expand All @@ -48,8 +42,6 @@ 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
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.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
2 changes: 1 addition & 1 deletion smol-core/src/Smol/Core/Parser/Identifiers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import Data.Set (Set)
import qualified Data.Set as S
import Data.Text (Text)
import Data.Void
import Smol.Core.Types.ModuleName
import Smol.Core.Parser.Shared
import Smol.Core.Typecheck.Typeclass.Types
import Smol.Core.Types
import Smol.Core.Types.ModuleName
import Text.Megaparsec

type Parser = Parsec Void Text
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.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
@@ -1,14 +1,15 @@
module Main (main) where

import Test.Hspec
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
import qualified Test.Modules.TypecheckSpec
import qualified Test.Modules.ParserSpec
import qualified Test.ParserSpec
import qualified Test.TransformSpec
import qualified Test.Typecheck.ExhaustivenessSpec
Expand Down Expand Up @@ -38,3 +39,4 @@ main = hspec $ parallel $ do
Test.Modules.TypecheckSpec.spec
Test.Modules.ParserSpec.spec
Test.TransformSpec.spec
Test.Interpreter.InterpreterSpec.spec
2 changes: 1 addition & 1 deletion smol-core/test/Test/Helpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ 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
import Smol.Core.Modules.Types.ModuleError
import Smol.Core.Modules.Types.ModuleItem
import Smol.Core.Typecheck.FromParsedExpr
import Test.BuiltInTypes (builtInTypes)
import Smol.Core.Modules.Parser

typedModule ::
(MonadError (ModuleError Annotation) m) =>
Expand Down
53 changes: 53 additions & 0 deletions smol-core/test/Test/Interpreter/InterpreterSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{-# LANGUAGE OverloadedStrings #-}

module Test.Interpreter.InterpreterSpec (spec) where

import Data.Foldable (traverse_)
import Data.Text (Text)
import Smol.Core
import Smol.Core.Interpreter.Types.Stack
import Smol.Core.Typecheck.FromParsedExpr
import Test.Helpers
import Test.Hspec

-- | interpret without typechecking etc
doBasicInterpret :: Text -> Expr ResolvedDep ()
doBasicInterpret =
fmap edAnnotation
. discardLeft
. interpret mempty
. addEmptyStackFrames
. fromParsedExpr
. unsafeParseExpr

discardLeft :: (Show e) => Either e a -> a
discardLeft (Left e) = error (show e)
discardLeft (Right a) = a

spec :: Spec
spec = do
describe "InterpreterSpec" $ do
describe "interpret" $ do
let cases =
[ ("1 + 1", "2"),
("-11 + 1", "-10"),
("(\\a -> a + 1) 41", "42"),
("(\\a -> if a then 1 else 2) False", "2"),
("(\\a -> if a then 1 else 2) True", "1"),
("let a = 41 in a + 1", "42"),
("Just (1 + 1)", "Just 2"),
("case (Just 1) { Just a -> a + 41, Nothing -> 0 }", "42"),
("case Nothing { Just a -> a + 41, Nothing -> 0 }", "0"),
("let stuff = { x: 1, y : 2 }; stuff.x + stuff.y", "3"),
("let id = \\a -> a; (id 1, id 2, id 3)", "(1,2,3)"),
("[1,2 + 3]", "[1,5]"),
("case [1,2,3] { [_, ...rest] -> rest, _ -> [42] }", "[2,3]"),
("let f = \\a -> if a == 10 then a else a + f (a + 1); f 0", "55")
]
traverse_
( \(input, expect) ->
it (show input <> " = " <> show expect) $ do
doBasicInterpret input
`shouldBe` fromParsedExpr (unsafeParseExpr expect)
)
cases
2 changes: 1 addition & 1 deletion smol-core/test/Test/Modules/CheckSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

-- | test evaluating and running tests for a module
module Test.Modules.CheckSpec (spec) where
import Smol.Core.Modules.Parser

import Data.Bifunctor (second)
import Data.FileEmbed
Expand All @@ -16,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
2 changes: 1 addition & 1 deletion smol-core/test/Test/Modules/InterpreterSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

module Test.Modules.InterpreterSpec (spec) where

import Smol.Core.Modules.Parser
import Control.Monad (void)
import Data.Foldable (traverse_)
import Data.Text (Text)
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
2 changes: 0 additions & 2 deletions smol-core/test/Test/Modules/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,3 @@ spec = do
result `shouldSatisfy` isRight
)
testInputs


2 changes: 1 addition & 1 deletion smol-core/test/Test/Modules/PrettyPrintSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
-- | test evaluating and running tests for a module
module Test.Modules.PrettyPrintSpec (spec) where

import qualified Smol.Core.Modules.Parser as Parse
import Data.Bifunctor (second)
import Data.FileEmbed
import Data.Foldable (traverse_)
import Data.Functor
import Data.Text (Text)
import qualified Data.Text.Encoding as T
import qualified Smol.Core.Modules.Parser as Parse
import Smol.Core.Modules.PrettyPrint (printModuleParts)
import Smol.Core.Modules.Types.ModuleItem
import Smol.Core.Printer
Expand Down
2 changes: 1 addition & 1 deletion smol-core/test/Test/Modules/RunTestsSpec.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{-# LANGUAGE OverloadedStrings #-}

module Test.Modules.RunTestsSpec (spec) where
import Smol.Core.Modules.Parser

import qualified Data.Text 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
import Smol.Core.Modules.Types.ModuleError
Expand Down
2 changes: 1 addition & 1 deletion smol-core/test/Test/Modules/TypecheckSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
{-# LANGUAGE TemplateHaskell #-}

module Test.Modules.TypecheckSpec (spec) where
import Smol.Core.Modules.Parser

import Data.Bifunctor (second)
import Data.Either (isRight)
Expand All @@ -14,6 +13,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.Types hiding (Entity (..))
import Smol.Core.Modules.Types.ModuleError
import Test.Helpers
Expand Down
1 change: 0 additions & 1 deletion smol-core/test/Test/ParserSpec.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

module Test.ParserSpec (spec) where

Expand Down
2 changes: 1 addition & 1 deletion smol-repl/src/Smol/Check.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Smol.Core.Modules.Check
import Smol.Core.Modules.Parser (parseModule)
import Smol.Core.Modules.PrettyPrint (printModuleParts)
import Smol.Core.Modules.RunTests
import Smol.Core.Modules.Types.ModuleError
import Smol.Core.Modules.Types.ModuleItem
import Smol.Core.Parser (parseModule)
import Smol.Core.Printer
import Smol.Repl.Helpers.Diagnostics
import Smol.Repl.Helpers.ShowTestResults
Expand Down
2 changes: 1 addition & 1 deletion smol-repl/src/Smol/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import qualified Smol.Backend.Compile.RunLLVM as Run
import Smol.Backend.IR.FromExpr.Expr
import Smol.Backend.IR.ToLLVM.ToLLVM
import Smol.Core.Modules.Check
import Smol.Core.Modules.Parser (parseModule)
import Smol.Core.Modules.RunTests
import Smol.Core.Modules.Types.ModuleError
import Smol.Core.Parser (parseModule)
import Smol.Repl.Helpers.Diagnostics
import Smol.Repl.Helpers.ShowTestResults
import System.Console.Haskeline
Expand Down

0 comments on commit 197cbde

Please sign in to comment.