Skip to content

Commit

Permalink
refactor: remove AST serialization (#1442)
Browse files Browse the repository at this point in the history
* feat: implement serialization for Program
* refactor: remove serialization of AST structs
  • Loading branch information
bobbinth authored Aug 11, 2024
1 parent 828557c commit 9dd4cd6
Show file tree
Hide file tree
Showing 28 changed files with 174 additions and 2,117 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

## Unreleased

#### Changes

- [BREAKING] Removed serialization of AST structs (#1442).

#### Enhancements

- Exposed some pretty printing internals for `MastNode`
- Made `KernelLibrary` impl `Clone` and `AsRef<Library>`
- Exposed some pretty printing internals for `MastNode` (#1441).
- Made `KernelLibrary` impl `Clone` and `AsRef<Library>` (#1441).
- Added serialization to the `Program` struct (#1442).


## 0.10.0 (2024-08-06)

Expand Down
54 changes: 10 additions & 44 deletions assembly/src/ast/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use alloc::vec::Vec;
use core::fmt;

use super::Op;
use crate::{
ast::AstSerdeOptions, ByteReader, ByteWriter, Deserializable, DeserializationError,
Serializable, SourceSpan, Spanned,
};
use crate::{SourceSpan, Spanned};

/// Represents a basic block in Miden Assembly syntax
// BASIC BLOCK
// ================================================================================================

/// Represents a basic block in Miden Assembly syntax.
///
/// Blocks can be nested, see [Op] for details.
#[derive(Clone, Default)]
Expand All @@ -17,17 +17,17 @@ pub struct Block {
}

impl Block {
/// Create a new [Block]
/// Creates a new [Block].
pub fn new(span: SourceSpan, body: Vec<Op>) -> Self {
Self { span, body }
}

/// Append `op` to this block
/// Appends `op` to this block.
pub fn push(&mut self, op: Op) {
self.body.push(op);
}

/// Get the number of ops in this block
/// Returns the number of ops in this block.
///
/// NOTE: The count does not include nested ops,
/// only those at the root of the block.
Expand All @@ -40,51 +40,17 @@ impl Block {
self.body.is_empty()
}

/// Get an iterator for the operations in this block
/// Returns an iterator for the operations in this block.
pub fn iter(&self) -> core::slice::Iter<'_, Op> {
self.body.iter()
}

/// Get a mutable iterator for the operations in this block
/// Returns a mutable iterator for the operations in this block.
pub fn iter_mut(&mut self) -> core::slice::IterMut<'_, Op> {
self.body.iter_mut()
}
}

/// Serialization
impl Block {
/// Serialize this block to `target` with `options`
pub fn write_into_with_options<W: ByteWriter>(&self, target: &mut W, options: AstSerdeOptions) {
if options.debug_info {
self.span.write_into(target);
}
target.write_u16(self.body.len() as u16);
for op in self.body.iter() {
op.write_into_with_options(target, options);
}
}

/// Deserialize this block from `source` with `options`
pub fn read_from_with_options<R: ByteReader>(
source: &mut R,
options: AstSerdeOptions,
) -> Result<Self, DeserializationError> {
let span = if options.debug_info {
SourceSpan::read_from(source)?
} else {
SourceSpan::default()
};

let body_len = source.read_u16()? as usize;
let mut body = Vec::with_capacity(body_len);
for _ in 0..body_len {
let op = Op::read_from_with_options(source, options)?;
body.push(op);
}
Ok(Self { span, body })
}
}

impl fmt::Debug for Block {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(&self.body).finish()
Expand Down
59 changes: 1 addition & 58 deletions assembly/src/ast/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use core::{
str::FromStr,
};

use crate::{
ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable, SourceSpan, Span,
Spanned,
};
use crate::{SourceSpan, Span, Spanned};

/// Represents the types of errors that can occur when parsing/validating an [Ident]
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
Expand Down Expand Up @@ -186,57 +183,3 @@ impl FromStr for Ident {
Ok(Self { span: SourceSpan::default(), name })
}
}

/// Serialization
impl Ident {
pub fn write_into_with_options<W: ByteWriter>(
&self,
target: &mut W,
options: crate::ast::AstSerdeOptions,
) {
if options.debug_info {
self.span.write_into(target);
}
target.write_usize(self.name.as_bytes().len());
target.write_bytes(self.name.as_bytes());
}

pub fn read_from_with_options<R: ByteReader>(
source: &mut R,
options: crate::ast::AstSerdeOptions,
) -> Result<Self, DeserializationError> {
let span = if options.debug_info {
SourceSpan::read_from(source)?
} else {
SourceSpan::default()
};
let nlen = source.read_usize()?;
let name = source.read_slice(nlen)?;
let name = core::str::from_utf8(name)
.map_err(|e| DeserializationError::InvalidValue(e.to_string()))?;
name.parse::<Ident>()
.map_err(|e| DeserializationError::InvalidValue(e.to_string()))
.map(|id| id.with_span(span))
}
}

impl Serializable for Ident {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.span.write_into(target);
target.write_usize(self.name.as_bytes().len());
target.write_bytes(self.name.as_bytes());
}
}

impl Deserializable for Ident {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let span = SourceSpan::read_from(source)?;
let nlen = source.read_usize()?;
let name = source.read_slice(nlen)?;
let name = core::str::from_utf8(name)
.map_err(|e| DeserializationError::InvalidValue(e.to_string()))?;
name.parse::<Ident>()
.map_err(|e| DeserializationError::InvalidValue(e.to_string()))
.map(|id| id.with_span(span))
}
}
34 changes: 1 addition & 33 deletions assembly/src/ast/imports.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use core::fmt;

use crate::{
ast::{AstSerdeOptions, Ident},
ByteReader, ByteWriter, Deserializable, DeserializationError, LibraryNamespace, LibraryPath,
Serializable, SourceSpan, Spanned,
};
use crate::{ast::Ident, LibraryNamespace, LibraryPath, SourceSpan, Spanned};

// IMPORT
// ================================================================================================
Expand Down Expand Up @@ -47,34 +43,6 @@ impl Import {
}
}

/// Serialization
impl Import {
/// Serializes this import to `target` with `options`
pub fn write_into_with_options<W: ByteWriter>(&self, target: &mut W, options: AstSerdeOptions) {
if options.debug_info {
self.span.write_into(target);
}
self.name.write_into_with_options(target, options);
self.path.write_into(target);
}

/// Deserializes this import from `source` with `options`.
pub fn read_from_with_options<R: ByteReader>(
source: &mut R,
options: AstSerdeOptions,
) -> Result<Self, DeserializationError> {
let span = if options.debug_info {
SourceSpan::read_from(source)?
} else {
SourceSpan::default()
};

let name = Ident::read_from_with_options(source, options)?;
let path = LibraryPath::read_from(source)?;
Ok(Self { span, name, path, uses: 0 })
}
}

impl fmt::Debug for Import {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Import")
Expand Down
Loading

0 comments on commit 9dd4cd6

Please sign in to comment.