为了提高 V2EX 的有效信息质量,在发布交易信息时,除了遵守安全提示中的说明外,也请注意下面的规则:
拼车信息请发到 /go/cosub 节点。
如果没有发送到 /go/cosub,那么会被移动到 /go/pointless。如果持续触发这样的移动,会导致账号被禁用。
需要实现两个类中的方法 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
1
wjx0912 2023-08-04 09:03:38 +08:00
好奇的问下付款方式,先给钱还是先给源码
|
3
hanssx 2023-08-04 09:52:23 +08:00 via iPhone
话说有个 ruff 插件,是不是里面有 ast 的生成?
|