Skip to content

Commit

Permalink
update moonbit; tag 0.0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Dec 4, 2024
1 parent f8d2624 commit e26b0f9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
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.8",
"version": "0.0.9",
"deps": {},
"readme": "README.md",
"repository": "https://github.com/Cirru/parser.mbt",
Expand Down
37 changes: 19 additions & 18 deletions src/lib/primes.mbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/// Cirru uses nested Vecters and Strings as data structure
///| Cirru uses nested Vecters and Strings as data structure
pub(all) enum Cirru {
/// Leaf node, with a string
Leaf(String)
/// List node, with a list of children
List(Array[Cirru])
} derive(Eq)

///|
pub impl Hash for Cirru with hash(self) {
let mut i = 0
match self {
Expand All @@ -18,6 +19,7 @@ pub impl Hash for Cirru with hash(self) {
i
}

///|
pub impl Hash for Cirru with hash_combine(self, hasher) {
match self {
Cirru::Leaf(s) => hasher.combine_string(s)
Expand All @@ -28,6 +30,7 @@ pub impl Hash for Cirru with hash_combine(self, hasher) {
}
}

///|
pub fn to_json(self : Cirru) -> Json {
match self {
Cirru::Leaf(s) => Json::String(s)
Expand All @@ -41,6 +44,7 @@ pub fn to_json(self : Cirru) -> Json {
}
}

///|
pub impl @json.FromJson for Cirru with from_json(json, path) {
match json {
String(a) => Cirru::Leaf(a)
Expand All @@ -60,14 +64,17 @@ pub impl @json.FromJson for Cirru with from_json(json, path) {
}
}

///|
fn Cirru::default() -> Cirru {
Cirru::List(Array::new())
}

///|
pub fn output(self : Cirru, logger : Logger) -> Unit {
logger.write_string(self.to_string())
}

///|
pub fn to_string(self : Cirru) -> String {
match self {
Cirru::Leaf(s) =>
Expand All @@ -90,6 +97,7 @@ 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)
Expand All @@ -110,37 +118,23 @@ pub fn compare(self : Cirru, other : Cirru) -> Int {
}
}

pub fn debug_write(self : Cirru, buffer : Buffer) -> Unit {
match self {
Cirru::Leaf(s) => buffer.write_string(s)
Cirru::List(xs) => {
buffer.write_char('(')
for i = 0; i < xs.length(); i = i + 1 {
let x = xs[i]
buffer.write_string(x.to_string())
if i < xs.length() - 1 {
buffer.write_char(' ')
}
}
buffer.write_char(')')
}
}
}

///|
pub fn length(self : Cirru) -> Int {
match self {
Leaf(s) => s.length()
List(xs) => xs.length()
}
}

///|
pub fn is_empty(self : Cirru) -> Bool {
match self {
Leaf(s) => s.length() == 0
List(xs) => xs.length() == 0
}
}

///|
pub fn is_nested(self : Cirru) -> Bool {
match self {
Leaf(_) => false
Expand All @@ -156,13 +150,15 @@ pub fn is_nested(self : Cirru) -> Bool {
}
}

///|
pub fn is_comment(self : Cirru) -> Bool {
match self {
Leaf(s) => s[0] == ';'
_ => false
}
}

///|
enum CirruLexState {
Space
Token
Expand All @@ -171,17 +167,20 @@ enum CirruLexState {
Str
} derive(Show)

///|
enum CirruLexItem {
Open
Close
Indent(Int)
Str(String)
}

///|
fn output(self : CirruLexItem, logger : Logger) -> Unit {
logger.write_string(self.to_string())
}

///|
fn to_string(self : CirruLexItem) -> String {
match self {
CirruLexItem::Open => "("
Expand All @@ -191,6 +190,7 @@ fn to_string(self : CirruLexItem) -> String {
}
}

///|
fn CirruLexItem::is_normal_str(tok : String) -> Bool {
let size = tok.length()
if size == 0 {
Expand Down Expand Up @@ -227,6 +227,7 @@ fn CirruLexItem::is_normal_str(tok : String) -> Bool {
return true
}

///|
fn escape_cirru_leaf(s : String) -> String {
if is_normal_str(s) {
return "\"\{s}\""
Expand Down
12 changes: 7 additions & 5 deletions src/main/main.mbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///|
pub fn main_parser() -> Unit {
// let tree = @lib.parse?("DEMO")
// match tree {
Expand All @@ -23,30 +24,31 @@ pub fn main_parser() -> Unit {
}
}

///|
pub fn main_writer() -> Unit {
let demo = "unfolding"
let demo_file = fsReadSync("./test/cirru/\{demo}.cirru")
let json_file = fsReadSync("./test/data/\{demo}.json")

let defined= @json.parse?(json_file).unwrap()
let tree: Array[@lib.Cirru] = @json.from_json?(defined).unwrap()
let defined = @json.parse?(json_file).unwrap()
let tree : Array[@lib.Cirru] = @json.from_json?(defined).unwrap()
println("TREE:")
for item in tree {
println(item.to_json().stringify())
}

let ret = @lib.format?(tree, {use_inline: false}).unwrap()
let ret = @lib.format?(tree, { use_inline: false }).unwrap()
println("Generated:")
println(ret)
println("Expected:")
println(demo_file)
println("")
}

///|
fn main {
main_parser()
}

///|
pub extern "js" fn fsReadSync(path : String) -> String =
#|(path) => {
#| const fs = require("node:fs");
Expand Down

0 comments on commit e26b0f9

Please sign in to comment.