2022-12-27 20:16:18 -08:00
|
|
|
use {super::*, CompileErrorKind::*};
|
2017-11-17 17:28:06 -08:00
|
|
|
|
2019-12-07 03:09:21 -08:00
|
|
|
pub(crate) struct AssignmentResolver<'src: 'run, 'run> {
|
|
|
|
assignments: &'run Table<'src, Assignment<'src>>,
|
2021-09-16 06:44:40 -07:00
|
|
|
stack: Vec<&'src str>,
|
|
|
|
evaluated: BTreeSet<&'src str>,
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
|
2019-12-07 03:09:21 -08:00
|
|
|
impl<'src: 'run, 'run> AssignmentResolver<'src, 'run> {
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn resolve_assignments(
|
2019-12-07 03:09:21 -08:00
|
|
|
assignments: &Table<'src, Assignment<'src>>,
|
2021-07-26 01:26:06 -07:00
|
|
|
) -> CompileResult<'src, ()> {
|
2017-11-18 01:18:04 -08:00
|
|
|
let mut resolver = AssignmentResolver {
|
2021-07-19 18:10:35 -07:00
|
|
|
stack: Vec::new(),
|
|
|
|
evaluated: BTreeSet::new(),
|
2018-03-05 13:21:35 -08:00
|
|
|
assignments,
|
2017-11-18 01:18:04 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
for name in assignments.keys() {
|
|
|
|
resolver.resolve_assignment(name)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
fn resolve_assignment(&mut self, name: &'src str) -> CompileResult<'src, ()> {
|
2017-11-16 23:30:08 -08:00
|
|
|
if self.evaluated.contains(name) {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
self.stack.push(name);
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
if let Some(assignment) = self.assignments.get(name) {
|
2019-12-07 03:09:21 -08:00
|
|
|
self.resolve_expression(&assignment.value)?;
|
2017-11-16 23:30:08 -08:00
|
|
|
self.evaluated.insert(name);
|
|
|
|
} else {
|
2022-12-15 16:53:21 -08:00
|
|
|
let message = format!("attempted to resolve unknown assignment `{name}`");
|
2019-11-13 19:32:50 -08:00
|
|
|
let token = Token {
|
2021-09-16 06:44:40 -07:00
|
|
|
src: "",
|
2019-04-15 22:40:02 -07:00
|
|
|
offset: 0,
|
2021-09-16 06:44:40 -07:00
|
|
|
line: 0,
|
2017-11-16 23:30:08 -08:00
|
|
|
column: 0,
|
2019-11-13 19:32:50 -08:00
|
|
|
length: 0,
|
2021-09-16 06:44:40 -07:00
|
|
|
kind: TokenKind::Unspecified,
|
2019-11-13 19:32:50 -08:00
|
|
|
};
|
2022-09-11 01:41:24 -07:00
|
|
|
return Err(CompileError::new(token, Internal { message }));
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
2021-07-19 18:10:35 -07:00
|
|
|
|
|
|
|
self.stack.pop();
|
|
|
|
|
2017-11-16 23:30:08 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
fn resolve_expression(&mut self, expression: &Expression<'src>) -> CompileResult<'src, ()> {
|
2019-04-11 23:58:08 -07:00
|
|
|
match expression {
|
2019-11-07 10:55:15 -08:00
|
|
|
Expression::Variable { name } => {
|
|
|
|
let variable = name.lexeme();
|
|
|
|
if self.evaluated.contains(variable) {
|
2019-11-21 10:14:10 -08:00
|
|
|
Ok(())
|
2021-07-19 18:10:35 -07:00
|
|
|
} else if self.stack.contains(&variable) {
|
2019-11-07 10:55:15 -08:00
|
|
|
let token = self.assignments[variable].name.token();
|
|
|
|
self.stack.push(variable);
|
2019-11-21 10:14:10 -08:00
|
|
|
Err(token.error(CircularVariableDependency {
|
Gargantuan refactor (#522)
- Instead of changing the current directory with `env::set_current_dir`
to be implicitly inherited by subprocesses, we now use
`Command::current_dir` to set it explicitly. This feels much better,
since we aren't dependent on the implicit state of the process's
current directory.
- Subcommand execution is much improved.
- Added a ton of tests for config parsing, config execution, working
dir, and search dir.
- Error messages are improved. Many more will be colored.
- The Config is now onwed, instead of borrowing from the arguments and
the `clap::ArgMatches` object. This is a huge ergonomic improvement,
especially in tests, and I don't think anyone will notice.
- `--edit` now uses `$VISUAL`, `$EDITOR`, or `vim`, in that order,
matching git, which I think is what most people will expect.
- Added a cute `tmptree!{}` macro, for creating temporary directories
populated with directories and files for tests.
- Admitted that grammer is LL(k) and I don't know what `k` is.
2019-11-09 21:43:20 -08:00
|
|
|
variable,
|
2018-12-08 14:29:41 -08:00
|
|
|
circle: self.stack.clone(),
|
2019-11-21 10:14:10 -08:00
|
|
|
}))
|
2019-11-07 10:55:15 -08:00
|
|
|
} else if self.assignments.contains_key(variable) {
|
2019-11-21 10:14:10 -08:00
|
|
|
self.resolve_assignment(variable)
|
2017-11-16 23:30:08 -08:00
|
|
|
} else {
|
2019-11-21 10:14:10 -08:00
|
|
|
Err(name.token().error(UndefinedVariable { variable }))
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2019-11-21 10:14:10 -08:00
|
|
|
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)
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2021-10-14 17:00:58 -07:00
|
|
|
Thunk::BinaryPlus {
|
|
|
|
args: ([a, b], rest),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
self.resolve_expression(a)?;
|
|
|
|
self.resolve_expression(b)?;
|
|
|
|
for arg in rest {
|
|
|
|
self.resolve_expression(arg)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-07-03 12:39:45 -07:00
|
|
|
Thunk::Ternary {
|
|
|
|
args: [a, b, c], ..
|
|
|
|
} => {
|
|
|
|
self.resolve_expression(a)?;
|
|
|
|
self.resolve_expression(b)?;
|
|
|
|
self.resolve_expression(c)
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2019-11-21 10:14:10 -08:00
|
|
|
},
|
2022-09-11 00:48:02 -07:00
|
|
|
Expression::Concatenation { lhs, rhs } => {
|
2017-11-16 23:30:08 -08:00
|
|
|
self.resolve_expression(lhs)?;
|
2019-11-21 10:14:10 -08:00
|
|
|
self.resolve_expression(rhs)
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2022-09-11 00:48:02 -07:00
|
|
|
Expression::Join { lhs, rhs } => {
|
|
|
|
if let Some(lhs) = lhs {
|
|
|
|
self.resolve_expression(lhs)?;
|
|
|
|
}
|
|
|
|
self.resolve_expression(rhs)
|
|
|
|
}
|
2020-10-26 18:16:42 -07:00
|
|
|
Expression::Conditional {
|
|
|
|
lhs,
|
|
|
|
rhs,
|
|
|
|
then,
|
|
|
|
otherwise,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
self.resolve_expression(lhs)?;
|
|
|
|
self.resolve_expression(rhs)?;
|
|
|
|
self.resolve_expression(then)?;
|
|
|
|
self.resolve_expression(otherwise)
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2019-11-21 10:14:10 -08:00
|
|
|
Expression::StringLiteral { .. } | Expression::Backtick { .. } => Ok(()),
|
|
|
|
Expression::Group { contents } => self.resolve_expression(contents),
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-11-07 10:55:15 -08:00
|
|
|
mod tests {
|
2017-11-16 23:30:08 -08:00
|
|
|
use super::*;
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
analysis_error! {
|
2017-11-18 01:44:59 -08:00
|
|
|
name: circular_variable_dependency,
|
2021-03-28 23:39:23 -07:00
|
|
|
input: "a := b\nb := a",
|
2019-04-15 22:40:02 -07:00
|
|
|
offset: 0,
|
2017-11-18 01:44:59 -08:00
|
|
|
line: 0,
|
|
|
|
column: 0,
|
2019-04-15 22:40:02 -07:00
|
|
|
width: 1,
|
2017-11-18 01:44:59 -08:00
|
|
|
kind: CircularVariableDependency{variable: "a", circle: vec!["a", "b", "a"]},
|
2017-11-17 17:28:06 -08:00
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
analysis_error! {
|
2017-11-18 01:44:59 -08:00
|
|
|
name: self_variable_dependency,
|
2021-03-28 23:39:23 -07:00
|
|
|
input: "a := a",
|
|
|
|
offset: 0,
|
2017-11-18 01:44:59 -08:00
|
|
|
line: 0,
|
|
|
|
column: 0,
|
2019-04-15 22:40:02 -07:00
|
|
|
width: 1,
|
2017-11-18 01:44:59 -08:00
|
|
|
kind: CircularVariableDependency{variable: "a", circle: vec!["a", "a"]},
|
|
|
|
}
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
analysis_error! {
|
2017-11-18 01:44:59 -08:00
|
|
|
name: unknown_expression_variable,
|
2021-03-28 23:39:23 -07:00
|
|
|
input: "x := yy",
|
|
|
|
offset: 5,
|
2017-11-18 01:44:59 -08:00
|
|
|
line: 0,
|
2021-03-28 23:39:23 -07:00
|
|
|
column: 5,
|
2019-04-15 22:40:02 -07:00
|
|
|
width: 2,
|
2017-11-18 01:44:59 -08:00
|
|
|
kind: UndefinedVariable{variable: "yy"},
|
2017-11-17 17:28:06 -08:00
|
|
|
}
|
2017-12-02 05:37:10 -08:00
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
analysis_error! {
|
2019-11-21 10:14:10 -08:00
|
|
|
name: unknown_function_parameter,
|
|
|
|
input: "x := env_var(yy)",
|
|
|
|
offset: 13,
|
2017-12-02 05:37:10 -08:00
|
|
|
line: 0,
|
2019-11-21 10:14:10 -08:00
|
|
|
column: 13,
|
|
|
|
width: 2,
|
|
|
|
kind: UndefinedVariable{variable: "yy"},
|
|
|
|
}
|
|
|
|
|
|
|
|
analysis_error! {
|
|
|
|
name: unknown_function_parameter_binary_first,
|
|
|
|
input: "x := env_var_or_default(yy, 'foo')",
|
|
|
|
offset: 24,
|
|
|
|
line: 0,
|
|
|
|
column: 24,
|
|
|
|
width: 2,
|
|
|
|
kind: UndefinedVariable{variable: "yy"},
|
|
|
|
}
|
|
|
|
|
|
|
|
analysis_error! {
|
|
|
|
name: unknown_function_parameter_binary_second,
|
|
|
|
input: "x := env_var_or_default('foo', yy)",
|
|
|
|
offset: 31,
|
|
|
|
line: 0,
|
|
|
|
column: 31,
|
|
|
|
width: 2,
|
|
|
|
kind: UndefinedVariable{variable: "yy"},
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|