added rust ast
This commit is contained in:
98
src/ast.rs
98
src/ast.rs
@@ -11,9 +11,9 @@ impl IdGenerator {
|
|||||||
IdGenerator{counter: 0}
|
IdGenerator{counter: 0}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next(&mut self) -> i64 {
|
pub fn next(&mut self) -> String {
|
||||||
self.counter += 1;
|
self.counter += 1;
|
||||||
self.counter
|
("S" + self.counter.to_string()).to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +67,7 @@ pub struct Spanned<T> {
|
|||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct FunctionTypeUsage {
|
pub struct FunctionTypeUsage {
|
||||||
pub arguments: Vec<TypeUsage>,
|
pub arguments: Vec<TypeUsage>,
|
||||||
pub return_type: TypeUsage,
|
pub return_type: Box<TypeUsage>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
@@ -87,6 +87,38 @@ pub enum TypeUsage {
|
|||||||
Unknown(UnknownTypeUsage),
|
Unknown(UnknownTypeUsage),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TypeUsage {
|
||||||
|
pub fn new_unknown(id_gen: &mut IdGenerator) -> TypeUsage {
|
||||||
|
return TypeUsage::Unknown(UnknownTypeUsage{
|
||||||
|
name: id_gen.next(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_named(identifier: &Identifier) -> TypeUsage {
|
||||||
|
return TypeUsage::Named(NamedTypeUsage{
|
||||||
|
name: identifier.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_builtin(name: String) -> TypeUsage {
|
||||||
|
ast::TypeUsage::Named(ast::NamedTypeUsage{
|
||||||
|
name: ast::Identifier{
|
||||||
|
name: ast::Spanned{
|
||||||
|
span: ast::Span{left: 0, right: 0}, //todo: figure out a sane value for these
|
||||||
|
value: name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_function(arg_count: usize, id_gen: &mut IdGenerator) -> TypeUsage {
|
||||||
|
return TypeUsage::Function(FunctionTypeUsage{
|
||||||
|
arguments: 0..arg_count.map(|_| => TypeUsage.new_unknown(&mut id_gen)).collect(),
|
||||||
|
return_type: Box::new(TypeUsage.new_unknown(&mut id_gen)),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum Operator {
|
pub enum Operator {
|
||||||
Mul,
|
Mul,
|
||||||
@@ -98,19 +130,20 @@ pub enum Operator {
|
|||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct LiteralInt {
|
pub struct LiteralInt {
|
||||||
pub value: Spanned<i64>,
|
pub value: Spanned<i64>,
|
||||||
pub type: TypeUsage,
|
pub type_: TypeUsage,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct LiteralFloat {
|
pub struct LiteralFloat {
|
||||||
pub value: Spanned<f64>,
|
pub value: Spanned<f64>,
|
||||||
pub type: TypeUsage,
|
pub type_: TypeUsage,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct LiteralStruct {
|
pub struct LiteralStruct {
|
||||||
|
pub name: Identifier,
|
||||||
pub fields: HashMap<Identifier, Expression>,
|
pub fields: HashMap<Identifier, Expression>,
|
||||||
pub type: TypeUsage,
|
pub type_: TypeUsage,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
@@ -122,14 +155,14 @@ pub struct Identifier {
|
|||||||
pub struct FunctionCall {
|
pub struct FunctionCall {
|
||||||
pub source: Expression,
|
pub source: Expression,
|
||||||
pub arguments: Vec<Expression>,
|
pub arguments: Vec<Expression>,
|
||||||
pub type: TypeUsage,
|
pub type_: TypeUsage,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct StructGetter {
|
pub struct StructGetter {
|
||||||
pub source: Box<Expression>,
|
pub source: Expression,
|
||||||
pub attribute: Identifier,
|
pub attribute: Identifier,
|
||||||
pub type: TypeUsage,
|
pub type_: TypeUsage,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
@@ -137,18 +170,20 @@ pub struct Operation {
|
|||||||
pub left: Expression,
|
pub left: Expression,
|
||||||
pub op: Operator,
|
pub op: Operator,
|
||||||
pub right: Expression,
|
pub right: Expression,
|
||||||
pub type: TypeUsage,
|
pub type_: TypeUsage,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct VariableUsage {
|
pub struct VariableUsage {
|
||||||
pub name: Identifier,
|
pub name: Identifier,
|
||||||
pub type: TypeUsage,
|
pub type_: TypeUsage,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum Subexpression {
|
pub enum Subexpression {
|
||||||
LiteralInt(LiteralInt),
|
LiteralInt(LiteralInt),
|
||||||
|
LiteralFloat(LiteralFloat),
|
||||||
|
LiteralStruct(LiteralStruct),
|
||||||
FunctionCall(FunctionCall),
|
FunctionCall(FunctionCall),
|
||||||
Identifier(Identifier),
|
Identifier(Identifier),
|
||||||
Op(Operation),
|
Op(Operation),
|
||||||
@@ -157,20 +192,19 @@ pub enum Subexpression {
|
|||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct Expression {
|
pub struct Expression {
|
||||||
pub subexpression: Spanned<Box<Subexpression>>,
|
pub subexpression: Spanned<Box<Subexpression>>,
|
||||||
pub type: TypeUsage,
|
pub type_: TypeUsage,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct ReturnStatement {
|
pub struct ReturnStatement {
|
||||||
pub source: Expression,
|
pub source: Expression,
|
||||||
pub type: TypeUsage,
|
};
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct LetStatement {
|
pub struct LetStatement {
|
||||||
variable_name: Identifier,
|
variable_name: Identifier,
|
||||||
type: VariableUsage,
|
|
||||||
expression: Expression,
|
expression: Expression,
|
||||||
|
type_: TypeUsage,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum AssignmentTarget {
|
pub enum AssignmentTarget {
|
||||||
@@ -182,25 +216,26 @@ pub enum AssignmentTarget {
|
|||||||
pub struct AssignmentStatement {
|
pub struct AssignmentStatement {
|
||||||
pub source: AssignmentTarget,
|
pub source: AssignmentTarget,
|
||||||
pub expression: Expression,
|
pub expression: Expression,
|
||||||
pub type: TypeUsage,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum Statement {
|
pub enum Statement {
|
||||||
Assignment(Assignment),
|
Return(ReturnStatement),
|
||||||
|
Let(LetStatement),
|
||||||
|
Assignment(AssignmentStatement),
|
||||||
Expression(Expression),
|
Expression(Expression),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct Block {
|
pub struct Block {
|
||||||
pub statements: Vec<Spanned<Statement>>,
|
pub statements: Vec<Statement>,
|
||||||
pub type: TypeUsage,
|
pub type_: TypeUsage,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct VariableDeclaration {
|
pub struct VariableDeclaration {
|
||||||
pub name: Identifier,
|
pub name: Identifier,
|
||||||
pub type: TypeUsage,
|
pub type_: TypeUsage,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
@@ -221,10 +256,16 @@ pub struct PrimitiveTypeDeclaration {
|
|||||||
pub name: String, // cannot be identifier as it's not declared anywhere specific, it's builtins
|
pub name: String, // cannot be identifier as it's not declared anywhere specific, it's builtins
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct StructField {
|
||||||
|
pub name: Identifier,
|
||||||
|
pub type_: TypeUsage,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct StructTypeDeclaration {
|
pub struct StructTypeDeclaration {
|
||||||
pub name: Identifier,
|
pub name: Identifier,
|
||||||
pub fields: HashMap<Identifier, TypeUsage>,
|
pub fields: Vec<StructField>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
@@ -241,14 +282,19 @@ pub enum TypeDeclaration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum Impl {
|
pub struct Impl {
|
||||||
pub struct_name: Identifier,
|
pub struct_name: Identifier,
|
||||||
pub functions: Vec<Function>,
|
pub functions: Vec<Function>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct Module {
|
pub enum ModuleItem {
|
||||||
pub functions: Vec<Function>,
|
Function(Function),
|
||||||
pub types: Vec<TypeDeclaration>,
|
TypeDeclaration(TypeDeclaration),
|
||||||
pub impls: Vec<Impls>,
|
Impl(Impl),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct Module {
|
||||||
|
pub items: Vec<ModuleItem>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ grammar(id_generator: ast::IdGenerator);
|
|||||||
|
|
||||||
|
|
||||||
pub TypeUsage: ast::TypeUsage = {
|
pub TypeUsage: ast::TypeUsage = {
|
||||||
"fn" "(" <args:Comma<TypeUsage>> ")" => ast::TypeUsage::Function(ast::FunctionTypeUsage{arguments: args, return_type: ast::new_unit()}),
|
"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: return_type),
|
"fn" "(" <args:Comma<TypeUsage>> ")" ":" <return_type:TypeUsage> => ast::TypeUsage::Function(ast::FunctionTypeUsage{arguments: args, return_type: Box::new(return_type)),
|
||||||
<name:Identifier> => ast::TypeUsage::Named(ast::NamedTypeUsage{name: name})
|
<name:SpannedIdentifier> => ast::TypeUsage::Named(ast::NamedTypeUsage{name: name})
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ pub LiteralInt: i64 = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub SpannedLiteralInt: ast::LiteralInt {
|
pub SpannedLiteralInt: ast::LiteralInt {
|
||||||
<literal_int:Spanned<LiteralInt>> => ast::LiteralInt{value: literal_int, type: ast::new_named("i64")},
|
<literal_int:Spanned<LiteralInt>> => ast::LiteralInt{value: literal_int, type_: ast::TypeUsage::new_builtin("i64")}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub LiteralFloat: f64 = {
|
pub LiteralFloat: f64 = {
|
||||||
@@ -25,70 +25,150 @@ pub LiteralFloat: f64 = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub SpannedLiteralInt: ast::LiteralFloat {
|
pub SpannedLiteralInt: ast::LiteralFloat {
|
||||||
<literal_float:Spanned<LiteralFloat>> => ast::LiteralFloat{value: literal_float, type: ast::new_named("f64")},
|
<literal_float:Spanned<LiteralFloat>> => ast::LiteralFloat{value: literal_float, type_: ast::TypeUsage::new_builtin("f64")}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub Identifier: ast::Identifier = {
|
pub Identifier: String = {
|
||||||
<i:r"[A-Za-z][A-Za-z0-9_]*"> => ast::Identifier{name: i.to_string()}
|
<i:r"[A-Za-z_][A-Za-z0-9_]*"> => i.to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
pub LiteralStructField: (String, 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(),
|
||||||
|
type_: ast::TypeUsage::new_named(i.clone()),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub SpannedIdentifier: ast::Identifier {
|
||||||
|
<i:Spanned<Identifier>> => ast::Identifier{name: i}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub FunctionCall: ast::FunctionCall = {
|
pub FunctionCall: ast::FunctionCall = {
|
||||||
<i:Spanned<Identifier>> "(" <args:Comma<Spanned<Expression>>> ")" => ast::FunctionCall{name:i, arguments: args}
|
<source:Expression> "(" <args:Comma<Expression>> ")" => ast::FunctionCall{source: source, arguments: args, type_: ast::TypeUsage::new_unknown(&mut id_generator)}
|
||||||
}
|
};
|
||||||
|
|
||||||
pub Expression: Box<ast::Expression> = {
|
pub StructGetter: ast::StructGetter = {
|
||||||
<l:Spanned<Expression>> "+" <r:Spanned<Factor>> => Box::new(ast::Expression::Op(ast::Operation{left: l, op: ast::Operator::Plus, right: r})),
|
<source:Expression> "." <field:SpannedIdentifier> => ast::StructGetter{source: source, attribute: field, type: ast::TypeUsage::new_unknown(&mut id_generator)}
|
||||||
<l:Spanned<Expression>> "-" <r:Spanned<Factor>> => Box::new(ast::Expression::Op(ast::Operation{left: l, op: ast::Operator::Minus, right: r})),
|
};
|
||||||
|
|
||||||
|
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,
|
Factor,
|
||||||
}
|
};
|
||||||
|
|
||||||
pub Factor: Box<ast::Expression> = {
|
pub Factor: Box<ast::Subexpression> = {
|
||||||
<l:Spanned<Factor>> "*" <r:Spanned<Term>> => Box::new(ast::Expression::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::Mul, right: r})),
|
||||||
<l:Spanned<Factor>> "/" <r:Spanned<Term>> => Box::new(ast::Expression::Op(ast::Operation{left: l, op: ast::Operator::Div, right: r})),
|
<l:Factor> "/" <r:Term> => Box::new(ast::Subexpression::Op(ast::Operation{left: l, op: ast::Operator::Div, right: r})),
|
||||||
Term,
|
Term,
|
||||||
}
|
};
|
||||||
|
|
||||||
pub Term: Box<ast::Expression> = {
|
pub Term: Box<ast::Subexpression> = {
|
||||||
Spanned<LiteralInt> => Box::new(ast::Expression::LiteralInt(<>)),
|
SpannedLiteralInt => Box::new(ast::Subexpression::LiteralInt(<>)),
|
||||||
Spanned<Identifier> => Box::new(ast::Expression::Identifier(<>)),
|
SpannedLiteralFloat => Box::new(ast::Subexpression::LiteralFloat(<>)),
|
||||||
Spanned<FunctionCall> => Box::new(ast::Expression::FunctionCall(<>)),
|
SpannedLiteralStruct => Box::new(ast::Subexpression::LiteralStruct(<>)),
|
||||||
"(" <e:Expression> ")" => e
|
FunctionCall => Box::new(ast::Subexpression::FunctionCall(<>)),
|
||||||
}
|
Identifier => Box::new(ast::Subexpression::Identifier(<>)),
|
||||||
|
"(" <e:Expression> ")" => e.subexpression,
|
||||||
|
};
|
||||||
|
|
||||||
pub LetStatement: ast::Assignment = {
|
pub Expression: ast::Expression = {
|
||||||
|
<sub:Subxpression> => ast::Expression{subexpression: sub, type_: ast::TypeUsage::new_unknown(&mut id_generator)}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub ReturnStatement: ast::ReturnStatement = {
|
||||||
|
"return" <e:Expression> => ast::ReturnStatement{source: e}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub LetStatement: ast::LetStatement = {
|
||||||
//TODO: support destructuring with tuples, when they exist.
|
//TODO: support destructuring with tuples, when they exist.
|
||||||
//TODO: add mut, weak
|
//TODO: add mut, weak
|
||||||
"let" <n:Spanned<Identifier>> "=" <e:Spanned<Expression>> => ast::Assignment{name: n, type_usage: None, expression: e},
|
"let" <n:SpannedIdentifier> "=" <e:Expression> => ast::LetStatement{variable_name: n, type_usage: ast::TypeUsage::new_unknown(&mut id_generator), expression: e},
|
||||||
"let" <n:Spanned<Identifier>> ":" <t:Spanned<TypeUsage>> "=" <e:Spanned<Expression>> => ast::Assignment{name: n, type_usage: Some(t), 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},
|
||||||
|
};
|
||||||
|
|
||||||
|
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 = {
|
pub Statement: ast::Statement = {
|
||||||
<l:Spanned<LetStatement>> => ast::Statement::Assignment(l),
|
<r:ReturnStatement> ";" => ast::Statement::Return(r),
|
||||||
<e:Spanned<Expression>> => ast::Statement::Expression(e),
|
<l:LetStatement> ";" => ast::Statement::Let(l),
|
||||||
|
<a:AssignmentStatement> ";" => ast::Statement::Assignment(l),
|
||||||
|
<e:Expression> ";" => ast::Statement::Expression(e),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub Block: ast::Block = {
|
pub Block: ast::Block = {
|
||||||
"{" <s:LineDelimiter<Spanned<Statement>>> "}" => ast::Block{statements: s}
|
"{" <v:(Statement ";")*> <e:Expression?> "}" => match e {
|
||||||
|
None => ast::Block{statements: s, 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)}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub TypeUsage: ast::TypeUsage = {
|
pub TypeUsage: ast::TypeUsage = {
|
||||||
<n:Spanned<Identifier>> => ast::TypeUsage{name: n, ty: types::SpecifiedType::Unknown}
|
<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>> ")" ":" <rt:TypeUsage> => ast::TypeUsage::Function(ast::FunctionTypeUsage{arguments: args, return_type: Box::new(rt)}),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub VariableDeclaration: ast::VariableDeclaration = {
|
pub VariableDeclaration: ast::VariableDeclaration = {
|
||||||
<i:Spanned<Identifier>> ":" <t:Spanned<TypeUsage>> => ast::VariableDeclaration{name: i, type_usage: t},
|
<i:SpannedIdentifier> ":" <t:TypeUsage> => ast::VariableDeclaration{name: i, type_usage: 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)},
|
||||||
|
"fn" <n:SpannedIdentifier> "(" <args:Comma<VariableDeclaration>> ")" ":" <rt:TypeUsage> => ast::FunctionDeclaration{name: n, arguments: args, return_type: rt},
|
||||||
}
|
}
|
||||||
|
|
||||||
pub Function: ast::Function = {
|
pub Function: ast::Function = {
|
||||||
"fn" <n:Spanned<Identifier>> "(" <args:Comma<VariableDeclaration>> ")" <rt:Spanned<TypeUsage>> <b:Block> => ast::Function{name: n, arguments: args, block: b, return_type: rt}
|
<d:FunctionDeclaration> <b:Block> => ast::Function{declaration: d, block: b}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub StructField: ast::StructField = {
|
||||||
|
<i:SpannedIdentifier> ":" <t:TypeUsage> => (i, t),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub StructTypeDeclaration: ast::StructTypeDeclaration {
|
||||||
|
"type" <i:SpannedIdentifier> "struct" "{" Comma<StructField> "}"
|
||||||
|
}
|
||||||
|
|
||||||
|
pub TypeAliasDeclaration: ast::TypeAliasDeclaration {
|
||||||
|
"type" <i:SpannedIdentifier> "=" <t:TypeUsage> ";" => ast::AliasTypeDeclaration{name: i, replaces: t}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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 = {
|
pub Module: ast::Module = {
|
||||||
<fs:Function*> => ast::Module{functions: fs}
|
<i:ModuleItem*> => ast::Module{items: i}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// From https://lalrpop.github.io/lalrpop/tutorial/006_macros.html
|
// From https://lalrpop.github.io/lalrpop/tutorial/006_macros.html
|
||||||
// Comma seperated list of T with optional trailing comma
|
// Comma seperated list of T with optional trailing comma
|
||||||
Comma<T>: Vec<T> = {
|
Comma<T>: Vec<T> = {
|
||||||
@@ -102,16 +182,6 @@ Comma<T>: Vec<T> = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
LineDelimiter<T>: Vec<T> = {
|
|
||||||
<v:(<T> r"\n|;")*> <e:T> => {
|
|
||||||
let mut v = v;
|
|
||||||
v.push(e);
|
|
||||||
v
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
Spanned<Rule>: ast::Spanned<Rule> = {
|
Spanned<Rule>: ast::Spanned<Rule> = {
|
||||||
<l: @L> <rule: Rule> <r: @R> => ast::Spanned{span: ast::Span{left: l, right: r}, value: rule}
|
<l: @L> <rule: Rule> <r: @R> => ast::Spanned{span: ast::Span{left: l, right: r}, value: rule}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user