Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement %%, %%=, and operator"" #371

Merged
merged 2 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
LemonInTheDark marked this conversation as resolved.
Show resolved Hide resolved
"#", 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() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A less shocking change might be to look for an empty Token::String here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handled, I think?

last_part.push_str("\"\"")
}
SUCCESS
}
Expand Down