1
1
mirror of https://github.com/github/semantic.git synced 2024-12-29 18:06:14 +03:00

Tokenize and print subtraction as well

This commit is contained in:
Timothy Clem 2018-08-24 14:07:49 -07:00
parent c8d6cab47f
commit 9a347c9d27
5 changed files with 14 additions and 8 deletions

View File

@ -62,5 +62,6 @@ data Context
-- and given appropriate precedence.
data Operator
= Add
| Mult
| Multiply
| Subtract
deriving (Show, Eq)

View File

@ -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)

View File

@ -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'
])

View File

@ -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)

View File

@ -1,3 +1,4 @@
3 - 4 + 10
1 * 2 + 3
(1 * 2) + 3
1 * (2 + 3)