Permalink
Browse files

Implemented ternary operator.

  • Loading branch information...
1 parent 7b4ab61 commit f83ffe578db6298bee4ee79ebfbdd58210dcce87 superatrain committed Dec 19, 2012
Showing with 21 additions and 3 deletions.
  1. +6 −1 Graphics/Implicit/ExtOpenScad/Default.hs
  2. +15 −2 Graphics/Implicit/ExtOpenScad/Parser/Expr.hs
View
7 Graphics/Implicit/ExtOpenScad/Default.hs
@@ -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
@@ -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
+
+
View
17 Graphics/Implicit/ExtOpenScad/Parser/Expr.hs
@@ -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
@@ -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.