Skip to content

Commit b104d5e

Browse files
authored
Merge pull request #2 from Cirru/fmt
retry format; upgrade cirru-parser
2 parents ca85758 + 73fe21c commit b104d5e

File tree

11 files changed

+84
-10
lines changed

11 files changed

+84
-10
lines changed

moon.mod.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "tiye/cirru-edn",
3-
"version": "0.0.2",
3+
"version": "0.0.4",
44
"deps": {
5-
"tiye/cirru-parser": "0.0.8"
5+
"tiye/cirru-parser": "0.0.10"
66
},
77
"readme": "README.md",
88
"repository": "https://github.com/Cirru/cirru-edn.mbt",

src/lib/buffer.mbt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
///|
12
type EdnBufferView Array[UInt] derive(Eq)
23

4+
///|
35
impl Hash for EdnBufferView with hash(self) { self._.length() }
46

7+
///|
58
impl Hash for EdnBufferView with hash_combine(self, hasher) {
69
for i in self._ {
710
hasher.combine(i)
811
}
912
}
1013

14+
///|
1115
fn to_string(self : EdnBufferView) -> String {
1216
let mut s = ""
1317
s = s + "["
@@ -21,6 +25,7 @@ fn to_string(self : EdnBufferView) -> String {
2125
s
2226
}
2327

28+
///|
2429
fn compare(self : EdnBufferView, right : EdnBufferView) -> Int {
2530
for i = 0, len = self._.length(); i < len; i = i + 1 {
2631
if i >= right._.length() {

src/lib/edn.mbt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
///|
12
pub fn hello() -> String {
23
"Hello, world!"
34
}
45

6+
///|
57
pub(all) enum Edn {
68
Nil
79
Bool(Bool)
@@ -20,6 +22,7 @@ pub(all) enum Edn {
2022
// AnyRef(EdnAnyRef)
2123
} derive(Eq, Hash, Default)
2224

25+
///|
2326
pub fn to_string(self : Edn) -> String {
2427
match self {
2528
Edn::Nil => "nil".to_string()
@@ -53,12 +56,13 @@ pub fn to_string(self : Edn) -> String {
5356

5457
// TODO Hash
5558

59+
///|
5660
pub fn compare(self : Edn, right : Edn) -> Int {
5761
let ret = match (self, right) {
5862
(Edn::Nil, Edn::Nil) => 0
5963
(Edn::Nil, _) => -1
6064
(_, Edn::Nil) => 1
61-
(Edn::Bool(a), Edn::Bool(b)) => a.op_compare(b)
65+
(Edn::Bool(a), Edn::Bool(b)) => a.compare(b)
6266
(Edn::Bool(_), _) => -1
6367
(_, Edn::Bool(_)) => 1
6468
(Edn::Number(a), Edn::Number(b)) => a.compare(b)
@@ -98,24 +102,29 @@ pub fn compare(self : Edn, right : Edn) -> Int {
98102
ret
99103
}
100104

105+
///|
101106
pub fn Edn::str(s : String) -> Edn {
102107
Edn::Str(s)
103108
}
104109

110+
///|
105111
pub fn Edn::tag(s : String) -> Edn {
106112
Edn::Tag(EdnTag::new(s))
107113
}
108114

115+
///|
109116
pub fn Edn::symbol(s : String) -> Edn {
110117
Edn::Symbol(s)
111118
}
112119

120+
///|
113121
pub fn Edn::tuple(tag : Edn, extra : Array[Edn]) -> Edn {
114122
Edn::Tuple(EdnTupleView::new(tag, extra))
115123
}
116124

117125
// TODO any-ref
118126

127+
///|
119128
pub fn Edn::is_literal(self : Edn) -> Bool {
120129
match self {
121130
Edn::Nil => true
@@ -128,6 +137,7 @@ pub fn Edn::is_literal(self : Edn) -> Bool {
128137
}
129138
}
130139

140+
///|
131141
fn is_simple_char(c : Char) -> Bool {
132142
match c {
133143
'-' | '?' | '.' | '$' | ',' => true
@@ -144,6 +154,7 @@ fn is_simple_char(c : Char) -> Bool {
144154
}
145155
}
146156

157+
///|
147158
fn is_simple_token(tok : String) -> Bool {
148159
for s in tok {
149160
if not(is_simple_char(s)) {

src/lib/lib.mbt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
///|
12
pub(all) type! EdnCommonError String derive(Eq, Hash, Default)
23

4+
///|
35
pub fn to_string(self : EdnCommonError) -> String {
46
match self {
57
EdnCommonError(s) => s
68
}
79
}
810

11+
///|
912
pub impl Show for EdnCommonError with output(self, logger) {
1013
Show::output(self.to_string(), logger)
1114
}
1215

13-
/// parse Cirru code into data
16+
///| parse Cirru code into data
1417
pub fn parse(s : String) -> Edn!EdnCommonError {
1518
let xs = @cirru_parser.parse?(s).unwrap()
1619
if xs.length() == 1 {
@@ -24,6 +27,7 @@ pub fn parse(s : String) -> Edn!EdnCommonError {
2427
}
2528
}
2629

30+
///|
2731
fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
2832
match node {
2933
@cirru_parser.Cirru::Leaf(s) =>
@@ -244,13 +248,15 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
244248
}
245249
}
246250

251+
///|
247252
fn is_comment(node : @cirru_parser.Cirru) -> Bool {
248253
match node {
249254
@cirru_parser.Cirru::Leaf(_) => false
250255
@cirru_parser.Cirru::List(xs) => xs[0] == @cirru_parser.Cirru::Leaf(";")
251256
}
252257
}
253258

259+
///|
254260
fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru!EdnCommonError {
255261
match data {
256262
Edn::Nil => @cirru_parser.Cirru::Leaf("nil")
@@ -363,7 +369,7 @@ fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru!EdnCommonError {
363369
}
364370
}
365371

366-
/// generate string from Edn
372+
///| generate string from Edn
367373
pub fn format(data : Edn, use_inline : Bool) -> String!EdnCommonError {
368374
let options : @cirru_parser.CirruWriterOptions = { use_inline, }
369375
let ret = match assemble_cirru_node!(data) {

src/lib/list.mbt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
///|
12
type EdnListView Array[Edn] derive(Eq)
23

4+
///|
35
impl Hash for EdnListView with hash(self) { self._.length() }
46

7+
///|
58
impl Hash for EdnListView with hash_combine(self, hasher) {
69
for i in self._ {
710
i.hash_combine(hasher)
811
}
912
}
1013

14+
///|
1115
fn to_string(self : EdnListView) -> String {
1216
let mut s = ""
1317
s = s + "(list )"
@@ -21,6 +25,7 @@ fn to_string(self : EdnListView) -> String {
2125
s
2226
}
2327

28+
///|
2429
fn compare(self : EdnListView, right : EdnListView) -> Int {
2530
for i = 0, len = self._.length(); i < len; i = i + 1 {
2631
if i >= right._.length() {
@@ -37,10 +42,12 @@ fn compare(self : EdnListView, right : EdnListView) -> Int {
3742
0
3843
}
3944

45+
///|
4046
fn EdnListView::default() -> EdnListView {
4147
[]
4248
}
4349

50+
///|
4451
fn get(self : EdnListView, idx : UInt) -> Edn? {
4552
if idx < self._.length().reinterpret_as_uint() {
4653
Some(self._[idx.reinterpret_as_int()])
@@ -49,6 +56,7 @@ fn get(self : EdnListView, idx : UInt) -> Edn? {
4956
}
5057
}
5158

59+
///|
5260
fn get_or_nil(self : EdnListView, idx : UInt) -> Edn {
5361
if idx < self._.length().reinterpret_as_uint() {
5462
self._[idx.reinterpret_as_int()]
@@ -57,20 +65,22 @@ fn get_or_nil(self : EdnListView, idx : UInt) -> Edn {
5765
}
5866
}
5967

68+
///|
6069
fn length(self : EdnListView) -> UInt {
6170
self._.length().reinterpret_as_uint()
6271
}
6372

73+
///|
6474
fn is_empty(self : EdnListView) -> Bool {
6575
self._.length() == 0
6676
}
6777

68-
/// mutablely push an element to the end of the list
78+
///| mutablely push an element to the end of the list
6979
fn push(self : EdnListView, x : Edn) -> Unit {
7080
self._.push(x)
7181
}
7282

73-
/// implement iterator for EdnListView
83+
///| implement iterator for EdnListView
7484
fn iter(self : EdnListView) -> Iter[Edn] {
7585
Iter::new(
7686
fn(yielding) {

src/lib/map.mbt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
///|
12
type EdnMapView Map[Edn, Edn]
23

3-
/// implement Eq for EdnMapView,
4+
///| implement Eq for EdnMapView,
45
/// TODO: this is not efficient, we should implement a more efficient way to compare two maps
56
fn op_equal(self : EdnMapView, other : EdnMapView) -> Bool {
67
let mut equal = true
@@ -26,8 +27,10 @@ fn op_equal(self : EdnMapView, other : EdnMapView) -> Bool {
2627
equal
2728
}
2829

30+
///|
2931
impl Hash for EdnMapView with hash(self) { self._.size() }
3032

33+
///|
3134
impl Hash for EdnMapView with hash_combine(self, hasher) {
3235
self._.each(
3336
fn(k, v) {
@@ -37,6 +40,7 @@ impl Hash for EdnMapView with hash_combine(self, hasher) {
3740
)
3841
}
3942

43+
///|
4044
fn to_string(self : EdnMapView) -> String {
4145
let mut s = ""
4246
s = s + "(map"
@@ -47,41 +51,50 @@ fn to_string(self : EdnMapView) -> String {
4751
s
4852
}
4953

54+
///|
5055
fn tag_get(self : EdnMapView, k : Edn) -> Edn? {
5156
self._.get(k)
5257
}
5358

59+
///|
5460
fn str_get(self : EdnMapView, k : String) -> Edn? {
5561
self._.get(Edn::Str(k))
5662
}
5763

64+
///|
5865
fn get(self : EdnMapView, k : Edn) -> Edn? {
5966
self._.get(k)
6067
}
6168

69+
///|
6270
fn get_or_nil(self : EdnMapView, k : Edn) -> Edn {
6371
match self._.get(k) {
6472
Some(v) => v
6573
None => Edn::Nil
6674
}
6775
}
6876

77+
///|
6978
fn insert(self : EdnMapView, k : Edn, v : Edn) -> Unit {
7079
self._.set(k, v)
7180
}
7281

82+
///|
7383
fn insert_key_str(self : EdnMapView, k : String, v : Edn) -> Unit {
7484
self._.set(Edn::Str(k), v)
7585
}
7686

87+
///|
7788
fn length(self : EdnMapView) -> UInt {
7889
self._.size().reinterpret_as_uint()
7990
}
8091

92+
///|
8193
fn is_empty(self : EdnMapView) -> Bool {
8294
self._.is_empty()
8395
}
8496

97+
///|
8598
fn compare(self : EdnMapView, right : EdnMapView) -> Int {
8699
if self._.size() < right._.size() {
87100
return -1

src/lib/record.mbt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
///|
12
pub(all) struct EdnRecordView {
23
tag : EdnTag
34
extra : Array[(EdnTag, Edn)]
45
} derive(Eq)
56

7+
///|
68
impl Hash for EdnRecordView with hash(self) { self.extra.length() + 1 }
79

10+
///|
811
impl Hash for EdnRecordView with hash_combine(self, hasher) {
912
self.tag.hash_combine(hasher)
1013
for i in self.extra {
@@ -13,6 +16,7 @@ impl Hash for EdnRecordView with hash_combine(self, hasher) {
1316
}
1417
}
1518

19+
///|
1620
pub fn to_string(self : EdnRecordView) -> String {
1721
let mut s = ""
1822
s = s + "(record"
@@ -24,13 +28,15 @@ pub fn to_string(self : EdnRecordView) -> String {
2428
s
2529
}
2630

31+
///|
2732
fn EdnRecordView::new(
2833
tag : EdnTag,
2934
extra : Array[(EdnTag, Edn)]
3035
) -> EdnRecordView {
3136
{ tag, extra }
3237
}
3338

39+
///|
3440
fn has_key(self : EdnRecordView, key : EdnTag) -> Bool {
3541
for i = 0, len = self.extra.length(); i < len; i = i + 1 {
3642
if self.extra[i].0 == key {
@@ -40,11 +46,12 @@ fn has_key(self : EdnRecordView, key : EdnTag) -> Bool {
4046
false
4147
}
4248

43-
/// order not guaranteed yet, better be sorted before used by runtime
49+
///| order not guaranteed yet, better be sorted before used by runtime
4450
fn insert(self : EdnRecordView, key : EdnTag, value : Edn) -> Unit {
4551
self.extra.push((key, value))
4652
}
4753

54+
///|
4855
fn compare(self : EdnRecordView, right : EdnRecordView) -> Int {
4956
let len = self.extra.length()
5057
let right_len = right.extra.length()

0 commit comments

Comments
 (0)