Skip to content
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "openqasm-parser"
version = "0.1.0"
version = "0.2.0"
edition = "2024"

[dependencies]
Expand Down
36 changes: 36 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{ pkgs, localLib }:
let
openqasm-parser = pkgs.rustPlatform.buildRustPackage {
pname = "openqasm-parser";
version = "0.2.0";
src = localLib.filters.cleanSourceWithFiles {
src = ./.;
files = [
"Cargo.toml"
"Cargo.lock"
"src/"
];
};

cargoLock.lockFile = ./Cargo.lock;
};
in
{
packages = {
inherit openqasm-parser;
};

devShells = {
openqasm-parser = pkgs.mkShell {
inputsFrom = [
openqasm-parser
];

buildInputs = with pkgs; [
rust-analyzer
rustfmt
clippy
];
};
};
}
34 changes: 10 additions & 24 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,22 @@
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
localLib = import ./localLib { inherit pkgs; };
project = import ./. { inherit pkgs localLib; };
in
{
packages = {
openqasm-parser = pkgs.rustPlatform.buildRustPackage {
pname = "openqasm-parser";
version = "0.1.0";
src = self;

cargoLock.lockFile = ./Cargo.lock;
packages =
project.packages //
{
default = project.packages.openqasm-parser;
};

default = self.packages.${system}.openqasm-parser;
};

devShells = {
openqasm-parser = pkgs.mkShell {
inputsFrom = [
self.packages.${system}.openqasm-parser
];

buildInputs = with pkgs; [
rust-analyzer
rustfmt
clippy
];
devShells =
project.devShells //
{
default = project.devShells.openqasm-parser;
};

default = self.devShells.${system}.openqasm-parser;
};

formatter = pkgs.nixpkgs-fmt;
}
);
Expand Down
4 changes: 4 additions & 0 deletions localLib/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{ pkgs }:
{
filters = import ./filters.nix { inherit pkgs; };
}
28 changes: 28 additions & 0 deletions localLib/filters.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{ pkgs }:
let
testFiles = files: relPath:
let
filesHead = builtins.head files;
isDirectory = (builtins.substring ((builtins.stringLength filesHead) - 1) 1 filesHead) == "/";
current =
if isDirectory
then builtins.substring 0 ((builtins.stringLength filesHead) - 1) filesHead
else filesHead;
in
files != [ ] && (
relPath == current ||
(isDirectory && builtins.match "${current}/.*" relPath != null) ||
testFiles (builtins.tail files) relPath
);
in
{
cleanSourceWithFiles = { src, files }:
pkgs.lib.cleanSourceWith {
inherit src;
filter = path: type:
let
relPath = builtins.replaceStrings [ (toString src + "/") ] [ "" ] (toString path);
in
testFiles files relPath;
};
}
4 changes: 4 additions & 0 deletions src/ast/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ pub fn make_reg_dec(loc: Location, name: String, ty: node::RegTy, size: u32) ->
name: name,
ty: ty,
size: size,
dec: None,
})
}

pub fn make_simple_reg(loc: Location, name: String) -> node::Reg {
node::Reg::SimpleReg {
loc: loc,
name: name,
dec: None,
}
}

Expand All @@ -23,6 +25,7 @@ pub fn make_subscript_reg(loc: Location, name: String, index: u32) -> node::Reg
loc: loc,
name: name,
index: index,
dec: None,
}
}

Expand All @@ -37,6 +40,7 @@ pub fn make_gate_stmt(
gate: gate,
pars: pars,
args: args,
dec: None,
}
}

Expand Down
27 changes: 26 additions & 1 deletion src/ast/node.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use std::rc::Rc;

use crate::bind::gate;
use crate::bind::reg;

use crate::utils::location::Location;

pub enum RegTy {
Expand All @@ -14,12 +19,22 @@ impl ToString for RegTy {
}
}

impl Clone for RegTy {
fn clone(&self) -> Self {
match self {
Self::QReg => Self::QReg,
Self::CReg => Self::CReg,
}
}
}

pub enum Dec {
RegDec {
loc: Location,
name: String,
ty: RegTy,
size: u32,
dec: Option<Rc<reg::RegDec>>,
},
}

Expand All @@ -31,6 +46,7 @@ impl Dec {
name: _,
ty: _,
size: _,
dec: _,
} => &loc,
}
}
Expand All @@ -40,22 +56,29 @@ pub enum Reg {
SimpleReg {
loc: Location,
name: String,
dec: Option<Rc<reg::RegDec>>,
},
SubscriptReg {
loc: Location,
name: String,
index: u32,
dec: Option<Rc<reg::RegDec>>,
},
}

impl Reg {
pub fn get_loc(&self) -> &Location {
match self {
Reg::SimpleReg { loc, name: _ } => &loc,
Reg::SimpleReg {
loc,
name: _,
dec: _,
} => &loc,
Reg::SubscriptReg {
loc,
name: _,
index: _,
dec: _,
} => &loc,
}
}
Expand All @@ -68,6 +91,7 @@ pub enum Stmt {
gate: String,
pars: Vec<Exp>,
args: Vec<Reg>,
dec: Option<Rc<gate::GateDec>>,
},
MeasureStmt {
loc: Location,
Expand All @@ -89,6 +113,7 @@ impl Stmt {
gate: _,
pars: _,
args: _,
dec: _,
} => &loc,
Stmt::MeasureStmt {
loc,
Expand Down
40 changes: 37 additions & 3 deletions src/ast/pretty_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,48 @@ impl Visitor for PrettyPrinter {
ref name,
ref ty,
size,
} => println!("{} {}[{}];", ty.to_string(), name, size),
ref dec,
} => {
if let Some(dec) = dec {
println!(
"{} {} /* {} */ [{}];",
ty.to_string(),
name,
dec.to_string(),
size
)
} else {
println!("{} {}[{}];", ty.to_string(), name, size)
}
}
}
}

fn visit_reg(&mut self, e: &node::Reg) {
match e {
&node::Reg::SimpleReg { loc: _, ref name } => print!("{}", name),
&node::Reg::SimpleReg {
loc: _,
ref name,
ref dec,
} => {
if let Some(dec) = dec {
print!("{} /* {} */", name, dec.to_string())
} else {
print!("{}", name)
}
}
&node::Reg::SubscriptReg {
loc: _,
ref name,
index,
} => print!("{}[{}]", name, index),
ref dec,
} => {
if let Some(dec) = dec {
print!("{} /* {} */ [{}]", name, dec.to_string(), index)
} else {
print!("{}[{}]", name, index)
}
}
}
}

Expand All @@ -36,8 +66,12 @@ impl Visitor for PrettyPrinter {
ref gate,
ref pars,
ref args,
ref dec,
} => {
print!("{} ", gate);
if let Some(dec) = dec {
print!("/* {} */ ", dec.to_string());
}
if !pars.is_empty() {
print!("(");
self.visit_exp(&pars[0]);
Expand Down
Loading