Skip to content

Commit

Permalink
Update workflow rust toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
camsteffen committed Dec 12, 2022
1 parent f615d3d commit f0d0291
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 313 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
with:
components: clippy, rustfmt
override: true
toolchain: 1.56.0
toolchain: 1.65.0

- uses: Swatinem/rust-cache@v1

Expand Down
30 changes: 4 additions & 26 deletions src/assignment_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,10 @@ impl<'src: 'run, 'run> AssignmentResolver<'src, 'run> {
Err(name.token().error(UndefinedVariable { variable }))
}
}
Expression::Call { thunk } => match thunk {
Thunk::Nullary { .. } => Ok(()),
Thunk::Unary { arg, .. } => self.resolve_expression(arg),
Thunk::Binary { args: [a, b], .. } => {
self.resolve_expression(a)?;
self.resolve_expression(b)
}
Thunk::BinaryPlus {
args: ([a, b], rest),
..
} => {
self.resolve_expression(a)?;
self.resolve_expression(b)?;
for arg in rest {
self.resolve_expression(arg)?;
}
Ok(())
}
Thunk::Ternary {
args: [a, b, c], ..
} => {
self.resolve_expression(a)?;
self.resolve_expression(b)?;
self.resolve_expression(c)
}
},
Expression::Call { thunk } => thunk
.arguments
.iter()
.try_for_each(|arg| self.resolve_expression(arg)),
Expression::Concatenation { lhs, rhs } => {
self.resolve_expression(lhs)?;
self.resolve_expression(rhs)
Expand Down
90 changes: 28 additions & 62 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,81 +66,47 @@ impl<'src, 'run> Evaluator<'src, 'run> {
}
}
Expression::Call { thunk } => {
use Thunk::*;
use Function::*;

let context = FunctionContext {
dotenv: self.dotenv,
invocation_directory: &self.config.invocation_directory,
search: self.search,
};

match thunk {
Nullary { name, function, .. } => {
function(&context).map_err(|message| Error::FunctionCall {
function: *name,
message,
})
}
Unary {
name,
function,
arg,
..
} => function(&context, &self.evaluate_expression(arg)?).map_err(|message| {
Error::FunctionCall {
function: *name,
message,
macro_rules! func {
($func:ident($($a:ident),*)) => {
match thunk.arguments.as_slice() {
[$($a),*] => $func(&context $(, &self.evaluate_expression($a)?)*),
_ => panic!("thunk has wrong number of arguments"),
}
}),
Binary {
name,
function,
args: [a, b],
..
} => function(
&context,
&self.evaluate_expression(a)?,
&self.evaluate_expression(b)?,
)
.map_err(|message| Error::FunctionCall {
function: *name,
message,
}),
BinaryPlus {
name,
function,
args: ([a, b], rest),
..
} => {
}
}

let result = match thunk.function {
Nullary(function) => func!(function()),
Unary(function) => func!(function(a)),
Binary(function) => func!(function(a, b)),
BinaryPlus(function) => {
let [a, b, rest @ ..] = thunk.arguments.as_slice() else {
panic!("thunk has wrong number of arguments");
};
let a = self.evaluate_expression(a)?;
let b = self.evaluate_expression(b)?;

let mut rest_evaluated = Vec::new();
for arg in rest {
rest_evaluated.push(self.evaluate_expression(arg)?);
}
let rest_evaluated = rest
.iter()
.map(|arg| self.evaluate_expression(arg))
.collect::<Result<Vec<_>, _>>()?;

function(&context, &a, &b, &rest_evaluated).map_err(|message| Error::FunctionCall {
function: *name,
message,
})
function(&context, &a, &b, &rest_evaluated)
}
Ternary {
name,
function,
args: [a, b, c],
..
} => function(
&context,
&self.evaluate_expression(a)?,
&self.evaluate_expression(b)?,
&self.evaluate_expression(c)?,
)
.map_err(|message| Error::FunctionCall {
function: *name,
message,
}),
}
Ternary(function) => func!(function(a, b, c)),
};
result.map_err(|message| Error::FunctionCall {
function: thunk.name,
message,
})
}
Expression::StringLiteral { string_literal } => Ok(string_literal.cooked.clone()),
Expression::Backtick { contents, token } => {
Expand Down
1 change: 1 addition & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use heck::{

use Function::*;

#[derive(Clone)]
pub(crate) enum Function {
Nullary(fn(&FunctionContext) -> Result<String, String>),
Unary(fn(&FunctionContext, &str) -> Result<String, String>),
Expand Down
41 changes: 3 additions & 38 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,45 +69,10 @@ impl<'src> Node<'src> for Expression<'src> {
tree
}
Expression::Call { thunk } => {
use Thunk::*;

let mut tree = Tree::atom("call");

match thunk {
Nullary { name, .. } => tree.push_mut(name.lexeme()),
Unary { name, arg, .. } => {
tree.push_mut(name.lexeme());
tree.push_mut(arg.tree());
}
Binary {
name, args: [a, b], ..
} => {
tree.push_mut(name.lexeme());
tree.push_mut(a.tree());
tree.push_mut(b.tree());
}
BinaryPlus {
name,
args: ([a, b], rest),
..
} => {
tree.push_mut(name.lexeme());
tree.push_mut(a.tree());
tree.push_mut(b.tree());
for arg in rest {
tree.push_mut(arg.tree());
}
}
Ternary {
name,
args: [a, b, c],
..
} => {
tree.push_mut(name.lexeme());
tree.push_mut(a.tree());
tree.push_mut(b.tree());
tree.push_mut(c.tree());
}
tree.push_mut(thunk.name.lexeme());
for arg in &thunk.arguments {
tree.push_mut(arg.tree());
}

tree
Expand Down
42 changes: 4 additions & 38 deletions src/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod full {
pub(crate) use crate::{
assignment::Assignment, conditional_operator::ConditionalOperator, dependency::Dependency,
expression::Expression, fragment::Fragment, justfile::Justfile, line::Line,
parameter::Parameter, parameter_kind::ParameterKind, recipe::Recipe, thunk::Thunk,
parameter::Parameter, parameter_kind::ParameterKind, recipe::Recipe,
};
}

Expand Down Expand Up @@ -215,43 +215,9 @@ impl Expression {
Backtick { contents, .. } => Expression::Backtick {
command: (*contents).clone(),
},
Call { thunk } => match thunk {
full::Thunk::Nullary { name, .. } => Expression::Call {
name: name.lexeme().to_owned(),
arguments: Vec::new(),
},
full::Thunk::Unary { name, arg, .. } => Expression::Call {
name: name.lexeme().to_owned(),
arguments: vec![Expression::new(arg)],
},
full::Thunk::Binary {
name, args: [a, b], ..
} => Expression::Call {
name: name.lexeme().to_owned(),
arguments: vec![Expression::new(a), Expression::new(b)],
},
full::Thunk::BinaryPlus {
name,
args: ([a, b], rest),
..
} => {
let mut arguments = vec![Expression::new(a), Expression::new(b)];
for arg in rest {
arguments.push(Expression::new(arg));
}
Expression::Call {
name: name.lexeme().to_owned(),
arguments,
}
}
full::Thunk::Ternary {
name,
args: [a, b, c],
..
} => Expression::Call {
name: name.lexeme().to_owned(),
arguments: vec![Expression::new(a), Expression::new(b), Expression::new(c)],
},
Call { thunk } => Expression::Call {
name: thunk.name.lexeme().to_owned(),
arguments: thunk.arguments.iter().map(Expression::new).collect(),
},
Concatenation { lhs, rhs } => Expression::Concatenation {
lhs: Box::new(Expression::new(lhs)),
Expand Down
Loading

0 comments on commit f0d0291

Please sign in to comment.