diff --git a/crates/aiken-lang/src/expr.rs b/crates/aiken-lang/src/expr.rs index 07659b36..9a19ce64 100644 --- a/crates/aiken-lang/src/expr.rs +++ b/crates/aiken-lang/src/expr.rs @@ -30,6 +30,7 @@ pub enum TypedExpr { location: Span, tipo: Rc, value: String, + base: Base, }, String { @@ -42,12 +43,14 @@ pub enum TypedExpr { location: Span, tipo: Rc, bytes: Vec, + preferred_format: ByteArrayFormatPreference, }, CurvePoint { location: Span, tipo: Rc, point: Box, + preferred_format: ByteArrayFormatPreference, }, Sequence { diff --git a/crates/aiken-lang/src/format.rs b/crates/aiken-lang/src/format.rs index 7530e10a..6bfbc3d0 100644 --- a/crates/aiken-lang/src/format.rs +++ b/crates/aiken-lang/src/format.rs @@ -340,12 +340,53 @@ impl<'comments> Formatter<'comments> { pub fn docs_const_expr<'a>(&mut self, name: &'a str, value: &'a TypedExpr) -> Document<'a> { let mut printer = tipo::pretty::Printer::new(); - name.to_doc() + let doc = name + .to_doc() .append(": ") - .append(printer.print(&value.tipo())) - // TODO: Show full expression in docs when simple enough - // .append(" = ") - // .append(self.const_expr(value)) + .append(printer.print(&value.tipo())); + + // NOTE: Only display the full value for simple expressions. + let value = self.const_expr(value); + if value.is_empty() { + doc + } else { + doc.append(" = ").append(value) + } + } + + pub fn const_expr<'a>(&mut self, value: &'a TypedExpr) -> Document<'a> { + match value { + TypedExpr::UInt { value, base, .. } => self.int(value, base), + TypedExpr::String { value, .. } => self.string(value), + TypedExpr::ByteArray { + bytes, + preferred_format, + .. + } => self.bytearray(bytes, None, preferred_format), + TypedExpr::CurvePoint { + point, + preferred_format, + .. + } => self.bytearray( + &point.compress(), + Some(point.as_ref().into()), + preferred_format, + ), + TypedExpr::Tuple { elems, .. } => { + wrap_args(elems.iter().map(|e| (self.const_expr(e), false))).group() + } + TypedExpr::Pair { fst, snd, .. } => { + let elems = [fst, snd]; + "Pair" + .to_doc() + .append(wrap_args(elems.iter().map(|e| (self.const_expr(e), false))).group()) + } + TypedExpr::List { elements, .. } => { + wrap_args(elements.iter().map(|e| (self.const_expr(e), false))).group() + } + TypedExpr::Var { name, .. } => name.to_doc(), + _ => Document::Str(""), + } } fn documented_definition<'a>(&mut self, s: &'a UntypedDefinition) -> Document<'a> { diff --git a/crates/aiken-lang/src/parser/expr/sequence.rs b/crates/aiken-lang/src/parser/expr/sequence.rs index 1920bdc6..3b36b871 100644 --- a/crates/aiken-lang/src/parser/expr/sequence.rs +++ b/crates/aiken-lang/src/parser/expr/sequence.rs @@ -1,9 +1,8 @@ -use chumsky::prelude::*; - use crate::{ expr::UntypedExpr, parser::{error::ParseError, token::Token}, }; +use chumsky::prelude::*; pub fn parser() -> impl Parser { recursive(|sequence| { diff --git a/crates/aiken-lang/src/tipo/expr.rs b/crates/aiken-lang/src/tipo/expr.rs index 376281db..a802ff25 100644 --- a/crates/aiken-lang/src/tipo/expr.rs +++ b/crates/aiken-lang/src/tipo/expr.rs @@ -20,6 +20,7 @@ use crate::{ builtins::{from_default_function, BUILTIN}, expr::{FnStyle, TypedExpr, UntypedExpr}, format, + parser::token::Base, tipo::{fields::FieldMap, DefaultFunction, PatternConstructor, TypeVar}, IdGenerator, }; @@ -434,8 +435,8 @@ impl<'a, 'b> ExprTyper<'a, 'b> { UntypedExpr::UInt { location, value, - base: _, - } => Ok(self.infer_uint(value, location)), + base, + } => Ok(self.infer_uint(value, base, location)), UntypedExpr::Sequence { expressions, @@ -550,8 +551,8 @@ impl<'a, 'b> ExprTyper<'a, 'b> { UntypedExpr::CurvePoint { location, point, - preferred_format: _, - } => self.infer_curve_point(*point, location), + preferred_format, + } => self.infer_curve_point(*point, preferred_format, location), UntypedExpr::RecordUpdate { location, @@ -592,10 +593,16 @@ impl<'a, 'b> ExprTyper<'a, 'b> { location, bytes, tipo: Type::byte_array(), + preferred_format, }) } - fn infer_curve_point(&mut self, curve: Curve, location: Span) -> Result { + fn infer_curve_point( + &mut self, + curve: Curve, + preferred_format: ByteArrayFormatPreference, + location: Span, + ) -> Result { let tipo = match curve { Curve::Bls12_381(point) => match point { Bls12_381Point::G1(_) => Type::g1_element(), @@ -607,6 +614,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> { location, point: curve.into(), tipo, + preferred_format, }) } @@ -1707,11 +1715,12 @@ impl<'a, 'b> ExprTyper<'a, 'b> { Ok((args, body, return_type)) } - fn infer_uint(&mut self, value: String, location: Span) -> TypedExpr { + fn infer_uint(&mut self, value: String, base: Base, location: Span) -> TypedExpr { TypedExpr::UInt { location, value, tipo: Type::int(), + base, } } @@ -2720,6 +2729,7 @@ fn diagnose_expr(expr: TypedExpr) -> TypedExpr { tipo: Type::byte_array(), bytes: vec![], location, + preferred_format: ByteArrayFormatPreference::HexadecimalString, }, }, ],