Skip to content

Commit eb576c3

Browse files
committed
Implement base declarations and binary expression lowering
1 parent d7e6e38 commit eb576c3

36 files changed

+6521
-341
lines changed

compiler/qsc_qasm3/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#![allow(dead_code)]
66

77
mod angle;
8-
mod ast;
98
mod ast_builder;
109
mod compile;
1110
pub use compile::qasm_to_program;

compiler/qsc_qasm3/src/parser.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use crate::ast::{Program, StmtKind};
4+
pub mod ast;
55
use crate::io::SourceResolver;
6+
use ast::{Program, StmtKind};
67
use qsc_frontend::compile::SourceMap;
78
use qsc_frontend::error::WithSource;
89
use scan::ParserContext;
@@ -231,7 +232,7 @@ where
231232
Ok((program, errors, included))
232233
}
233234

234-
fn parse_includes<R>(program: &crate::ast::Program, resolver: &R) -> miette::Result<Vec<QasmSource>>
235+
fn parse_includes<R>(program: &Program, resolver: &R) -> miette::Result<Vec<QasmSource>>
235236
where
236237
R: SourceResolver,
237238
{

compiler/qsc_qasm3/src/ast.rs compiler/qsc_qasm3/src/parser/ast.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ pub enum Identifier {
481481
}
482482

483483
impl Identifier {
484+
#[must_use]
484485
pub fn span(&self) -> Span {
485486
match self {
486487
Identifier::Ident(ident) => ident.span,
@@ -878,6 +879,7 @@ pub enum TypeDef {
878879
}
879880

880881
impl TypeDef {
882+
#[must_use]
881883
pub fn span(&self) -> Span {
882884
match self {
883885
TypeDef::Scalar(ident) => ident.span,
@@ -1649,6 +1651,16 @@ impl Display for IndexElement {
16491651
}
16501652
}
16511653

1654+
impl IndexElement {
1655+
#[must_use]
1656+
pub fn span(&self) -> Span {
1657+
match self {
1658+
IndexElement::DiscreteSet(set) => set.span,
1659+
IndexElement::IndexSet(set) => set.span,
1660+
}
1661+
}
1662+
}
1663+
16521664
#[derive(Clone, Debug, Default)]
16531665
pub enum IndexSetItem {
16541666
RangeDefinition(RangeDefinition),
@@ -1680,7 +1692,7 @@ impl Display for IndexSetItem {
16801692
}
16811693
}
16821694

1683-
#[derive(Clone, Debug)]
1695+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
16841696
pub enum IOKeyword {
16851697
Input,
16861698
Output,
@@ -1695,6 +1707,15 @@ impl Display for IOKeyword {
16951707
}
16961708
}
16971709

1710+
impl From<IOKeyword> for crate::semantic::symbols::IOKind {
1711+
fn from(value: IOKeyword) -> Self {
1712+
match value {
1713+
IOKeyword::Input => crate::semantic::symbols::IOKind::Input,
1714+
IOKeyword::Output => crate::semantic::symbols::IOKind::Output,
1715+
}
1716+
}
1717+
}
1718+
16981719
#[derive(Clone, Debug)]
16991720
pub enum TimeUnit {
17001721
Dt,

compiler/qsc_qasm3/src/parser/expr.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,26 @@ use num_traits::Num;
1212
use qsc_data_structures::span::Span;
1313

1414
use crate::{
15-
ast::{
16-
self, list_from_iter, BinOp, BinaryOpExpr, Cast, DiscreteSet, Expr, ExprKind, FunctionCall,
17-
GateOperand, HardwareQubit, Ident, IndexElement, IndexExpr, IndexSet, IndexSetItem,
18-
IndexedIdent, List, Lit, LiteralKind, MeasureExpr, RangeDefinition, TimeUnit, TypeDef,
19-
UnaryOp, ValueExpression, Version,
20-
},
2115
keyword::Keyword,
2216
lex::{
2317
cooked::{ComparisonOp, Literal, TimingLiteralKind},
2418
ClosedBinOp, Delim, Radix, Token, TokenKind,
2519
},
26-
parser::{
27-
completion::WordKinds,
28-
prim::{shorten, token},
29-
scan::ParserContext,
30-
},
3120
};
3221

3322
use crate::parser::Result;
3423

3524
use super::{
25+
ast::{
26+
list_from_iter, BinOp, BinaryOpExpr, Cast, DiscreteSet, Expr, ExprKind, FunctionCall,
27+
GateOperand, HardwareQubit, Ident, IndexElement, IndexExpr, IndexSet, IndexSetItem,
28+
IndexedIdent, List, Lit, LiteralKind, MeasureExpr, RangeDefinition, TimeUnit, TypeDef,
29+
UnaryOp, UnaryOpExpr, ValueExpression, Version,
30+
},
31+
completion::WordKinds,
3632
error::{Error, ErrorKind},
37-
prim::{ident, many, opt, recovering_token, seq, FinalSep},
33+
prim::{ident, many, opt, recovering_token, seq, shorten, token, FinalSep},
34+
scan::ParserContext,
3835
stmt::scalar_or_array_type,
3936
};
4037

@@ -94,7 +91,7 @@ fn expr_op(s: &mut ParserContext, context: OpContext) -> Result<Expr> {
9491
let rhs = expr_op(s, OpContext::Precedence(op.precedence))?;
9592
Expr {
9693
span: s.span(lo),
97-
kind: Box::new(ExprKind::UnaryOp(ast::UnaryOpExpr {
94+
kind: Box::new(ExprKind::UnaryOp(UnaryOpExpr {
9895
op: op.kind,
9996
expr: rhs,
10097
})),
@@ -444,7 +441,7 @@ pub(crate) fn paren_expr(s: &mut ParserContext, lo: u32) -> Result<Expr> {
444441
})
445442
}
446443

447-
fn funcall(s: &mut ParserContext, ident: ast::Ident) -> Result<ExprKind> {
444+
fn funcall(s: &mut ParserContext, ident: Ident) -> Result<ExprKind> {
448445
let lo = ident.span.lo;
449446
let (args, _) = seq(s, expr)?;
450447
token(s, TokenKind::Close(Delim::Paren))?;

compiler/qsc_qasm3/src/parser/expr/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use super::expr;
55
use crate::{
6-
ast::StmtKind,
6+
parser::ast::StmtKind,
77
parser::{scan::ParserContext, stmt, tests::check},
88
};
99
use expect_test::{expect, Expect};

compiler/qsc_qasm3/src/parser/prgm.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ use super::{
66
stmt, Result,
77
};
88
use crate::{
9-
ast::{Program, Stmt, StmtKind, Version},
109
lex::{Delim, TokenKind},
1110
parser::{completion::WordKinds, expr},
1211
};
1312

13+
use super::ast::{Program, Stmt, StmtKind, Version};
14+
1415
use super::ParserContext;
1516

1617
pub(super) fn parse(s: &mut ParserContext) -> Result<Program> {

compiler/qsc_qasm3/src/parser/prim.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ use super::{
99
scan::ParserContext,
1010
Parser, Result,
1111
};
12-
use crate::{
13-
ast::{Ident, IncompletePath, Path, PathKind},
14-
lex::TokenKind,
15-
parser::completion::WordKinds,
16-
};
12+
use crate::{lex::TokenKind, parser::completion::WordKinds};
13+
14+
use super::ast::{Ident, IncompletePath, Path, PathKind};
1715

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

compiler/qsc_qasm3/src/parser/prim/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
use super::{ident, opt, seq};
55
use crate::{
6-
ast::PathKind,
76
keyword::Keyword,
87
lex::TokenKind,
8+
parser::ast::PathKind,
99
parser::{
1010
completion::WordKinds,
1111
error::{Error, ErrorKind},

compiler/qsc_qasm3/src/parser/stmt.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ use super::{
1515
Result,
1616
};
1717
use crate::{
18-
ast::{
19-
list_from_iter, AccessControl, AliasDeclStmt, AngleType, Annotation, ArrayBaseTypeKind,
20-
ArrayReferenceType, ArrayType, ArrayTypedParameter, AssignOpStmt, AssignStmt, BarrierStmt,
21-
BitType, Block, BoxStmt, BreakStmt, CalibrationGrammarStmt, CalibrationStmt, Cast,
22-
ClassicalDeclarationStmt, ComplexType, ConstantDeclStmt, ContinueStmt, DefCalStmt, DefStmt,
23-
DelayStmt, EndStmt, EnumerableSet, Expr, ExprKind, ExprStmt, ExternDecl, ExternParameter,
24-
FloatType, ForStmt, FunctionCall, GPhase, GateCall, GateModifierKind, GateOperand,
25-
IODeclaration, IOKeyword, Ident, Identifier, IfStmt, IncludeStmt, IndexElement, IndexExpr,
26-
IndexSetItem, IntType, List, LiteralKind, MeasureStmt, Pragma, QuantumGateDefinition,
27-
QuantumGateModifier, QuantumTypedParameter, QubitDeclaration, RangeDefinition, ResetStmt,
28-
ReturnStmt, ScalarType, ScalarTypeKind, ScalarTypedParameter, Stmt, StmtKind, SwitchCase,
29-
SwitchStmt, TypeDef, TypedParameter, UIntType, WhileLoop,
30-
},
3118
keyword::Keyword,
3219
lex::{cooked::Type, Delim, TokenKind},
3320
};
3421

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

3737
#[allow(clippy::too_many_lines)]

0 commit comments

Comments
 (0)