mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 08:34:33 +00:00
Make expr displayable
This commit is contained in:
parent
974e8e9392
commit
81ef0cbc7b
1 changed files with 48 additions and 1 deletions
49
src/expr.rs
49
src/expr.rs
|
@ -1,3 +1,5 @@
|
|||
use std::fmt;
|
||||
use self::Expr::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Expr {
|
||||
|
@ -21,6 +23,52 @@ pub enum Expr {
|
|||
Error(Problem),
|
||||
}
|
||||
|
||||
impl fmt::Display for Expr {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
// PRIMITIVES
|
||||
Int(num) => write!(f, "{} : Int", *num),
|
||||
Frac(numerator, denominator) => {
|
||||
if *denominator == 10 {
|
||||
write!(f, "{} : Frac", (*numerator as f64 / 10.0))
|
||||
} else {
|
||||
write!(f, "{}/{} : Frac", numerator, denominator)
|
||||
}
|
||||
},
|
||||
Str(string) => {
|
||||
let escaped_str =
|
||||
(*string)
|
||||
.replace("\\", "\\\\")
|
||||
.replace("\"", "\\\"")
|
||||
.replace("\t", "\\t")
|
||||
.replace("\n", "\\n")
|
||||
.replace("\r", "\\r");
|
||||
|
||||
write!(f, "\"{}\" : String", escaped_str)
|
||||
},
|
||||
Char(ch) => write!(f, "'{}' : Char", *ch),
|
||||
Bool(true) => write!(f, "True : Bool"),
|
||||
Bool(false) => write!(f, "False : Bool"),
|
||||
Closure(args, _) => write!(f, "<{}-argument function>", args.len()),
|
||||
|
||||
// ERRORS
|
||||
Error(Problem::UnrecognizedVarName(name)) => write!(f, "NAMING ERROR: Unrecognized var name `{}`", name),
|
||||
Error(Problem::TypeMismatch(info)) => write!(f, "TYPE ERROR: {}", info),
|
||||
Error(Problem::ReassignedVarName(name)) => write!(f, "REASSIGNED CONSTANT: {}", name),
|
||||
Error(Problem::WrongArity(expected_arity, provided_arity)) => {
|
||||
if provided_arity > expected_arity {
|
||||
write!(f, "TOO MANY ARGUMENTS: needed {} arguments, but got {}", expected_arity, provided_arity)
|
||||
} else {
|
||||
write!(f, "MISSING ARGUMENTS: needed {} arguments, but got {}", expected_arity, provided_arity)
|
||||
}
|
||||
}
|
||||
|
||||
// UNFORMATTED
|
||||
_ => write!(f, "<partially evaluated expression>")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Problem {
|
||||
UnrecognizedVarName(String),
|
||||
|
@ -29,7 +77,6 @@ pub enum Problem {
|
|||
WrongArity(u32 /* Expected */, u32 /* Provided */),
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Pattern {
|
||||
Identifier(String),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue