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 all commits
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
12 changes: 12 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,15 @@ impl<'a> ConstantFolder<'a> {
None => return Err(self.error("malformed nameof() call, expression appears to have no name")),
}
}
"fexists" if self.defines.is_some() => {
if args.len() != 1 {
return Err(self.error(format!("malformed fexists() call, must have 1 argument and instead has {}", args.len())));
}
match args[0].as_term() {
Some(Term::String(ref path)) => Constant::from(std::path::Path::new(path).exists()),
_ => return Err(self.error("malformed fexists() call, argument given isn't a string.")),
}
}
// 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 no_fexists_outside_preproc() {
assert_eq!(
eval("fexists()").unwrap_err().description(),
"non-constant function call: fexists",
);
}
ZeWaka marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 17 additions & 0 deletions crates/dreammaker/tests/macro_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,20 @@ ok2
Ident("ok2".into(), false),
]);
}

#[test]
fn fexists_function() {
assert_eq!(process(r#"
#if fexists("README.md")
exists
#endif
"#), &[
Ident("exists".into(), false),
]);

assert_eq!(process(r#"
#if fexists("this file does not exist")
exists
#endif
"#), &[]);
}