Working c like parsing
This commit is contained in:
+21
-13
@@ -29,16 +29,22 @@ pub fn parse(source: &str) -> Result<Vec<TopLevel>, ParseError> {
|
||||
Ok(statements)
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for TypeExpr {
|
||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
match pair.as_rule() {
|
||||
impl TryFrom<pest::iterators::Pair<'_, Rule>> for TypeExpr {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'_, Rule>) -> Result<Self, Self::Error> {
|
||||
if pair.as_str() == "void" {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
Ok(match pair.as_rule() {
|
||||
Rule::type_expr => {
|
||||
let inner = pair.into_inner().next().unwrap();
|
||||
TypeExpr::from(inner)
|
||||
TypeExpr::try_from(inner)?
|
||||
}
|
||||
Rule::identifier => TypeExpr::Identifier(pair.as_str().to_string()),
|
||||
_ => unimplemented!("TypeExpr parsing not implemented yet"),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,8 +61,8 @@ impl From<(bool, pest::iterators::Pair<'_, Rule>)> for ParamList {
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let param_type = TypeExpr::try_from(param_inner.next().unwrap()).unwrap();
|
||||
let param_name = param_inner.next().unwrap().as_str().to_string();
|
||||
let param_type = TypeExpr::from(param_inner.next().unwrap());
|
||||
(param_name, (export, param_type))
|
||||
})
|
||||
.collect();
|
||||
@@ -90,15 +96,10 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let name = inner.next().unwrap().as_str().to_string();
|
||||
let params = if inner.peek().unwrap().as_rule() == Rule::param_list {
|
||||
ParamList::from((false, inner.next().unwrap()))
|
||||
} else {
|
||||
ParamList(HashMap::new())
|
||||
};
|
||||
|
||||
let return_type = if let Some(next) = inner.peek() {
|
||||
if next.as_rule() == Rule::type_expr {
|
||||
Some(TypeExpr::from(inner.next().unwrap()))
|
||||
TypeExpr::try_from(inner.next().unwrap()).ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -106,6 +107,13 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
||||
None
|
||||
};
|
||||
|
||||
let name = inner.next().unwrap().as_str().to_string();
|
||||
let params = if inner.peek().unwrap().as_rule() == Rule::param_list {
|
||||
ParamList::from((false, inner.next().unwrap()))
|
||||
} else {
|
||||
ParamList(HashMap::new())
|
||||
};
|
||||
|
||||
let body = Block::from(inner.next().unwrap());
|
||||
|
||||
Ok(TopLevel::FunctionDecl {
|
||||
|
||||
Reference in New Issue
Block a user