Skip to content

Commit

Permalink
Adds support for %% and its derivatives (Also operator""())
Browse files Browse the repository at this point in the history
The float stuff was easy. Operator was too, just took a bit to realize
how it worked
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 c2a23e1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 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
40 changes: 24 additions & 16 deletions crates/dreammaker/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ table! {
"!", Not;
"!=", NotEq;
"\"", DoubleQuote;
"\"\"", EmptyQuote;
"#", Hash;
"##", TokenPaste;
"%", Mod;
"%%", FloatMod;
"%%=", FloatModAssign;
"%=", ModAssign;
"&", BitAnd;
"&&", And;
Expand Down Expand Up @@ -146,22 +149,26 @@ impl fmt::Display for Punctuation {
/// character in the input, blazing fast. The code to generate it is contained
/// in the following test.
static SPEEDY_TABLE: [(usize, usize); 127] = [
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 1), (1, 2), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(2, 3), (3, 5), (5, 6), (6, 8), (0, 0), (8, 10), (10, 14), (14, 15),
(15, 16), (16, 17), (17, 20), (20, 23), (23, 24), (24, 27), (27, 30), (30, 34),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (34, 37), (37, 38), (38, 43), (43, 45), (45, 49), (49, 53),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (53, 54), (0, 0), (54, 55), (55, 57), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (57, 59), (59, 63), (63, 64), (64, 67)];
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 1), (1, 2), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (2, 3), (3, 5), (5, 7),
(7, 9), (0, 0), (9, 13), (13, 17), (17, 18), (18, 19), (19, 20),
(20, 23), (23, 26), (26, 27), (27, 30), (30, 33), (33, 37), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (37, 40), (40, 41), (41, 46), (46, 48), (48, 52),
(52, 56), (0, 0), (0,0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(56, 57), (0, 0), (57, 58), (58, 60), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (60, 62), (62, 66), (66, 67),
(67, 70)
];

#[test]
fn make_speedy_table() {
Expand Down Expand Up @@ -278,6 +285,7 @@ impl Token {
Eq |
NotEq |
Mod |
FloatMod |
And |
BitAndAssign |
AndAssign |
Expand Down
12 changes: 10 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 Expand Up @@ -944,6 +950,8 @@ impl<'ctx, 'an, 'inp> Parser<'ctx, 'an, 'inp> {
} else {
last_part.push_str("[]");
}
} else if self.exact(Punct(EmptyQuote))?.is_some() {
last_part.push_str("\"\"")
}
SUCCESS
}
Expand Down

0 comments on commit c2a23e1

Please sign in to comment.