Skip to content

Commit 1f89b37

Browse files
committed
added the new binary operator concept to exponentiation
1 parent c3b25cb commit 1f89b37

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

src/Tablebot/Plugins/Roll/Dice/DiceData.hs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,15 @@ data Negation = Neg Expo | NoNeg Expo
118118
deriving (Show)
119119

120120
-- | The type representing a value with exponentials.
121-
data Expo = Expo Func Expo | NoExpo Func
121+
newtype Expo = Expo (BinOp Func ExpoType)
122122
deriving (Show)
123123

124+
data ExpoType = Expo'
125+
deriving (Show)
126+
127+
instance Operation ExpoType where
128+
getOperation _ = (^)
129+
124130
-- | The type representing a single function application, or a base item.
125131
data Func = Func FuncInfo [ArgValue] | NoFunc Base
126132
deriving (Show)
@@ -224,7 +230,7 @@ instance (Converter a Expo) => Converter a Negation where
224230
promote = NoNeg . promote
225231

226232
instance (Converter a Func) => Converter a Expo where
227-
promote = NoExpo . promote
233+
promote = Expo . promote
228234

229235
instance (Converter a Base) => Converter a Func where
230236
promote = NoFunc . promote

src/Tablebot/Plugins/Roll/Dice/DiceEval.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,14 @@ instance IOEval Negation where
436436
return (negate expo', "-" <> expo's)
437437

438438
instance IOEval Expo where
439-
evalShow' (NoExpo b) = evalShow b
440-
evalShow' (Expo b expo) = do
441-
(expo', expo's) <- evalShow expo
442-
if expo' < 0
443-
then evaluationException ("the exponent is negative: " <> formatInput Code expo') [parseShow expo]
444-
else do
445-
(b', b's) <- evalShow b
446-
return (b' ^ expo', b's <> " ^ " <> expo's)
439+
evalShow' (Expo (BinOp a tas)) = foldl' foldel (evalShow a) tas
440+
where
441+
foldel at (typ, b) = do
442+
(a', t) <- at
443+
(b', t') <- evalShow b
444+
if b' == 0
445+
then evaluationException ("the exponent is negative: " <> formatInput Code t') [parseShow b]
446+
else return (getOperation typ a' b', t <> " " <> parseShow typ <> " " <> t')
447447

448448
instance IOEval NumBase where
449449
evalShow' (NBParen (Paren e)) = do

src/Tablebot/Plugins/Roll/Dice/DiceParsing.hs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ instance CanParse ListValuesBase where
110110
unnest (Paren (LVBase (LVBParen e))) = e
111111
unnest e = e
112112

113-
-- | Helper function to try to parse the second part of a binary operator.
114-
binOpParseHelp :: (CanParse a) => Char -> (a -> a) -> Parser a
115-
binOpParseHelp c con = try (skipSpace *> char c) *> skipSpace *> (con <$> pars)
116-
117113
instance (CanParse b) => CanParse (If b) where
118114
pars = do
119115
a <- string "if" *> skipSpace1 *> pars <* skipSpace1
@@ -171,16 +167,17 @@ instance CanParse Negation where
171167
<|> NoNeg <$> pars
172168

173169
instance CanParse Expo where
174-
pars = do
175-
t <- pars
176-
binOpParseHelp '^' (Expo t) <|> (return . NoExpo) t
170+
pars = Expo <$> pars
171+
172+
instance CanParse ExpoType where
173+
pars = try (char '^' $> Expo')
177174

178175
instance CanParse NumBase where
179176
pars =
180177
(NBParen . unnest <$> pars)
181178
<|> Value <$> integer <??> "could not parse integer"
182179
where
183-
unnest (Paren (Expr (SingBinOp (Term (SingBinOp (NoNeg (NoExpo (NoFunc (NBase (NBParen e)))))))))) = e
180+
unnest (Paren (Expr (SingBinOp (Term (SingBinOp (NoNeg (Expo (SingBinOp (NoFunc (NBase (NBParen e))))))))))) = e
184181
unnest e = e
185182

186183
instance (CanParse a) => CanParse (Paren a) where
@@ -285,7 +282,7 @@ instance ParseShow ArgValue where
285282
instance ParseShow ListValues where
286283
parseShow (LVBase e) = parseShow e
287284
parseShow (MultipleValues nb b) = parseShow nb <> "#" <> parseShow b
288-
parseShow (LVFunc s n) = funcInfoName s <> "(" <> T.intercalate "," (parseShow <$> n) <> ")"
285+
parseShow (LVFunc s n) = funcInfoName s <> "(" <> T.intercalate ", " (parseShow <$> n) <> ")"
289286
parseShow (LVVar t) = t
290287
parseShow (ListValuesMisc l) = parseShow l
291288

@@ -304,14 +301,14 @@ instance ParseShow ExprType where
304301
parseShow Add = "+"
305302
parseShow Sub = "-"
306303

307-
instance ParseShow TermType where
308-
parseShow Multi = "*"
309-
parseShow Div = "/"
310-
311304
instance ParseShow Expr where
312305
parseShow (Expr e) = parseShow e
313306
parseShow (ExprMisc e) = parseShow e
314307

308+
instance ParseShow TermType where
309+
parseShow Multi = "*"
310+
parseShow Div = "/"
311+
315312
instance ParseShow Term where
316313
parseShow (Term f) = parseShow f
317314

@@ -323,9 +320,11 @@ instance ParseShow Negation where
323320
parseShow (Neg expo) = "-" <> parseShow expo
324321
parseShow (NoNeg expo) = parseShow expo
325322

323+
instance ParseShow ExpoType where
324+
parseShow _ = "^"
325+
326326
instance ParseShow Expo where
327-
parseShow (NoExpo b) = parseShow b
328-
parseShow (Expo b expo) = parseShow b <> " ^ " <> parseShow expo
327+
parseShow (Expo e) = parseShow e
329328

330329
instance ParseShow NumBase where
331330
parseShow (NBParen p) = parseShow p

src/Tablebot/Plugins/Roll/Dice/DiceStats.hs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,13 @@ instance Range Negation where
136136
range' (NoNeg t) = range t
137137

138138
instance Range Expo where
139-
range' (NoExpo t) = range t
140-
range' (Expo t e) = do
141-
d <- range t
142-
d' <- range e
143-
-- if the exponent is always negative, the distribution will be empty
144-
return $ (^) <$> d <*> from (assuming (>= 0) (run d'))
139+
range' (Expo (BinOp a tas)) = foldl' foldel (range a) tas
140+
where
141+
foldel at (typ, b) = do
142+
a' <- at
143+
b' <- range b
144+
-- if the exponent is always negative, the distribution will be empty
145+
return $ getOperation typ <$> a' <*> from (assuming (>= 0) (run b'))
145146

146147
instance Range Func where
147148
range' (NoFunc t) = range t

0 commit comments

Comments
 (0)