Skip to content
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
16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,25 @@ license = "CC0-1.0"
repository = "https://github.com/terror/val"
resolver = "2"

[profile.release]
lto = true
codegen-units = 1

[lints]
workspace = true

[workspace]
members = [".", "crates/*"]

[workspace.lints.clippy]
all = { level = "deny", priority = -1 }
arbitrary-source-item-ordering = "deny"
pedantic = { level = "deny", priority = -1 }
wildcard_imports = "allow"

[workspace.lints.rust]
unreachable_pub = "deny"

[[bench]]
name = "main"
harness = false
Expand Down
8 changes: 4 additions & 4 deletions benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn bench_increment_value(criterion: &mut Criterion) {
group.bench_function(format!("n = {number}"), |bencher| {
bencher.iter(|| {
black_box(Evaluator::from(Environment::default()).eval(&ast)).unwrap();
})
});
});
}

Expand All @@ -27,7 +27,7 @@ fn bench_prime_count(criterion: &mut Criterion) {

for &number in &[5_000_u32, 10_000_u32] {
let program = format!(
r#"
r"
fn prime(n) {{
if (n < 2) {{
return false
Expand Down Expand Up @@ -63,7 +63,7 @@ fn bench_prime_count(criterion: &mut Criterion) {
}}

count({number})
"#
"
);

let ast = val::parse(&program).unwrap();
Expand Down Expand Up @@ -91,7 +91,7 @@ fn bench_recursive_factorial(criterion: &mut Criterion) {
group.bench_function(format!("n = {number}"), |bencher| {
bencher.iter(|| {
black_box(Evaluator::from(Environment::default()).eval(&ast)).unwrap();
})
});
});
}

Expand Down
3 changes: 3 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cognitive-complexity-threshold = 1337
too-many-lines-threshold = 250
source-item-ordering = ['enum', 'struct', 'trait']
46 changes: 22 additions & 24 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,8 @@ pub struct Arguments {
}

impl Arguments {
pub fn run(self) -> Result {
match (&self.filename, &self.expression) {
(Some(filename), _) => self.eval(filename.clone()),
(_, Some(expression)) => self.eval_expression(expression.clone()),
_ => {
#[cfg(not(target_family = "wasm"))]
{
self.read()
}
#[cfg(target_family = "wasm")]
{
Err(anyhow::anyhow!("Interactive mode not supported in WASM"))
}
}
}
}

fn eval(&self, filename: PathBuf) -> Result {
let content = fs::read_to_string(&filename)?;
fn eval(&self, filename: &PathBuf) -> Result {
let content = fs::read_to_string(filename)?;

let filename = filename.to_string_lossy().to_string();

Expand Down Expand Up @@ -124,7 +107,7 @@ impl Arguments {
return Ok(());
}

println!("{}", value);
println!("{value}");

Ok(())
}
Expand Down Expand Up @@ -228,6 +211,23 @@ impl Arguments {
}
}
}

pub fn run(self) -> Result {
match (&self.filename, &self.expression) {
(Some(filename), _) => self.eval(filename),
(_, Some(expression)) => self.eval_expression(expression.clone()),
_ => {
#[cfg(not(target_family = "wasm"))]
{
self.read()
}
#[cfg(target_family = "wasm")]
{
Err(anyhow::anyhow!("Interactive mode not supported in WASM"))
}
}
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -301,8 +301,7 @@ mod tests {

assert!(
error.contains("cannot be used with"),
"Error should mention conflicts: {}",
error
"Error should mention conflicts: {error}"
);
}

Expand All @@ -321,8 +320,7 @@ mod tests {

assert!(
error.contains("cannot be used with"),
"Error should mention conflicts: {}",
error
"Error should mention conflicts: {error}"
);
}
}
9 changes: 6 additions & 3 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl Display for Program<'_> {
}

impl Program<'_> {
#[must_use]
pub fn kind(&self) -> String {
String::from(match self {
Program::Statements(_) => "statements",
Expand Down Expand Up @@ -160,6 +161,7 @@ impl Display for Statement<'_> {
}

impl Statement<'_> {
#[must_use]
pub fn kind(&self) -> String {
String::from(match self {
Statement::Assignment(_, _) => "assignment",
Expand Down Expand Up @@ -251,7 +253,7 @@ impl Display for Expression<'_> {
Expression::BinaryOp(op, lhs, rhs) => {
write!(f, "binary_op({}, {}, {})", op, lhs.0, rhs.0)
}
Expression::Boolean(boolean) => write!(f, "boolean({})", boolean),
Expression::Boolean(boolean) => write!(f, "boolean({boolean})"),
Expression::FunctionCall(name, arguments) => {
write!(
f,
Expand All @@ -265,7 +267,7 @@ impl Display for Expression<'_> {
)
}
Expression::Identifier(identifier) => {
write!(f, "identifier({})", identifier)
write!(f, "identifier({identifier})")
}
Expression::List(list) => {
write!(
Expand All @@ -283,7 +285,7 @@ impl Display for Expression<'_> {
}
Expression::Null => write!(f, "null"),
Expression::Number(number) => write!(f, "number({})", number.display()),
Expression::String(string) => write!(f, "string(\"{}\")", string),
Expression::String(string) => write!(f, "string(\"{string}\")"),
Expression::UnaryOp(op, expr) => {
write!(f, "unary_op({}, {})", op, expr.0)
}
Expand All @@ -292,6 +294,7 @@ impl Display for Expression<'_> {
}

impl Expression<'_> {
#[must_use]
pub fn kind(&self) -> String {
String::from(match self {
Expression::BinaryOp(_, _, _) => "binary_op",
Expand Down
6 changes: 4 additions & 2 deletions src/builtin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

pub type BuiltinFunction =
for<'src> fn(BuiltinFunctionPayload<'src>) -> Result<Value<'src>, Error>;
for<'src> fn(&BuiltinFunctionPayload<'src>) -> Result<Value<'src>, Error>;

pub struct BuiltinFunctionPayload<'src> {
pub arguments: Vec<Value<'src>>,
Expand All @@ -13,7 +13,7 @@ pub struct BuiltinFunctionPayload<'src> {
pub enum Builtin {
Constant {
name: &'static str,
value: fn(Config) -> Value<'static>,
value: fn(&Config) -> Value<'static>,
},
Function {
function: BuiltinFunction,
Expand All @@ -22,13 +22,15 @@ pub enum Builtin {
}

impl Builtin {
#[must_use]
pub fn kind(&self) -> &'static str {
match self {
Self::Constant { .. } => "constant",
Self::Function { .. } => "function",
}
}

#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::Constant { name, .. } | Self::Function { name, .. } => name,
Expand Down
Loading
Loading