Working parser
This commit is contained in:
@@ -48,6 +48,7 @@ fn cmd_build(path: &str) {
|
|||||||
match parser::parse(&source) {
|
match parser::parse(&source) {
|
||||||
Ok(ast) => {
|
Ok(ast) => {
|
||||||
println!("parsed {} top-level items", ast.statements.len());
|
println!("parsed {} top-level items", ast.statements.len());
|
||||||
|
println!("{:#?}", ast.statements)
|
||||||
// codegen will go here
|
// codegen will go here
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|||||||
+16
-4
@@ -16,12 +16,24 @@ pub fn parse(source: &str) -> Result<Program, ParseError> {
|
|||||||
let pairs = MistParser::parse(Rule::program, source)?;
|
let pairs = MistParser::parse(Rule::program, source)?;
|
||||||
let mut statements = vec![];
|
let mut statements = vec![];
|
||||||
|
|
||||||
|
// pairs is an iterator over the top-level program pair
|
||||||
|
// we need to get its inner children
|
||||||
for pair in pairs {
|
for pair in pairs {
|
||||||
match pair.as_rule() {
|
match pair.as_rule() {
|
||||||
Rule::function_decl => statements.push(TopLevel::Function(parse_function(pair))),
|
Rule::program => {
|
||||||
Rule::struct_decl => statements.push(TopLevel::Struct(parse_struct(pair))),
|
for inner in pair.into_inner() {
|
||||||
Rule::class_decl => statements.push(TopLevel::Class(parse_class(pair))),
|
match inner.as_rule() {
|
||||||
Rule::import_decl => statements.push(TopLevel::Import(parse_import(pair))),
|
Rule::function_decl => {
|
||||||
|
statements.push(TopLevel::Function(parse_function(inner)))
|
||||||
|
}
|
||||||
|
Rule::struct_decl => statements.push(TopLevel::Struct(parse_struct(inner))),
|
||||||
|
Rule::class_decl => statements.push(TopLevel::Class(parse_class(inner))),
|
||||||
|
Rule::import_decl => statements.push(TopLevel::Import(parse_import(inner))),
|
||||||
|
Rule::EOI => {}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Rule::EOI => {}
|
Rule::EOI => {}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
fn main() {
|
||||||
|
let message: string = "Hello, World!"
|
||||||
|
print(message)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user