Skip to content

Commit

Permalink
rename to PathKind
Browse files Browse the repository at this point in the history
  • Loading branch information
minestarks committed Oct 4, 2024
1 parent 9cf51c8 commit 74a3930
Show file tree
Hide file tree
Showing 25 changed files with 159 additions and 161 deletions.
4 changes: 2 additions & 2 deletions compiler/qsc/src/interpret/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ mod given_interpreter {
use indoc::indoc;

use qsc_ast::ast::{
Expr, ExprKind, NodeId, Package, Path, PathResult, Stmt, StmtKind, TopLevelNode,
Expr, ExprKind, NodeId, Package, Path, PathKind, Stmt, StmtKind, TopLevelNode,
};
use qsc_data_structures::span::Span;
use qsc_frontend::compile::SourceMap;
Expand Down Expand Up @@ -1867,7 +1867,7 @@ mod given_interpreter {
let path_expr = Expr {
id: NodeId::default(),
span: Span::default(),
kind: Box::new(ExprKind::Path(PathResult::Ok(Box::new(path)))),
kind: Box::new(ExprKind::Path(PathKind::Ok(Box::new(path)))),
};
let expr = Expr {
id: NodeId::default(),
Expand Down
28 changes: 14 additions & 14 deletions compiler/qsc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ pub enum ItemKind {
#[default]
Err,
/// An `open` item for a namespace with an optional alias.
Open(PathResult, Option<Box<Ident>>),
Open(PathKind, Option<Box<Ident>>),
/// A `newtype` declaration.
Ty(Box<Ident>, Box<TyDef>),
/// A `struct` declaration.
Expand Down Expand Up @@ -672,7 +672,7 @@ pub enum TyKind {
/// A type wrapped in parentheses.
Paren(Box<Ty>),
/// A named type.
Path(PathResult),
Path(PathKind),
/// A type parameter.
Param(Box<Ident>),
/// A tuple type.
Expand Down Expand Up @@ -884,15 +884,15 @@ pub enum ExprKind {
/// Parentheses: `(a)`.
Paren(Box<Expr>),
/// A path: `a` or `a.b`.
Path(PathResult),
Path(PathKind),
/// A range: `start..step..end`, `start..end`, `start...`, `...end`, or `...`.
Range(Option<Box<Expr>>, Option<Box<Expr>>, Option<Box<Expr>>),
/// A repeat-until loop with an optional fixup: `repeat { ... } until a fixup { ... }`.
Repeat(Box<Block>, Box<Expr>, Option<Box<Block>>),
/// A return: `return a`.
Return(Box<Expr>),
/// A struct constructor.
Struct(PathResult, Option<Box<Expr>>, Box<[Box<FieldAssign>]>),
Struct(PathKind, Option<Box<Expr>>, Box<[Box<FieldAssign>]>),
/// A ternary operator.
TernOp(TernOp, Box<Expr>, Box<Expr>, Box<Expr>),
/// A tuple: `(a, b, c)`.
Expand Down Expand Up @@ -1147,7 +1147,7 @@ fn display_repeat(

fn display_struct(
mut indent: Indented<Formatter>,
name: &PathResult,
name: &PathKind,
copy: &Option<Box<Expr>>,
fields: &[Box<FieldAssign>],
) -> fmt::Result {
Expand Down Expand Up @@ -1404,17 +1404,17 @@ impl Display for QubitInitKind {

/// A path that may or may not have been successfully parsed.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum PathResult {
pub enum PathKind {
/// A successfully parsed path.
Ok(Box<Path>),

/// An invalid path.
Err(Option<Box<IncompletePath>>),
}

impl Default for PathResult {
impl Default for PathKind {
fn default() -> Self {
PathResult::Err(None)
PathKind::Err(None)
}
}

Expand All @@ -1429,19 +1429,19 @@ pub struct IncompletePath {
pub segments: Box<[Ident]>,
}

impl Display for PathResult {
impl Display for PathKind {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
PathResult::Ok(path) => write!(f, "{path}")?,
PathResult::Err(Some(incomplete_path)) => {
PathKind::Ok(path) => write!(f, "{path}")?,
PathKind::Err(Some(incomplete_path)) => {
let mut indent = set_indentation(indented(f), 0);
write!(indent, "Err IncompletePath {}:", incomplete_path.span)?;
indent = set_indentation(indent, 1);
for part in &incomplete_path.segments {
write!(indent, "\n{part}")?;
}
}
PathResult::Err(None) => write!(f, "Err",)?,
PathKind::Err(None) => write!(f, "Err",)?,
}
Ok(())
}
Expand Down Expand Up @@ -1905,7 +1905,7 @@ pub struct ImportOrExportItem {
/// The span of the import path including the glob and alias, if any.
pub span: Span,
/// The path to the item being exported.
pub path: PathResult,
pub path: PathKind,
/// An optional alias for the item being exported.
pub alias: Option<Ident>,
/// Whether this is a glob import/export.
Expand Down Expand Up @@ -1942,7 +1942,7 @@ impl ImportOrExportItem {
match &self.alias {
Some(_) => self.alias.as_ref(),
None => {
if let PathResult::Ok(path) = &self.path {
if let PathKind::Ok(path) = &self.path {
Some(&path.name)
} else {
None
Expand Down
24 changes: 12 additions & 12 deletions compiler/qsc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::ast::{
Attr, Block, CallableBody, CallableDecl, Expr, ExprKind, FieldAssign, FieldDef, FunctorExpr,
FunctorExprKind, Ident, Item, ItemKind, Namespace, Package, Pat, PatKind, Path, PathResult,
FunctorExprKind, Ident, Item, ItemKind, Namespace, Package, Pat, PatKind, Path, PathKind,
QubitInit, QubitInitKind, SpecBody, SpecDecl, Stmt, StmtKind, StringComponent, StructDecl,
TopLevelNode, Ty, TyDef, TyDefKind, TyKind,
};
Expand Down Expand Up @@ -82,8 +82,8 @@ pub trait MutVisitor: Sized {
walk_path(self, path);
}

fn visit_path_result(&mut self, path: &mut PathResult) {
walk_path_result(self, path);
fn visit_path_kind(&mut self, path: &mut PathKind) {
walk_path_kind(self, path);
}

fn visit_ident(&mut self, ident: &mut Ident) {
Expand Down Expand Up @@ -120,7 +120,7 @@ pub fn walk_item(vis: &mut impl MutVisitor, item: &mut Item) {
ItemKind::Callable(decl) => vis.visit_callable_decl(decl),
ItemKind::Err => {}
ItemKind::Open(ns, alias) => {
vis.visit_path_result(ns);
vis.visit_path_kind(ns);
alias.iter_mut().for_each(|a| vis.visit_ident(a));
}
ItemKind::Ty(ident, def) => {
Expand All @@ -132,7 +132,7 @@ pub fn walk_item(vis: &mut impl MutVisitor, item: &mut Item) {
vis.visit_span(&mut export.span);
for item in &mut *export.items {
vis.visit_span(&mut item.span);
vis.visit_path_result(&mut item.path);
vis.visit_path_kind(&mut item.path);
if let Some(ref mut alias) = item.alias {
vis.visit_ident(alias);
}
Expand Down Expand Up @@ -227,7 +227,7 @@ pub fn walk_ty(vis: &mut impl MutVisitor, ty: &mut Ty) {
TyKind::Hole | TyKind::Err => {}
TyKind::Paren(ty) => vis.visit_ty(ty),
TyKind::Param(name) => vis.visit_ident(name),
TyKind::Path(path) => vis.visit_path_result(path),
TyKind::Path(path) => vis.visit_path_kind(path),
TyKind::Tuple(tys) => tys.iter_mut().for_each(|t| vis.visit_ty(t)),
}
}
Expand Down Expand Up @@ -319,7 +319,7 @@ pub fn walk_expr(vis: &mut impl MutVisitor, expr: &mut Expr) {
ExprKind::Paren(expr) | ExprKind::Return(expr) | ExprKind::UnOp(_, expr) => {
vis.visit_expr(expr);
}
ExprKind::Path(path) => vis.visit_path_result(path),
ExprKind::Path(path) => vis.visit_path_kind(path),
ExprKind::Range(start, step, end) => {
start.iter_mut().for_each(|s| vis.visit_expr(s));
step.iter_mut().for_each(|s| vis.visit_expr(s));
Expand All @@ -331,7 +331,7 @@ pub fn walk_expr(vis: &mut impl MutVisitor, expr: &mut Expr) {
fixup.iter_mut().for_each(|f| vis.visit_block(f));
}
ExprKind::Struct(name, copy, fields) => {
vis.visit_path_result(name);
vis.visit_path_kind(name);
copy.iter_mut().for_each(|c| vis.visit_expr(c));
fields.iter_mut().for_each(|f| vis.visit_field_assign(f));
}
Expand Down Expand Up @@ -389,17 +389,17 @@ pub fn walk_path(vis: &mut impl MutVisitor, path: &mut Path) {
vis.visit_ident(&mut path.name);
}

pub fn walk_path_result(vis: &mut impl MutVisitor, path: &mut PathResult) {
pub fn walk_path_kind(vis: &mut impl MutVisitor, path: &mut PathKind) {
match path {
PathResult::Ok(path) => vis.visit_path(path),
PathResult::Err(Some(incomplete_path)) => {
PathKind::Ok(path) => vis.visit_path(path),
PathKind::Err(Some(incomplete_path)) => {
vis.visit_span(&mut incomplete_path.span);

for ref mut ident in &mut incomplete_path.segments {
vis.visit_ident(ident);
}
}
PathResult::Err(None) => {}
PathKind::Err(None) => {}
}
}

Expand Down
24 changes: 12 additions & 12 deletions compiler/qsc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::ast::{
Attr, Block, CallableBody, CallableDecl, Expr, ExprKind, FieldAssign, FieldDef, FunctorExpr,
FunctorExprKind, Ident, Item, ItemKind, Namespace, Package, Pat, PatKind, Path, PathResult,
FunctorExprKind, Ident, Item, ItemKind, Namespace, Package, Pat, PatKind, Path, PathKind,
QubitInit, QubitInitKind, SpecBody, SpecDecl, Stmt, StmtKind, StringComponent, StructDecl,
TopLevelNode, Ty, TyDef, TyDefKind, TyKind,
};
Expand Down Expand Up @@ -81,8 +81,8 @@ pub trait Visitor<'a>: Sized {
walk_path(self, path);
}

fn visit_path_result(&mut self, path: &'a PathResult) {
walk_path_result(self, path);
fn visit_path_kind(&mut self, path: &'a PathKind) {
walk_path_kind(self, path);
}

fn visit_ident(&mut self, _: &'a Ident) {}
Expand Down Expand Up @@ -111,7 +111,7 @@ pub fn walk_item<'a>(vis: &mut impl Visitor<'a>, item: &'a Item) {
ItemKind::Err => {}
ItemKind::Callable(decl) => vis.visit_callable_decl(decl),
ItemKind::Open(ns, alias) => {
vis.visit_path_result(ns);
vis.visit_path_kind(ns);
alias.iter().for_each(|a| vis.visit_ident(a));
}
ItemKind::Ty(ident, def) => {
Expand All @@ -121,7 +121,7 @@ pub fn walk_item<'a>(vis: &mut impl Visitor<'a>, item: &'a Item) {
ItemKind::Struct(decl) => vis.visit_struct_decl(decl),
ItemKind::ImportOrExport(decl) => {
for item in &decl.items {
vis.visit_path_result(&item.path);
vis.visit_path_kind(&item.path);
if let Some(ref alias) = item.alias {
vis.visit_ident(alias);
}
Expand Down Expand Up @@ -200,7 +200,7 @@ pub fn walk_ty<'a>(vis: &mut impl Visitor<'a>, ty: &'a Ty) {
}
TyKind::Hole | TyKind::Err => {}
TyKind::Paren(ty) => vis.visit_ty(ty),
TyKind::Path(path) => vis.visit_path_result(path),
TyKind::Path(path) => vis.visit_path_kind(path),
TyKind::Param(name) => vis.visit_ident(name),
TyKind::Tuple(tys) => tys.iter().for_each(|t| vis.visit_ty(t)),
}
Expand Down Expand Up @@ -288,7 +288,7 @@ pub fn walk_expr<'a>(vis: &mut impl Visitor<'a>, expr: &'a Expr) {
ExprKind::Paren(expr) | ExprKind::Return(expr) | ExprKind::UnOp(_, expr) => {
vis.visit_expr(expr);
}
ExprKind::Path(path) => vis.visit_path_result(path),
ExprKind::Path(path) => vis.visit_path_kind(path),
ExprKind::Range(start, step, end) => {
start.iter().for_each(|s| vis.visit_expr(s));
step.iter().for_each(|s| vis.visit_expr(s));
Expand All @@ -300,7 +300,7 @@ pub fn walk_expr<'a>(vis: &mut impl Visitor<'a>, expr: &'a Expr) {
fixup.iter().for_each(|f| vis.visit_block(f));
}
ExprKind::Struct(name, copy, fields) => {
vis.visit_path_result(name);
vis.visit_path_kind(name);
copy.iter().for_each(|c| vis.visit_expr(c));
fields.iter().for_each(|f| vis.visit_field_assign(f));
}
Expand Down Expand Up @@ -352,13 +352,13 @@ pub fn walk_path<'a>(vis: &mut impl Visitor<'a>, path: &'a Path) {
vis.visit_ident(&path.name);
}

pub fn walk_path_result<'a>(vis: &mut impl Visitor<'a>, path: &'a PathResult) {
pub fn walk_path_kind<'a>(vis: &mut impl Visitor<'a>, path: &'a PathKind) {
match path {
PathResult::Ok(path) => vis.visit_path(path),
PathResult::Err(Some(incomplete_path)) => {
PathKind::Ok(path) => vis.visit_path(path),
PathKind::Err(Some(incomplete_path)) => {
vis.visit_idents(&incomplete_path.segments);
}
PathResult::Err(None) => {}
PathKind::Err(None) => {}
}
}

Expand Down
20 changes: 10 additions & 10 deletions compiler/qsc_codegen/src/qsharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use std::vec;
use qsc_ast::ast::{
self, Attr, BinOp, Block, CallableBody, CallableDecl, CallableKind, Expr, ExprKind, Functor,
FunctorExpr, FunctorExprKind, Ident, Idents, ImportOrExportItem, Item, ItemKind, Lit,
Mutability, Pat, PatKind, Path, PathResult, Pauli, QubitInit, QubitInitKind, QubitSource,
SetOp, SpecBody, SpecDecl, SpecGen, Stmt, StmtKind, StringComponent, TernOp, TopLevelNode, Ty,
TyDef, TyDefKind, TyKind, UnOp,
Mutability, Pat, PatKind, Path, PathKind, Pauli, QubitInit, QubitInitKind, QubitSource, SetOp,
SpecBody, SpecDecl, SpecGen, Stmt, StmtKind, StringComponent, TernOp, TopLevelNode, Ty, TyDef,
TyDefKind, TyKind, UnOp,
};
use qsc_ast::ast::{Namespace, Package};
use qsc_ast::visit::Visitor;
Expand Down Expand Up @@ -135,7 +135,7 @@ impl<W: Write> Visitor<'_> for QSharpGen<W> {
ItemKind::Callable(decl) => self.visit_callable_decl(decl),
ItemKind::Open(ns, alias) => {
self.write("open ");
self.visit_path_result(ns);
self.visit_path_kind(ns);
if let Some(alias) = alias {
self.write(" as ");
self.visit_ident(alias);
Expand Down Expand Up @@ -168,7 +168,7 @@ impl<W: Write> Visitor<'_> for QSharpGen<W> {
) in decl.items.iter().enumerate()
{
let is_last = ix == decl.items.len() - 1;
self.visit_path_result(path);
self.visit_path_kind(path);

if *is_glob {
self.write(".*");
Expand Down Expand Up @@ -348,7 +348,7 @@ impl<W: Write> Visitor<'_> for QSharpGen<W> {
self.visit_ty(ty);
self.write(")");
}
TyKind::Path(path) => self.visit_path_result(path),
TyKind::Path(path) => self.visit_path_kind(path),
TyKind::Param(name) => self.visit_ident(name),
TyKind::Tuple(tys) => {
if tys.is_empty() {
Expand Down Expand Up @@ -550,7 +550,7 @@ impl<W: Write> Visitor<'_> for QSharpGen<W> {
self.write("return ");
self.visit_expr(expr);
}
ExprKind::Struct(PathResult::Ok(path), copy, assigns) => {
ExprKind::Struct(PathKind::Ok(path), copy, assigns) => {
self.write("new ");
self.visit_path(path);
self.writeln(" {");
Expand Down Expand Up @@ -581,7 +581,7 @@ impl<W: Write> Visitor<'_> for QSharpGen<W> {
self.visit_expr(expr);
}
}
ExprKind::Path(PathResult::Ok(path)) => self.visit_path(path),
ExprKind::Path(PathKind::Ok(path)) => self.visit_path(path),
ExprKind::Range(start, step, end) => {
// A range: `start..step..end`, `start..end`, `start...`, `...end`, or `...`.
match (start, step, end) {
Expand Down Expand Up @@ -715,8 +715,8 @@ impl<W: Write> Visitor<'_> for QSharpGen<W> {
self.write("_");
}
ExprKind::Err
| ExprKind::Path(PathResult::Err(_))
| ExprKind::Struct(PathResult::Err(_), ..) => {
| ExprKind::Path(PathKind::Err(_))
| ExprKind::Struct(PathKind::Err(_), ..) => {
unreachable!();
}
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/qsc_doc_gen/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ impl<'a> Display for AstTy<'a> {
}
ast::TyKind::Hole => write!(f, "_"),
ast::TyKind::Paren(ty) => write!(f, "{}", AstTy { ty }),
ast::TyKind::Path(path) => write!(f, "{}", AstPathResult { path }),
ast::TyKind::Path(path) => write!(f, "{}", AstPathKind { path }),
ast::TyKind::Param(id) => write!(f, "{}", id.name),
ast::TyKind::Tuple(tys) => fmt_tuple(f, tys, |ty| AstTy { ty }),
ast::TyKind::Err => write!(f, "?"),
Expand All @@ -540,13 +540,13 @@ impl<'a> Display for FunctorExpr<'a> {
}
}

struct AstPathResult<'a> {
path: &'a ast::PathResult,
struct AstPathKind<'a> {
path: &'a ast::PathKind,
}

impl<'a> Display for AstPathResult<'a> {
impl<'a> Display for AstPathKind<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
if let ast::PathResult::Ok(path) = self.path {
if let ast::PathKind::Ok(path) = self.path {
write!(f, "{}", path.full_name())
} else {
write!(f, "?")
Expand Down
Loading

0 comments on commit 74a3930

Please sign in to comment.