有偿实现 rust 实现简单的语法树

2023-08-04 08:30:57 +08:00
 beckyho

需要实现两个类中的方法 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

917 次点击
所在节点    二手交易
3 条回复
wjx0912
2023-08-04 09:03:38 +08:00
好奇的问下付款方式,先给钱还是先给源码
beckyho
2023-08-04 09:13:09 +08:00
@wjx0912 付款走咸鱼,你先远程操作我看下实现的功能,没问题的话走咸鱼交易
hanssx
2023-08-04 09:52:23 +08:00
话说有个 ruff 插件,是不是里面有 ast 的生成?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/962268

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX