Skip to content

Commit 8f088de

Browse files
committed
Add rust-gc-sample
1 parent 06ab7ec commit 8f088de

File tree

6 files changed

+214
-37
lines changed

6 files changed

+214
-37
lines changed

rust-calc/Cargo.lock

+40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust-calc/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
anyhow = "1.0.52"
9+
regex = "1"
10+
once_cell = "1"
11+
anyhow = "1"

rust-calc/src/main.rs

+11-36
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
mod parse;
2-
mod tokenize;
1+
mod parsers;
2+
mod json;
33

4-
use crate::parse::parse;
5-
use crate::tokenize::tokenize;
64
use anyhow::Result;
75
use std::io::{self, Write};
86

97
fn print_prompt() -> io::Result<()> {
10-
print!("expr> ");
8+
print!("json> ");
119
io::stdout().flush()?;
1210
Ok(())
1311
}
@@ -20,38 +18,15 @@ fn read_line() -> io::Result<Option<String>> {
2018
}
2119
}
2220

23-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
24-
pub enum Token {
25-
Eof,
26-
Integer(i64),
27-
Plus,
28-
Minus,
29-
Asterisk,
30-
Slash,
31-
LeftParen,
32-
RightParen,
33-
}
34-
35-
impl Default for Token {
36-
fn default() -> Self {
37-
Token::Eof
38-
}
39-
}
40-
41-
#[derive(Debug, Clone, PartialEq, Eq)]
42-
pub enum Node {
43-
Integer(i64),
44-
Add(Box<Node>, Box<Node>),
45-
Sub(Box<Node>, Box<Node>),
46-
Mul(Box<Node>, Box<Node>),
47-
Div(Box<Node>, Box<Node>),
48-
}
49-
5021
fn do_something(line: &str) -> Result<()> {
51-
let tokens = tokenize(&line)?;
52-
println!("Tokens: {:?}", tokens);
53-
let node = parse(tokens.into_iter())?;
54-
println!("AST: {:?}", node);
22+
match crate::json::parse(line) {
23+
Some(value) => {
24+
print!("{:?}\n\n", value);
25+
}
26+
None => {
27+
eprintln!("parse error");
28+
}
29+
}
5530
Ok(())
5631
}
5732

rust-gc-sample/Cargo.lock

+78
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust-gc-sample/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "rust-gc-sample"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
gc = { version = "0.4", features = ["derive"] }

rust-gc-sample/src/main.rs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use gc::{Finalize, Gc, GcCell, Trace};
2+
3+
#[derive(Trace, Finalize)]
4+
struct Node {
5+
prev: GcCell<Option<Gc<Node>>>,
6+
next: GcCell<Option<Gc<Node>>>,
7+
value: String,
8+
}
9+
10+
#[derive(Trace, Finalize)]
11+
struct DoublyLinkedList {
12+
head: Option<Gc<Node>>,
13+
tail: Option<Gc<Node>>,
14+
}
15+
16+
impl DoublyLinkedList {
17+
fn new() -> Self {
18+
Self { head: None, tail: None }
19+
}
20+
21+
fn push(&mut self, value: String) {
22+
match &self.tail {
23+
None => {
24+
let node = Gc::new(Node {
25+
prev: GcCell::new(None),
26+
next: GcCell::new(None),
27+
value,
28+
});
29+
self.head = Some(node.clone());
30+
self.tail = Some(node);
31+
}
32+
Some(tail) => {
33+
let node = Gc::new(Node {
34+
prev: GcCell::new(Some(tail.clone())),
35+
next: GcCell::new(None),
36+
value,
37+
});
38+
*tail.next.borrow_mut() = Some(node.clone());
39+
self.tail = Some(node);
40+
}
41+
}
42+
}
43+
44+
fn iterate_forward(&self, mut f: impl FnMut(&str)) {
45+
let mut p = self.head.clone();
46+
while let Some(node) = p {
47+
f(&node.value);
48+
p = node.next.borrow().clone();
49+
}
50+
}
51+
52+
fn iterate_backward(&self, mut f: impl FnMut(&str)) {
53+
let mut p = self.tail.clone();
54+
while let Some(node) = p {
55+
f(&node.value);
56+
p = node.prev.borrow().clone();
57+
}
58+
}
59+
}
60+
61+
fn main() {
62+
let mut list = DoublyLinkedList::new();
63+
64+
for value in &vec!["foo", "bar", "fizz", "buzz"] {
65+
list.push(value.to_string());
66+
}
67+
68+
println!("Iterate forward:");
69+
list.iterate_forward(|value| println!(" {}", value));
70+
71+
println!("Iterate backward:");
72+
list.iterate_backward(|value| println!(" {}", value));
73+
}

0 commit comments

Comments
 (0)