1
1
mirror of https://github.com/github/semantic.git synced 2024-12-30 18:36:27 +03:00

Implement until as a negation of while

This commit is contained in:
Timothy Clem 2016-11-07 13:33:41 -08:00
parent cafcd8fd91
commit ba9e0424ff
4 changed files with 14 additions and 10 deletions

View File

@ -223,7 +223,9 @@ toTermName source term = case unwrap term of
(S.Negate condition) -> termNameFromSource condition
_ -> termNameFromSource expr
S.For clauses _ -> termNameFromChildren term clauses
S.While expr _ -> toTermName' expr
S.While expr _ -> case unwrap expr of
(S.Negate condition) -> termNameFromSource condition
_ -> toTermName' expr
S.DoWhile _ expr -> toTermName' expr
S.Throw expr -> termNameFromSource expr
S.Constructor expr -> toTermName' expr
@ -240,7 +242,6 @@ toTermName source term = case unwrap term of
S.Export (Just identifier) [] -> "{ " <> toTermName' identifier <> " }"
S.Export (Just identifier) expr -> "{ " <> intercalate ", " (termNameFromSource <$> expr) <> " }" <> " from " <> toTermName' identifier
S.ConditionalAssignment id _ -> toTermName' id
S.Until expr _ -> toTermName' expr
S.Negate expr -> toTermName' expr
S.Rescue args _ -> intercalate ", " $ toTermName' <$> args
where toTermName' = toTermName source

View File

@ -35,6 +35,16 @@ termConstructor source sourceSpan name range children
condition <- withDefaultInfo (S.Negate expr)
withDefaultInfo $ S.If condition rest
_ -> withDefaultInfo $ S.Error children
| name == "until_modifier" = case children of
[ lhs, rhs ] -> do
condition <- withDefaultInfo (S.Negate rhs)
withDefaultInfo $ S.While condition [lhs]
_ -> withDefaultInfo $ S.Error children
| name == "until_statement" = case children of
( expr : rest ) -> do
condition <- withDefaultInfo (S.Negate expr)
withDefaultInfo $ S.While condition rest
_ -> withDefaultInfo $ S.Error children
| otherwise = withDefaultInfo $ case (name, children) of
("array", _ ) -> S.Array children
("assignment", [ identifier, value ]) -> S.Assignment identifier value
@ -100,11 +110,6 @@ termConstructor source sourceSpan name range children
("rescue_modifier", [lhs, rhs] ) -> S.Rescue [lhs] [rhs]
("rescue_modifier", _ ) -> S.Error children
("return_statement", _ ) -> S.Return (listToMaybe children)
("unless_statement", _ ) -> S.Error children
("until_modifier", [ lhs, condition ]) -> S.Until condition [lhs]
("until_modifier", _ ) -> S.Error children
("until_statement", expr : rest ) -> S.Until expr rest
("until_statement", _ ) -> S.Error children
("while_modifier", [ lhs, condition ]) -> S.While condition [lhs]
("while_modifier", _ ) -> S.Error children
("while_statement", expr : rest ) -> S.While expr rest

View File

@ -127,7 +127,6 @@ syntaxToTermField syntax = case syntax of
S.Export identifier statements -> [ "identifier" .= identifier ] <> [ "statements" .= statements ]
S.ConditionalAssignment id value -> [ "conditionalIdentifier" .= id ] <> [ "value" .= value ]
S.Yield expr -> [ "yieldExpression" .= expr ]
S.Until expr body -> [ "untilExpr" .= expr ] <> [ "untilBody" .= body ]
S.Negate expr -> [ "negate" .= expr ]
S.Rescue args expressions -> [ "args" .= args ] <> childrenFields expressions
where childrenFields c = [ "children" .= c ]

View File

@ -79,8 +79,7 @@ data Syntax a f
-- | A conditional assignment represents expressions whose operator classifies as conditional (e.g. ||= or &&=).
| ConditionalAssignment { conditionalAssignmentId :: f, value :: f }
| Yield (Maybe f)
| Until { untilExpr :: f, untilBody :: [f] }
-- | A negation has a single expression.
-- | A negation of a single expression.
| Negate f
-- | A rescue block has a list of arguments to rescue and a list of expressions.
| Rescue [f] [f]