Working function parsing
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
pub enum TypeExpr {
|
||||
Identifier(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
pub enum TopLevel {
|
||||
Import(String),
|
||||
StructDecl {
|
||||
@@ -21,11 +23,13 @@ pub enum TopLevel {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
pub enum Statement {
|
||||
Expression(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
pub enum Expression {
|
||||
Identifier(String),
|
||||
IntLiteral(i64),
|
||||
|
||||
+12
-11
@@ -12,12 +12,14 @@ pub struct MistParser;
|
||||
pub type ParseError = pest::error::Error<Rule>;
|
||||
|
||||
pub fn parse(source: &str) -> Result<Vec<TopLevel>, ParseError> {
|
||||
let pairs = MistParser::parse(Rule::program, source)?;
|
||||
let mut pairs = MistParser::parse(Rule::program, source)?;
|
||||
|
||||
let mut statements = vec![];
|
||||
|
||||
for pair in pairs {
|
||||
statements.push(TopLevel::from_pair(pair));
|
||||
for pair in pairs.next().unwrap().into_inner() {
|
||||
if let Some(stmt) = TopLevel::from_pair(pair) {
|
||||
statements.push(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(statements)
|
||||
@@ -37,15 +39,11 @@ impl TypeExpr {
|
||||
}
|
||||
|
||||
impl TopLevel {
|
||||
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Option<Self> {
|
||||
match pair.as_rule() {
|
||||
Rule::program => {
|
||||
let inner = pair.into_inner().next().unwrap();
|
||||
TopLevel::from_pair(inner)
|
||||
}
|
||||
Rule::import => {
|
||||
let path = pair.into_inner().next().unwrap().as_str().to_string();
|
||||
TopLevel::Import(path)
|
||||
Some(TopLevel::Import(path))
|
||||
}
|
||||
Rule::function_decl => {
|
||||
let mut inner = pair.into_inner();
|
||||
@@ -83,14 +81,17 @@ impl TopLevel {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// For now, we'll just ignore the function body and return an empty vector
|
||||
TopLevel::FunctionDecl {
|
||||
Some(TopLevel::FunctionDecl {
|
||||
name,
|
||||
params,
|
||||
return_type,
|
||||
body: vec![],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Rule::EOI => None,
|
||||
_ => unimplemented!("TopLevel parsing not implemented yet {:?}", pair.as_rule()),
|
||||
}
|
||||
}
|
||||
|
||||
+1
-3
@@ -1,5 +1,3 @@
|
||||
import "fmt";
|
||||
|
||||
function add(a: int, b: int): int {
|
||||
hello;
|
||||
}
|
||||
function add(a: int, b: int): int {}
|
||||
|
||||
Reference in New Issue
Block a user