Skip to content

Commit

Permalink
get compare on Cirru; tag 0.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Sep 21, 2024
1 parent ed9b0fe commit 3a41a99
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
20 changes: 20 additions & 0 deletions lib/primes.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ pub fn to_string(self : Cirru) -> String {
}
}

pub fn compare(self : Cirru, other : Cirru) -> Int {
match (self, other) {
(Cirru::Leaf(a), Cirru::Leaf(b)) => a.compare(b)
(Cirru::List(xs), Cirru::List(ys)) => {
let size = @math.minimum(xs.length(), ys.length())
for i = 0; i < size; i = i + 1 {
let x = xs[i]
let y = ys[i]
let c = x.compare(y)
if c != 0 {
return c
}
}
return xs.length().compare(ys.length())
}
(Cirru::Leaf(_), Cirru::List(_)) => -1
(Cirru::List(_), Cirru::Leaf(_)) => 1
}
}

pub fn debug_write(self : Cirru, buffer : Buffer) -> Unit {
match self {
Cirru::Leaf(s) => buffer.write_string(s)
Expand Down
6 changes: 3 additions & 3 deletions lib/s_expr.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ fn format_lispy_expr(self : Cirru, indent : Int) -> String!FormatError {
}

// chunk = format!("{}{}", chunk.trim_end(), gen_newline(indent));
chunk = "\{chunk.trim_right(emptySpace)}\{gen_newline(indent)}"
chunk = "\{chunk.trim_end(emptySpace)}\{gen_newline(indent)}"
chunk
} else {
let mut chunk = "("
for idx, x in xs {
if x.is_nested() {
chunk = "\{chunk.trim_right(emptySpace)}\{gen_newline(indent + 1)}"
chunk = "\{chunk.trim_end(emptySpace)}\{gen_newline(indent + 1)}"
}
let next = format_lispy_expr!(x, indent + 1)
if next.starts_with("\n") {
chunk = "\{chunk.trim_right(emptySpace)}\{next}"
chunk = "\{chunk.trim_end(emptySpace)}\{next}"
} else {
chunk = "\{chunk}\{next}"
}
Expand Down
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiye/cirru-parser",
"version": "0.0.5",
"version": "0.0.6",
"deps": {},
"readme": "README.md",
"repository": "",
Expand Down

0 comments on commit 3a41a99

Please sign in to comment.