Skip to content

Commit e1bc631

Browse files
authored
Merge pull request #3 from Cirru/add-atom
Add atom syntax
2 parents b104d5e + 10a4e5c commit e1bc631

File tree

6 files changed

+173
-171
lines changed

6 files changed

+173
-171
lines changed

.github/workflows/check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
2828
- name: moon test
2929
run: |
30-
# moon test --target js
30+
moon test --target js
3131
moon run src/main --target js
3232
3333
# - name: moon bundle

moon.mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tiye/cirru-edn",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"deps": {
55
"tiye/cirru-parser": "0.0.10"
66
},

src/lib/edn.mbt

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ pub(all) enum Edn {
2020
Buffer(EdnBufferView)
2121
/// reference to Rust data, not interpretable in Calcit
2222
// AnyRef(EdnAnyRef)
23+
Atom(Edn)
2324
} derive(Eq, Hash, Default)
2425

2526
///|
2627
pub fn to_string(self : Edn) -> String {
2728
match self {
28-
Edn::Nil => "nil".to_string()
29-
Edn::Tag(t) => t.to_string()
30-
Edn::Bool(b) => if b { "true" } else { "false" }
31-
Edn::Number(n) => n.to_string()
32-
Edn::Symbol(s) => s
33-
Edn::Str(s) =>
29+
Nil => "nil".to_string()
30+
Tag(t) => t.to_string()
31+
Bool(b) => if b { "true" } else { "false" }
32+
Number(n) => n.to_string()
33+
Symbol(s) => s
34+
Str(s) =>
3435
if is_simple_token(s) {
3536
s
3637
} else {
@@ -44,60 +45,69 @@ pub fn to_string(self : Edn) -> String {
4445
}
4546
ret
4647
}
47-
Edn::Quote(e) => e.to_string()
48-
Edn::Buffer(b) => b.to_string()
49-
Edn::Tuple(t) => t.to_string()
50-
Edn::List(l) => l.to_string()
51-
Edn::Set(s) => s.to_string()
52-
Edn::Map(m) => m.to_string()
53-
Edn::Record(r) => r.to_string()
48+
Quote(e) => e.to_string()
49+
Buffer(b) => b.to_string()
50+
Tuple(t) => t.to_string()
51+
List(l) => l.to_string()
52+
Set(s) => s.to_string()
53+
Map(m) => m.to_string()
54+
Record(r) => r.to_string()
55+
Atom(a) => "atom " + a.to_string()
5456
}
5557
}
5658

59+
///|
60+
pub impl Show for Edn with output(self, logger) -> Unit {
61+
logger.write_string(self.to_string())
62+
}
63+
5764
// TODO Hash
5865

5966
///|
6067
pub fn compare(self : Edn, right : Edn) -> Int {
6168
let ret = match (self, right) {
62-
(Edn::Nil, Edn::Nil) => 0
63-
(Edn::Nil, _) => -1
64-
(_, Edn::Nil) => 1
65-
(Edn::Bool(a), Edn::Bool(b)) => a.compare(b)
66-
(Edn::Bool(_), _) => -1
67-
(_, Edn::Bool(_)) => 1
68-
(Edn::Number(a), Edn::Number(b)) => a.compare(b)
69-
(Edn::Number(_), _) => -1
70-
(_, Edn::Number(_)) => 1
71-
(Edn::Symbol(a), Edn::Symbol(b)) => a.compare(b)
72-
(Edn::Symbol(_), _) => -1
73-
(_, Edn::Symbol(_)) => 1
74-
(Edn::Tag(a), Edn::Tag(b)) => a.compare(b)
75-
(Edn::Tag(_), _) => -1
76-
(_, Edn::Tag(_)) => 1
77-
(Edn::Str(a), Edn::Str(b)) => a.compare(b)
78-
(Edn::Str(_), _) => -1
79-
(_, Edn::Str(_)) => 1
80-
(Edn::Quote(a), Edn::Quote(b)) => a.compare(b)
81-
(Edn::Quote(_), _) => -1
82-
(_, Edn::Quote(_)) => 1
83-
(Edn::Tuple(a), Edn::Tuple(b)) => a.compare(b)
84-
(Edn::Tuple(_), _) => -1
85-
(_, Edn::Tuple(_)) => 1
86-
(Edn::List(a), Edn::List(b)) => a.compare(b)
87-
(Edn::List(_), _) => -1
88-
(_, Edn::List(_)) => 1
89-
(Edn::Buffer(a), Edn::Buffer(b)) => a.compare(b)
90-
(Edn::Buffer(_), _) => -1
91-
(_, Edn::Buffer(_)) => 1
92-
(Edn::Set(a), Edn::Set(b)) => a.compare(b)
93-
(Edn::Set(_), _) => -1
94-
(_, Edn::Set(_)) => 1
95-
(Edn::Map(a), Edn::Map(b)) => a.compare(b)
96-
(Edn::Map(_), _) => -1
97-
(_, Edn::Map(_)) => 1
98-
(Edn::Record(a), Edn::Record(b)) => a.compare(b)
99-
// (Edn::Record(_), _) => -1
100-
// (_, Edn::Record(_)) => 1
69+
(Nil, Nil) => 0
70+
(Nil, _) => -1
71+
(_, Nil) => 1
72+
(Bool(a), Bool(b)) => a.compare(b)
73+
(Bool(_), _) => -1
74+
(_, Bool(_)) => 1
75+
(Number(a), Number(b)) => a.compare(b)
76+
(Number(_), _) => -1
77+
(_, Number(_)) => 1
78+
(Symbol(a), Symbol(b)) => a.compare(b)
79+
(Symbol(_), _) => -1
80+
(_, Symbol(_)) => 1
81+
(Tag(a), Tag(b)) => a.compare(b)
82+
(Tag(_), _) => -1
83+
(_, Tag(_)) => 1
84+
(Str(a), Str(b)) => a.compare(b)
85+
(Str(_), _) => -1
86+
(_, Str(_)) => 1
87+
(Quote(a), Quote(b)) => a.compare(b)
88+
(Quote(_), _) => -1
89+
(_, Quote(_)) => 1
90+
(Tuple(a), Tuple(b)) => a.compare(b)
91+
(Tuple(_), _) => -1
92+
(_, Tuple(_)) => 1
93+
(List(a), List(b)) => a.compare(b)
94+
(List(_), _) => -1
95+
(_, List(_)) => 1
96+
(Buffer(a), Buffer(b)) => a.compare(b)
97+
(Buffer(_), _) => -1
98+
(_, Buffer(_)) => 1
99+
(Set(a), Set(b)) => a.compare(b)
100+
(Set(_), _) => -1
101+
(_, Set(_)) => 1
102+
(Map(a), Map(b)) => a.compare(b)
103+
(Map(_), _) => -1
104+
(_, Map(_)) => 1
105+
(Atom(a), Atom(b)) => a.compare(b)
106+
(Atom(_), _) => -1
107+
(_, Atom(_)) => 1
108+
(Record(a), Record(b)) => a.compare(b)
109+
// (Record(_), _) => -1
110+
// (_, Record(_)) => 1
101111
}
102112
ret
103113
}
@@ -127,12 +137,12 @@ pub fn Edn::tuple(tag : Edn, extra : Array[Edn]) -> Edn {
127137
///|
128138
pub fn Edn::is_literal(self : Edn) -> Bool {
129139
match self {
130-
Edn::Nil => true
131-
Edn::Bool(_) => true
132-
Edn::Number(_) => true
133-
Edn::Symbol(_) => true
134-
Edn::Str(_) => true
135-
Edn::Tag(_) => true
140+
Nil => true
141+
Bool(_) => true
142+
Number(_) => true
143+
Symbol(_) => true
144+
Str(_) => true
145+
Tag(_) => true
136146
_ => false
137147
}
138148
}

src/lib/edn_test.mbt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ test "hello" {
33
fail!("@lib.hello() != \"Hello, world!\"")
44
}
55
}
6+
7+
test "atom inside edn" {
8+
let edn = @lib.Edn::Atom(Number(1.0))
9+
assert_eq!(@lib.format!(edn, true).trim("\n"), "atom 1")
10+
let parsed = @lib.parse!("atom 1")
11+
assert_eq!(parsed, @lib.Atom(Number(1.0)))
12+
}

0 commit comments

Comments
 (0)