Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Implement Ternary #87

Merged
merged 1 commit into from

2 participants

@superatrain

No description provided.

@colah colah merged commit 2e1dbfc into from
@colah
Owner

Thanks, Atrain!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 19, 2012
  1. Implemented ternary operator.

    superatrain authored
This page is out of date. Refresh to see the latest.
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)
Something went wrong with that request. Please try again.