Skip to content
Open
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
52 changes: 8 additions & 44 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ pub enum Item {
TypeAlias,
/// A function.
Function(Function),
/// A module, which is ignored.
Use,
Module,
/// A placeholder used for error recovery during parsing.
Ignored,
}

/// Definition of a function.
Expand Down Expand Up @@ -984,7 +986,11 @@ impl AbstractSyntaxTree for Item {
Error::UseKeywordIsNotSupported,
*use_decl.span(),
)),
parse::Item::Module => Ok(Self::Module),
parse::Item::Module(module) => Err(RichError::new(
Error::ModuleKeywordIsNotSupported,
*module.span(),
)),
parse::Item::Ignored => Ok(Self::Ignored),
};

scope.file_id = previous_file_id;
Expand Down Expand Up @@ -1663,48 +1669,6 @@ impl AbstractSyntaxTree for Match {
}
}

impl AbstractSyntaxTree for Module {
type From = parse::Module;

fn analyze(from: &Self::From, ty: &ResolvedType, scope: &mut Scope) -> Result<Self, RichError> {
assert!(ty.is_unit(), "Modules cannot return anything");
assert!(scope.is_topmost(), "Modules live in the topmost scope only");
let assignments = from
.assignments()
.iter()
.map(|s| ModuleAssignment::analyze(s, ty, scope))
.collect::<Result<Arc<[ModuleAssignment]>, RichError>>()?;
debug_assert!(scope.is_topmost());

Ok(Self {
name: from.name().shallow_clone(),
span: *from.as_ref(),
assignments,
})
}
}

impl AbstractSyntaxTree for ModuleAssignment {
type From = parse::ModuleAssignment;

fn analyze(from: &Self::From, ty: &ResolvedType, scope: &mut Scope) -> Result<Self, RichError> {
assert!(ty.is_unit(), "Assignments cannot return anything");
let ty_expr = scope.resolve(from.ty()).with_span(from)?;
let expression = Expression::analyze(from.expression(), &ty_expr, scope)?;
let value = Value::from_const_expr(&expression)
.ok_or(Error::ExpressionUnexpectedType {
ty: ty_expr.clone(),
})
.with_span(from.expression())?;

Ok(Self {
name: from.name().clone(),
value,
span: *from.as_ref(),
})
}
}

impl AsRef<Span> for Assignment {
fn as_ref(&self) -> &Span {
&self.span
Expand Down
6 changes: 4 additions & 2 deletions src/driver/resolve_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ impl Program {
}

// Safe to skip: `Use` items are handled earlier in the loop, and `Module` currently has no functionality.
parse::Item::Module | parse::Item::Use(_) => continue,
// It will handle properly in the following commits.
parse::Item::Module(_) | parse::Item::Use(_) | parse::Item::Ignored => continue,
}
items.push(new_elem);
}
Expand Down Expand Up @@ -238,7 +239,8 @@ impl DependencyGraph {
}

// Safe to skip: `Use` items are handled earlier in the loop, and `Module` currently has no functionality.
parse::Item::Module | parse::Item::Use(_) => continue,
// It will handle properly in the following commits.
parse::Item::Module(_) | parse::Item::Use(_) | parse::Item::Ignored => continue,
}
items.push(new_elem);
}
Expand Down
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,9 @@ pub enum Error {
declared: ResolvedType,
assigned: ResolvedType,
},
// TODO: Remove these once `use` and `mod` are supported by the AST
UseKeywordIsNotSupported,
ModuleKeywordIsNotSupported,
}

#[rustfmt::skip]
Expand Down Expand Up @@ -860,6 +862,10 @@ impl fmt::Display for Error {
f,
"The `use` keyword is not supported yet"
),
Error::ModuleKeywordIsNotSupported => write!(
f,
"The `mod` keyword is not supported yet"
),
}
}
}
Expand Down
Loading
Loading