module Graphics.Implicit.ExtOpenScad.Parser.Statement where
import Graphics.Implicit.Definitions
import Text.ParserCombinators.Parsec hiding (State)
import Text.ParserCombinators.Parsec.Expr
import Graphics.Implicit.ExtOpenScad.Definitions
import Graphics.Implicit.ExtOpenScad.Parser.Util
import Graphics.Implicit.ExtOpenScad.Parser.Expr
parseProgram name s = parse program name s where
program = do
sts <- many1 computation
eof
return sts
computation :: GenParser Char st StatementI
computation =
do
genSpace
s <- tryMany [
ifStatementI,
forStatementI,
throwAway,
userModuleDeclaration
]
genSpace
return s
*<|> do
genSpace
s <- tryMany [
echo,
assignment,
include--,
--use
]
stringGS " ; "
return s
*<|> do
genSpace
s <- userModule
genSpace
return s
suite :: GenParser Char st [StatementI]
suite = (fmap return computation <|> do
char '{'
genSpace
stmts <- many (try computation)
genSpace
char '}'
return stmts
) <?> " suite"
throwAway :: GenParser Char st StatementI
throwAway = do
line <- lineNumber
genSpace
oneOf "%*"
genSpace
computation
return $ StatementI line DoNothing
include :: GenParser Char st StatementI
include = (do
line <- lineNumber
injectVals <- (string "include" >> return True )
<|> (string "use" >> return False)
stringGS " < "
filename <- many (noneOf "<> ")
stringGS " > "
return $ StatementI line $ Include filename injectVals
) <?> "include "
assignment :: GenParser Char st StatementI
assignment = ("assignment " ?:) $
do
line <- lineNumber
pattern <- patternMatcher
stringGS " = "
valExpr <- expr0
return $ StatementI line$ pattern := valExpr
*<|> do
line <- lineNumber
varSymb <- (string "function" >> space >> genSpace >> variableSymb)
*<|> variableSymb
stringGS " ( "
argVars <- sepBy patternMatcher (stringGS " , ")
stringGS " ) = "
valExpr <- expr0
return $ StatementI line $ Name varSymb := LamE argVars valExpr
echo :: GenParser Char st StatementI
echo = do
line <- lineNumber
stringGS " echo ( "
exprs <- expr0 `sepBy` (stringGS " , ")
stringGS " ) "
return $ StatementI line $ Echo exprs
ifStatementI :: GenParser Char st StatementI
ifStatementI =
"if " ?: do
line <- lineNumber
stringGS "if ( "
bexpr <- expr0
stringGS " ) "
sTrueCase <- suite
genSpace
sFalseCase <- (stringGS "else " >> suite ) *<|> (return [])
return $ StatementI line $ If bexpr sTrueCase sFalseCase
forStatementI :: GenParser Char st StatementI
forStatementI =
"for " ?: do
line <- lineNumber
stringGS " for ( "
pattern <- patternMatcher
stringGS " = "
vexpr <- expr0
stringGS " ) "
loopContent <- suite
return $ StatementI line $ For pattern vexpr loopContent
userModule :: GenParser Char st StatementI
userModule = do
line <- lineNumber
name <- variableSymb
genSpace
args <- moduleArgsUnit
genSpace
s <- suite *<|> (stringGS " ; " >> return [])
return $ StatementI line $ ModuleCall name args s
userModuleDeclaration :: GenParser Char st StatementI
userModuleDeclaration = do
line <- lineNumber
stringGS "module "
newModuleName <- variableSymb
genSpace
args <- moduleArgsUnitDecl
genSpace
s <- suite
return $ StatementI line $ NewModule newModuleName args s
moduleArgsUnit :: GenParser Char st [(Maybe String, Expr)]
moduleArgsUnit = do
stringGS " ( "
args <- sepBy (
do
symb <- variableSymb
stringGS " = "
expr <- expr0
return $ (Just symb, expr)
*<|> do
symb <- variableSymb
stringGS " ( "
argVars <- sepBy variableSymb (try $ stringGS " , ")
stringGS " ) = "
expr <- expr0
return $ (Just symb, LamE (map Name argVars) expr)
*<|> do
expr <- expr0
return (Nothing, expr)
) (try $ stringGS " , ")
stringGS " ) "
return args
moduleArgsUnitDecl :: GenParser Char st [(String, Maybe Expr)]
moduleArgsUnitDecl = do
stringGS " ( "
argTemplate <- sepBy (
do
symb <- variableSymb;
stringGS " = "
expr <- expr0
return (symb, Just expr)
*<|> do
symb <- variableSymb;
stringGS " ( "
argVars <- sepBy variableSymb (try $ stringGS " , ")
stringGS " ) = "
expr <- expr0
return (symb, Just expr)
*<|> do
symb <- variableSymb
return (symb, Nothing)
) (try $ stringGS " , ")
stringGS " ) "
return argTemplate
lineNumber = fmap sourceLine getPosition