Skip to content

Commit

Permalink
Adds support for %% and its derivatives
Browse files Browse the repository at this point in the history
I'm assuming this handles operator overloading automatically.
Nothin much else to say, outside that I needed to roll my own operation
for this one, since float remainder is kinda hard to find (and I
couldn't after cursory reading)
  • Loading branch information
LemonInTheDark committed Oct 5, 2023
1 parent 6df1be0 commit 0a10a78
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions crates/dreammaker/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub enum BinaryOp {
Div,
Pow,
Mod,
FloatMod,
Eq,
NotEq,
Less,
Expand Down Expand Up @@ -142,6 +143,7 @@ impl fmt::Display for BinaryOp {
Div => "/",
Pow => "**",
Mod => "%",
FloatMod => "%%",
Eq => "==",
NotEq => "!=",
Less => "<",
Expand Down Expand Up @@ -172,6 +174,7 @@ pub enum AssignOp {
MulAssign,
DivAssign,
ModAssign,
FloatModAssign,
AssignInto,
BitAndAssign,
AndAssign,
Expand All @@ -192,6 +195,7 @@ impl fmt::Display for AssignOp {
MulAssign => "*=",
DivAssign => "/=",
ModAssign => "%=",
FloatModAssign => "%%=",
AssignInto => ":=",
BitAndAssign => "&=",
AndAssign => "&&=",
Expand Down Expand Up @@ -233,6 +237,7 @@ augmented! {
Mul = MulAssign;
Div = DivAssign;
Mod = ModAssign;
FloatMod = FloatModAssign;
BitAnd = BitAndAssign;
BitOr = BitOrAssign;
BitXor = BitXorAssign;
Expand Down
8 changes: 8 additions & 0 deletions crates/dreammaker/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,14 @@ impl<'a> ConstantFolder<'a> {
numeric!(LessEq <=);
numeric!(Greater >);
numeric!(GreaterEq >=);
match (op, lhs, rhs) {
(BinaryOp::FloatMod, Float(lhs), Float(rhs)) => return Ok(Constant::from(lhs - ((lhs / rhs).floor() * rhs))),
(_, lhs_, rhs_) => {
lhs = lhs_;
rhs = rhs_;
}
}

match (op, lhs, rhs) {
(BinaryOp::Pow, Float(lhs), Float(rhs)) => return Ok(Constant::from(lhs.powf(rhs))),
(_, lhs_, rhs_) => {
Expand Down
3 changes: 3 additions & 0 deletions crates/dreammaker/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ table! {
"##", TokenPaste;
"%", Mod;
"%=", ModAssign;
"%%", FloatMod;
"%%=", FloatModAssign;
"&", BitAnd;
"&&", And;
"&&=", AndAssign;
Expand Down Expand Up @@ -278,6 +280,7 @@ impl Token {
Eq |
NotEq |
Mod |
FloatMod |
And |
BitAndAssign |
AndAssign |
Expand Down
10 changes: 8 additions & 2 deletions crates/dreammaker/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,12 @@ oper_table! { BINARY_OPS;
Pow {
(BinaryOp, Pow),
}
// * / %
// * / % %%
Mul {
(BinaryOp, Mul), //
(BinaryOp, Div = Slash), //
(BinaryOp, Mod),
(BinaryOp, FloatMod),
}
// + -
Add {
Expand Down Expand Up @@ -205,14 +206,15 @@ oper_table! { BINARY_OPS;
Conditional {
(TernaryOp, Conditional = QuestionMark),
}
// = += -= -= *= /= %= &= |= ^= <<= >>=
// = += -= -= *= /= %= %%= &= |= ^= <<= >>=
Assign {
(AssignOp, Assign),
(AssignOp, AddAssign),
(AssignOp, SubAssign),
(AssignOp, MulAssign),
(AssignOp, DivAssign),
(AssignOp, ModAssign),
(AssignOp, FloatModAssign),
(AssignOp, BitAndAssign),
(AssignOp, BitOrAssign),
(AssignOp, BitXorAssign),
Expand Down Expand Up @@ -887,6 +889,10 @@ impl<'ctx, 'an, 'inp> Parser<'ctx, 'an, 'inp> {
last_part.push('%');
} else if self.exact(Punct(ModAssign))?.is_some() {
last_part.push_str("%=");
} else if self.exact(Punct(FloatMod))?.is_some() {
last_part.push_str("%%");
} else if self.exact(Punct(FloatModAssign))?.is_some() {
last_part.push_str("%%=");
} else if self.exact(Punct(BitAnd))?.is_some() {
last_part.push('&');
} else if self.exact(Punct(BitAndAssign))?.is_some() {
Expand Down

0 comments on commit 0a10a78

Please sign in to comment.