Skip to content

Commit

Permalink
Implemented ternary operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
superatrain committed Dec 19, 2012
1 parent 7b4ab61 commit f83ffe5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 6 additions & 1 deletion Graphics/Implicit/ExtOpenScad/Default.hs
Expand Up @@ -85,7 +85,8 @@ defaultPolymorphicFunctions =
(">=", toOObj ((>=) :: -> -> Bool) ),
("<=", toOObj ((<=) :: -> -> Bool) ),
("==", toOObj ((==) :: OVal -> OVal -> Bool) ),
("!=", toOObj ((/=) :: OVal -> OVal -> Bool) ),
("!=", toOObj ((/=) :: OVal -> OVal -> Bool) ),
("?", toOObj ( ternary :: Bool -> OVal -> OVal -> OVal) ),
("list_gen", toOObj list_gen)
] where

Expand Down Expand Up @@ -196,3 +197,7 @@ defaultPolymorphicFunctions =
[fromIntegral (ceiling a), fromIntegral (ceiling (a+b)).. fromIntegral (floor c)]
list_gen _ = Nothing

ternary True a b = a
ternary False a b = b


17 changes: 15 additions & 2 deletions Graphics/Implicit/ExtOpenScad/Parser/Expr.hs
Expand Up @@ -179,8 +179,7 @@ expression n@3 =
expr <- expression $ n+1
return expr
) <|> try (expression $ n+1)
expression n@2 = try (expression $ n+1)
expression n@1 =
expression n@2 =
try ( do
firstExpr <- expression $ n+1
otherComparisonsExpr <- many $ do
Expand All @@ -201,5 +200,19 @@ expression n@1 =
[x] -> x :$ exprs
_ -> collector "all" [(comparisons!!n) :$ [exprs!!n, exprs!!(n+1)] | n <- [0.. length comparisons - 1] ]
)<|> try (expression $ n+1)
expression n@1 =
try (( do
a <- expression (n+1)
genSpace
string "?"
genSpace
b <- expression n
genSpace
string ":"
genSpace
c <- expression n
return $ Var "?" :$ [a,b,c]
) <?> "ternary")
<|> try (expression $ n+1)
expression n@0 = try (do { genSpace; expr <- expression $ n+1; genSpace; return expr}) <|> try (expression $ n+1)

0 comments on commit f83ffe5

Please sign in to comment.