Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into swernli/unimplemented…
Browse files Browse the repository at this point in the history
…-attr
  • Loading branch information
swernli committed Nov 9, 2023
2 parents c46e5a8 + b74867c commit 1fe86d4
Show file tree
Hide file tree
Showing 42 changed files with 2,061 additions and 463 deletions.
112 changes: 102 additions & 10 deletions compiler/qsc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use indenter::{indented, Format, Indented};
use num_bigint::BigInt;
use qsc_data_structures::span::Span;
use qsc_data_structures::span::{Span, WithSpan};
use std::{
cmp::Ordering,
fmt::{self, Display, Formatter, Write},
Expand Down Expand Up @@ -107,6 +107,22 @@ impl Hash for NodeId {
}
}

/// Trait that allows creation of a default value with a span.
pub trait DefaultWithSpan {
/// Creates a default value with the given span by using the `Default` and `WithSpan` traits.
#[must_use]
fn default_with_span(span: Span) -> Self;
}

impl<T> DefaultWithSpan for Box<T>
where
T: Default + WithSpan,
{
fn default_with_span(span: Span) -> Self {
Box::new(T::default().with_span(span))
}
}

/// The root node of an AST.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Package {
Expand Down Expand Up @@ -317,7 +333,7 @@ impl Display for Attr {
}

/// A type definition.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Default)]
pub struct TyDef {
/// The node ID.
pub id: NodeId,
Expand All @@ -333,15 +349,24 @@ impl Display for TyDef {
}
}

impl WithSpan for TyDef {
fn with_span(self, span: Span) -> Self {
Self { span, ..self }
}
}

/// A type definition kind.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Default)]
pub enum TyDefKind {
/// A field definition with an optional name but required type.
Field(Option<Box<Ident>>, Box<Ty>),
/// A parenthesized type definition.
Paren(Box<TyDef>),
/// A tuple.
Tuple(Box<[Box<TyDef>]>),
/// An invalid type definition.
#[default]
Err,
}

impl Display for TyDefKind {
Expand Down Expand Up @@ -372,6 +397,7 @@ impl Display for TyDefKind {
}
}
}
TyDefKind::Err => write!(indent, "Err")?,
}
Ok(())
}
Expand Down Expand Up @@ -541,7 +567,7 @@ impl Display for FunctorExprKind {
}

/// A type.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Default)]
pub struct Ty {
/// The node ID.
pub id: NodeId,
Expand All @@ -557,8 +583,22 @@ impl Display for Ty {
}
}

impl WithSpan for Ty {
fn with_span(self, span: Span) -> Self {
Self { span, ..self }
}
}

impl DefaultWithSpan for Ty {
/// Creates a default value with the given span by using the `Default` and `WithSpan` traits.
#[must_use]
fn default_with_span(span: Span) -> Self {
Self::default().with_span(span)
}
}

/// A type kind.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Default)]
pub enum TyKind {
/// An array type.
Array(Box<Ty>),
Expand All @@ -574,6 +614,9 @@ pub enum TyKind {
Param(Box<Ident>),
/// A tuple type.
Tuple(Box<[Ty]>),
/// An invalid type.
#[default]
Err,
}

impl Display for TyKind {
Expand Down Expand Up @@ -607,6 +650,7 @@ impl Display for TyKind {
}
}
}
TyKind::Err => write!(indent, "Err")?,
}
Ok(())
}
Expand Down Expand Up @@ -722,6 +766,12 @@ impl Display for Expr {
}
}

impl WithSpan for Expr {
fn with_span(self, span: Span) -> Self {
Self { span, ..self }
}
}

/// An expression kind.
#[derive(Clone, Debug, Default, PartialEq)]
pub enum ExprKind {
Expand Down Expand Up @@ -1082,7 +1132,7 @@ pub enum StringComponent {
}

/// A pattern.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Default)]
pub struct Pat {
/// The node ID.
pub id: NodeId,
Expand All @@ -1098,8 +1148,14 @@ impl Display for Pat {
}
}

impl WithSpan for Pat {
fn with_span(self, span: Span) -> Self {
Self { span, ..self }
}
}

/// A pattern kind.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Default)]
pub enum PatKind {
/// A binding with an optional type annotation.
Bind(Box<Ident>, Option<Box<Ty>>),
Expand All @@ -1111,6 +1167,9 @@ pub enum PatKind {
Paren(Box<Pat>),
/// A tuple: `(a, b, c)`.
Tuple(Box<[Box<Pat>]>),
/// An invalid pattern.
#[default]
Err,
}

impl Display for PatKind {
Expand Down Expand Up @@ -1150,13 +1209,14 @@ impl Display for PatKind {
}
}
}
PatKind::Err => write!(indent, "Err")?,
}
Ok(())
}
}

/// A qubit initializer.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Default)]
pub struct QubitInit {
/// The node ID.
pub id: NodeId,
Expand All @@ -1172,8 +1232,14 @@ impl Display for QubitInit {
}
}

impl WithSpan for QubitInit {
fn with_span(self, span: Span) -> Self {
Self { span, ..self }
}
}

/// A qubit initializer kind.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Default)]
pub enum QubitInitKind {
/// An array of qubits: `Qubit[a]`.
Array(Box<Expr>),
Expand All @@ -1183,6 +1249,9 @@ pub enum QubitInitKind {
Single,
/// A tuple: `(a, b, c)`.
Tuple(Box<[Box<QubitInit>]>),
/// An invalid initializer.
#[default]
Err,
}

impl Display for QubitInitKind {
Expand Down Expand Up @@ -1211,13 +1280,14 @@ impl Display for QubitInitKind {
}
}
}
QubitInitKind::Err => write!(indent, "Err")?,
}
Ok(())
}
}

/// A path to a declaration.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Default)]
pub struct Path {
/// The node ID.
pub id: NodeId,
Expand All @@ -1240,6 +1310,12 @@ impl Display for Path {
}
}

impl WithSpan for Path {
fn with_span(self, span: Span) -> Self {
Self { span, ..self }
}
}

/// An identifier.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Ident {
Expand All @@ -1251,6 +1327,22 @@ pub struct Ident {
pub name: Rc<str>,
}

impl Default for Ident {
fn default() -> Self {
Ident {
id: NodeId::default(),
span: Span::default(),
name: "".into(),
}
}
}

impl WithSpan for Ident {
fn with_span(self, span: Span) -> Self {
Self { span, ..self }
}
}

impl Display for Ident {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "Ident {} {} \"{}\"", self.id, self.span, self.name)
Expand Down
7 changes: 4 additions & 3 deletions compiler/qsc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub fn walk_ty_def(vis: &mut impl MutVisitor, def: &mut TyDef) {
}
TyDefKind::Paren(def) => vis.visit_ty_def(def),
TyDefKind::Tuple(defs) => defs.iter_mut().for_each(|d| vis.visit_ty_def(d)),
TyDefKind::Err => {}
}
}

Expand Down Expand Up @@ -184,7 +185,7 @@ pub fn walk_ty(vis: &mut impl MutVisitor, ty: &mut Ty) {
vis.visit_ty(rhs);
functors.iter_mut().for_each(|f| vis.visit_functor_expr(f));
}
TyKind::Hole => {}
TyKind::Hole | TyKind::Err => {}
TyKind::Paren(ty) => vis.visit_ty(ty),
TyKind::Param(name) => vis.visit_ident(name),
TyKind::Path(path) => vis.visit_path(path),
Expand Down Expand Up @@ -313,7 +314,7 @@ pub fn walk_pat(vis: &mut impl MutVisitor, pat: &mut Pat) {
ty.iter_mut().for_each(|t| vis.visit_ty(t));
}
PatKind::Discard(ty) => ty.iter_mut().for_each(|t| vis.visit_ty(t)),
PatKind::Elided => {}
PatKind::Elided | PatKind::Err => {}
PatKind::Paren(pat) => vis.visit_pat(pat),
PatKind::Tuple(pats) => pats.iter_mut().for_each(|p| vis.visit_pat(p)),
}
Expand All @@ -325,7 +326,7 @@ pub fn walk_qubit_init(vis: &mut impl MutVisitor, init: &mut QubitInit) {
match &mut *init.kind {
QubitInitKind::Array(len) => vis.visit_expr(len),
QubitInitKind::Paren(init) => vis.visit_qubit_init(init),
QubitInitKind::Single => {}
QubitInitKind::Single | QubitInitKind::Err => {}
QubitInitKind::Tuple(inits) => inits.iter_mut().for_each(|i| vis.visit_qubit_init(i)),
}
}
Expand Down
7 changes: 4 additions & 3 deletions compiler/qsc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub fn walk_ty_def<'a>(vis: &mut impl Visitor<'a>, def: &'a TyDef) {
}
TyDefKind::Paren(def) => vis.visit_ty_def(def),
TyDefKind::Tuple(defs) => defs.iter().for_each(|d| vis.visit_ty_def(d)),
TyDefKind::Err => {}
}
}

Expand Down Expand Up @@ -161,7 +162,7 @@ pub fn walk_ty<'a>(vis: &mut impl Visitor<'a>, ty: &'a Ty) {
vis.visit_ty(rhs);
functors.iter().for_each(|f| vis.visit_functor_expr(f));
}
TyKind::Hole => {}
TyKind::Hole | TyKind::Err => {}
TyKind::Paren(ty) => vis.visit_ty(ty),
TyKind::Path(path) => vis.visit_path(path),
TyKind::Param(name) => vis.visit_ident(name),
Expand Down Expand Up @@ -283,7 +284,7 @@ pub fn walk_pat<'a>(vis: &mut impl Visitor<'a>, pat: &'a Pat) {
ty.iter().for_each(|t| vis.visit_ty(t));
}
PatKind::Discard(ty) => ty.iter().for_each(|t| vis.visit_ty(t)),
PatKind::Elided => {}
PatKind::Elided | PatKind::Err => {}
PatKind::Paren(pat) => vis.visit_pat(pat),
PatKind::Tuple(pats) => pats.iter().for_each(|p| vis.visit_pat(p)),
}
Expand All @@ -293,7 +294,7 @@ pub fn walk_qubit_init<'a>(vis: &mut impl Visitor<'a>, init: &'a QubitInit) {
match &*init.kind {
QubitInitKind::Array(len) => vis.visit_expr(len),
QubitInitKind::Paren(init) => vis.visit_qubit_init(init),
QubitInitKind::Single => {}
QubitInitKind::Single | QubitInitKind::Err => {}
QubitInitKind::Tuple(inits) => inits.iter().for_each(|i| vis.visit_qubit_init(i)),
}
}
Expand Down
Loading

0 comments on commit 1fe86d4

Please sign in to comment.