Struct decleration
This commit is contained in:
+5
-2
@@ -12,11 +12,11 @@ pub enum TopLevel {
|
||||
Import(String),
|
||||
StructDecl {
|
||||
name: String,
|
||||
fields: Vec<(String, TypeExpr)>,
|
||||
fields: ParamList,
|
||||
},
|
||||
FunctionDecl {
|
||||
name: String,
|
||||
params: Vec<(String, TypeExpr)>,
|
||||
params: ParamList,
|
||||
return_type: Option<TypeExpr>,
|
||||
body: Vec<Statement>,
|
||||
},
|
||||
@@ -41,3 +41,6 @@ pub enum Expression {
|
||||
args: Vec<Expression>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ParamList(pub Vec<(String, TypeExpr)>);
|
||||
|
||||
@@ -13,14 +13,19 @@ inner_str = @{ (!"\"" ~ ANY)* }
|
||||
|
||||
// ================= TOP LEVEL =================
|
||||
|
||||
top_level = _{ import | function_decl }
|
||||
top_level = _{ import | function_decl | struct_decl }
|
||||
|
||||
export = { "export"? }
|
||||
|
||||
import = { "import" ~ string_lit ~ ";" }
|
||||
|
||||
param_list = { param ~ ("," ~ param)* ~ ","? }
|
||||
param = { identifier ~ ":" ~ type_expr }
|
||||
param_list = { param ~ ("," ~ param)* ~ ","? }
|
||||
param = { identifier ~ ":" ~ type_expr }
|
||||
|
||||
struct_decl = {
|
||||
export ~ "struct" ~ identifier ~ "{" ~ param_list? ~ "}"
|
||||
}
|
||||
|
||||
function_decl = {
|
||||
export ~ "function" ~ identifier ~ "(" ~ param_list? ~ ")" ~ (":" ~ type_expr)? ~ block
|
||||
}
|
||||
|
||||
+37
-10
@@ -38,6 +38,22 @@ impl TypeExpr {
|
||||
}
|
||||
}
|
||||
|
||||
impl ParamList {
|
||||
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
let params = pair
|
||||
.into_inner()
|
||||
.map(|p| {
|
||||
let mut param_inner = p.into_inner();
|
||||
let param_name = param_inner.next().unwrap().as_str().to_string();
|
||||
let param_type = TypeExpr::from_pair(param_inner.next().unwrap());
|
||||
(param_name, param_type)
|
||||
})
|
||||
.collect();
|
||||
|
||||
ParamList(params)
|
||||
}
|
||||
}
|
||||
|
||||
impl TopLevel {
|
||||
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Option<Self> {
|
||||
match pair.as_rule() {
|
||||
@@ -60,17 +76,9 @@ impl TopLevel {
|
||||
let name = inner.next().unwrap().as_str().to_string();
|
||||
let params_pair = inner.next().unwrap();
|
||||
let params = if params_pair.as_rule() == Rule::param_list {
|
||||
params_pair
|
||||
.into_inner()
|
||||
.map(|p| {
|
||||
let mut param_inner = p.into_inner();
|
||||
let param_name = param_inner.next().unwrap().as_str().to_string();
|
||||
let param_type = TypeExpr::from_pair(param_inner.next().unwrap());
|
||||
(param_name, param_type)
|
||||
})
|
||||
.collect()
|
||||
ParamList::from_pair(params_pair)
|
||||
} else {
|
||||
vec![]
|
||||
ParamList(vec![])
|
||||
};
|
||||
let return_type = if let Some(next) = inner.peek() {
|
||||
if next.as_rule() == Rule::type_expr {
|
||||
@@ -91,6 +99,25 @@ impl TopLevel {
|
||||
})
|
||||
}
|
||||
|
||||
Rule::struct_decl => {
|
||||
let mut inner = pair.into_inner();
|
||||
let export = if let Some(first) = inner.peek() {
|
||||
if first.as_rule() == Rule::export {
|
||||
inner.next();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let name = inner.next().unwrap().as_str().to_string();
|
||||
let fields_pair = inner.next().unwrap();
|
||||
let fields = ParamList::from_pair(fields_pair);
|
||||
|
||||
Some(TopLevel::StructDecl { name, fields })
|
||||
}
|
||||
|
||||
Rule::EOI => None,
|
||||
_ => unimplemented!("TopLevel parsing not implemented yet {:?}", pair.as_rule()),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user