Skip to content

Commit

Permalink
"moon fmt" updated; tag 0.0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Dec 17, 2024
1 parent c266e1e commit 3856a93
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 20 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.9",
"version": "0.0.10",
"deps": {},
"readme": "README.md",
"repository": "https://github.com/Cirru/parser.mbt",
Expand Down
11 changes: 7 additions & 4 deletions src/lib/parser.mbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///|
fn build_exprs(tokens : Array[CirruLexItem]) -> Array[Cirru]!CirruParseError {
let acc : Array[Cirru] = Array::new()
let mut idx = 0
Expand Down Expand Up @@ -56,7 +57,7 @@ fn build_exprs(tokens : Array[CirruLexItem]) -> Array[Cirru]!CirruParseError {
}
}

/// main function to parse Cirru code into syntax tree
///| main function to parse Cirru code into syntax tree
pub fn parse(code : String) -> Array[Cirru]!CirruParseError {
let tokens = resolve_indentations(lex!(code))
// println("tokens: \{tokens}")
Expand All @@ -65,15 +66,17 @@ pub fn parse(code : String) -> Array[Cirru]!CirruParseError {
resolve_comma(resolve_dollar(tree))
}

///|
type! CirruParseError String

/// display Cirru parse error
///| display Cirru parse error
pub fn to_string(self : CirruParseError) -> String {
match self {
CirruParseError(s) => s
}
}

///|
fn parse_indentation(size : Int) -> CirruLexItem!CirruParseError {
if size % 2 == 0 {
CirruLexItem::Indent(size >> 1)
Expand All @@ -82,7 +85,7 @@ fn parse_indentation(size : Int) -> CirruLexItem!CirruParseError {
}
}

/// lexer is a simpler state machine to tokenize Cirru code
///| lexer is a simpler state machine to tokenize Cirru code
fn lex(initial_code : String) -> Array[CirruLexItem]!CirruParseError {
let acc = []
let mut state = CirruLexState::Indent
Expand Down Expand Up @@ -264,7 +267,7 @@ fn lex(initial_code : String) -> Array[CirruLexItem]!CirruParseError {
}
}

/// internal function for figuring out indentations after lexing
///| internal function for figuring out indentations after lexing
fn resolve_indentations(tokens : Array[CirruLexItem]) -> Array[CirruLexItem] {
let size = tokens.length()
let mut acc : Array[CirruLexItem] = Array::new()
Expand Down
13 changes: 7 additions & 6 deletions src/lib/parser_test.mbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
test "parser" {
assert_eq!(
@lib.parse!("def a"),
[@lib.Cirru::List([@lib.Cirru::Leaf("def"), @lib.Cirru::Leaf("a")])],
)
assert_eq!(@lib.parse!("def a"), [
@lib.Cirru::List([@lib.Cirru::Leaf("def"), @lib.Cirru::Leaf("a")]),
])
}

test "existed demos" {
Expand All @@ -18,13 +17,15 @@ test "existed demos" {
let defined = @json.parse?(json_file).unwrap()
let defined_str = defined.stringify()
assert_eq!(tree, defined_str)

let formatted = @lib.format!(@json.from_json!(defined), {use_inline: false})
let formatted = @lib.format!(@json.from_json!(defined), {
use_inline: false,
})
let ret = @lib.parse?(formatted).unwrap().to_json().stringify()
assert_eq!(ret, defined_str)
}
}

///|
pub extern "js" fn fsReadSync(path : String) -> String =
#|(path) => {
#| const fs = require("node:fs");
Expand Down
4 changes: 2 additions & 2 deletions src/lib/primes.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn Cirru::default() -> Cirru {
}

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

Expand Down Expand Up @@ -176,7 +176,7 @@ enum CirruLexItem {
}

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

Expand Down
9 changes: 6 additions & 3 deletions src/lib/s_expr.mbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
///|
type! FormatError String

/// format program to Cirru to Lisp-like code
///| format program to Cirru to Lisp-like code
pub fn format_to_lisp(xs : Array[Cirru]) -> String!FormatError {
let mut content : String = "\n"
for expr in xs {
Expand All @@ -9,7 +10,7 @@ pub fn format_to_lisp(xs : Array[Cirru]) -> String!FormatError {
content
}

/// format single expression to Lisp-like code
///| format single expression to Lisp-like code
fn format_lispy_expr(self : Cirru, indent : Int) -> String!FormatError {
let emptySpace = " "
match self {
Expand Down Expand Up @@ -62,6 +63,7 @@ fn format_lispy_expr(self : Cirru, indent : Int) -> String!FormatError {
}
}

///|
fn ends_with_newline(s : String) -> Bool {
for c in s.rev() {
if c == 'c' {
Expand All @@ -74,6 +76,7 @@ fn ends_with_newline(s : String) -> Bool {
false
}

///|
fn gen_newline(n : Int) -> String {
let mut chunk : String = ""
chunk = "\{chunk}\n"
Expand All @@ -83,7 +86,7 @@ fn gen_newline(n : Int) -> String {
chunk
}

/// `.escape_default()` in Rust
///| `.escape_default()` in Rust
fn escape_string(s : String) -> String {
let mut chunk : String = ""
for c in s {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/tree.mbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///|
fn resolve_comma(xs : Array[Cirru]) -> Array[Cirru] {
if xs.is_empty() {
[]
Expand All @@ -6,6 +7,7 @@ fn resolve_comma(xs : Array[Cirru]) -> Array[Cirru] {
}
}

///|
fn comma_helper(initial_after : Array[Cirru]) -> Array[Cirru] {
let before : Array[Cirru] = []
let after : Array[Cirru] = initial_after
Expand Down Expand Up @@ -35,6 +37,7 @@ fn comma_helper(initial_after : Array[Cirru]) -> Array[Cirru] {
}
}

///|
fn resolve_dollar(xs : Array[Cirru]) -> Array[Cirru] {
if xs.is_empty() {
[]
Expand All @@ -43,6 +46,7 @@ fn resolve_dollar(xs : Array[Cirru]) -> Array[Cirru] {
}
}

///|
fn dollar_helper(initial_after : Array[Cirru]) -> Array[Cirru] {
let before : Array[Cirru] = []
let after : Array[Cirru] = initial_after
Expand Down
26 changes: 22 additions & 4 deletions src/lib/writer.mbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///|
enum WriterNode {
Nil
Leaf
Expand All @@ -6,18 +7,23 @@ enum WriterNode {
Expr
} derive(Eq)

///|
let char_close : Char = ')'

///|
let char_open : Char = '('

///|
let allowed_chars : String = "$-:<>[]{}*=+.,\\/!?~_@#&%^|;'"

///|
fn is_a_digit(c : Char) -> Bool {
let n = c.to_int()
// ascii table https://tool.oschina.net/commons?type=4
n >= 48 && n <= 57
}

///|
fn is_a_letter(c : Char) -> Bool {
let n = c.to_int()
if n >= 65 && n <= 90 {
Expand All @@ -29,6 +35,7 @@ fn is_a_letter(c : Char) -> Bool {
false
}

///|
fn is_simple_expr(ys : Array[Cirru]) -> Bool {
for y in ys {
match y {
Expand All @@ -39,6 +46,7 @@ fn is_simple_expr(ys : Array[Cirru]) -> Bool {
true
}

///|
fn is_boxed(ys : Array[Cirru]) -> Bool {
for y in ys {
match y {
Expand All @@ -49,17 +57,20 @@ fn is_boxed(ys : Array[Cirru]) -> Bool {
true
}

///|
fn is_simple_char(x : Char) -> Bool {
is_a_letter(x) || is_a_digit(x)
}

///|
fn is_char_allowed(x : Char) -> Bool {
if is_simple_char(x) {
return true
}
allowed_chars.contains_char(x)
}

///|
fn generate_leaf(s : String) -> String {
let mut all_allowed = true
for x in s {
Expand Down Expand Up @@ -89,10 +100,12 @@ fn generate_leaf(s : String) -> String {
}
}

///|
fn generate_empty_expr() -> String {
"()"
}

///|
fn generate_inline_expr(xs : Array[Cirru]) -> String {
let mut result = char_open.to_string()
for idx, x in xs {
Expand All @@ -109,7 +122,7 @@ fn generate_inline_expr(xs : Array[Cirru]) -> String {
result
}

/// by 2 spaces
///| by 2 spaces
fn push_spaces(buf : String, n : Int) -> String {
let mut ret = buf
// for _ in 0..n {
Expand All @@ -121,19 +134,20 @@ fn push_spaces(buf : String, n : Int) -> String {
return ret
}

///|
fn render_newline(n : Int) -> String {
let mut ret = ""
ret = ret + "\n"
ret = push_spaces(ret, n)
ret
}

/// options for writer, `use_inline` for more compact format.
///| options for writer, `use_inline` for more compact format.
pub(all) struct CirruWriterOptions {
use_inline : Bool
}

/// kind for writer nodes
///| kind for writer nodes
fn get_node_kind(self : Cirru) -> WriterNode {
match self {
Cirru::Leaf(_) => WriterNode::Leaf
Expand All @@ -150,14 +164,17 @@ fn get_node_kind(self : Cirru) -> WriterNode {
}
}

///|
pub(all) type! FormatCirruError String

///|
pub fn to_string(self : FormatCirruError) -> String {
match self {
FormatCirruError(s) => s
}
}

///|
fn generate_tree(
xs : Array[Cirru],
insist_head : Bool,
Expand Down Expand Up @@ -291,6 +308,7 @@ fn generate_tree(
result
}

///|
fn generate_statements(
ys : Array[Cirru],
options : CirruWriterOptions
Expand All @@ -309,7 +327,7 @@ fn generate_statements(
zs
}

/// format Cirru code, use options to control `use_inline` option
///| format Cirru code, use options to control `use_inline` option
pub fn format(
xs : Array[Cirru],
options : CirruWriterOptions
Expand Down

0 comments on commit 3856a93

Please sign in to comment.