Add generics parsing
This commit is contained in:
parent
ff83d7b4f0
commit
13e462def7
12
examples/generics.reid
Normal file
12
examples/generics.reid
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
// Arithmetic, function calls and imports!
|
||||||
|
|
||||||
|
fn test<T>(value: T) -> T {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> u32 {
|
||||||
|
let value = 0b110;
|
||||||
|
let other = 0o17;
|
||||||
|
|
||||||
|
return value * other + 7 * -value;
|
||||||
|
}
|
@ -162,6 +162,7 @@ impl BinaryOperator {
|
|||||||
pub struct FunctionCallExpression {
|
pub struct FunctionCallExpression {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub params: Vec<Expression>,
|
pub params: Vec<Expression>,
|
||||||
|
pub generics: Vec<Type>,
|
||||||
pub range: TokenRange,
|
pub range: TokenRange,
|
||||||
pub is_macro: bool,
|
pub is_macro: bool,
|
||||||
}
|
}
|
||||||
@ -192,6 +193,7 @@ pub struct FunctionDefinition(pub FunctionSignature, pub bool, pub Block, pub To
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct FunctionSignature {
|
pub struct FunctionSignature {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub generics: Vec<String>,
|
||||||
pub documentation: Option<String>,
|
pub documentation: Option<String>,
|
||||||
pub self_kind: SelfKind,
|
pub self_kind: SelfKind,
|
||||||
pub params: Vec<(String, Type, TokenRange)>,
|
pub params: Vec<(String, Type, TokenRange)>,
|
||||||
|
@ -182,6 +182,7 @@ impl Parse for AssociatedFunctionCall {
|
|||||||
ty,
|
ty,
|
||||||
FunctionCallExpression {
|
FunctionCallExpression {
|
||||||
name: String::new(),
|
name: String::new(),
|
||||||
|
generics: Vec::new(),
|
||||||
params: Vec::new(),
|
params: Vec::new(),
|
||||||
range: stream.get_range_prev_curr().unwrap(),
|
range: stream.get_range_prev_curr().unwrap(),
|
||||||
is_macro: false,
|
is_macro: false,
|
||||||
@ -200,6 +201,7 @@ impl Parse for AssociatedFunctionCall {
|
|||||||
ty,
|
ty,
|
||||||
FunctionCallExpression {
|
FunctionCallExpression {
|
||||||
name: fn_name,
|
name: fn_name,
|
||||||
|
generics: Vec::new(),
|
||||||
params: Vec::new(),
|
params: Vec::new(),
|
||||||
range: stream.get_range_prev_curr().unwrap(),
|
range: stream.get_range_prev_curr().unwrap(),
|
||||||
is_macro: false,
|
is_macro: false,
|
||||||
@ -211,6 +213,7 @@ impl Parse for AssociatedFunctionCall {
|
|||||||
ty,
|
ty,
|
||||||
FunctionCallExpression {
|
FunctionCallExpression {
|
||||||
name: String::new(),
|
name: String::new(),
|
||||||
|
generics: Vec::new(),
|
||||||
params: Vec::new(),
|
params: Vec::new(),
|
||||||
range: stream.get_range_prev_curr().unwrap(),
|
range: stream.get_range_prev_curr().unwrap(),
|
||||||
is_macro: false,
|
is_macro: false,
|
||||||
@ -591,7 +594,8 @@ impl Parse for FunctionCallExpression {
|
|||||||
let args = stream.parse::<FunctionArgs>()?;
|
let args = stream.parse::<FunctionArgs>()?;
|
||||||
Ok(FunctionCallExpression {
|
Ok(FunctionCallExpression {
|
||||||
name,
|
name,
|
||||||
params: args.0,
|
generics: args.0,
|
||||||
|
params: args.1,
|
||||||
range: stream.get_range().unwrap(),
|
range: stream.get_range().unwrap(),
|
||||||
is_macro,
|
is_macro,
|
||||||
})
|
})
|
||||||
@ -602,10 +606,23 @@ impl Parse for FunctionCallExpression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FunctionArgs(Vec<Expression>);
|
pub struct FunctionArgs(Vec<Type>, Vec<Expression>);
|
||||||
|
|
||||||
impl Parse for FunctionArgs {
|
impl Parse for FunctionArgs {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
|
let mut generics: Vec<Type> = Vec::new();
|
||||||
|
if let Some(Token::LessThan) = stream.peek() {
|
||||||
|
stream.next();
|
||||||
|
if let Ok(ty) = stream.parse() {
|
||||||
|
generics.push(ty);
|
||||||
|
while let Some(Token::Comma) = stream.peek() {
|
||||||
|
stream.next();
|
||||||
|
generics.push(stream.parse()?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stream.expect(Token::GreaterThan)?;
|
||||||
|
}
|
||||||
|
|
||||||
stream.expect(Token::ParenOpen)?;
|
stream.expect(Token::ParenOpen)?;
|
||||||
|
|
||||||
let mut params = Vec::new();
|
let mut params = Vec::new();
|
||||||
@ -618,7 +635,7 @@ impl Parse for FunctionArgs {
|
|||||||
}
|
}
|
||||||
stream.expect(Token::ParenClose)?;
|
stream.expect(Token::ParenClose)?;
|
||||||
|
|
||||||
Ok(FunctionArgs(params))
|
Ok(FunctionArgs(generics, params))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -780,6 +797,18 @@ impl Parse for SelfParam {
|
|||||||
impl Parse for FunctionSignature {
|
impl Parse for FunctionSignature {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
if let Some(Token::Identifier(name)) = stream.next() {
|
if let Some(Token::Identifier(name)) = stream.next() {
|
||||||
|
let mut generics = Vec::new();
|
||||||
|
|
||||||
|
if let Some(Token::LessThan) = stream.peek() {
|
||||||
|
stream.next();
|
||||||
|
while let Some(Token::Identifier(ident)) = stream.peek() {
|
||||||
|
stream.next();
|
||||||
|
generics.push(ident);
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.expect(Token::GreaterThan)?;
|
||||||
|
}
|
||||||
|
|
||||||
stream.expect(Token::ParenOpen)?;
|
stream.expect(Token::ParenOpen)?;
|
||||||
let mut params = Vec::new();
|
let mut params = Vec::new();
|
||||||
|
|
||||||
@ -814,6 +843,7 @@ impl Parse for FunctionSignature {
|
|||||||
|
|
||||||
Ok(FunctionSignature {
|
Ok(FunctionSignature {
|
||||||
name,
|
name,
|
||||||
|
generics,
|
||||||
documentation: None,
|
documentation: None,
|
||||||
params,
|
params,
|
||||||
self_kind,
|
self_kind,
|
||||||
@ -961,7 +991,8 @@ impl Parse for DotIndexKind {
|
|||||||
if let Ok(args) = stream.parse::<FunctionArgs>() {
|
if let Ok(args) = stream.parse::<FunctionArgs>() {
|
||||||
Ok(Self::FunctionCall(FunctionCallExpression {
|
Ok(Self::FunctionCall(FunctionCallExpression {
|
||||||
name,
|
name,
|
||||||
params: args.0,
|
generics: args.0,
|
||||||
|
params: args.1,
|
||||||
range: stream.get_range_prev().unwrap(),
|
range: stream.get_range_prev().unwrap(),
|
||||||
is_macro: false,
|
is_macro: false,
|
||||||
}))
|
}))
|
||||||
|
@ -44,8 +44,7 @@ impl ast::Module {
|
|||||||
let def = mir::FunctionDefinition {
|
let def = mir::FunctionDefinition {
|
||||||
name: signature.name.clone(),
|
name: signature.name.clone(),
|
||||||
documentation: signature.documentation.clone(),
|
documentation: signature.documentation.clone(),
|
||||||
// TODO generics parsing
|
generics: signature.generics.clone(),
|
||||||
generics: Vec::new(),
|
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: false,
|
is_pub: false,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
@ -383,8 +382,7 @@ impl ast::Expression {
|
|||||||
),
|
),
|
||||||
ast::ExpressionKind::FunctionCall(fn_call_expr) => mir::ExprKind::FunctionCall(mir::FunctionCall {
|
ast::ExpressionKind::FunctionCall(fn_call_expr) => mir::ExprKind::FunctionCall(mir::FunctionCall {
|
||||||
name: fn_call_expr.name.clone(),
|
name: fn_call_expr.name.clone(),
|
||||||
// TODO generics parsing
|
generics: fn_call_expr.generics.iter().map(|g| g.0.into_mir(module_id)).collect(),
|
||||||
generics: Vec::new(),
|
|
||||||
return_type: mir::TypeKind::Vague(mir::VagueType::Unknown),
|
return_type: mir::TypeKind::Vague(mir::VagueType::Unknown),
|
||||||
parameters: fn_call_expr.params.iter().map(|e| e.process(module_id)).collect(),
|
parameters: fn_call_expr.params.iter().map(|e| e.process(module_id)).collect(),
|
||||||
meta: fn_call_expr.range.as_meta(module_id),
|
meta: fn_call_expr.range.as_meta(module_id),
|
||||||
@ -474,8 +472,7 @@ impl ast::Expression {
|
|||||||
ty.0.into_mir(module_id),
|
ty.0.into_mir(module_id),
|
||||||
mir::FunctionCall {
|
mir::FunctionCall {
|
||||||
name: fn_call_expr.name.clone(),
|
name: fn_call_expr.name.clone(),
|
||||||
// TODO generics parsing
|
generics: fn_call_expr.generics.iter().map(|g| g.0.into_mir(module_id)).collect(),
|
||||||
generics: Vec::new(),
|
|
||||||
return_type: mir::TypeKind::Vague(mir::VagueType::Unknown),
|
return_type: mir::TypeKind::Vague(mir::VagueType::Unknown),
|
||||||
parameters: fn_call_expr.params.iter().map(|e| e.process(module_id)).collect(),
|
parameters: fn_call_expr.params.iter().map(|e| e.process(module_id)).collect(),
|
||||||
meta: fn_call_expr.range.as_meta(module_id),
|
meta: fn_call_expr.range.as_meta(module_id),
|
||||||
@ -499,8 +496,7 @@ impl ast::Expression {
|
|||||||
mir::TypeKind::Vague(mir::VagueType::Unknown),
|
mir::TypeKind::Vague(mir::VagueType::Unknown),
|
||||||
mir::FunctionCall {
|
mir::FunctionCall {
|
||||||
name: fn_call_expr.name.clone(),
|
name: fn_call_expr.name.clone(),
|
||||||
// TODO generics parsing
|
generics: fn_call_expr.generics.iter().map(|g| g.0.into_mir(module_id)).collect(),
|
||||||
generics: Vec::new(),
|
|
||||||
return_type: mir::TypeKind::Vague(mir::VagueType::Unknown),
|
return_type: mir::TypeKind::Vague(mir::VagueType::Unknown),
|
||||||
parameters: params,
|
parameters: params,
|
||||||
meta: fn_call_expr.range.as_meta(module_id),
|
meta: fn_call_expr.range.as_meta(module_id),
|
||||||
|
Loading…
Reference in New Issue
Block a user