Add parsing for &self and &mut self
This commit is contained in:
parent
bee31f4b92
commit
a253c032d8
@ -40,6 +40,7 @@ pub enum TypeKind {
|
||||
Custom(String),
|
||||
Borrow(Box<TypeKind>, bool),
|
||||
Ptr(Box<TypeKind>),
|
||||
Unknown,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@ -170,12 +171,20 @@ pub struct FunctionDefinition(pub FunctionSignature, pub bool, pub Block, pub To
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FunctionSignature {
|
||||
pub name: String,
|
||||
pub args: Vec<(String, Type)>,
|
||||
pub self_kind: SelfKind,
|
||||
pub params: Vec<(String, Type)>,
|
||||
pub return_type: Option<Type>,
|
||||
#[allow(dead_code)]
|
||||
pub range: TokenRange,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum SelfKind {
|
||||
Borrow(TypeKind),
|
||||
MutBorrow(TypeKind),
|
||||
None,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum ReturnType {
|
||||
Soft,
|
||||
|
@ -579,18 +579,47 @@ impl Parse for FunctionParam {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SelfParam(SelfKind);
|
||||
|
||||
impl Parse for SelfParam {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
stream.expect(Token::Et)?;
|
||||
let mutable = if let Some(Token::MutKeyword) = stream.peek() {
|
||||
stream.next();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let Some(Token::Identifier(name)) = stream.next() else {
|
||||
return Err(stream.expected_err("parameter name")?);
|
||||
};
|
||||
if name == "self" {
|
||||
match mutable {
|
||||
true => Ok(SelfParam(SelfKind::MutBorrow(TypeKind::Unknown))),
|
||||
false => Ok(SelfParam(SelfKind::Borrow(TypeKind::Unknown))),
|
||||
}
|
||||
} else {
|
||||
Err(stream.expected_err("self parameter")?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse for FunctionSignature {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
if let Some(Token::Identifier(name)) = stream.next() {
|
||||
stream.expect(Token::ParenOpen)?;
|
||||
let mut args = Vec::new();
|
||||
let mut params = Vec::new();
|
||||
|
||||
let self_kind = stream.parse::<SelfParam>().map(|s| s.0).unwrap_or(SelfKind::None);
|
||||
|
||||
if let Ok(param) = stream.parse::<FunctionParam>() {
|
||||
args.push((param.0, param.1));
|
||||
params.push((param.0, param.1));
|
||||
while let Some(Token::Comma) = stream.peek() {
|
||||
stream.next();
|
||||
let param = stream.parse::<FunctionParam>()?;
|
||||
args.push((param.0, param.1));
|
||||
params.push((param.0, param.1));
|
||||
}
|
||||
}
|
||||
|
||||
@ -603,7 +632,8 @@ impl Parse for FunctionSignature {
|
||||
|
||||
Ok(FunctionSignature {
|
||||
name,
|
||||
args,
|
||||
params,
|
||||
self_kind,
|
||||
return_type,
|
||||
range: stream.get_range().unwrap(),
|
||||
})
|
||||
|
@ -32,6 +32,26 @@ impl ast::Module {
|
||||
imports.push(mir::Import(import.0.clone(), import.1.as_meta(module_id)));
|
||||
}
|
||||
FunctionDefinition(ast::FunctionDefinition(signature, is_pub, block, range)) => {
|
||||
let mut params = Vec::new();
|
||||
match &signature.self_kind {
|
||||
ast::SelfKind::Borrow(type_kind) => params.push((
|
||||
"self".to_owned(),
|
||||
mir::TypeKind::Borrow(Box::new(type_kind.into_mir(module_id)), false),
|
||||
)),
|
||||
ast::SelfKind::MutBorrow(type_kind) => params.push((
|
||||
"self".to_owned(),
|
||||
mir::TypeKind::Borrow(Box::new(type_kind.into_mir(module_id)), true),
|
||||
)),
|
||||
ast::SelfKind::None => {}
|
||||
}
|
||||
|
||||
params.extend(
|
||||
signature
|
||||
.params
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|p| (p.0, p.1 .0.into_mir(module_id))),
|
||||
);
|
||||
let def = mir::FunctionDefinition {
|
||||
name: signature.name.clone(),
|
||||
is_pub: *is_pub,
|
||||
@ -41,12 +61,7 @@ impl ast::Module {
|
||||
.clone()
|
||||
.map(|r| r.0.into_mir(module_id))
|
||||
.unwrap_or(mir::TypeKind::Void),
|
||||
parameters: signature
|
||||
.args
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|p| (p.0, p.1 .0.into_mir(module_id)))
|
||||
.collect(),
|
||||
parameters: params,
|
||||
kind: mir::FunctionDefinitionKind::Local(
|
||||
block.into_mir(module_id),
|
||||
(*range).as_meta(module_id),
|
||||
@ -65,7 +80,7 @@ impl ast::Module {
|
||||
.map(|r| r.0.into_mir(module_id))
|
||||
.unwrap_or(mir::TypeKind::Void),
|
||||
parameters: signature
|
||||
.args
|
||||
.params
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|p| (p.0, p.1 .0.into_mir(module_id)))
|
||||
@ -475,6 +490,7 @@ impl ast::TypeKind {
|
||||
ast::TypeKind::F128 => mir::TypeKind::F128,
|
||||
ast::TypeKind::F128PPC => mir::TypeKind::F128PPC,
|
||||
ast::TypeKind::Char => mir::TypeKind::Char,
|
||||
ast::TypeKind::Unknown => mir::TypeKind::Vague(mir::VagueType::Unknown),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -292,6 +292,12 @@ pub struct FunctionDefinition {
|
||||
pub kind: FunctionDefinitionKind,
|
||||
}
|
||||
|
||||
pub enum SelfKind {
|
||||
Borrow,
|
||||
MutBorrow,
|
||||
None,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum FunctionDefinitionKind {
|
||||
/// Actual definition block and surrounding signature range
|
||||
|
Loading…
Reference in New Issue
Block a user