-
-
Notifications
You must be signed in to change notification settings - Fork 148
Allow for evaluation of modules in places where expressions (but not … #506
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
base: refactor_module_calling
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,13 +5,13 @@ | |||||||||||||||
| -- Allow us to use string literals for Text | ||||||||||||||||
| {-# LANGUAGE OverloadedStrings #-} | ||||||||||||||||
|
|
||||||||||||||||
| module Graphics.Implicit.ExtOpenScad.Eval.Expr (evalExpr, rawRunExpr, matchPat, StateE, ExprState(ExprState), addMessage) where | ||||||||||||||||
| module Graphics.Implicit.ExtOpenScad.Eval.Expr (evalArgs, evalExpr, rawRunExpr, matchPat, StateE, ExprState(ExprState), addMessage) where | ||||||||||||||||
|
|
||||||||||||||||
| import Prelude (String, Maybe(Just, Nothing), Bool (True), ($), elem, pure, zip, (&&), const, (<>), foldr, foldMap, (.), (<$>), traverse) | ||||||||||||||||
| import Prelude (String, Monoid, Maybe(Just, Nothing), Bool (False, True), ($), elem, mempty, pure, show, zip, (&&), const, (<>), foldr, foldMap, (.), (<$>), traverse) | ||||||||||||||||
|
|
||||||||||||||||
| import Graphics.Implicit.ExtOpenScad.Definitions ( | ||||||||||||||||
| Pattern(Name, ListP, Wild), | ||||||||||||||||
| OVal(OList, OError, OFunc, OUndefined), | ||||||||||||||||
| OVal(OList, OError, OFunc, OUndefined, OUModule, ONModule, ONModuleWithSuite, OVargsModule), | ||||||||||||||||
| Expr(LitE, ListE, LamE, Var, (:$)), | ||||||||||||||||
| Symbol(Symbol), | ||||||||||||||||
| VarLookup(VarLookup), | ||||||||||||||||
|
|
@@ -21,12 +21,16 @@ import Graphics.Implicit.ExtOpenScad.Definitions ( | |||||||||||||||
| StateC, ImplicitCadM, runImplicitCadM | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| import Graphics.Implicit.ExtOpenScad.Util.ArgParser (argMap) | ||||||||||||||||
|
|
||||||||||||||||
| import Graphics.Implicit.ExtOpenScad.Util.OVal (oTypeStr, getErrors) | ||||||||||||||||
|
|
||||||||||||||||
| import Graphics.Implicit.ExtOpenScad.Util.StateC (getVarLookup) | ||||||||||||||||
| import Graphics.Implicit.ExtOpenScad.Util.StateC (errorC, getVarLookup) | ||||||||||||||||
|
|
||||||||||||||||
| import qualified Graphics.Implicit.ExtOpenScad.Util.StateC as GIEUS (addMessage) | ||||||||||||||||
|
|
||||||||||||||||
| import Graphics.Implicit.ExtOpenScad.Eval.Module (checkOptions, runModule) | ||||||||||||||||
|
|
||||||||||||||||
| import Data.Maybe (fromMaybe, isNothing) | ||||||||||||||||
|
|
||||||||||||||||
| import Data.Map (fromList, lookup) | ||||||||||||||||
|
|
@@ -35,9 +39,9 @@ import Data.Foldable (fold, traverse_) | |||||||||||||||
|
|
||||||||||||||||
| import Data.Traversable (for) | ||||||||||||||||
|
|
||||||||||||||||
| import Control.Monad (zipWithM) | ||||||||||||||||
| import Control.Monad (unless, zipWithM) | ||||||||||||||||
|
|
||||||||||||||||
| import Data.Text.Lazy (Text, unpack) | ||||||||||||||||
| import Data.Text.Lazy (Text, pack, unpack) | ||||||||||||||||
|
|
||||||||||||||||
| import Data.Eq (Eq, (==)) | ||||||||||||||||
| import Text.Show (Show) | ||||||||||||||||
|
|
@@ -57,8 +61,8 @@ newtype ExprState = ExprState | |||||||||||||||
| -- so we can put them into a reader, so they can never | ||||||||||||||||
| -- accidentally be written to. | ||||||||||||||||
| data Input = Input | ||||||||||||||||
| { varLookup :: VarLookup | ||||||||||||||||
| , sourcePos :: SourcePosition | ||||||||||||||||
| { _varLookup :: VarLookup | ||||||||||||||||
| , _sourcePos :: SourcePosition | ||||||||||||||||
| } deriving (Eq, Show) | ||||||||||||||||
|
|
||||||||||||||||
| -- Check Graphics.Implicit.ExtOpenScad.Definitions for an explanation | ||||||||||||||||
|
|
@@ -96,9 +100,82 @@ patMatch _ _ = Nothing | |||||||||||||||
| matchPat :: Pattern -> OVal -> Maybe VarLookup | ||||||||||||||||
| matchPat pat val = VarLookup . fromList . zip (Symbol <$> patVars pat) <$> patMatch pat val | ||||||||||||||||
|
|
||||||||||||||||
| -- | The entry point from StateC. evaluates an expression, pureing the result, and moving any error messages generated into the calling StateC. | ||||||||||||||||
| -- | Evaluate the arguments, turning them from expressions into values. | ||||||||||||||||
| evalArgs :: [(Maybe Symbol, Expr)] -> SourcePosition -> StateC [(Maybe Symbol, OVal)] | ||||||||||||||||
| evalArgs args sourcePos = for args $ \(posName, expr) -> do | ||||||||||||||||
| val <- evalExpr sourcePos expr | ||||||||||||||||
| pure (posName, val) | ||||||||||||||||
|
|
||||||||||||||||
| -- | The entry point from StateC. Evaluates either an expression or an eligible module call. | ||||||||||||||||
| evalExpr :: SourcePosition -> Expr -> StateC OVal | ||||||||||||||||
| evalExpr pos expr = do | ||||||||||||||||
| evalExpr sourcePos expr = case expr of | ||||||||||||||||
| (maybeMod :$ argExprs) -> do | ||||||||||||||||
| -- Yes, we're recursing, after dropping argument expressions, for the OVal | ||||||||||||||||
| rVal <- evalExpr sourcePos maybeMod | ||||||||||||||||
| if isModule rVal | ||||||||||||||||
| then do | ||||||||||||||||
| -- Perform a module call. | ||||||||||||||||
| res <- runExprModule sourcePos rVal argExprs | ||||||||||||||||
| pure $ canonicalizeRes $ OList res | ||||||||||||||||
| else | ||||||||||||||||
| -- Evaluate expression. | ||||||||||||||||
| evalExprStateC sourcePos expr | ||||||||||||||||
| _ -> evalExprStateC sourcePos expr | ||||||||||||||||
|
Comment on lines
+109
to
+123
|
||||||||||||||||
| where | ||||||||||||||||
| isModule (OUModule _ _ _) = True | ||||||||||||||||
| isModule (ONModule _ _ _) = True | ||||||||||||||||
| isModule (ONModuleWithSuite _ _ _) = True | ||||||||||||||||
| isModule (OVargsModule _ _) = True | ||||||||||||||||
| isModule _ = False | ||||||||||||||||
| -- FIXME: We may need a better result cannonicalizer here. | ||||||||||||||||
| canonicalizeRes (OList [oneItem]) = oneItem | ||||||||||||||||
| canonicalizeRes other = other | ||||||||||||||||
|
|
||||||||||||||||
| -- | Execute a module call, in place of an expression. | ||||||||||||||||
| runExprModule :: SourcePosition -> OVal -> [Expr] -> StateC [OVal] | ||||||||||||||||
| runExprModule sourcePos mod argExprsRaw = do | ||||||||||||||||
| let | ||||||||||||||||
| -- Mark all of our arguments as unnamed. There are no named arguments in expressions. | ||||||||||||||||
| argExprs = (\a -> (Nothing, a)) <$> argExprsRaw | ||||||||||||||||
| -- Common error messages. | ||||||||||||||||
| noSuiteError,notModError :: (Monoid a) => StateC a | ||||||||||||||||
| noSuiteError = do | ||||||||||||||||
| errorC sourcePos $ "tried to use a " <> oTypeStr mod <> " that uses suites on the right hand side of assignment." | ||||||||||||||||
| pure mempty | ||||||||||||||||
| notModError = do | ||||||||||||||||
| errorC sourcePos $ "tried to run something that is not a module:" <> pack (show mod) | ||||||||||||||||
| pure mempty | ||||||||||||||||
|
|
||||||||||||||||
| -- Fully evaluate arguments. Since we're in Expr context, we can only handle unnamed arguments. | ||||||||||||||||
| evaluatedArgs <- evalArgs argExprs sourcePos | ||||||||||||||||
|
|
||||||||||||||||
| -- We can't handle any suites, either. | ||||||||||||||||
| _ <- case mod of | ||||||||||||||||
| (OUModule _ _ _) -> pure mempty :: StateC () | ||||||||||||||||
| (ONModule _ _ _) -> pure mempty | ||||||||||||||||
| (ONModuleWithSuite _ _ _) -> noSuiteError | ||||||||||||||||
| (OVargsModule _ _) -> noSuiteError | ||||||||||||||||
| _ -> notModError | ||||||||||||||||
|
|
||||||||||||||||
|
Comment on lines
+152
to
+159
|
||||||||||||||||
| -- We can't handle any suites, either. | |
| _ <- case mod of | |
| (OUModule _ _ _) -> pure mempty :: StateC () | |
| (ONModule _ _ _) -> pure mempty | |
| (ONModuleWithSuite _ _ _) -> noSuiteError | |
| (OVargsModule _ _) -> noSuiteError | |
| _ -> notModError |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,150 @@ | ||||
| -- Implicit CAD. Copyright (C) 2011, Christopher Olah (chris@colah.ca) | ||||
| -- Copyright (C) 2016, Julia Longtin (julial@turinglace.com) | ||||
| -- Released under the GNU AGPLV3+, see LICENSE | ||||
|
|
||||
| -- Allow us to use string literals for Text | ||||
| {-# LANGUAGE OverloadedStrings #-} | ||||
|
|
||||
| -- Utility functions for handling module calling. | ||||
| module Graphics.Implicit.ExtOpenScad.Eval.Module ( | ||||
| checkInstances, | ||||
| checkOptions, | ||||
| ensureNoSuite, | ||||
| nameOfModule, | ||||
| runModule, | ||||
| ) where | ||||
|
|
||||
| import Prelude(Maybe(Just, Nothing), Bool(False), (.), ($), elem, error, filter, fmap, fst, init, last, length, not, notElem, null, show, snd, pure, zip, (<>), (&&), (==), (/=), String, (<$>)) | ||||
|
|
||||
| import Graphics.Implicit.ExtOpenScad.Definitions ( | ||||
| OVal(OUModule, ONModule, ONModuleWithSuite, OVargsModule), | ||||
|
julialongtin marked this conversation as resolved.
|
||||
| SourcePosition, | ||||
| StateC, | ||||
| StatementI, | ||||
| Symbol(Symbol) | ||||
| ) | ||||
|
|
||||
| import Graphics.Implicit.ExtOpenScad.Util.StateC (errorC) | ||||
|
|
||||
| import qualified Data.List as DL (intercalate) | ||||
|
|
||||
| import Data.Maybe (isJust, fromMaybe, mapMaybe, catMaybes) | ||||
|
|
||||
| import Control.Monad (when) | ||||
|
|
||||
| import Data.Foldable (for_) | ||||
|
|
||||
| import Data.Traversable (for) | ||||
|
|
||||
| import Data.Text.Lazy as DTL (concat, intercalate) | ||||
|
|
||||
|
Comment on lines
+40
to
+41
|
||||
| import Data.Text.Lazy as DTL (concat, intercalate) |
Check failure on line 44 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 44 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.2.8, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 44 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 62 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 62 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.2.8, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 62 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 64 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 64 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.2.8, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 64 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 68 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 68 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.2.8, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 68 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 70 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 70 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 70 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.2.8, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 70 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.2.8, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 70 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 70 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 70 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.2.8, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 109 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 109 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.2.8, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 109 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 133 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 133 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.2.8, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
Check failure on line 133 in Graphics/Implicit/ExtOpenScad/Eval/Module.hs
GitHub Actions / GHC 9.6.3, Cabal 3.10, OS ubuntu-latest
Not in scope: type constructor or class ‘Expr’
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changelog entry has an empty markdown link
[](). Please link to the PR/issue for this change (or remove the link markup) to keep the changelog consistent with surrounding entries.