Skip to content

Commit

Permalink
refactor some syntax; gain little perf via Rc; tag 0.1.27
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Jan 24, 2024
1 parent eb59cce commit 2749309
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ wasm-example/node_modules/
wasm-example/pkg/

wasm-example/dist/

/profile.json
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cirru_parser"
version = "0.1.26"
version = "0.1.27"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
5 changes: 3 additions & 2 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ defn fib (n)
"#,
);

let large_demo = "/Users/chenyong/repo/calcit-lang/editor/compact.cirru";
let large_demo = "/Users/chenyong/repo/calcit-lang/editor/calcit.cirru";
// let large_demo = "/Users/chen/repo/calcit-lang/respo-calcit-workflow/js-out/program-ir.cirru";
// let large_demo = "/Users/chen/repo/calcit-lang/calcit_runner.rs/js-out/program-ir.cirru";
let content = fs::read_to_string(large_demo).unwrap();

match parse(&content) {
Ok(v) => {
let writer_options = CirruWriterOptions { use_inline: false };
println!("{}", format(&v, writer_options).unwrap());
let t = format(&v, writer_options).unwrap();
println!("{}", t.len());
}
Err(e) => println!("{:?}", e),
}
Expand Down
82 changes: 40 additions & 42 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,47 +57,45 @@ fn build_exprs(tokens: &[CirruLexItem]) -> Result<Vec<Cirru>, String> {
loop {
let chunk = pull_token();

if chunk.is_none() {
return Ok(acc);
}
match chunk.unwrap() {
CirruLexItem::Open => {
let mut pointer: Vec<Cirru> = vec![];
// guess a nested level of 16
let mut pointer_stack: Vec<Vec<Cirru>> = Vec::with_capacity(16);
loop {
let cursor = pull_token();
if cursor.is_none() {
return Err(String::from("unexpected end of file"));
}
match cursor.unwrap() {
CirruLexItem::Close => {
if pointer_stack.is_empty() {
acc.push(Cirru::List(pointer.to_owned()));
break;
} else {
let v = pointer_stack.pop();
let prev_p = pointer;
match v {
Some(collected) => {
pointer = collected;
pointer.push(Cirru::List(prev_p))
match &chunk {
None => return Ok(acc),
Some(ck) => {
match ck {
CirruLexItem::Open => {
let mut pointer: Vec<Cirru> = vec![];
// guess a nested level of 16
let mut pointer_stack: Vec<Vec<Cirru>> = Vec::with_capacity(16);
loop {
let cursor = pull_token();

match &cursor {
None => return Err(String::from("unexpected end of file")),
Some(c) => match c {
CirruLexItem::Close => match pointer_stack.pop() {
None => {
acc.push(Cirru::List(pointer));
break;
}
Some(v) => {
let prev_p = pointer;
pointer = v;
pointer.push(Cirru::List(prev_p));
}
},
CirruLexItem::Open => {
pointer_stack.push(pointer);
pointer = vec![];
}
None => return Err(String::from("unknown close item")),
}
CirruLexItem::Str(s) => pointer.push(Cirru::Leaf((**s).into())),
CirruLexItem::Indent(n) => return Err(format!("unknown indent: {}", n)),
},
}
}
CirruLexItem::Open => {
pointer_stack.push(pointer);
pointer = vec![];
}
CirruLexItem::Str(s) => pointer.push(Cirru::Leaf(s.as_str().into())),
CirruLexItem::Indent(n) => return Err(format!("unknown indent: {}", n)),
}
CirruLexItem::Close => return Err(String::from("unexpected \")\"")),
a => return Err(format!("unknown item: {:?}", a)),
}
}
CirruLexItem::Close => return Err(String::from("unexpected \")\"")),
a => return Err(format!("unknown item: {:?}", a)),
}
}
}
Expand Down Expand Up @@ -151,28 +149,28 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
},
CirruLexState::Token => match c {
' ' => {
acc.push(CirruLexItem::Str(buffer));
acc.push(CirruLexItem::Str(buffer.into()));
state = CirruLexState::Space;
buffer = String::from("");
}
'"' => {
acc.push(CirruLexItem::Str(buffer));
acc.push(CirruLexItem::Str(buffer.into()));
state = CirruLexState::Str;
buffer = String::from("");
}
'\n' => {
acc.push(CirruLexItem::Str(buffer));
acc.push(CirruLexItem::Str(buffer.into()));
state = CirruLexState::Indent;
buffer = String::from("");
}
'(' => {
acc.push(CirruLexItem::Str(buffer));
acc.push(CirruLexItem::Str(buffer.into()));
acc.push(CirruLexItem::Open);
state = CirruLexState::Space;
buffer = String::from("")
}
')' => {
acc.push(CirruLexItem::Str(buffer));
acc.push(CirruLexItem::Str(buffer.into()));
acc.push(CirruLexItem::Close);
state = CirruLexState::Space;
buffer = String::from("")
Expand All @@ -184,7 +182,7 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
},
CirruLexState::Str => match c {
'"' => {
acc.push(CirruLexItem::Str(buffer));
acc.push(CirruLexItem::Str(buffer.into()));
state = CirruLexState::Space;
buffer = String::from("");
}
Expand Down Expand Up @@ -270,7 +268,7 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
match state {
CirruLexState::Space => Ok(acc),
CirruLexState::Token => {
acc.push(CirruLexItem::Str(buffer));
acc.push(CirruLexItem::Str(buffer.into()));
Ok(acc)
}
CirruLexState::Escape => Err(String::from("unknown escape")),
Expand Down
4 changes: 2 additions & 2 deletions src/primes.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bincode::{Decode, Encode};
use std::clone::Clone;
use std::fmt;
use std::hash::Hash;
use std::str;
use std::sync::Arc;
use std::{clone::Clone, rc::Rc};

#[cfg(feature = "use-serde")]
use serde::{
Expand Down Expand Up @@ -206,7 +206,7 @@ pub enum CirruLexItem {
Close,
// supposed to be enough with indentation of 255
Indent(u8),
Str(String),
Str(Rc<str>),
}

impl From<&str> for CirruLexItem {
Expand Down

0 comments on commit 2749309

Please sign in to comment.