需要实现两个类中的方法 expression.rs 类
#[derive(PartialEq)] pub enum Expression{}
impl Display for Expression{ fn fmt(&self, f : &mut std::fmt::Formatter<'_>) -> Result<(), Error>{ unimplemented!() } }
impl Expression {
pub fn eval(&self) -> Result<Expression, String>{
unimplemented!()
}
}
parse.rs 类 pub struct Parser {}
impl Parser { pub fn new(program : &str) -> Self { unimplemented!() }
pub fn parse(&mut self) -> Result<Expression, String>{
unimplemented!()
}
}
测试用例: #[test] fn parse_var(){ let mut prog = Parser::new(&"x"); let result = prog.parse(); assert!(result.is_ok()); let e = result.unwrap(); assert_eq!("x", format!("{}", e)); }
#[test]
fn parse_int(){
let mut prog = Parser::new(&"123");
let result = prog.parse();
assert!(result.is_ok());
let e = result.unwrap();
assert_eq!("123", format!("{}", e));
}
#[test]
fn parse_bool(){
let mut prog = Parser::new(&"T");
let result = prog.parse();
assert!(result.is_ok());
let e = result.unwrap();
assert_eq!("T", format!("{}", e));
}
#[test]
fn parse_plus(){
let mut prog = Parser::new(&"+(1, 1)");
let result = prog.parse();
assert!(result.is_ok());
let e = result.unwrap();
assert_eq!("1 + 1", format!("{}", e));
}
#[test]
fn parse_minus(){
let mut prog = Parser::new(&"-(1, 1)");
let result = prog.parse();
assert!(result.is_ok());
let e = result.unwrap();
assert_eq!("1 - 1", format!("{}", e));
}
#[test]
fn parse_mult(){
let mut prog = Parser::new(&"*(1, 1)");
let result = prog.parse();
assert!(result.is_ok());
let e = result.unwrap();
assert_eq!("1 * 1", format!("{}", e));
}
#[test]
fn parse_div(){
let mut prog = Parser::new(&"/(1, 1)");
let result = prog.parse();
assert!(result.is_ok());
let e = result.unwrap();
assert_eq!("1 / 1", format!("{}", e));
}
#[test]
fn parse_lt(){
let mut prog = Parser::new(&"<(1, 1)");
let result = prog.parse();
assert!(result.is_ok());
let e = result.unwrap();
assert_eq!("1 < 1", format!("{}", e));
}
#[test]
fn parse_not(){
let mut prog = Parser::new(&"!T");
let result = prog.parse();
assert!(result.is_ok());
let e = result.unwrap();
assert_eq!("!T", format!("{}", e));
}
#[test]
fn parse_eq(){
let mut prog = Parser::new(&"=(1, 1)");
let result = prog.parse();
assert!(result.is_ok());
let e = result.unwrap();
assert_eq!("1 = 1", format!("{}", e));
}
#[test]
fn parse_func(){
let mut prog = Parser::new(&"func x => T");
let result = prog.parse();
assert!(result.is_ok());
let e = result.unwrap();
assert_eq!("func x => T", format!("{}", e));
}
#[test]
fn parse_app(){
let mut prog = Parser::new(&"apply(func x => x, 1)");
let result = prog.parse();
assert!(result.is_ok());
let e = result.unwrap();
assert_eq!("func x => x (1)", format!("{}", e));
}
#[test]
fn parse_if(){
let mut prog = Parser::new(&"if <(1, 5) then 8 else 9");
let result = prog.parse();
assert!(result.is_ok());
let e = result.unwrap();
assert_eq!("if 1 < 5 then 8 else 9", format!("{}", e));
}
要求,通过测试用例即可,最好今天能完成。有意联系 @abcdef_d
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.