Last active
November 1, 2020 15:39
-
-
Save yshui/f17435daea55e514067e to your computer and use it in GitHub Desktop.
GitCafe格调秀第二周第一题,Proof-of-concept。需要rust-peg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(plugin)] | |
#![feature(collections)] | |
#![feature(str_char)] | |
#![feature(convert)] | |
#![plugin(peg_syntax_ext)] | |
use std::io; | |
peg! pynum(r#" | |
digit -> i64 | |
= digit_nozero | |
/ "ling" {0i64} | |
digit_nozero -> i64 | |
= "yi" {1i64} | |
/ "er" {2i64} | |
/ "san" {3i64} | |
/ "si" {4i64} | |
/ "wu" {5i64} | |
/ "liu" {6i64} | |
/ "qi" {7i64} | |
/ "ba" {8i64} | |
/ "jiu" {9i64} | |
tens -> i64 | |
= hi:digit_nozero " shi " lo:digit {hi*10+lo} | |
/ "shi " lo:digit {lo+10} | |
/ hi:digit_nozero " shi" {hi*10} | |
/ "shi" {10i64} | |
hundreds_lo -> i64 | |
= " " lo:tens {lo} / " ling " lo:digit {lo} | |
hundreds -> i64 | |
= hi:digit_nozero " bai" lo:hundreds_lo? { | |
let f = hi*100; | |
if let Some(lop) = lo { | |
f + lop | |
} else { | |
f | |
} | |
} | |
number_positive -> i64 | |
= hundreds / tens / digit | |
#[pub] | |
number -> i64 | |
= number_positive / "fu "p: number_positive {-p} | |
"#); | |
fn main() { | |
let mut input = io::stdin(); | |
let mut line = "".to_string(); | |
if let Ok(_) = input.read_line(&mut line) { | |
let line_str = line.as_str().trim_right(); | |
if let Ok(res) = pynum::number(line_str) { | |
println!("{}", res); | |
} else { | |
println!("Parse error"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment