1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
use alloc::{boxed::Box, string::String};
use core::fmt;
use vm_core::FieldElement;
use crate::{ast::Ident, parser::ParsingError, Felt, SourceSpan, Span, Spanned};
// CONSTANT
// ================================================================================================
/// Represents a constant definition in Miden Assembly syntax, i.e. `const.FOO = 1 + 1`.
pub struct Constant {
/// The source span of the definition.
pub span: SourceSpan,
/// The documentation string attached to this definition.
pub docs: Option<Span<String>>,
/// The name of the constant.
pub name: Ident,
/// The expression associated with the constant.
pub value: ConstantExpr,
}
impl Constant {
/// Creates a new [Constant] from the given source span, name, and value.
pub fn new(span: SourceSpan, name: Ident, value: ConstantExpr) -> Self {
Self { span, docs: None, name, value }
}
/// Adds documentation to this constant declaration.
pub fn with_docs(mut self, docs: Option<Span<String>>) -> Self {
self.docs = docs;
self
}
}
impl fmt::Debug for Constant {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Constant")
.field("docs", &self.docs)
.field("name", &self.name)
.field("value", &self.value)
.finish()
}
}
impl crate::prettier::PrettyPrint for Constant {
fn render(&self) -> crate::prettier::Document {
use crate::prettier::*;
let mut doc = Document::Empty;
if let Some(docs) = self.docs.as_ref() {
let fragment =
docs.lines().map(text).reduce(|acc, line| acc + nl() + const_text("#! ") + line);
if let Some(fragment) = fragment {
doc += fragment;
}
}
doc += nl();
doc += flatten(const_text("const") + const_text(".") + display(&self.name));
doc += const_text("=");
doc + self.value.render()
}
}
impl Eq for Constant {}
impl PartialEq for Constant {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.value == other.value
}
}
impl Spanned for Constant {
fn span(&self) -> SourceSpan {
self.span
}
}
// CONSTANT EXPRESSION
// ================================================================================================
/// Represents a constant expression or value in Miden Assembly syntax.
pub enum ConstantExpr {
/// A literal integer value.
Literal(Span<Felt>),
/// A reference to another constant.
Var(Ident),
/// An binary arithmetic operator.
BinaryOp {
span: SourceSpan,
op: ConstantOp,
lhs: Box<ConstantExpr>,
rhs: Box<ConstantExpr>,
},
}
impl ConstantExpr {
/// Unwrap a literal value from this expression or panic.
///
/// This is used in places where we expect the expression to have been folded to a value,
/// otherwise a bug occurred.
#[track_caller]
pub fn expect_literal(&self) -> Felt {
match self {
Self::Literal(spanned) => spanned.into_inner(),
other => panic!("expected constant expression to be a literal, got {other:#?}"),
}
}
/// Attempt to fold to a single value.
///
/// This will only succeed if the expression has no references to other constants.
///
/// # Errors
/// Returns an error if an invalid expression is found while folding, such as division by zero.
pub fn try_fold(self) -> Result<Self, ParsingError> {
match self {
Self::Literal(_) | Self::Var(_) => Ok(self),
Self::BinaryOp { span, op, lhs, rhs } => {
if rhs.is_literal() {
let rhs = Self::into_inner(rhs).try_fold()?;
match rhs {
Self::Literal(rhs) => {
let lhs = Self::into_inner(lhs).try_fold()?;
match lhs {
Self::Literal(lhs) => {
let lhs = lhs.into_inner();
let rhs = rhs.into_inner();
let is_division =
matches!(op, ConstantOp::Div | ConstantOp::IntDiv);
let is_division_by_zero = is_division && rhs == Felt::ZERO;
if is_division_by_zero {
return Err(ParsingError::DivisionByZero { span });
}
match op {
ConstantOp::Add => {
Ok(Self::Literal(Span::new(span, lhs + rhs)))
},
ConstantOp::Sub => {
Ok(Self::Literal(Span::new(span, lhs - rhs)))
},
ConstantOp::Mul => {
Ok(Self::Literal(Span::new(span, lhs * rhs)))
},
ConstantOp::Div => {
Ok(Self::Literal(Span::new(span, lhs / rhs)))
},
ConstantOp::IntDiv => Ok(Self::Literal(Span::new(
span,
Felt::new(lhs.as_int() / rhs.as_int()),
))),
}
},
lhs => Ok(Self::BinaryOp {
span,
op,
lhs: Box::new(lhs),
rhs: Box::new(Self::Literal(rhs)),
}),
}
},
rhs => {
let lhs = Self::into_inner(lhs).try_fold()?;
Ok(Self::BinaryOp {
span,
op,
lhs: Box::new(lhs),
rhs: Box::new(rhs),
})
},
}
} else {
let lhs = Self::into_inner(lhs).try_fold()?;
Ok(Self::BinaryOp { span, op, lhs: Box::new(lhs), rhs })
}
},
}
}
fn is_literal(&self) -> bool {
match self {
Self::Literal(_) => true,
Self::Var(_) => false,
Self::BinaryOp { lhs, rhs, .. } => lhs.is_literal() && rhs.is_literal(),
}
}
#[inline(always)]
#[allow(clippy::boxed_local)]
fn into_inner(self: Box<Self>) -> Self {
*self
}
}
impl Eq for ConstantExpr {}
impl PartialEq for ConstantExpr {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Literal(l), Self::Literal(y)) => l == y,
(Self::Var(l), Self::Var(y)) => l == y,
(
Self::BinaryOp { op: lop, lhs: llhs, rhs: lrhs, .. },
Self::BinaryOp { op: rop, lhs: rlhs, rhs: rrhs, .. },
) => lop == rop && llhs == rlhs && lrhs == rrhs,
_ => false,
}
}
}
impl fmt::Debug for ConstantExpr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Literal(ref lit) => fmt::Debug::fmt(&**lit, f),
Self::Var(ref name) => fmt::Debug::fmt(&**name, f),
Self::BinaryOp { ref op, ref lhs, ref rhs, .. } => {
f.debug_tuple(op.name()).field(lhs).field(rhs).finish()
},
}
}
}
impl crate::prettier::PrettyPrint for ConstantExpr {
fn render(&self) -> crate::prettier::Document {
use crate::prettier::*;
match self {
Self::Literal(ref literal) => display(literal),
Self::Var(ref ident) => display(ident),
Self::BinaryOp { op, lhs, rhs, .. } => {
let single_line = lhs.render() + display(op) + rhs.render();
let multi_line = lhs.render() + nl() + (display(op)) + rhs.render();
single_line | multi_line
},
}
}
}
impl Spanned for ConstantExpr {
fn span(&self) -> SourceSpan {
match self {
Self::Literal(spanned) => spanned.span(),
Self::Var(spanned) => spanned.span(),
Self::BinaryOp { span, .. } => *span,
}
}
}
// CONSTANT OPERATION
// ================================================================================================
/// Represents the set of binary arithmetic operators supported in Miden Assembly syntax.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ConstantOp {
Add,
Sub,
Mul,
Div,
IntDiv,
}
impl ConstantOp {
const fn name(&self) -> &'static str {
match self {
Self::Add => "Add",
Self::Sub => "Sub",
Self::Mul => "Mul",
Self::Div => "Div",
Self::IntDiv => "IntDiv",
}
}
}
impl fmt::Display for ConstantOp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Add => f.write_str("+"),
Self::Sub => f.write_str("-"),
Self::Mul => f.write_str("*"),
Self::Div => f.write_str("/"),
Self::IntDiv => f.write_str("//"),
}
}
}