Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement base declarations and binary expression lowering #2221

Merged
merged 1 commit into from
Mar 12, 2025
Merged
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
1 change: 0 additions & 1 deletion compiler/qsc_qasm3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#![allow(dead_code)]

mod angle;
mod ast;
mod ast_builder;
mod compile;
pub use compile::qasm_to_program;
Expand Down
5 changes: 3 additions & 2 deletions compiler/qsc_qasm3/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::ast::{Program, StmtKind};
pub mod ast;
use crate::io::SourceResolver;
use ast::{Program, StmtKind};
use qsc_frontend::compile::SourceMap;
use qsc_frontend::error::WithSource;
use scan::ParserContext;
Expand Down Expand Up @@ -231,7 +232,7 @@ where
Ok((program, errors, included))
}

fn parse_includes<R>(program: &crate::ast::Program, resolver: &R) -> miette::Result<Vec<QasmSource>>
fn parse_includes<R>(program: &Program, resolver: &R) -> miette::Result<Vec<QasmSource>>
where
R: SourceResolver,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ pub enum Identifier {
}

impl Identifier {
#[must_use]
pub fn span(&self) -> Span {
match self {
Identifier::Ident(ident) => ident.span,
Expand Down Expand Up @@ -878,6 +879,7 @@ pub enum TypeDef {
}

impl TypeDef {
#[must_use]
pub fn span(&self) -> Span {
match self {
TypeDef::Scalar(ident) => ident.span,
Expand Down Expand Up @@ -1649,6 +1651,16 @@ impl Display for IndexElement {
}
}

impl IndexElement {
#[must_use]
pub fn span(&self) -> Span {
match self {
IndexElement::DiscreteSet(set) => set.span,
IndexElement::IndexSet(set) => set.span,
}
}
}

#[derive(Clone, Debug, Default)]
pub enum IndexSetItem {
RangeDefinition(RangeDefinition),
Expand Down Expand Up @@ -1680,7 +1692,7 @@ impl Display for IndexSetItem {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IOKeyword {
Input,
Output,
Expand All @@ -1695,6 +1707,15 @@ impl Display for IOKeyword {
}
}

impl From<IOKeyword> for crate::semantic::symbols::IOKind {
fn from(value: IOKeyword) -> Self {
match value {
IOKeyword::Input => crate::semantic::symbols::IOKind::Input,
IOKeyword::Output => crate::semantic::symbols::IOKind::Output,
}
}
}

#[derive(Clone, Debug)]
pub enum TimeUnit {
Dt,
Expand Down
25 changes: 11 additions & 14 deletions compiler/qsc_qasm3/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,26 @@ use num_traits::Num;
use qsc_data_structures::span::Span;

use crate::{
ast::{
self, list_from_iter, BinOp, BinaryOpExpr, Cast, DiscreteSet, Expr, ExprKind, FunctionCall,
GateOperand, HardwareQubit, Ident, IndexElement, IndexExpr, IndexSet, IndexSetItem,
IndexedIdent, List, Lit, LiteralKind, MeasureExpr, RangeDefinition, TimeUnit, TypeDef,
UnaryOp, ValueExpression, Version,
},
keyword::Keyword,
lex::{
cooked::{ComparisonOp, Literal, TimingLiteralKind},
ClosedBinOp, Delim, Radix, Token, TokenKind,
},
parser::{
completion::WordKinds,
prim::{shorten, token},
scan::ParserContext,
},
};

use crate::parser::Result;

use super::{
ast::{
list_from_iter, BinOp, BinaryOpExpr, Cast, DiscreteSet, Expr, ExprKind, FunctionCall,
GateOperand, HardwareQubit, Ident, IndexElement, IndexExpr, IndexSet, IndexSetItem,
IndexedIdent, List, Lit, LiteralKind, MeasureExpr, RangeDefinition, TimeUnit, TypeDef,
UnaryOp, UnaryOpExpr, ValueExpression, Version,
},
completion::WordKinds,
error::{Error, ErrorKind},
prim::{ident, many, opt, recovering_token, seq, FinalSep},
prim::{ident, many, opt, recovering_token, seq, shorten, token, FinalSep},
scan::ParserContext,
stmt::scalar_or_array_type,
};

Expand Down Expand Up @@ -94,7 +91,7 @@ fn expr_op(s: &mut ParserContext, context: OpContext) -> Result<Expr> {
let rhs = expr_op(s, OpContext::Precedence(op.precedence))?;
Expr {
span: s.span(lo),
kind: Box::new(ExprKind::UnaryOp(ast::UnaryOpExpr {
kind: Box::new(ExprKind::UnaryOp(UnaryOpExpr {
op: op.kind,
expr: rhs,
})),
Expand Down Expand Up @@ -444,7 +441,7 @@ pub(crate) fn paren_expr(s: &mut ParserContext, lo: u32) -> Result<Expr> {
})
}

fn funcall(s: &mut ParserContext, ident: ast::Ident) -> Result<ExprKind> {
fn funcall(s: &mut ParserContext, ident: Ident) -> Result<ExprKind> {
let lo = ident.span.lo;
let (args, _) = seq(s, expr)?;
token(s, TokenKind::Close(Delim::Paren))?;
Expand Down
2 changes: 1 addition & 1 deletion compiler/qsc_qasm3/src/parser/expr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use super::expr;
use crate::{
ast::StmtKind,
parser::ast::StmtKind,
parser::{scan::ParserContext, stmt, tests::check},
};
use expect_test::{expect, Expect};
Expand Down
3 changes: 2 additions & 1 deletion compiler/qsc_qasm3/src/parser/prgm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use super::{
stmt, Result,
};
use crate::{
ast::{Program, Stmt, StmtKind, Version},
lex::{Delim, TokenKind},
parser::{completion::WordKinds, expr},
};

use super::ast::{Program, Stmt, StmtKind, Version};

use super::ParserContext;

/// Grammar: `version? statementOrScope* EOF`.
Expand Down
8 changes: 3 additions & 5 deletions compiler/qsc_qasm3/src/parser/prim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ use super::{
scan::ParserContext,
Parser, Result,
};
use crate::{
ast::{Ident, IncompletePath, Path, PathKind},
lex::TokenKind,
parser::completion::WordKinds,
};
use crate::{lex::TokenKind, parser::completion::WordKinds};

use super::ast::{Ident, IncompletePath, Path, PathKind};

use qsc_data_structures::span::{Span, WithSpan};

Expand Down
2 changes: 1 addition & 1 deletion compiler/qsc_qasm3/src/parser/prim/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

use super::{ident, opt, seq};
use crate::{
ast::PathKind,
keyword::Keyword,
lex::TokenKind,
parser::ast::PathKind,
parser::{
completion::WordKinds,
error::{Error, ErrorKind},
Expand Down
26 changes: 13 additions & 13 deletions compiler/qsc_qasm3/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ use super::{
Result,
};
use crate::{
ast::{
list_from_iter, AccessControl, AliasDeclStmt, AngleType, Annotation, ArrayBaseTypeKind,
ArrayReferenceType, ArrayType, ArrayTypedParameter, AssignOpStmt, AssignStmt, BarrierStmt,
BitType, Block, BoxStmt, BreakStmt, CalibrationGrammarStmt, CalibrationStmt, Cast,
ClassicalDeclarationStmt, ComplexType, ConstantDeclStmt, ContinueStmt, DefCalStmt, DefStmt,
DelayStmt, EndStmt, EnumerableSet, Expr, ExprKind, ExprStmt, ExternDecl, ExternParameter,
FloatType, ForStmt, FunctionCall, GPhase, GateCall, GateModifierKind, GateOperand,
IODeclaration, IOKeyword, Ident, Identifier, IfStmt, IncludeStmt, IndexElement, IndexExpr,
IndexSetItem, IndexedIdent, IntType, List, LiteralKind, MeasureStmt, Pragma,
QuantumGateDefinition, QuantumGateModifier, QuantumTypedParameter, QubitDeclaration,
RangeDefinition, ResetStmt, ReturnStmt, ScalarType, ScalarTypeKind, ScalarTypedParameter,
Stmt, StmtKind, SwitchCase, SwitchStmt, TypeDef, TypedParameter, UIntType, WhileLoop,
},
keyword::Keyword,
lex::{cooked::Type, Delim, TokenKind},
};

use super::ast::{
list_from_iter, AccessControl, AliasDeclStmt, AngleType, Annotation, ArrayBaseTypeKind,
ArrayReferenceType, ArrayType, ArrayTypedParameter, AssignOpStmt, AssignStmt, BarrierStmt,
BitType, Block, BoxStmt, BreakStmt, CalibrationGrammarStmt, CalibrationStmt, Cast,
ClassicalDeclarationStmt, ComplexType, ConstantDeclStmt, ContinueStmt, DefCalStmt, DefStmt,
DelayStmt, EndStmt, EnumerableSet, Expr, ExprKind, ExprStmt, ExternDecl, ExternParameter,
FloatType, ForStmt, FunctionCall, GPhase, GateCall, GateModifierKind, GateOperand,
IODeclaration, IOKeyword, Ident, Identifier, IfStmt, IncludeStmt, IndexElement, IndexExpr,
IndexSetItem, IndexedIdent, IntType, List, LiteralKind, MeasureStmt, Pragma,
QuantumGateDefinition, QuantumGateModifier, QuantumTypedParameter, QubitDeclaration,
RangeDefinition, ResetStmt, ReturnStmt, ScalarType, ScalarTypeKind, ScalarTypedParameter, Stmt,
StmtKind, SwitchCase, SwitchStmt, TypeDef, TypedParameter, UIntType, WhileLoop,
};
use super::{prim::token, ParserContext};

/// Our implementation differs slightly from the grammar in
Expand Down
Loading