Skip to content

Commit 8513635

Browse files
committed
rustell wip
1 parent 8657049 commit 8513635

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

rust/rustell/rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
edition = "2024"
2+
max_width = 88

rust/rustell/src/main.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
use chumsky::prelude::*;
2+
use std::io::{self, Read};
3+
4+
#[derive(Clone, Debug)]
5+
pub enum Stmt {
6+
Expr,
7+
Loop(Vec<Stmt>),
8+
}
9+
10+
fn parser<'a>() -> impl Parser<'a, &'a str, Vec<Stmt>> {
11+
let expr = just("expr"); // TODO
12+
13+
let block = recursive(|block| {
14+
let indent = just(' ')
15+
.repeated()
16+
.configure(|cfg, parent_indent| cfg.exactly(*parent_indent));
17+
18+
let expr_stmt = expr.then_ignore(text::newline()).to(Stmt::Expr);
19+
let control_flow = just("loop:")
20+
.then(text::newline())
21+
.ignore_then(block)
22+
.map(Stmt::Loop);
23+
let stmt = expr_stmt.or(control_flow);
24+
25+
text::whitespace()
26+
.count()
27+
.ignore_with_ctx(stmt.separated_by(indent).collect())
28+
});
29+
30+
block.with_ctx(0)
31+
}
32+
133
fn main() {
2-
println!("Hello, world!");
34+
let mut src = String::new();
35+
io::stdin()
36+
.read_to_string(&mut src)
37+
.expect("Failed to read stdin");
38+
match parser().parse(src.trim()).into_result() {
39+
Ok(ast) => println!("{:#?}", ast),
40+
Err(errs) => errs.into_iter().for_each(|e| println!("{e:?}")),
41+
};
342
}

0 commit comments

Comments
 (0)