@@ -78,9 +78,7 @@ impl RustCodegen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate(&mut self, toplevels: Vec<TopLevel>) -> String {
|
pub fn generate(&mut self, toplevels: Vec<TopLevel>) -> String {
|
||||||
let mut ctx = Context {
|
let mut ctx = Context { expr_super: None };
|
||||||
expr_super: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
for tl in toplevels {
|
for tl in toplevels {
|
||||||
tl.gen_rust(&mut ctx, self);
|
tl.gen_rust(&mut ctx, self);
|
||||||
@@ -275,6 +273,13 @@ impl GetRust for TypeExpr {
|
|||||||
return_type.get_rust(),
|
return_type.get_rust(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Self::Array(ty, count) => {
|
||||||
|
if let Some(count) = count {
|
||||||
|
format!("[{}; {count}]", ty.get_rust())
|
||||||
|
} else {
|
||||||
|
format!("[{}]", ty.get_rust())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -215,7 +215,11 @@ impl GenRust for VarDecl {
|
|||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|t| {
|
.map(|t| {
|
||||||
let t = vec![t.get_rust(); self.tuple_names.len() + 1];
|
let t = vec![t.get_rust(); self.tuple_names.len() + 1];
|
||||||
|
if self.tuple_names.is_empty() {
|
||||||
|
format!(": {}", t.join(", "))
|
||||||
|
} else {
|
||||||
format!(": ({})", t.join(", "))
|
format!(": ({})", t.join(", "))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ pub enum TypeExpr {
|
|||||||
return_type: Box<TypeExpr>,
|
return_type: Box<TypeExpr>,
|
||||||
params: Vec<TypeExpr>,
|
params: Vec<TypeExpr>,
|
||||||
},
|
},
|
||||||
|
Array(Box<TypeExpr>, Option<isize>)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ pub enum ErrorCode {
|
|||||||
InvalidStatement,
|
InvalidStatement,
|
||||||
AstGenBug,
|
AstGenBug,
|
||||||
Module,
|
Module,
|
||||||
|
NumberParse,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<pest::error::Error<Rule>> for ParseError<'_> {
|
impl From<pest::error::Error<Rule>> for ParseError<'_> {
|
||||||
|
|||||||
@@ -141,8 +141,12 @@ void_type = {
|
|||||||
"void"
|
"void"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
array_type = {
|
||||||
|
"[" ~ integer? ~ "]"
|
||||||
|
}
|
||||||
|
|
||||||
type_expr = {
|
type_expr = {
|
||||||
(void_type | path_type | tuple_type | dyn_type) ~ (unsafe_ref_type | ref_type | fn_type)*
|
(void_type | path_type | tuple_type | dyn_type) ~ (unsafe_ref_type | ref_type | fn_type | array_type)*
|
||||||
}
|
}
|
||||||
|
|
||||||
lifetime = {
|
lifetime = {
|
||||||
|
|||||||
@@ -50,6 +50,25 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Rule::array_type => {
|
||||||
|
ty = TypeExpr::Array(
|
||||||
|
Box::new(ty),
|
||||||
|
ref_inner
|
||||||
|
.next()
|
||||||
|
.map(|v| {
|
||||||
|
v.as_str().parse().map_err(|_| AstError {
|
||||||
|
span: v.as_span(),
|
||||||
|
error_code: crate::error::ErrorCode::NumberParse,
|
||||||
|
error_message: format!(
|
||||||
|
"Number exceeds isize limits: {}",
|
||||||
|
v.as_str()
|
||||||
|
),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.transpose()?,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
_ => AstError::bug_unimplemented(ref_pair)?,
|
_ => AstError::bug_unimplemented(ref_pair)?,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user