|
@@ -41,20 +41,26 @@ type DtypeFactorPower = (DTypeFactor, Exponent);
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
pub struct DType {
|
|
|
// Always in canonical form
|
|
|
- pub factors: Vec<DtypeFactorPower>, // TODO make this private
|
|
|
+ factors: Vec<DtypeFactorPower>,
|
|
|
}
|
|
|
|
|
|
impl DType {
|
|
|
- pub fn from_factors(factors: &[DtypeFactorPower]) -> DType {
|
|
|
- let mut dtype = DType {
|
|
|
- factors: factors.into(),
|
|
|
- };
|
|
|
+ pub fn factors(&self) -> &[DtypeFactorPower] {
|
|
|
+ &self.factors
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn into_factors(self) -> Vec<DtypeFactorPower> {
|
|
|
+ self.factors
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn from_factors(factors: Vec<DtypeFactorPower>) -> DType {
|
|
|
+ let mut dtype = DType { factors };
|
|
|
dtype.canonicalize();
|
|
|
dtype
|
|
|
}
|
|
|
|
|
|
pub fn scalar() -> DType {
|
|
|
- DType::from_factors(&[])
|
|
|
+ DType::from_factors(vec![])
|
|
|
}
|
|
|
|
|
|
pub fn is_scalar(&self) -> bool {
|
|
@@ -76,11 +82,12 @@ impl DType {
|
|
|
names.extend(registry.get_derived_entry_names_for(&base_representation));
|
|
|
match &names[..] {
|
|
|
[] => self.pretty_print(),
|
|
|
- [single] => m::type_identifier(single),
|
|
|
- multiple => {
|
|
|
- Itertools::intersperse(multiple.iter().map(m::type_identifier), m::dimmed(" or "))
|
|
|
- .sum()
|
|
|
- }
|
|
|
+ [single] => m::type_identifier(single.to_string()),
|
|
|
+ multiple => Itertools::intersperse(
|
|
|
+ multiple.iter().cloned().map(m::type_identifier),
|
|
|
+ m::dimmed(" or "),
|
|
|
+ )
|
|
|
+ .sum(),
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -92,11 +99,11 @@ impl DType {
|
|
|
}
|
|
|
|
|
|
pub fn from_type_variable(v: TypeVariable) -> DType {
|
|
|
- DType::from_factors(&[(DTypeFactor::TVar(v), Exponent::from_integer(1))])
|
|
|
+ DType::from_factors(vec![(DTypeFactor::TVar(v), Exponent::from_integer(1))])
|
|
|
}
|
|
|
|
|
|
pub fn from_type_parameter(name: String) -> DType {
|
|
|
- DType::from_factors(&[(DTypeFactor::TPar(name), Exponent::from_integer(1))])
|
|
|
+ DType::from_factors(vec![(DTypeFactor::TPar(name), Exponent::from_integer(1))])
|
|
|
}
|
|
|
|
|
|
pub fn deconstruct_as_single_type_variable(&self) -> Option<TypeVariable> {
|
|
@@ -109,14 +116,14 @@ impl DType {
|
|
|
}
|
|
|
|
|
|
pub fn from_tgen(i: usize) -> DType {
|
|
|
- DType::from_factors(&[(
|
|
|
+ DType::from_factors(vec![(
|
|
|
DTypeFactor::TVar(TypeVariable::Quantified(i)),
|
|
|
Exponent::from_integer(1),
|
|
|
)])
|
|
|
}
|
|
|
|
|
|
pub fn base_dimension(name: &str) -> DType {
|
|
|
- DType::from_factors(&[(
|
|
|
+ DType::from_factors(vec![(
|
|
|
DTypeFactor::BaseDimension(name.into()),
|
|
|
Exponent::from_integer(1),
|
|
|
)])
|
|
@@ -157,16 +164,16 @@ impl DType {
|
|
|
pub fn multiply(&self, other: &DType) -> DType {
|
|
|
let mut factors = self.factors.clone();
|
|
|
factors.extend(other.factors.clone());
|
|
|
- DType::from_factors(&factors)
|
|
|
+ DType::from_factors(factors)
|
|
|
}
|
|
|
|
|
|
pub fn power(&self, n: Exponent) -> DType {
|
|
|
- let factors: Vec<_> = self
|
|
|
+ let factors = self
|
|
|
.factors
|
|
|
.iter()
|
|
|
.map(|(f, m)| (f.clone(), n * m))
|
|
|
.collect();
|
|
|
- DType::from_factors(&factors)
|
|
|
+ DType::from_factors(factors)
|
|
|
}
|
|
|
|
|
|
pub fn inverse(&self) -> DType {
|
|
@@ -220,7 +227,7 @@ impl DType {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- Self::from_factors(&factors)
|
|
|
+ Self::from_factors(factors)
|
|
|
}
|
|
|
|
|
|
pub fn to_base_representation(&self) -> BaseRepresentation {
|
|
@@ -259,11 +266,11 @@ impl std::fmt::Display for DType {
|
|
|
|
|
|
impl From<BaseRepresentation> for DType {
|
|
|
fn from(base_representation: BaseRepresentation) -> Self {
|
|
|
- let factors: Vec<_> = base_representation
|
|
|
+ let factors = base_representation
|
|
|
.into_iter()
|
|
|
.map(|BaseRepresentationFactor(name, exp)| (DTypeFactor::BaseDimension(name), exp))
|
|
|
.collect();
|
|
|
- DType::from_factors(&factors)
|
|
|
+ DType::from_factors(factors)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -325,11 +332,11 @@ impl std::fmt::Display for Type {
|
|
|
impl PrettyPrint for Type {
|
|
|
fn pretty_print(&self) -> Markup {
|
|
|
match self {
|
|
|
- Type::TVar(TypeVariable::Named(name)) => m::type_identifier(name),
|
|
|
+ Type::TVar(TypeVariable::Named(name)) => m::type_identifier(name.clone()),
|
|
|
Type::TVar(TypeVariable::Quantified(_)) => {
|
|
|
unreachable!("Quantified types should not be printed")
|
|
|
}
|
|
|
- Type::TPar(name) => m::type_identifier(name),
|
|
|
+ Type::TPar(name) => m::type_identifier(name.clone()),
|
|
|
Type::Dimension(d) => d.pretty_print(),
|
|
|
Type::Boolean => m::type_identifier("Bool"),
|
|
|
Type::String => m::type_identifier("String"),
|
|
@@ -349,7 +356,7 @@ impl PrettyPrint for Type {
|
|
|
+ return_type.pretty_print()
|
|
|
+ m::operator("]")
|
|
|
}
|
|
|
- Type::Struct(info) => m::type_identifier(&info.name),
|
|
|
+ Type::Struct(info) => m::type_identifier(info.name.clone()),
|
|
|
Type::List(element_type) => {
|
|
|
m::type_identifier("List")
|
|
|
+ m::operator("<")
|
|
@@ -451,16 +458,16 @@ impl Type {
|
|
|
}
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
-pub enum StringPart {
|
|
|
+pub enum StringPart<'a> {
|
|
|
Fixed(String),
|
|
|
Interpolation {
|
|
|
span: Span,
|
|
|
- expr: Box<Expression>,
|
|
|
- format_specifiers: Option<String>,
|
|
|
+ expr: Box<Expression<'a>>,
|
|
|
+ format_specifiers: Option<&'a str>,
|
|
|
},
|
|
|
}
|
|
|
|
|
|
-impl PrettyPrint for StringPart {
|
|
|
+impl PrettyPrint for StringPart<'_> {
|
|
|
fn pretty_print(&self) -> Markup {
|
|
|
match self {
|
|
|
StringPart::Fixed(s) => m::string(escape_numbat_string(s)),
|
|
@@ -472,7 +479,7 @@ impl PrettyPrint for StringPart {
|
|
|
let mut markup = m::operator("{") + expr.pretty_print();
|
|
|
|
|
|
if let Some(format_specifiers) = format_specifiers {
|
|
|
- markup += m::text(format_specifiers);
|
|
|
+ markup += m::text(format_specifiers.to_string());
|
|
|
}
|
|
|
|
|
|
markup += m::operator("}");
|
|
@@ -483,23 +490,23 @@ impl PrettyPrint for StringPart {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl PrettyPrint for &Vec<StringPart> {
|
|
|
+impl PrettyPrint for &Vec<StringPart<'_>> {
|
|
|
fn pretty_print(&self) -> Markup {
|
|
|
m::operator("\"") + self.iter().map(|p| p.pretty_print()).sum() + m::operator("\"")
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
-pub enum Expression {
|
|
|
+pub enum Expression<'a> {
|
|
|
Scalar(Span, Number, TypeScheme),
|
|
|
- Identifier(Span, String, TypeScheme),
|
|
|
+ Identifier(Span, &'a str, TypeScheme),
|
|
|
UnitIdentifier(Span, Prefix, String, String, TypeScheme),
|
|
|
- UnaryOperator(Span, UnaryOperator, Box<Expression>, TypeScheme),
|
|
|
+ UnaryOperator(Span, UnaryOperator, Box<Expression<'a>>, TypeScheme),
|
|
|
BinaryOperator(
|
|
|
Option<Span>,
|
|
|
BinaryOperator,
|
|
|
- Box<Expression>,
|
|
|
- Box<Expression>,
|
|
|
+ Box<Expression<'a>>,
|
|
|
+ Box<Expression<'a>>,
|
|
|
TypeScheme,
|
|
|
),
|
|
|
/// A special binary operator that has a DateTime as one (or both) of the operands
|
|
@@ -507,32 +514,37 @@ pub enum Expression {
|
|
|
Option<Span>,
|
|
|
BinaryOperator,
|
|
|
/// LHS must evaluate to a DateTime
|
|
|
- Box<Expression>,
|
|
|
+ Box<Expression<'a>>,
|
|
|
/// RHS can evaluate to a DateTime or a quantity of type Time
|
|
|
- Box<Expression>,
|
|
|
+ Box<Expression<'a>>,
|
|
|
TypeScheme,
|
|
|
),
|
|
|
// A 'proper' function call
|
|
|
- FunctionCall(Span, Span, String, Vec<Expression>, TypeScheme),
|
|
|
+ FunctionCall(Span, Span, &'a str, Vec<Expression<'a>>, TypeScheme),
|
|
|
// A call via a function object
|
|
|
- CallableCall(Span, Box<Expression>, Vec<Expression>, TypeScheme),
|
|
|
+ CallableCall(Span, Box<Expression<'a>>, Vec<Expression<'a>>, TypeScheme),
|
|
|
Boolean(Span, bool),
|
|
|
- Condition(Span, Box<Expression>, Box<Expression>, Box<Expression>),
|
|
|
- String(Span, Vec<StringPart>),
|
|
|
- InstantiateStruct(Span, Vec<(String, Expression)>, StructInfo),
|
|
|
+ Condition(
|
|
|
+ Span,
|
|
|
+ Box<Expression<'a>>,
|
|
|
+ Box<Expression<'a>>,
|
|
|
+ Box<Expression<'a>>,
|
|
|
+ ),
|
|
|
+ String(Span, Vec<StringPart<'a>>),
|
|
|
+ InstantiateStruct(Span, Vec<(&'a str, Expression<'a>)>, StructInfo),
|
|
|
AccessField(
|
|
|
Span,
|
|
|
Span,
|
|
|
- Box<Expression>,
|
|
|
- String, // field name
|
|
|
+ Box<Expression<'a>>,
|
|
|
+ &'a str, // field name
|
|
|
TypeScheme, // struct type
|
|
|
TypeScheme, // resulting field type
|
|
|
),
|
|
|
- List(Span, Vec<Expression>, TypeScheme),
|
|
|
+ List(Span, Vec<Expression<'a>>, TypeScheme),
|
|
|
TypedHole(Span, TypeScheme),
|
|
|
}
|
|
|
|
|
|
-impl Expression {
|
|
|
+impl Expression<'_> {
|
|
|
pub fn full_span(&self) -> Span {
|
|
|
match self {
|
|
|
Expression::Scalar(span, ..) => *span,
|
|
@@ -570,9 +582,9 @@ impl Expression {
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
pub struct DefineVariable<'a>(
|
|
|
- pub String,
|
|
|
+ pub &'a str,
|
|
|
pub Vec<Decorator<'a>>,
|
|
|
- pub Expression,
|
|
|
+ pub Expression<'a>,
|
|
|
pub Option<TypeAnnotation>,
|
|
|
pub TypeScheme,
|
|
|
pub Markup,
|
|
@@ -580,41 +592,41 @@ pub struct DefineVariable<'a>(
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
pub enum Statement<'a> {
|
|
|
- Expression(Expression),
|
|
|
+ Expression(Expression<'a>),
|
|
|
DefineVariable(DefineVariable<'a>),
|
|
|
DefineFunction(
|
|
|
- String,
|
|
|
- Vec<Decorator<'a>>, // decorators
|
|
|
- Vec<(String, Option<TypeParameterBound>)>, // type parameters
|
|
|
+ &'a str,
|
|
|
+ Vec<Decorator<'a>>, // decorators
|
|
|
+ Vec<(&'a str, Option<TypeParameterBound>)>, // type parameters
|
|
|
Vec<(
|
|
|
// parameters:
|
|
|
Span, // span of the parameter
|
|
|
- String, // parameter name
|
|
|
+ &'a str, // parameter name
|
|
|
Option<TypeAnnotation>, // parameter type annotation
|
|
|
Markup, // readable parameter type
|
|
|
)>,
|
|
|
- Option<Expression>, // function body
|
|
|
+ Option<Expression<'a>>, // function body
|
|
|
Vec<DefineVariable<'a>>, // local variables
|
|
|
TypeScheme, // function type
|
|
|
Option<TypeAnnotation>, // return type annotation
|
|
|
Markup, // readable return type
|
|
|
),
|
|
|
- DefineDimension(String, Vec<TypeExpression>),
|
|
|
+ DefineDimension(&'a str, Vec<TypeExpression>),
|
|
|
DefineBaseUnit(
|
|
|
- String,
|
|
|
+ &'a str,
|
|
|
Vec<Decorator<'a>>,
|
|
|
Option<TypeAnnotation>,
|
|
|
TypeScheme,
|
|
|
),
|
|
|
DefineDerivedUnit(
|
|
|
- String,
|
|
|
- Expression,
|
|
|
+ &'a str,
|
|
|
+ Expression<'a>,
|
|
|
Vec<Decorator<'a>>,
|
|
|
Option<TypeAnnotation>,
|
|
|
TypeScheme,
|
|
|
Markup,
|
|
|
),
|
|
|
- ProcedureCall(crate::ast::ProcedureKind, Vec<Expression>),
|
|
|
+ ProcedureCall(crate::ast::ProcedureKind, Vec<Expression<'a>>),
|
|
|
DefineStruct(StructInfo),
|
|
|
}
|
|
|
|
|
@@ -668,9 +680,8 @@ impl Statement<'_> {
|
|
|
return_type_annotation,
|
|
|
readable_return_type,
|
|
|
) => {
|
|
|
- let (fn_type, _) = fn_type.instantiate_for_printing(Some(
|
|
|
- type_parameters.iter().map(|(n, _)| n.as_str()),
|
|
|
- ));
|
|
|
+ let (fn_type, _) =
|
|
|
+ fn_type.instantiate_for_printing(Some(type_parameters.iter().map(|(n, _)| *n)));
|
|
|
|
|
|
for DefineVariable(_, _, _, type_annotation, type_, readable_type) in
|
|
|
local_variables
|
|
@@ -751,7 +762,7 @@ impl Statement<'_> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl Expression {
|
|
|
+impl Expression<'_> {
|
|
|
pub fn get_type(&self) -> Type {
|
|
|
match self {
|
|
|
Expression::Scalar(_, _, type_) => type_.unsafe_as_concrete(),
|
|
@@ -846,8 +857,8 @@ fn decorator_markup(decorators: &Vec<Decorator>) -> Markup {
|
|
|
m::decorator("@aliases")
|
|
|
+ m::operator("(")
|
|
|
+ Itertools::intersperse(
|
|
|
- names.iter().map(|(name, accepts_prefix)| {
|
|
|
- m::unit(name) + accepts_prefix_markup(accepts_prefix)
|
|
|
+ names.iter().map(|(name, accepts_prefix, _)| {
|
|
|
+ m::unit(name.to_string()) + accepts_prefix_markup(accepts_prefix)
|
|
|
}),
|
|
|
m::operator(", "),
|
|
|
)
|
|
@@ -855,15 +866,32 @@ fn decorator_markup(decorators: &Vec<Decorator>) -> Markup {
|
|
|
+ m::operator(")")
|
|
|
}
|
|
|
Decorator::Url(url) => {
|
|
|
- m::decorator("@url") + m::operator("(") + m::string(url) + m::operator(")")
|
|
|
+ m::decorator("@url")
|
|
|
+ + m::operator("(")
|
|
|
+ + m::string(url.clone())
|
|
|
+ + m::operator(")")
|
|
|
}
|
|
|
Decorator::Name(name) => {
|
|
|
- m::decorator("@name") + m::operator("(") + m::string(name) + m::operator(")")
|
|
|
+ m::decorator("@name")
|
|
|
+ + m::operator("(")
|
|
|
+ + m::string(name.clone())
|
|
|
+ + m::operator(")")
|
|
|
}
|
|
|
Decorator::Description(description) => {
|
|
|
m::decorator("@description")
|
|
|
+ m::operator("(")
|
|
|
- + m::string(description)
|
|
|
+ + m::string(description.clone())
|
|
|
+ + m::operator(")")
|
|
|
+ }
|
|
|
+ Decorator::Example(example_code, example_description) => {
|
|
|
+ m::decorator("@example")
|
|
|
+ + m::operator("(")
|
|
|
+ + m::string(example_code.clone())
|
|
|
+ + if let Some(example_description) = example_description {
|
|
|
+ m::operator(", ") + m::string(example_description.clone())
|
|
|
+ } else {
|
|
|
+ m::empty()
|
|
|
+ }
|
|
|
+ m::operator(")")
|
|
|
}
|
|
|
}
|
|
@@ -890,7 +918,7 @@ pub fn pretty_print_function_signature<'a>(
|
|
|
m::operator("<")
|
|
|
+ Itertools::intersperse(
|
|
|
type_parameters.iter().map(|tv| {
|
|
|
- m::type_identifier(tv.unsafe_name())
|
|
|
+ m::type_identifier(tv.unsafe_name().to_string())
|
|
|
+ if fn_type.bounds.is_dtype_bound(tv) {
|
|
|
m::operator(":") + m::space() + m::type_identifier("Dim")
|
|
|
} else {
|
|
@@ -905,7 +933,7 @@ pub fn pretty_print_function_signature<'a>(
|
|
|
|
|
|
let markup_parameters = Itertools::intersperse(
|
|
|
parameters.map(|(name, parameter_type)| {
|
|
|
- m::identifier(name) + m::operator(":") + m::space() + parameter_type.clone()
|
|
|
+ m::identifier(name.to_string()) + m::operator(":") + m::space() + parameter_type
|
|
|
}),
|
|
|
m::operator(", "),
|
|
|
)
|
|
@@ -916,7 +944,7 @@ pub fn pretty_print_function_signature<'a>(
|
|
|
|
|
|
m::keyword("fn")
|
|
|
+ m::space()
|
|
|
- + m::identifier(function_name)
|
|
|
+ + m::identifier(function_name.to_string())
|
|
|
+ markup_type_parameters
|
|
|
+ m::operator("(")
|
|
|
+ markup_parameters
|
|
@@ -937,7 +965,7 @@ impl PrettyPrint for Statement<'_> {
|
|
|
)) => {
|
|
|
m::keyword("let")
|
|
|
+ m::space()
|
|
|
- + m::identifier(identifier)
|
|
|
+ + m::identifier(identifier.to_string())
|
|
|
+ m::operator(":")
|
|
|
+ m::space()
|
|
|
+ readable_type.clone()
|
|
@@ -957,9 +985,8 @@ impl PrettyPrint for Statement<'_> {
|
|
|
_return_type_annotation,
|
|
|
readable_return_type,
|
|
|
) => {
|
|
|
- let (fn_type, type_parameters) = fn_type.instantiate_for_printing(Some(
|
|
|
- type_parameters.iter().map(|(n, _)| n.as_str()),
|
|
|
- ));
|
|
|
+ let (fn_type, type_parameters) =
|
|
|
+ fn_type.instantiate_for_printing(Some(type_parameters.iter().map(|(n, _)| *n)));
|
|
|
|
|
|
let mut pretty_local_variables = None;
|
|
|
let mut first = true;
|
|
@@ -984,7 +1011,7 @@ impl PrettyPrint for Statement<'_> {
|
|
|
plv += m::nl()
|
|
|
+ introducer_keyword
|
|
|
+ m::space()
|
|
|
- + m::identifier(identifier)
|
|
|
+ + m::identifier(identifier.to_string())
|
|
|
+ m::operator(":")
|
|
|
+ m::space()
|
|
|
+ readable_type.clone()
|
|
@@ -1002,7 +1029,7 @@ impl PrettyPrint for Statement<'_> {
|
|
|
&type_parameters,
|
|
|
parameters
|
|
|
.iter()
|
|
|
- .map(|(_, name, _, type_)| (name.as_str(), type_.clone())),
|
|
|
+ .map(|(_, name, _, type_)| (*name, type_.clone())),
|
|
|
readable_return_type,
|
|
|
) + body
|
|
|
.as_ref()
|
|
@@ -1012,12 +1039,12 @@ impl PrettyPrint for Statement<'_> {
|
|
|
}
|
|
|
Statement::Expression(expr) => expr.pretty_print(),
|
|
|
Statement::DefineDimension(identifier, dexprs) if dexprs.is_empty() => {
|
|
|
- m::keyword("dimension") + m::space() + m::type_identifier(identifier)
|
|
|
+ m::keyword("dimension") + m::space() + m::type_identifier(identifier.to_string())
|
|
|
}
|
|
|
Statement::DefineDimension(identifier, dexprs) => {
|
|
|
m::keyword("dimension")
|
|
|
+ m::space()
|
|
|
- + m::type_identifier(identifier)
|
|
|
+ + m::type_identifier(identifier.to_string())
|
|
|
+ m::space()
|
|
|
+ m::operator("=")
|
|
|
+ m::space()
|
|
@@ -1031,7 +1058,7 @@ impl PrettyPrint for Statement<'_> {
|
|
|
decorator_markup(decorators)
|
|
|
+ m::keyword("unit")
|
|
|
+ m::space()
|
|
|
- + m::unit(identifier)
|
|
|
+ + m::unit(identifier.to_string())
|
|
|
+ m::operator(":")
|
|
|
+ m::space()
|
|
|
+ annotation
|
|
@@ -1050,7 +1077,7 @@ impl PrettyPrint for Statement<'_> {
|
|
|
decorator_markup(decorators)
|
|
|
+ m::keyword("unit")
|
|
|
+ m::space()
|
|
|
- + m::unit(identifier)
|
|
|
+ + m::unit(identifier.to_string())
|
|
|
+ m::operator(":")
|
|
|
+ m::space()
|
|
|
+ readable_type.clone()
|
|
@@ -1087,7 +1114,7 @@ impl PrettyPrint for Statement<'_> {
|
|
|
m::space()
|
|
|
+ Itertools::intersperse(
|
|
|
fields.iter().map(|(n, (_, t))| {
|
|
|
- m::identifier(n)
|
|
|
+ m::identifier(n.clone())
|
|
|
+ m::operator(":")
|
|
|
+ m::space()
|
|
|
+ t.pretty_print()
|
|
@@ -1158,7 +1185,7 @@ fn pretty_print_binop(op: &BinaryOperator, lhs: &Expression, rhs: &Expression) -
|
|
|
}
|
|
|
(Expression::Scalar(_, s, _), Expression::Identifier(_, name, _type)) => {
|
|
|
// Fuse multiplication of a scalar and identifier
|
|
|
- pretty_scalar(*s) + m::space() + m::identifier(name)
|
|
|
+ pretty_scalar(*s) + m::space() + m::identifier(name.to_string())
|
|
|
}
|
|
|
_ => {
|
|
|
let add_parens_if_needed = |expr: &Expression| {
|
|
@@ -1242,13 +1269,13 @@ fn pretty_print_binop(op: &BinaryOperator, lhs: &Expression, rhs: &Expression) -
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl PrettyPrint for Expression {
|
|
|
+impl PrettyPrint for Expression<'_> {
|
|
|
fn pretty_print(&self) -> Markup {
|
|
|
use Expression::*;
|
|
|
|
|
|
match self {
|
|
|
Scalar(_, n, _) => pretty_scalar(*n),
|
|
|
- Identifier(_, name, _type) => m::identifier(name),
|
|
|
+ Identifier(_, name, _type) => m::identifier(name.to_string()),
|
|
|
UnitIdentifier(_, prefix, _name, full_name, _type) => {
|
|
|
m::unit(format!("{}{}", prefix.as_string_long(), full_name))
|
|
|
}
|
|
@@ -1264,7 +1291,7 @@ impl PrettyPrint for Expression {
|
|
|
BinaryOperator(_, op, lhs, rhs, _type) => pretty_print_binop(op, lhs, rhs),
|
|
|
BinaryOperatorForDate(_, op, lhs, rhs, _type) => pretty_print_binop(op, lhs, rhs),
|
|
|
FunctionCall(_, _, name, args, _type) => {
|
|
|
- m::identifier(name)
|
|
|
+ m::identifier(name.to_string())
|
|
|
+ m::operator("(")
|
|
|
+ itertools::Itertools::intersperse(
|
|
|
args.iter().map(|e| e.pretty_print()),
|
|
@@ -1308,7 +1335,7 @@ impl PrettyPrint for Expression {
|
|
|
m::space()
|
|
|
+ itertools::Itertools::intersperse(
|
|
|
exprs.iter().map(|(n, e)| {
|
|
|
- m::identifier(n)
|
|
|
+ m::identifier(n.to_string())
|
|
|
+ m::operator(":")
|
|
|
+ m::space()
|
|
|
+ e.pretty_print()
|
|
@@ -1321,7 +1348,7 @@ impl PrettyPrint for Expression {
|
|
|
+ m::operator("}")
|
|
|
}
|
|
|
AccessField(_, _, expr, attr, _, _) => {
|
|
|
- expr.pretty_print() + m::operator(".") + m::identifier(attr)
|
|
|
+ expr.pretty_print() + m::operator(".") + m::identifier(attr.to_string())
|
|
|
}
|
|
|
List(_, elements, _) => {
|
|
|
m::operator("[")
|
|
@@ -1410,7 +1437,7 @@ mod tests {
|
|
|
let transformed_statements = transformer.transform(statements).unwrap().replace_spans();
|
|
|
|
|
|
crate::typechecker::TypeChecker::default()
|
|
|
- .check(transformed_statements)
|
|
|
+ .check(&transformed_statements)
|
|
|
.unwrap()
|
|
|
.last()
|
|
|
.unwrap()
|