Skip to content

Commit

Permalink
EOD save. Grammar, lexer and AST being debugged
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcnn committed Oct 19, 2023
1 parent a8e321c commit 52794c7
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 241 deletions.
18 changes: 8 additions & 10 deletions hir-parser/src/ast/block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

const INDENT : String = " ";
const INDENT : String = " ".to_string();

/// Represents the label at the start of a basic block.
///
Expand All @@ -24,7 +24,6 @@ impl fmt::Display for Label {


/// Represents an argument for a basic block
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockArgument {
pub value: Value,
pub ty: Type,
Expand All @@ -39,19 +38,18 @@ impl BlockArgument {
}
impl fmt::Display for BlockArgument {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} : {} ", self.value, self.ty);
write!(f, "{} : {} ", self.value, self.ty)
}
}


/// Represents the label and the arguments of a basic block
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockHeader {
pub label: Label,
pub args: Vec<BlockArg>,
pub args: Vec<BlockArgument>,
}
impl BlockHeader {
pub fn new(label:Label, args:Vec<BlockArg>) -> Self {
pub fn new(label:Label, args:Vec<BlockArgument>) -> Self {
Self {
label,
args,
Expand All @@ -61,17 +59,17 @@ impl BlockHeader {
impl fmt::Display for BlockHeader {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} ", self.label);
if args.len() = 0 {
if self.args.len() == 0 {
f.write_str(":\n")
}
else {
f.write_str("(");
for (i, arg) in self.args.iter().enumerate() {
if i != 0 {
write!(f, ", {}", arg)
write!(f, ", {}", arg);
}
else {
write!(f, "{}", arg)
write!(f, "{}", arg);
}
}
f.write_str(") :\n")
Expand All @@ -80,7 +78,7 @@ impl fmt::Display for BlockHeader {
}

/// Represents a basic block of instructions
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Spanned)]
pub struct Block {
#[span]
pub span: SourceSpan,
Expand Down
43 changes: 0 additions & 43 deletions hir-parser/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,6 @@ use crate::symbols::Symbol;

use super::*;

/// Represents any type of identifier in Miden IR.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Spanned)]
pub struct Identifier(Span<Symbol>);
impl Identifier {
pub fn new(span: SourceSpan, name: Symbol) -> Self {
Self(Span::new(span, name))
}

/// Returns the underlying symbol of the identifier.
pub fn name(&self) -> Symbol {
self.0.item
}

#[inline]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl PartialEq<&str> for Identifier {
#[inline]
fn eq(&self, other: &&str) -> bool {
self.0.item == *other
}
}
impl PartialEq<&Identifier> for Identifier {
#[inline]
fn eq(&self, other: &&Self) -> bool {
self == *other
}
}
impl fmt::Debug for Identifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Identifier")
.field(&format!("{}", &self.0.item))
.finish()
}
}
impl fmt::Display for Identifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", &self.0)
}
}

///// Represents an identifier qualified with both its parent module and namespace.
/////
///// This represents a globally-unique identity for a declaration
Expand Down
61 changes: 30 additions & 31 deletions hir-parser/src/ast/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,37 @@ use super::*;
/// itself. The other identifiers denote the module that the function can be found in. If
/// the function identifier only consists of a single identifier, then the function must
/// be found in the current module.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Spanned)]
#[derive(Spanned)]
pub struct FunctionIdentifier {
#[span]
span: Span<Symbol>,
names: Vec<Identifier>,
}
impl FunctionIdentifier {
pub fn id(&self) -> Identifier {
self.names.last().unwrap()
pub fn new(span:Span<Symbol>, names:Vec<Identifier>) -> Self {
Self {
span,
names,
}
}

pub fn module(&self) -> &[Identifier] {
&self.names.split_last().unwrap().1
}
}
impl AsRef<Identifier> for FunctionIdentifier {
fn as_ref(&self) -> &Identifier {
&self.names.last()
pub fn id(&self) -> &Identifier {
self.names.last().unwrap()
}
}
impl fmt::Display for FunctionIdentifier {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self.as_ref(), f)
for (i, id) in self.names.iter().enumerate() {
if i > 0 {
f.write_str("::");
}
write!(f, "{}", id);
}
Ok(())
}
}

/// The possible visibilities of a function
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Visibility {
/// (Module) private visibility
Private,
Expand All @@ -42,7 +46,6 @@ pub enum Visibility {
}

/// The possible calling convetions of a function
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum CallConvention {
/// Default call convention
Default,
Expand All @@ -53,7 +56,6 @@ pub enum CallConvention {
}

/// The possible purposes of a function parameter
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ParameterPurpose {
/// Standard parameter
Standard,
Expand All @@ -62,7 +64,6 @@ pub enum ParameterPurpose {
}

/// The possible extensions of a function parameter when filling up a word
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ParameterExtension {
/// No extension
None,
Expand All @@ -74,7 +75,6 @@ pub enum ParameterExtension {

/// A single parameter to a function.
/// Parameter names are defined in the entry block for the function.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct FunctionParameter {
/// The purpose of the parameter (default or struct return)
pub purpose: ParameterPurpose,
Expand All @@ -97,18 +97,17 @@ impl fmt::Display for FunctionParameter {
match self.purpose {
ParameterPurpose::Standard => Ok(()),
ParameterPurpose::Sret => f.write_str("sret ")
}
};
match self.extension {
ParameterExtension::None => Ok(()),
ParameterExtension::Zero => f.write_str("zext "),
ParameterExtension::Signed => f.write_str("sext "),
}
};
write!(f, "{}", self.ty)
}
}

/// A single return value from a function.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct FunctionReturn {
/// The bit extension for the parameter
pub extension: ParameterExtension,
Expand All @@ -129,13 +128,13 @@ impl fmt::Display for FunctionReturn {
ParameterExtension::None => Ok(()),
ParameterExtension::Zero => f.write_str("zext "),
ParameterExtension::Signed => f.write_str("sext "),
}
};
write!(f, "{}", self.ty)
}
}

/// Represents the type signature of a function
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Spanned)]
pub struct FunctionSignature {
#[span]
pub span: SourceSpan,
Expand All @@ -160,14 +159,14 @@ impl FunctionSignature {
impl fmt::Display for FunctionSignature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.visibility {
Visibility::Private => Ok(());
Visibility::Public => f.write_str("pub ");
}
Visibility::Private => Ok(()),
Visibility::Public => f.write_str("pub "),
};
match self.call_convention {
CallConvention::Default => Ok(());
CallConvention::Kernel => f.write_str("kernel ");
CallConvention::Fast => f.write_str("fast ");
}
CallConvention::Default => Ok(()),
CallConvention::Kernel => f.write_str("kernel "),
CallConvention::Fast => f.write_str("fast "),
};
write!(f, "{}(", self.name);
for (i, param) in self.params.iter().enumerate() {
if i != 0 {
Expand All @@ -186,12 +185,12 @@ impl fmt::Display for FunctionSignature {
write!(f, "{}", ret);
}
}
OK(())
Ok(())
}
}

/// Represents the declaration of a function
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Spanned)]
pub struct FunctionDeclaration {
#[span]
pub span: SourceSpan,
Expand All @@ -212,7 +211,7 @@ impl fmt::Display for FunctionDeclaration {
write!(f, "{}", self.signature);
f.write_str("{{\n");
for block in self.blocks.iter() {
write!(f, block);
write!(f, "{}", block);
}
f.write_str("}}\n")
}
Expand Down
30 changes: 15 additions & 15 deletions hir-parser/src/ast/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ use super::*;
pub type GlobalVarId = Identifier;

/// A constant value in the form of a hexadecimal string
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Spanned)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Constant {
pub data: Vec<u8>;
pub data: Vec<u8>,
}
impl fmt::Display for ConstantData {
impl fmt::Display for Constant {
/// Print the constant data in hexadecimal format, e.g. 0x000102030405060708090a0b0c0d0e0f.
///
/// The printed form of the constant renders the bytes in big-endian order, for readability.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if !self.is_empty() {
if !self.data.is_empty() {
write!(f, "0x")?;
for b in self.0.iter().rev() {
for b in self.data.iter().rev() {
write!(f, "{:02x}", b)?;
}
}
Expand All @@ -29,7 +29,7 @@ impl fmt::Display for ConstantData {


/// This represents the declaration of a Miden IR global variable
#[derive(Debug, Spanned)]
#[derive(Spanned)]
pub struct GlobalVarDeclaration {
#[span]
pub span: SourceSpan,
Expand All @@ -55,17 +55,17 @@ impl GlobalVarDeclaration {
self.init = Some(init)
}
}
impl Eq for GlobalVarDeclaration {}
impl PartialEq for GlobalVarDeclaration {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.ty == other.ty
&& self.linkage == other.linkage
&& self.init == other.init
impl fmt::Display for GlobalVarDeclaration {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {] {}", self.name, self.ty, self.linkage);

Check failure on line 60 in hir-parser/src/ast/globals.rs

View workflow job for this annotation

GitHub Actions / compiler

invalid format string: expected `'}'`, found `']'`
if let Some(i) = self.init {
write!(f, "= {}", i);
}
Ok(())
}
}
/// Represents the intended linkage for a global variable.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Spanned)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Linkage {
/// Global linkage
Internal,
Expand All @@ -74,7 +74,7 @@ pub enum Linkage {
/// External linkage
External,
}
impl fmt::Display for ResolvableIdentifier {
impl fmt::Display for Linkage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Internal => write!(f, "internal"),
Expand Down
Loading

0 comments on commit 52794c7

Please sign in to comment.