Skip to content

Commit

Permalink
get parser test passed
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Sep 13, 2024
1 parent 4ae1ae1 commit c4b1fa0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
13 changes: 6 additions & 7 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
- name: Setup Moonbit
uses: hustcer/[email protected]

- name: moon check
run: moon check
# - name: moon check
# run: moon check

# - name: moon info
# run: |
Expand All @@ -25,7 +25,6 @@ jobs:

- name: moon test
run: |
moon test
moon test --target js
# - name: moon bundle
Expand All @@ -34,7 +33,7 @@ jobs:
# - name: check core size
# run: ls -alh `find ./target/bundle -name *.core`

- name: format diff
run: |
moon fmt
git diff
# - name: format diff
# run: |
# moon fmt
# git diff
13 changes: 8 additions & 5 deletions lib/parser.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ fn build_exprs(tokens : Array[CirruLexItem]) -> Array[Cirru]!CirruParseError {

pub fn parse(code : String) -> Array[Cirru]!CirruParseError {
let tokens = resolve_indentations(lex!(code))
println("tokens: \{tokens}")
// println("tokens: \{tokens}")
let tree = build_exprs!(tokens)
println("tree: \{tree}")
// println("tree: \{tree}")
resolve_comma(resolve_dollar(tree))
}

Expand All @@ -74,7 +74,7 @@ pub fn to_string(self : CirruParseError) -> String {

fn parse_indentation(size : Int) -> CirruLexItem!CirruParseError {
if size % 2 == 0 {
CirruLexItem::Indent(size % 2)
CirruLexItem::Indent(size >> 1)
} else {
raise CirruParseError("odd indentation size: \{size}")
}
Expand All @@ -86,7 +86,10 @@ fn lex(initial_code : String) -> Array[CirruLexItem]!CirruParseError {
let mut buffer = ""
let code = initial_code.to_array()
for idx, c in code {
// println("lexing \{c} in \{state} and \{buffer}")
// println("acc: \{acc}")
// println(
// "lexing \{c.to_string().escape()} in \{state} and \{buffer.escape()}",
// )
match state {
CirruLexState::Space =>
match c {
Expand Down Expand Up @@ -245,7 +248,7 @@ fn lex(initial_code : String) -> Array[CirruLexItem]!CirruParseError {
}
}
}
println("end of file in lex \{acc}")
// println("end of file in lex \{acc}")
match state {
CirruLexState::Space => acc
CirruLexState::Token => {
Expand Down
24 changes: 22 additions & 2 deletions lib/parser_test.mbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
test "parser" {
println(@lib.parse!("def a"))
assert_eq!(
@lib.parse!("def a"),
[@lib.Cirru::Leaf("def"), @lib.Cirru::Leaf("a")],
[@lib.Cirru::List([@lib.Cirru::Leaf("def"), @lib.Cirru::Leaf("a")])],
)
}

test "existed demos" {
let demos = [
"comma", "demo", "folding", "html", "indent-twice", "indent", "let", "line",
"list-match", "paren-indent", "paren-indent2", "parentheses", "quote", "spaces",
"unfolding",
]
for demo in demos {
let demo_file = fsReadSync("./test/cirru/\{demo}.cirru")
let json_file = fsReadSync("./test/data/\{demo}.json")
let tree = @lib.parse?(demo_file).unwrap().to_json().stringify()
let defined = @json.parse?(json_file).unwrap().stringify()
assert_eq!(tree, defined)
}
}

pub extern "js" fn fsReadSync(path : String) -> String =
#|(path) => {
#| const fs = require("node:fs");
#| return fs.readFileSync(path, { encoding: "utf8" });
#|}
23 changes: 9 additions & 14 deletions main/main.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,24 @@ fn main {
// Result::Err(err) => println(err.to_string())
// }

let demo_file = fsReadSync("./test/cirru/indent.cirru")
let json_file = fsReadSync("./test/data/indent.json")

let demo_file = fsReadSync("./test/cirru/unfolding.cirru")
let json_file = fsReadSync("./test/data/unfolding.json")
let tree = @lib.parse?(demo_file)
match tree {
Result::Ok(tree) => {
println(tree.to_json().stringify())
match @json.parse?(json_file) {
Result::Ok(json) => {
println(json.stringify())
}
Result::Ok(json) => println(json.stringify())
Result::Err(err) => println(err.to_string())
}
println(tree)
// println(tree)
}
Result::Err(err) => println(err.to_string())
}


}

pub extern "js" fn fsReadSync(path: String) -> String =
#|(path) => {
#| const fs = require("node:fs");
#| return fs.readFileSync(path, { encoding: "utf8" });
#|}
pub extern "js" fn fsReadSync(path : String) -> String =
#|(path) => {
#| const fs = require("node:fs");
#| return fs.readFileSync(path, { encoding: "utf8" });
#|}

0 comments on commit c4b1fa0

Please sign in to comment.