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

Improve pretty printer: use consistent indentation levels #110

Merged
merged 19 commits into from
Apr 1, 2024
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
3 changes: 3 additions & 0 deletions inferno-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Revision History for inferno-core
*Note*: we use https://pvp.haskell.org/ (MAJOR.MAJOR.MINOR.PATCH)

## 0.11.3.0 -- 2024-04-01
* Add --parse CLI arg and allow trailing commas in parser

## 0.11.2.0 -- 2024-03-26
* Add record pattern matching

Expand Down
58 changes: 43 additions & 15 deletions inferno-core/app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Main where

import Data.Bifunctor (bimap)
import Data.Bifunctor (bimap, second)
import qualified Data.Map as Map
import qualified Data.Text.IO as Text
import Inferno.Core (Interpreter (..), mkInferno)
import Inferno.Module.Prelude (builtinModules)
import Inferno.Eval (runEvalM)
import Inferno.Infer (inferExpr)
import Inferno.Infer.Pinned (pinExpr)
import Inferno.Module.Prelude (baseOpsTable, builtinModules, builtinModulesOpsTable, builtinModulesPinMap, builtinModulesTerms)
import Inferno.Parse (parseExpr)
import Inferno.Parse.Commented (insertCommentsIntoExpr)
import Inferno.Types.VersionControl (pinnedToMaybe)
import Inferno.Utils.Prettyprinter (showPretty)
import Options.Applicative
Expand All @@ -30,13 +35,14 @@ import Options.Applicative
import System.Exit (exitFailure)
import System.IO (hPrint, stderr)

data CliArgs = CliArgs {file :: String, typecheck :: Bool}
data CliArgs = CliArgs {file :: String, typecheck :: Bool, parse :: Bool}

cliargs :: Parser CliArgs
cliargs =
CliArgs
<$> argument str (metavar "FILE" <> help "Input file path")
<*> switch (long "typecheck" <> short 't' <> help "Only run type inference")
<*> switch (long "parse" <> short 'p' <> help "Only run parser")

main :: IO ()
main = do
Expand All @@ -50,21 +56,43 @@ main = do
args <- execParser opts

src <- Text.readFile $ file args
Interpreter {evalExpr, defaultEnv, parseAndInfer} <-
mkInferno builtinModules [] :: IO (Interpreter IO ())
case parseAndInfer src of
let customTypes = []
let prelude = builtinModules @IO @()
let defaultEnv = builtinModulesTerms prelude
-- parse
case parseExpr (baseOpsTable prelude) (builtinModulesOpsTable prelude) customTypes src of
Left err -> do
hPrint stderr err
exitFailure
Right (ast, ty, _, _) -> do
if typecheck args
Right (ast, _comments) ->
if parse args
then do
putStrLn "Inferred type:"
showPretty ty
let ast' = insertCommentsIntoExpr _comments ast
putStrLn "Parsed Expr:"
print $ second (const ()) ast'
putStrLn "Pretty:"
showPretty ast'
else do
let ast' = bimap pinnedToMaybe (const ()) ast
evalExpr defaultEnv Map.empty ast' >>= \case
-- pin free variables to builtin prelude function hashes
case pinExpr (builtinModulesPinMap prelude) ast of
Left err -> do
hPrint stderr err
exitFailure
Right res -> showPretty res
Right pinnedAST ->
-- typecheck
case inferExpr prelude pinnedAST of
Left err -> do
hPrint stderr err
exitFailure
Right (ast', ty, _tyMap) ->
if typecheck args
then do
putStrLn "Inferred type:"
showPretty ty
else do
let ast'' = bimap pinnedToMaybe (const ()) ast'
runEvalM defaultEnv Map.empty ast'' >>= \case
Left err -> do
hPrint stderr err
exitFailure
Right res -> showPretty res
2 changes: 1 addition & 1 deletion inferno-core/inferno-core.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.4
name: inferno-core
version: 0.11.2.0
version: 0.11.3.0
synopsis: A statically-typed functional scripting language
description: Parser, type inference, and interpreter for a statically-typed functional scripting language
category: DSL,Scripting
Expand Down
4 changes: 2 additions & 2 deletions inferno-core/src/Inferno/Infer/Exhaustiveness.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ instance Ord Con where
compare a b = compare (mkOrd a) (mkOrd b)
where
mkOrd = \case
COne -> "one"
CEmpty -> "empty"
COne -> "Some"
CEmpty -> "None"
CTuple n -> show n
CInf v -> show v
CEnum _ e -> show e
Expand Down
2 changes: 2 additions & 0 deletions inferno-core/src/Inferno/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
enumE f = do
startPos <- getSourcePos
lexeme $
try (f startPos () <$> (Scope . ModuleName <$> variable) <*> (char '.' *> enumConstructor))

Check warning on line 231 in inferno-core/src/Inferno/Parse.hs

View workflow job for this annotation

GitHub Actions / build

Warning in enumE in module Inferno.Parse: Functor law ▫︎ Found: "f startPos () <$> (Scope . ModuleName <$> variable)" ▫︎ Perhaps: "(f startPos () . Scope . ModuleName <$> variable)"
<|> f startPos () LocalScope <$> enumConstructor

implVarE :: Parser (Expr () SourcePos)
Expand Down Expand Up @@ -310,6 +310,7 @@
symbol "|"
(sels, cond) <- rhsE
endPos <- getSourcePos
_ <- optional $ symbol ","
char ']'
return $ ArrayComp startPos e midPos (NEList.fromList sels) cond endPos
where
Expand Down Expand Up @@ -448,6 +449,7 @@
openModArgs modNm = do
symbol "("
is <- go
_ <- optional $ symbol ","
symbol ")"
(opsTable, modOpsTables, customTypes) <- ask
opsTable' <-
Expand Down Expand Up @@ -780,8 +782,8 @@

type TyParser = ReaderT (Map.Map Text Int, OpsTable, Map.Map ModuleName OpsTable, [CustomType]) (WriterT Comments (Parsec InfernoParsingError Text))

rws_type :: [Text] -- list of reserved type sig words

Check warning on line 785 in inferno-core/src/Inferno/Parse.hs

View workflow job for this annotation

GitHub Actions / build

Suggestion in rws_type in module Inferno.Parse: Use camelCase ▫︎ Found: "rws_type :: [Text]" ▫︎ Perhaps: "rwsType :: [Text]"
rws_type = ["define", "on", "forall"]

Check warning on line 786 in inferno-core/src/Inferno/Parse.hs

View workflow job for this annotation

GitHub Actions / build

Suggestion in rws_type in module Inferno.Parse: Use camelCase ▫︎ Found: "rws_type = ..." ▫︎ Perhaps: "rwsType = ..."

typeIdent :: TyParser Text
typeIdent = try (p >>= check)
Expand All @@ -808,11 +810,11 @@
<|> choice (map (\t -> symbol (pack t) $> TCustom t) customTypes)
)

type_variable_raw :: TyParser Text

Check warning on line 813 in inferno-core/src/Inferno/Parse.hs

View workflow job for this annotation

GitHub Actions / build

Suggestion in type_variable_raw in module Inferno.Parse: Use camelCase ▫︎ Found: "type_variable_raw :: TyParser Text" ▫︎ Perhaps: "typeVariableRaw :: TyParser Text"
type_variable_raw = char '\'' *> takeWhile1P Nothing isAlphaNum

Check warning on line 814 in inferno-core/src/Inferno/Parse.hs

View workflow job for this annotation

GitHub Actions / build

Suggestion in type_variable_raw in module Inferno.Parse: Use camelCase ▫︎ Found: "type_variable_raw = ..." ▫︎ Perhaps: "typeVariableRaw = ..."

type_variable :: TyParser Int

Check warning on line 816 in inferno-core/src/Inferno/Parse.hs

View workflow job for this annotation

GitHub Actions / build

Suggestion in type_variable in module Inferno.Parse: Use camelCase ▫︎ Found: "type_variable :: TyParser Int" ▫︎ Perhaps: "typeVariable :: TyParser Int"
type_variable = do

Check warning on line 817 in inferno-core/src/Inferno/Parse.hs

View workflow job for this annotation

GitHub Actions / build

Suggestion in type_variable in module Inferno.Parse: Use camelCase ▫︎ Found: "type_variable = ..." ▫︎ Perhaps: "typeVariable = ..."
nm <- type_variable_raw
(tys, _, _, _) <- ask
case Map.lookup nm tys of
Expand Down
3 changes: 3 additions & 0 deletions inferno-types/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Revision History for inferno-types
*Note*: we use https://pvp.haskell.org/ (MAJOR.MAJOR.MINOR.PATCH)

## 0.4.4.0 -- 2024-04-01
* Improve pretty printer: use consistent indentation levels

## 0.4.3.0 -- 2024-03-26
* Add record pattern matching

Expand Down
12 changes: 11 additions & 1 deletion inferno-types/inferno-types.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: >=1.10
name: inferno-types
version: 0.4.3.0
version: 0.4.4.0
synopsis: Core types for Inferno
description: Core types for the Inferno language
category: DSL,Scripting
Expand Down Expand Up @@ -60,3 +60,13 @@ library
, OverloadedStrings
, TupleSections
, RecordWildCards

executable pretty-test
main-is: Main.hs
hs-source-dirs: pretty-test
ghc-options: -Wall -Wunused-packages -Wincomplete-uni-patterns -Wincomplete-record-updates
build-depends:
base >=4.7 && <5
, containers
, inferno-types
default-language: Haskell2010
19 changes: 19 additions & 0 deletions inferno-types/pretty-test/Main.hs

Large diffs are not rendered by default.

Loading
Loading