updated rust ast through structs to work
This commit is contained in:
@@ -1,90 +1,129 @@
|
||||
use std::str::FromStr;
|
||||
use crate::ast;
|
||||
use crate::types;
|
||||
|
||||
grammar(id_generator: ast::IdGenerator);
|
||||
grammar(id_generator: &ast::IdGenerator);
|
||||
|
||||
match {
|
||||
r"[0-9]+",
|
||||
r"[0-9]+\.[0-9]+",
|
||||
r"[A-Za-z_][A-Za-z0-9_]*",
|
||||
":",
|
||||
";",
|
||||
"{",
|
||||
"}",
|
||||
"(",
|
||||
")",
|
||||
".",
|
||||
"+",
|
||||
"-",
|
||||
"*",
|
||||
"/",
|
||||
"fn",
|
||||
"return",
|
||||
"let",
|
||||
"=",
|
||||
"type",
|
||||
"struct",
|
||||
"impl",
|
||||
",",
|
||||
|
||||
r"\s*" => { },
|
||||
r"//[^\n\r]*[\n\r]*" => { }, // `// comment`
|
||||
}
|
||||
|
||||
|
||||
pub TypeUsage: ast::TypeUsage = {
|
||||
"fn" "(" <args:Comma<TypeUsage>> ")" => ast::TypeUsage::Function(ast::FunctionTypeUsage{arguments: args, return_type: Box::new(ast::new_unit())}),
|
||||
"fn" "(" <args:Comma<TypeUsage>> ")" ":" <return_type:TypeUsage> => ast::TypeUsage::Function(ast::FunctionTypeUsage{arguments: args, return_type: Box::new(return_type)),
|
||||
<name:SpannedIdentifier> => ast::TypeUsage::Named(ast::NamedTypeUsage{name: name})
|
||||
pub LiteralInt: String = {
|
||||
<literal:r"[0-9]+"> => literal.to_string()
|
||||
};
|
||||
|
||||
|
||||
pub LiteralInt: i64 = {
|
||||
<literal:r"[0-9]+"> => i64::from_str(literal).unwrap()
|
||||
pub SpannedLiteralInt: ast::LiteralInt = {
|
||||
<literal_int:Spanned<LiteralInt>> => ast::LiteralInt{value: literal_int, type_: ast::TypeUsage::new_builtin("i64".to_string())}
|
||||
};
|
||||
|
||||
pub SpannedLiteralInt: ast::LiteralInt {
|
||||
<literal_int:Spanned<LiteralInt>> => ast::LiteralInt{value: literal_int, type_: ast::TypeUsage::new_builtin("i64")}
|
||||
pub LiteralFloat: String = {
|
||||
<literal:r"[0-9]+\.[0-9]+"> => literal.to_string()
|
||||
};
|
||||
|
||||
pub LiteralFloat: f64 = {
|
||||
<literal:r"[0-9]+\.[0-9]+"> => f64::from_str(literal).unwrap()
|
||||
};
|
||||
|
||||
pub SpannedLiteralInt: ast::LiteralFloat {
|
||||
<literal_float:Spanned<LiteralFloat>> => ast::LiteralFloat{value: literal_float, type_: ast::TypeUsage::new_builtin("f64")}
|
||||
pub SpannedLiteralFloat: ast::LiteralFloat = {
|
||||
<literal_float:Spanned<LiteralFloat>> => ast::LiteralFloat{value: literal_float, type_: ast::TypeUsage::new_builtin("f64".to_string())}
|
||||
};
|
||||
|
||||
pub Identifier: String = {
|
||||
<i:r"[A-Za-z_][A-Za-z0-9_]*"> => i.to_string()
|
||||
};
|
||||
|
||||
pub LiteralStructField: (String, Expression) {
|
||||
pub LiteralStructField: (ast::Identifier, ast::Expression) = {
|
||||
<field:SpannedIdentifier> ":" <expr:Expression> => (field, expr)
|
||||
};
|
||||
|
||||
pub LiteralStruct: ast::LiteralStruct {
|
||||
<i:SpannedIdentifier> "{" <field_list:Comma<LiteralStructField>> "}" => LiteralStruct{
|
||||
name: i,
|
||||
fields: field_list.into_iter().collect(),
|
||||
pub LiteralStruct: ast::LiteralStruct = {
|
||||
<i:SpannedIdentifier> "{" <field_list:Comma<LiteralStructField>> "}" => ast::LiteralStruct{
|
||||
name: i.clone(),
|
||||
fields: field_list,
|
||||
type_: ast::TypeUsage::new_named(i.clone()),
|
||||
}
|
||||
};
|
||||
|
||||
pub SpannedIdentifier: ast::Identifier {
|
||||
pub SpannedIdentifier: ast::Identifier = {
|
||||
<i:Spanned<Identifier>> => ast::Identifier{name: i}
|
||||
};
|
||||
|
||||
pub FunctionCall: ast::FunctionCall = {
|
||||
<source:Expression> "(" <args:Comma<Expression>> ")" => ast::FunctionCall{source: source, arguments: args, type_: ast::TypeUsage::new_unknown(&mut id_generator)}
|
||||
<source:Term> "(" <args:Comma<Expression>> ")" => ast::FunctionCall{source: source, arguments: args, type_: ast::TypeUsage::new_unknown(&id_generator)}
|
||||
};
|
||||
|
||||
pub StructGetter: ast::StructGetter = {
|
||||
<source:Expression> "." <field:SpannedIdentifier> => ast::StructGetter{source: source, attribute: field, type: ast::TypeUsage::new_unknown(&mut id_generator)}
|
||||
<source:Term> "." <field:SpannedIdentifier> => ast::StructGetter{source: source, attribute: field, type_: ast::TypeUsage::new_unknown(&id_generator)}
|
||||
};
|
||||
|
||||
pub VariableUsage: ast::VariableUsage = {
|
||||
<identifier:SpannedIdentifier> => ast::VariableUsage{name: identifier, type_: ast::TypeUsage::new_unknown(&mut id_generator)}
|
||||
};
|
||||
|
||||
pub Subxpression: Box<ast::Subexpression> = {
|
||||
<l:Expression> "+" <r:Factor> => Box::new(ast::Subexpression::Op(ast::Operation{left: l, op: ast::Operator::Plus, right: r})),
|
||||
<l:Expression> "-" <r:Factor> => Box::new(ast::Subexpression::Op(ast::Operation{left: l, op: ast::Operator::Minus, right: r})),
|
||||
Factor,
|
||||
};
|
||||
|
||||
pub Factor: Box<ast::Subexpression> = {
|
||||
<l:Factor> "*" <r:Term> => Box::new(ast::Subexpression::Op(ast::Operation{left: l, op: ast::Operator::Mul, right: r})),
|
||||
<l:Factor> "/" <r:Term> => Box::new(ast::Subexpression::Op(ast::Operation{left: l, op: ast::Operator::Div, right: r})),
|
||||
Term,
|
||||
};
|
||||
|
||||
pub Term: Box<ast::Subexpression> = {
|
||||
SpannedLiteralInt => Box::new(ast::Subexpression::LiteralInt(<>)),
|
||||
SpannedLiteralFloat => Box::new(ast::Subexpression::LiteralFloat(<>)),
|
||||
SpannedLiteralStruct => Box::new(ast::Subexpression::LiteralStruct(<>)),
|
||||
FunctionCall => Box::new(ast::Subexpression::FunctionCall(<>)),
|
||||
Identifier => Box::new(ast::Subexpression::Identifier(<>)),
|
||||
"(" <e:Expression> ")" => e.subexpression,
|
||||
<identifier:SpannedIdentifier> => ast::VariableUsage{name: identifier, type_: ast::TypeUsage::new_unknown(&id_generator)}
|
||||
};
|
||||
|
||||
pub Expression: ast::Expression = {
|
||||
<sub:Subxpression> => ast::Expression{subexpression: sub, type_: ast::TypeUsage::new_unknown(&mut id_generator)}
|
||||
<l:Expression> "+" <r:Factor> => {
|
||||
ast::Expression{
|
||||
subexpression: Box::new(ast::Subexpression::Op(ast::Operation{left: l, op: ast::Operator::Plus, right: r})),
|
||||
type_: ast::TypeUsage::new_unknown(&id_generator),
|
||||
}
|
||||
},
|
||||
<l:Expression> "-" <r:Factor> => {
|
||||
ast::Expression{
|
||||
subexpression: Box::new(ast::Subexpression::Op(ast::Operation{left: l, op: ast::Operator::Minus, right: r})),
|
||||
type_: ast::TypeUsage::new_unknown(&id_generator),
|
||||
}
|
||||
},
|
||||
Factor,
|
||||
};
|
||||
|
||||
pub Factor: ast::Expression = {
|
||||
<l:Factor> "*" <r:Term> => {
|
||||
ast::Expression{
|
||||
subexpression: Box::new(ast::Subexpression::Op(ast::Operation{left: l, op: ast::Operator::Mul, right: r})),
|
||||
type_: ast::TypeUsage::new_unknown(&id_generator),
|
||||
}
|
||||
},
|
||||
<l:Factor> "/" <r:Term> => {
|
||||
ast::Expression{
|
||||
subexpression: Box::new(ast::Subexpression::Op(ast::Operation{left: l, op: ast::Operator::Div, right: r})),
|
||||
type_: ast::TypeUsage::new_unknown(&id_generator),
|
||||
}
|
||||
},
|
||||
Term,
|
||||
};
|
||||
|
||||
pub Term: ast::Expression = {
|
||||
SpannedLiteralInt => ast::Expression{subexpression: Box::new(ast::Subexpression::LiteralInt(<>)), type_: ast::TypeUsage::new_unknown(&id_generator)},
|
||||
SpannedLiteralFloat => ast::Expression{subexpression: Box::new(ast::Subexpression::LiteralFloat(<>)), type_: ast::TypeUsage::new_unknown(&id_generator)},
|
||||
LiteralStruct => ast::Expression{subexpression: Box::new(ast::Subexpression::LiteralStruct(<>)), type_: ast::TypeUsage::new_unknown(&id_generator)},
|
||||
FunctionCall => ast::Expression{subexpression: Box::new(ast::Subexpression::FunctionCall(<>)), type_: ast::TypeUsage::new_unknown(&id_generator)},
|
||||
StructGetter => ast::Expression{subexpression: Box::new(ast::Subexpression::StructGetter(<>)), type_: ast::TypeUsage::new_unknown(&id_generator)},
|
||||
VariableUsage => ast::Expression{subexpression: Box::new(ast::Subexpression::VariableUsage(<>)), type_: ast::TypeUsage::new_unknown(&id_generator)},
|
||||
Block => ast::Expression{subexpression: Box::new(ast::Subexpression::Block(<>)), type_: ast::TypeUsage::new_unknown(&id_generator)},
|
||||
"(" <e:Expression> ")" => e,
|
||||
};
|
||||
|
||||
|
||||
pub ReturnStatement: ast::ReturnStatement = {
|
||||
"return" <e:Expression> => ast::ReturnStatement{source: e}
|
||||
};
|
||||
@@ -92,82 +131,82 @@ pub ReturnStatement: ast::ReturnStatement = {
|
||||
pub LetStatement: ast::LetStatement = {
|
||||
//TODO: support destructuring with tuples, when they exist.
|
||||
//TODO: add mut, weak
|
||||
"let" <n:SpannedIdentifier> "=" <e:Expression> => ast::LetStatement{variable_name: n, type_usage: ast::TypeUsage::new_unknown(&mut id_generator), expression: e},
|
||||
"let" <n:SpannedIdentifier> ":" <t:TypeUsage> "=" <e:Expression> => ast::LetStatement{variable_name: n, type_usage: ast::TypeUsage::Named(ast::NamedTypeUsage{name: t}), expression: e},
|
||||
"let" <n:SpannedIdentifier> "=" <e:Expression> => ast::LetStatement{variable_name: n, type_: ast::TypeUsage::new_unknown(&id_generator), expression: e},
|
||||
"let" <n:SpannedIdentifier> ":" <t:TypeUsage> "=" <e:Expression> => ast::LetStatement{variable_name: n, type_: t, expression: e},
|
||||
};
|
||||
|
||||
pub AssignmentStatement: ast::AssignmentStatement {
|
||||
pub AssignmentStatement: ast::AssignmentStatement = {
|
||||
<v:VariableUsage> "=" <e:Expression> => ast::AssignmentStatement{source: ast::AssignmentTarget::Variable(v), expression: e},
|
||||
<sg:StructGetter> "=" <e:Expression> => ast::AssignmentStatement{source: ast::AssignmentTarget::StructAttr(sg), expression: e},
|
||||
}
|
||||
};
|
||||
|
||||
pub Statement: ast::Statement = {
|
||||
<r:ReturnStatement> ";" => ast::Statement::Return(r),
|
||||
<l:LetStatement> ";" => ast::Statement::Let(l),
|
||||
<a:AssignmentStatement> ";" => ast::Statement::Assignment(l),
|
||||
<a:AssignmentStatement> ";" => ast::Statement::Assignment(a),
|
||||
<e:Expression> ";" => ast::Statement::Expression(e),
|
||||
}
|
||||
};
|
||||
|
||||
pub Block: ast::Block = {
|
||||
"{" <v:(Statement ";")*> <e:Expression?> "}" => match e {
|
||||
None => ast::Block{statements: s, type_: ast::new_never()},
|
||||
"{" <v:(<Statement>)*> <e:Expression?> "}" => match e {
|
||||
None => ast::Block{statements: v, type_: ast::new_never()},
|
||||
Some(e) => {
|
||||
let mut v = v;
|
||||
v.push(ast::Statement::Expression(e));
|
||||
ast::Block{statements: v, type_: ast::TypeUsage::new_unknown(&mut id_generator)}
|
||||
ast::Block{statements: v, type_: ast::TypeUsage::new_unknown(&id_generator)}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
pub TypeUsage: ast::TypeUsage = {
|
||||
<n:SpannedIdentifier> => ast::TypeUsage::Named(ast::NamedTypeUsage{name: n}),
|
||||
"fn" "(" <args:Comma<TypeUsage>> ")" => ast::TypeUsage::Function(ast::FunctionTypeUsage{arguments: args, return_type: Box::new(ast::TypeUsage::new_unknown(&mut id_generator))}),
|
||||
"fn" "(" <args:Comma<TypeUsage>> ")" => ast::TypeUsage::Function(ast::FunctionTypeUsage{arguments: args, return_type: Box::new(ast::TypeUsage::new_unknown(&id_generator))}),
|
||||
"fn" "(" <args:Comma<TypeUsage>> ")" ":" <rt:TypeUsage> => ast::TypeUsage::Function(ast::FunctionTypeUsage{arguments: args, return_type: Box::new(rt)}),
|
||||
}
|
||||
};
|
||||
|
||||
pub VariableDeclaration: ast::VariableDeclaration = {
|
||||
<i:SpannedIdentifier> ":" <t:TypeUsage> => ast::VariableDeclaration{name: i, type_usage: t},
|
||||
}
|
||||
<i:SpannedIdentifier> ":" <t:TypeUsage> => ast::VariableDeclaration{name: i, type_: t},
|
||||
};
|
||||
|
||||
pub FunctionDeclaration: ast::FunctionDeclaration {
|
||||
"fn" <n:SpannedIdentifier> "(" <args:Comma<VariableDeclaration>> ")" => ast::FunctionDeclaration{name: n, arguments: args, return_type: ast::TypeUsage::new_unknown(&mut id_generator)},
|
||||
pub FunctionDeclaration: ast::FunctionDeclaration = {
|
||||
"fn" <n:SpannedIdentifier> "(" <args:Comma<VariableDeclaration>> ")" => ast::FunctionDeclaration{name: n, arguments: args, return_type: ast::TypeUsage::new_unknown(&id_generator)},
|
||||
"fn" <n:SpannedIdentifier> "(" <args:Comma<VariableDeclaration>> ")" ":" <rt:TypeUsage> => ast::FunctionDeclaration{name: n, arguments: args, return_type: rt},
|
||||
}
|
||||
};
|
||||
|
||||
pub Function: ast::Function = {
|
||||
<d:FunctionDeclaration> <b:Block> => ast::Function{declaration: d, block: b}
|
||||
}
|
||||
};
|
||||
|
||||
pub StructField: ast::StructField = {
|
||||
<i:SpannedIdentifier> ":" <t:TypeUsage> => (i, t),
|
||||
}
|
||||
<i:SpannedIdentifier> ":" <t:TypeUsage> => ast::StructField{name: i, type_: t},
|
||||
};
|
||||
|
||||
pub StructTypeDeclaration: ast::StructTypeDeclaration {
|
||||
"type" <i:SpannedIdentifier> "struct" "{" Comma<StructField> "}"
|
||||
}
|
||||
pub StructTypeDeclaration: ast::StructTypeDeclaration = {
|
||||
"type" <i:SpannedIdentifier> "struct" "{" <f:Comma<StructField>> "}" => ast::StructTypeDeclaration{name: i, fields: f}
|
||||
};
|
||||
|
||||
pub TypeAliasDeclaration: ast::TypeAliasDeclaration {
|
||||
pub AliasTypeDeclaration: ast::AliasTypeDeclaration = {
|
||||
"type" <i:SpannedIdentifier> "=" <t:TypeUsage> ";" => ast::AliasTypeDeclaration{name: i, replaces: t}
|
||||
}
|
||||
};
|
||||
|
||||
pub TypeDeclaration: ast::TypeDeclaration {
|
||||
pub TypeDeclaration: ast::TypeDeclaration = {
|
||||
<s:StructTypeDeclaration> => ast::TypeDeclaration::Struct(s),
|
||||
<a:AliasTypeDeclaration> => ast::TypeDeclaration::Alias(a),
|
||||
}
|
||||
};
|
||||
|
||||
pub Impl: ast::Impl {
|
||||
"impl" <i:SpannedIdentifier> "{" <f:Function*> "}" => ast::Impl{struct_name: i, functions: s}
|
||||
}
|
||||
pub Impl: ast::Impl = {
|
||||
"impl" <i:SpannedIdentifier> "{" <f:Function*> "}" => ast::Impl{struct_name: i, functions: f}
|
||||
};
|
||||
|
||||
pub ModuleItem: ast::ModuleItem {
|
||||
pub ModuleItem: ast::ModuleItem = {
|
||||
<f:Function> => ast::ModuleItem::Function(f),
|
||||
<td:TypeDeclaration> => ast::ModuleItem::TypeDeclaration(td),
|
||||
<i:Impl> => ast::ModuleItem::Impl(i),
|
||||
}
|
||||
};
|
||||
|
||||
pub Module: ast::Module = {
|
||||
<i:ModuleItem*> => ast::Module{items: i}
|
||||
}
|
||||
};
|
||||
|
||||
// From https://lalrpop.github.io/lalrpop/tutorial/006_macros.html
|
||||
// Comma seperated list of T with optional trailing comma
|
||||
|
||||
Reference in New Issue
Block a user