Struct decleration

This commit is contained in:
2026-04-14 02:58:51 +02:00
parent d04fd4d2b2
commit 00569da11d
4 changed files with 55 additions and 15 deletions
+5 -2
View File
@@ -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)>);
+6 -1
View File
@@ -13,7 +13,7 @@ inner_str = @{ (!"\"" ~ ANY)* }
// ================= TOP LEVEL ================= // ================= TOP LEVEL =================
top_level = _{ import | function_decl } top_level = _{ import | function_decl | struct_decl }
export = { "export"? } export = { "export"? }
@@ -21,6 +21,11 @@ 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
View File
@@ -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()),
} }
+5
View File
@@ -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 {}