Skip to content

Commit

Permalink
Merge pull request #3 from Cirru/add-atom
Browse files Browse the repository at this point in the history
Add atom syntax
  • Loading branch information
NoEgAm authored Jan 10, 2025
2 parents b104d5e + 10a4e5c commit e1bc631
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 171 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: moon test
run: |
# moon test --target js
moon test --target js
moon run src/main --target js
# - name: moon bundle
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-edn",
"version": "0.0.4",
"version": "0.0.5",
"deps": {
"tiye/cirru-parser": "0.0.10"
},
Expand Down
126 changes: 68 additions & 58 deletions src/lib/edn.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ pub(all) enum Edn {
Buffer(EdnBufferView)
/// reference to Rust data, not interpretable in Calcit
// AnyRef(EdnAnyRef)
Atom(Edn)
} derive(Eq, Hash, Default)

///|
pub fn to_string(self : Edn) -> String {
match self {
Edn::Nil => "nil".to_string()
Edn::Tag(t) => t.to_string()
Edn::Bool(b) => if b { "true" } else { "false" }
Edn::Number(n) => n.to_string()
Edn::Symbol(s) => s
Edn::Str(s) =>
Nil => "nil".to_string()
Tag(t) => t.to_string()
Bool(b) => if b { "true" } else { "false" }
Number(n) => n.to_string()
Symbol(s) => s
Str(s) =>
if is_simple_token(s) {
s
} else {
Expand All @@ -44,60 +45,69 @@ pub fn to_string(self : Edn) -> String {
}
ret
}
Edn::Quote(e) => e.to_string()
Edn::Buffer(b) => b.to_string()
Edn::Tuple(t) => t.to_string()
Edn::List(l) => l.to_string()
Edn::Set(s) => s.to_string()
Edn::Map(m) => m.to_string()
Edn::Record(r) => r.to_string()
Quote(e) => e.to_string()
Buffer(b) => b.to_string()
Tuple(t) => t.to_string()
List(l) => l.to_string()
Set(s) => s.to_string()
Map(m) => m.to_string()
Record(r) => r.to_string()
Atom(a) => "atom " + a.to_string()
}
}

///|
pub impl Show for Edn with output(self, logger) -> Unit {
logger.write_string(self.to_string())
}

// TODO Hash

///|
pub fn compare(self : Edn, right : Edn) -> Int {
let ret = match (self, right) {
(Edn::Nil, Edn::Nil) => 0
(Edn::Nil, _) => -1
(_, Edn::Nil) => 1
(Edn::Bool(a), Edn::Bool(b)) => a.compare(b)
(Edn::Bool(_), _) => -1
(_, Edn::Bool(_)) => 1
(Edn::Number(a), Edn::Number(b)) => a.compare(b)
(Edn::Number(_), _) => -1
(_, Edn::Number(_)) => 1
(Edn::Symbol(a), Edn::Symbol(b)) => a.compare(b)
(Edn::Symbol(_), _) => -1
(_, Edn::Symbol(_)) => 1
(Edn::Tag(a), Edn::Tag(b)) => a.compare(b)
(Edn::Tag(_), _) => -1
(_, Edn::Tag(_)) => 1
(Edn::Str(a), Edn::Str(b)) => a.compare(b)
(Edn::Str(_), _) => -1
(_, Edn::Str(_)) => 1
(Edn::Quote(a), Edn::Quote(b)) => a.compare(b)
(Edn::Quote(_), _) => -1
(_, Edn::Quote(_)) => 1
(Edn::Tuple(a), Edn::Tuple(b)) => a.compare(b)
(Edn::Tuple(_), _) => -1
(_, Edn::Tuple(_)) => 1
(Edn::List(a), Edn::List(b)) => a.compare(b)
(Edn::List(_), _) => -1
(_, Edn::List(_)) => 1
(Edn::Buffer(a), Edn::Buffer(b)) => a.compare(b)
(Edn::Buffer(_), _) => -1
(_, Edn::Buffer(_)) => 1
(Edn::Set(a), Edn::Set(b)) => a.compare(b)
(Edn::Set(_), _) => -1
(_, Edn::Set(_)) => 1
(Edn::Map(a), Edn::Map(b)) => a.compare(b)
(Edn::Map(_), _) => -1
(_, Edn::Map(_)) => 1
(Edn::Record(a), Edn::Record(b)) => a.compare(b)
// (Edn::Record(_), _) => -1
// (_, Edn::Record(_)) => 1
(Nil, Nil) => 0
(Nil, _) => -1
(_, Nil) => 1
(Bool(a), Bool(b)) => a.compare(b)
(Bool(_), _) => -1
(_, Bool(_)) => 1
(Number(a), Number(b)) => a.compare(b)
(Number(_), _) => -1
(_, Number(_)) => 1
(Symbol(a), Symbol(b)) => a.compare(b)
(Symbol(_), _) => -1
(_, Symbol(_)) => 1
(Tag(a), Tag(b)) => a.compare(b)
(Tag(_), _) => -1
(_, Tag(_)) => 1
(Str(a), Str(b)) => a.compare(b)
(Str(_), _) => -1
(_, Str(_)) => 1
(Quote(a), Quote(b)) => a.compare(b)
(Quote(_), _) => -1
(_, Quote(_)) => 1
(Tuple(a), Tuple(b)) => a.compare(b)
(Tuple(_), _) => -1
(_, Tuple(_)) => 1
(List(a), List(b)) => a.compare(b)
(List(_), _) => -1
(_, List(_)) => 1
(Buffer(a), Buffer(b)) => a.compare(b)
(Buffer(_), _) => -1
(_, Buffer(_)) => 1
(Set(a), Set(b)) => a.compare(b)
(Set(_), _) => -1
(_, Set(_)) => 1
(Map(a), Map(b)) => a.compare(b)
(Map(_), _) => -1
(_, Map(_)) => 1
(Atom(a), Atom(b)) => a.compare(b)
(Atom(_), _) => -1
(_, Atom(_)) => 1
(Record(a), Record(b)) => a.compare(b)
// (Record(_), _) => -1
// (_, Record(_)) => 1
}
ret
}
Expand Down Expand Up @@ -127,12 +137,12 @@ pub fn Edn::tuple(tag : Edn, extra : Array[Edn]) -> Edn {
///|
pub fn Edn::is_literal(self : Edn) -> Bool {
match self {
Edn::Nil => true
Edn::Bool(_) => true
Edn::Number(_) => true
Edn::Symbol(_) => true
Edn::Str(_) => true
Edn::Tag(_) => true
Nil => true
Bool(_) => true
Number(_) => true
Symbol(_) => true
Str(_) => true
Tag(_) => true
_ => false
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/lib/edn_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ test "hello" {
fail!("@lib.hello() != \"Hello, world!\"")
}
}

test "atom inside edn" {
let edn = @lib.Edn::Atom(Number(1.0))
assert_eq!(@lib.format!(edn, true).trim("\n"), "atom 1")
let parsed = @lib.parse!("atom 1")
assert_eq!(parsed, @lib.Atom(Number(1.0)))
}
Loading

0 comments on commit e1bc631

Please sign in to comment.