diff --git a/src/Data/Reprinting/Token.hs b/src/Data/Reprinting/Token.hs index 2fb1523bc..5621d6c59 100644 --- a/src/Data/Reprinting/Token.hs +++ b/src/Data/Reprinting/Token.hs @@ -62,5 +62,6 @@ data Context -- and given appropriate precedence. data Operator = Add - | Mult + | Multiply + | Subtract deriving (Show, Eq) diff --git a/src/Data/Syntax/Expression.hs b/src/Data/Syntax/Expression.hs index b44df0bc6..7be3e85aa 100644 --- a/src/Data/Syntax/Expression.hs +++ b/src/Data/Syntax/Expression.hs @@ -141,6 +141,9 @@ instance Evaluatable Minus where eval t = rvalBox =<< (traverse subtermValue t >>= go) where go (Minus a b) = liftNumeric2 sub a b where sub = liftReal (-) +instance Tokenize Minus where + tokenize Minus{..} = within' (TInfixL Subtract 6) $ lhs *> yield TSym <* rhs + data Times a = Times { lhs :: a, rhs :: a } deriving (Declarations1, Diffable, Eq, Foldable, FreeVariables1, Functor, Generic1, Hashable1, Ord, Show, ToJSONFields1, Traversable, Named1, Message1) @@ -153,7 +156,7 @@ instance Evaluatable Times where go (Times a b) = liftNumeric2 mul a b where mul = liftReal (*) instance Tokenize Times where - tokenize Times{..} = within' (TInfixL Mult 7) $ lhs *> yield TSym <* rhs + tokenize Times{..} = within' (TInfixL Multiply 7) $ lhs *> yield TSym <* rhs data DividedBy a = DividedBy { lhs :: a, rhs :: a } deriving (Declarations1, Diffable, Eq, Foldable, FreeVariables1, Functor, Generic1, Hashable1, Ord, Show, ToJSONFields1, Traversable, Named1, Message1) diff --git a/src/Language/Ruby/Assignment.hs b/src/Language/Ruby/Assignment.hs index 0c053cb08..06def9b1e 100644 --- a/src/Language/Ruby/Assignment.hs +++ b/src/Language/Ruby/Assignment.hs @@ -56,7 +56,7 @@ type MiniSyntax = '[ , Declaration.Function -- , Expression.Call , Expression.Plus - -- , Expression.Minus + , Expression.Minus , Expression.Times , Ruby.Syntax.Send -- , Ruby.Syntax.Load @@ -194,7 +194,7 @@ miniAssignment = handleError $ makeTerm <$> symbol Program <*> children (Stateme binary :: Assignment MiniTerm binary = makeTerm' <$> symbol Binary <*> children (infixTerm expression expression [ (inject .) . Expression.Plus <$ symbol AnonPlus - -- , (inject .) . Expression.Minus <$ symbol AnonMinus' + , (inject .) . Expression.Minus <$ symbol AnonMinus' , (inject .) . Expression.Times <$ symbol AnonStar' ]) diff --git a/src/Language/Ruby/PrettyPrint.hs b/src/Language/Ruby/PrettyPrint.hs index f4af813c6..9a38f441b 100644 --- a/src/Language/Ruby/PrettyPrint.hs +++ b/src/Language/Ruby/PrettyPrint.hs @@ -32,10 +32,11 @@ step (Defer el cs) = case (el, cs) of (TSep, TParams:_) -> pure $ emit "," <> space (TClose, TParams:_) -> pure $ emit ")" - (TOpen, TInfixL _ p:xs) -> emitIf (p < prec xs) "(" - (TSym, TInfixL Add _:_) -> pure $ space <> emit "+" <> space - (TSym, TInfixL Mult _:_) -> pure $ space <> emit "*" <> space - (TClose, TInfixL _ p:xs) -> emitIf (p < prec xs) ")" + (TOpen, TInfixL _ p:xs) -> emitIf (p < prec xs) "(" + (TSym, TInfixL Add _:_) -> pure $ space <> emit "+" <> space + (TSym, TInfixL Multiply _:_) -> pure $ space <> emit "*" <> space + (TSym, TInfixL Subtract _:_) -> pure $ space <> emit "-" <> space + (TClose, TInfixL _ p:xs) -> emitIf (p < prec xs) ")" (TOpen, [Imperative]) -> pure mempty (TOpen, Imperative:xs) -> pure $ layout HardWrap <> indent (depth xs) diff --git a/test/fixtures/ruby/reprinting/infix.rb b/test/fixtures/ruby/reprinting/infix.rb index 8533c97d6..c9cc31066 100644 --- a/test/fixtures/ruby/reprinting/infix.rb +++ b/test/fixtures/ruby/reprinting/infix.rb @@ -1,3 +1,4 @@ +3 - 4 + 10 1 * 2 + 3 (1 * 2) + 3 1 * (2 + 3)