Skip to content

Commit 6d8e046

Browse files
author
Jason P. Barmparesos
committed
Basic eval working.
1 parent c1d45da commit 6d8e046

File tree

2 files changed

+129
-137
lines changed

2 files changed

+129
-137
lines changed

src/expression.rs

Lines changed: 51 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,6 @@ use std::fmt;
44
use std::fmt::Formatter;
55
use std::cmp::Ordering;
66

7-
#[derive(Debug,Clone)]
8-
pub enum LValue {
9-
StringValue(String),
10-
NumericalValue(f64),
11-
BooleanValue(bool),
12-
Quoted(Box<Expression>),
13-
}
14-
15-
impl fmt::Display for LValue {
16-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
17-
match *self {
18-
LValue::StringValue(ref s) => write!(f, "\"{}\"", s),
19-
LValue::NumericalValue(v) => write!(f, "{}", v),
20-
LValue::BooleanValue(v) => if v { write!(f, "#t") } else { write!(f, "#f") },
21-
LValue::Quoted(ref e) => write!(f, "#<expression>:{:?}", e),
22-
}
23-
}
24-
}
25-
267
#[derive(Debug,Clone)]
278
pub enum Procedure {
289
UserDefined {
@@ -62,49 +43,45 @@ impl fmt::Display for Procedure {
6243
}
6344

6445
#[derive(Debug,Clone)]
65-
pub enum LResult {
66-
Value(LValue),
46+
pub enum LValue {
47+
StringValue(String),
48+
NumericalValue(f64),
49+
BooleanValue(bool),
50+
Quoted(Box<Expression>),
6751
Procedure(Procedure),
6852
Undefined,
6953
}
7054

71-
impl fmt::Display for LResult {
55+
impl fmt::Display for LValue {
7256
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
7357
match *self {
74-
LResult::Value(ref v) => write!(f, "{}", v),
75-
LResult::Procedure(ref p) => write!(f, "{}", p),
76-
LResult::Undefined => write!(f, "#<undefined>"),
58+
LValue::StringValue(ref s) => write!(f, "\"{}\"", s),
59+
LValue::NumericalValue(v) => write!(f, "{}", v),
60+
LValue::BooleanValue(v) => if v { write!(f, "#t") } else { write!(f, "#f") },
61+
LValue::Quoted(ref e) => write!(f, "#<expression>:{:?}", e),
62+
LValue::Procedure(ref p) => write!(f, "{}", p),
63+
LValue::Undefined => write!(f, "#<undefined>"),
7764
}
7865
}
7966
}
8067

81-
impl LResult {
82-
pub fn compare(&self, v: &LResult) -> Result<Ordering, String> {
83-
let lhs: LValue;
84-
let rhs: LValue;
68+
impl LValue {
69+
pub fn compare(&self, rhs: &LValue) -> Result<Ordering, String> {
8570
match *self {
86-
LResult::Value(ref lv) => lhs = lv.clone(),
87-
_ => return Err("Can't compare procedures or undefined results.".to_string()),
88-
}
89-
match *v {
90-
LResult::Value(ref lv) => rhs = lv.clone(),
91-
_ => return Err("Can't compare procedures or undefined results.".to_string()),
92-
}
93-
match lhs {
9471
LValue::StringValue(ref s1) => {
95-
match rhs {
72+
match *rhs {
9673
LValue::StringValue(ref s2) => Ok(s1.cmp(s2)),
9774
_ => Err("Expected string expression as the second argument.".to_string()),
9875
}
9976
}
10077
LValue::BooleanValue(b1) => {
101-
match rhs {
78+
match *rhs {
10279
LValue::BooleanValue(b2) => Ok(b1.cmp(&b2)),
10380
_ => Err("Expected boolean expression as the second argument.".to_string()),
10481
}
10582
}
10683
LValue::NumericalValue(x1) => {
107-
match rhs {
84+
match *rhs {
10885
LValue::NumericalValue(x2) => {
10986
Ok(if x1 > x2 {
11087
Ordering::Greater
@@ -118,30 +95,26 @@ impl LResult {
11895
}
11996
}
12097
LValue::Quoted(_) => Err("Can't compare quoted expressions.".to_string()),
98+
LValue::Procedure(_) => Err("Can't compare procedures.".to_string()),
99+
LValue::Undefined => Err("Can't compare #<undefined>'s.".to_string()),
121100
}
122101
}
123102

124103
pub fn to_boolean(&self) -> Result<bool, String> {
125104
match *self {
126-
LResult::Value(ref lv) => {
127-
match *lv {
128-
LValue::NumericalValue(x) => Ok(x >= 0.0),
129-
LValue::BooleanValue(b) => Ok(b),
130-
LValue::StringValue(_) => Ok(true),
131-
LValue::Quoted(_) => Ok(true),
132-
}
133-
}
105+
LValue::NumericalValue(x) => Ok(x >= 0.0),
106+
LValue::BooleanValue(b) => Ok(b),
107+
LValue::StringValue(_) => Ok(true),
108+
LValue::Quoted(_) => Ok(true),
134109
_ => Err("Can't convert procedures and #undefined's to booleans.".to_string()),
135110
}
136111
}
137112
}
138113

139114
#[derive(Debug,Clone)]
140115
pub enum Expression {
141-
Call {
142-
fun: Box<Expression>,
143-
arguments: Vec<Expression>,
144-
},
116+
List(Vec<Expression>),
117+
Eval(Box<Expression>),
145118
Definition {
146119
name: String,
147120
value: Box<Expression>,
@@ -167,6 +140,9 @@ impl Expression {
167140
let newv = (*v).clone();
168141
return Expression::process_quote(&ListNode::Node(false, newv));
169142
}
143+
if v.len() == 0 {
144+
return Ok(Expression::List(Vec::new()));
145+
}
170146
match v[0] {
171147
ListNode::Identifier(_, ref s) => {
172148
match s.as_str() {
@@ -181,10 +157,18 @@ impl Expression {
181157
Expression::process_quote(&v[1])
182158
}
183159
}
184-
_ => Expression::process_call(&v),
160+
"eval" => {
161+
if v.len() != 2 {
162+
Err("Eval expression must contain exactly one expression."
163+
.to_string())
164+
} else {
165+
Expression::process_eval(&v[1])
166+
}
167+
}
168+
_ => Expression::process_list(&v),
185169
}
186170
}
187-
_ => Expression::process_call(&v),
171+
_ => Expression::process_list(&v),
188172
}
189173
}
190174
ListNode::Identifier(quoted, ref s) => {
@@ -292,22 +276,21 @@ impl Expression {
292276
}
293277
}
294278

295-
fn process_call(params: &[ListNode]) -> Result<Expression, String> {
296-
let mut args: Vec<Expression> = Vec::new();
297-
for node in &params[1..] {
298-
match Expression::from_list(node) {
299-
Ok(e) => args.push(e),
300-
Err(s) => return Err(s),
301-
}
279+
fn process_eval(n: &ListNode) -> Result<Expression, String> {
280+
match Expression::from_list(n) {
281+
Ok(e) => Ok(Expression::Eval(Box::new(e))),
282+
Err(s) => Err(s),
302283
}
303-
match Expression::from_list(&params[0]) {
304-
Ok(e) => {
305-
Ok(Expression::Call {
306-
fun: Box::new(e),
307-
arguments: args,
308-
})
284+
}
285+
286+
fn process_list(elements: &[ListNode]) -> Result<Expression, String> {
287+
let mut children: Vec<Expression> = Vec::new();
288+
for e in elements {
289+
match Expression::from_list(e) {
290+
Ok(expr) => children.push(expr),
291+
Err(s) => return Err(s),
309292
}
310-
Err(s) => Err(s),
311293
}
294+
Ok(Expression::List(children))
312295
}
313296
}

0 commit comments

Comments
 (0)