Struct decleration
This commit is contained in:
+5
-2
@@ -12,11 +12,11 @@ pub enum TopLevel {
|
|||||||
Import(String),
|
Import(String),
|
||||||
StructDecl {
|
StructDecl {
|
||||||
name: String,
|
name: String,
|
||||||
fields: Vec<(String, TypeExpr)>,
|
fields: ParamList,
|
||||||
},
|
},
|
||||||
FunctionDecl {
|
FunctionDecl {
|
||||||
name: String,
|
name: String,
|
||||||
params: Vec<(String, TypeExpr)>,
|
params: ParamList,
|
||||||
return_type: Option<TypeExpr>,
|
return_type: Option<TypeExpr>,
|
||||||
body: Vec<Statement>,
|
body: Vec<Statement>,
|
||||||
},
|
},
|
||||||
@@ -41,3 +41,6 @@ pub enum Expression {
|
|||||||
args: Vec<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 =================
|
||||||
|
|
||||||
top_level = _{ import | function_decl }
|
top_level = _{ import | function_decl | struct_decl }
|
||||||
|
|
||||||
export = { "export"? }
|
export = { "export"? }
|
||||||
|
|
||||||
import = { "import" ~ string_lit ~ ";" }
|
import = { "import" ~ string_lit ~ ";" }
|
||||||
|
|
||||||
param_list = { param ~ ("," ~ param)* ~ ","? }
|
param_list = { param ~ ("," ~ param)* ~ ","? }
|
||||||
param = { identifier ~ ":" ~ type_expr }
|
param = { identifier ~ ":" ~ type_expr }
|
||||||
|
|
||||||
|
struct_decl = {
|
||||||
|
export ~ "struct" ~ identifier ~ "{" ~ param_list? ~ "}"
|
||||||
|
}
|
||||||
|
|
||||||
function_decl = {
|
function_decl = {
|
||||||
export ~ "function" ~ identifier ~ "(" ~ param_list? ~ ")" ~ (":" ~ type_expr)? ~ block
|
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 {
|
impl TopLevel {
|
||||||
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Option<Self> {
|
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Option<Self> {
|
||||||
match pair.as_rule() {
|
match pair.as_rule() {
|
||||||
@@ -60,17 +76,9 @@ impl TopLevel {
|
|||||||
let name = inner.next().unwrap().as_str().to_string();
|
let name = inner.next().unwrap().as_str().to_string();
|
||||||
let params_pair = inner.next().unwrap();
|
let params_pair = inner.next().unwrap();
|
||||||
let params = if params_pair.as_rule() == Rule::param_list {
|
let params = if params_pair.as_rule() == Rule::param_list {
|
||||||
params_pair
|
ParamList::from_pair(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()
|
|
||||||
} else {
|
} else {
|
||||||
vec![]
|
ParamList(vec![])
|
||||||
};
|
};
|
||||||
let return_type = if let Some(next) = inner.peek() {
|
let return_type = if let Some(next) = inner.peek() {
|
||||||
if next.as_rule() == Rule::type_expr {
|
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,
|
Rule::EOI => None,
|
||||||
_ => unimplemented!("TopLevel parsing not implemented yet {:?}", pair.as_rule()),
|
_ => unimplemented!("TopLevel parsing not implemented yet {:?}", pair.as_rule()),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
import "fmt";
|
import "fmt";
|
||||||
|
|
||||||
|
struct Point {
|
||||||
|
x: int,
|
||||||
|
y: int
|
||||||
|
}
|
||||||
|
|
||||||
function add(a: int, b: int): int {}
|
function add(a: int, b: int): int {}
|
||||||
|
|||||||
Reference in New Issue
Block a user