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

fexists can be called in the preprocessor #388

Merged
merged 3 commits into from
May 24, 2024
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
1 change: 1 addition & 0 deletions crates/dreamchecker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl<'o> AssumptionSet<'o> {
ConstFn::Newlist => AssumptionSet::from_valid_instance(objtree.expect("/list")),
ConstFn::Sound => AssumptionSet::from_valid_instance(objtree.expect("/sound")),
ConstFn::Generator => AssumptionSet::from_valid_instance(objtree.expect("/generator")),
ConstFn::FileExists => AssumptionSet::default(),
ConstFn::Filter => AssumptionSet::default(),
ConstFn::File => AssumptionSet::default(),
},
Expand Down
9 changes: 9 additions & 0 deletions crates/dreammaker/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ pub enum ConstFn {
File,
/// The `generator()` type constructor.
Generator,
/// `fexists()`, which can be used in preprocessor expressions.
FileExists,
}

/// A constant-evaluation error (usually type mismatch).
Expand Down Expand Up @@ -411,6 +413,7 @@ impl fmt::Display for ConstFn {
ConstFn::Filter => "filter",
ConstFn::File => "file",
ConstFn::Generator => "generator",
ConstFn::FileExists => "fexists"
})
}
}
Expand Down Expand Up @@ -812,6 +815,12 @@ impl<'a> ConstantFolder<'a> {
None => return Err(self.error("malformed nameof() call, expression appears to have no name")),
}
}
"fexists" => {
if args.len() != 1 {
return Err(self.error(format!("malformed fexists() call, must have 1 argument and instead has {}", args.len())));
}
Constant::Call(ConstFn::FileExists, self.arguments(args)?)
ZeWaka marked this conversation as resolved.
Show resolved Hide resolved
}
// other functions are no-goes
_ => return Err(self.error(format!("non-constant function call: {}", ident))),
},
Expand Down
8 changes: 8 additions & 0 deletions crates/dreammaker/tests/constants_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,11 @@ fn rgb_hcy() {
Constant::string("#000000"),
);
}

#[test]
fn fexists_noarg() {
assert_eq!(
eval("fexists()").unwrap_err().description(),
"malformed fexists() call, must have 1 argument and instead has 0",
);
}
ZeWaka marked this conversation as resolved.
Show resolved Hide resolved