mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 23:31:12 +00:00
Reading from stdin works
This commit is contained in:
parent
1c3cf5f675
commit
140fa5ffa9
6 changed files with 196 additions and 220 deletions
74
src/expr.rs
74
src/expr.rs
|
@ -1,5 +1,3 @@
|
|||
use std::fmt;
|
||||
use self::Expr::*;
|
||||
use smallvec::SmallVec;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
|
@ -30,80 +28,10 @@ pub enum Expr {
|
|||
// Conditionals
|
||||
If(Box<Expr>, Box<Expr>, Box<Expr>),
|
||||
Case(Box<Expr>, SmallVec<[(Pattern, Box<Expr>); 2]>),
|
||||
|
||||
// Error
|
||||
Error(Problem),
|
||||
}
|
||||
|
||||
pub type Ident = String;
|
||||
|
||||
impl fmt::Display for Expr {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
// PRIMITIVES
|
||||
Int(num) => write!(f, "{}", *num),
|
||||
Frac(numerator, denominator) => {
|
||||
if *denominator == 10 {
|
||||
write!(f, "{}", (*numerator as f64 / 10.0))
|
||||
} else {
|
||||
write!(f, "{}/{}", numerator, denominator)
|
||||
}
|
||||
},
|
||||
Str(string) => {
|
||||
let escaped_str =
|
||||
(*string)
|
||||
.replace("\\", "\\\\")
|
||||
.replace("\"", "\\\"")
|
||||
.replace("\t", "\\t")
|
||||
.replace("\n", "\\n")
|
||||
.replace("\r", "\\r");
|
||||
|
||||
write!(f, "\"{}\"", escaped_str)
|
||||
},
|
||||
Char(ch) => write!(f, "'{}'", *ch),
|
||||
Closure(args, _) => write!(f, "<{}-argument function>", args.len()),
|
||||
ApplyVariant(name, opt_exprs) => {
|
||||
match opt_exprs {
|
||||
None => write!(f, "{}", name),
|
||||
Some(exprs) => {
|
||||
let contents =
|
||||
exprs.into_iter()
|
||||
.map(|expr| format!(" {}", expr))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
|
||||
write!(f, "{}{}", name, contents)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// 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),
|
||||
TypeMismatch(String),
|
||||
ReassignedVarName(String),
|
||||
WrongArity(u32 /* Expected */, u32 /* Provided */),
|
||||
NotEqual, // Used when (for example) a string literal pattern match fails
|
||||
NoBranchesMatched,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Pattern {
|
||||
|
@ -111,7 +39,7 @@ pub enum Pattern {
|
|||
Variant(String, Option<Vec<Pattern>>),
|
||||
Integer(i64),
|
||||
Fraction(i64, u64),
|
||||
EmptyRecord,
|
||||
EmptyRecordLiteral,
|
||||
Underscore
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue