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