2020-04-08 00:04:36 -06:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
use crate::ast;
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
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`
|
|
|
|
|
}
|
2021-07-23 08:58:17 -06:00
|
|
|
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub LiteralInt: String = {
|
|
|
|
|
<literal:r"[0-9]+"> => literal.to_string()
|
2021-07-23 08:58:17 -06:00
|
|
|
};
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub SpannedLiteralInt: ast::LiteralInt = {
|
2021-09-11 12:32:08 -06:00
|
|
|
<literal_int:Spanned<LiteralInt>> => ast::LiteralInt{value: literal_int, type_: ast::TypeUsage::new_builtin("i64".to_string())}
|
2021-07-23 08:58:17 -06:00
|
|
|
};
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub LiteralFloat: String = {
|
|
|
|
|
<literal:r"[0-9]+\.[0-9]+"> => literal.to_string()
|
2021-07-23 08:58:17 -06:00
|
|
|
};
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub SpannedLiteralFloat: ast::LiteralFloat = {
|
2021-09-11 12:32:08 -06:00
|
|
|
<literal_float:Spanned<LiteralFloat>> => ast::LiteralFloat{value: literal_float, type_: ast::TypeUsage::new_builtin("f64".to_string())}
|
2020-04-08 00:04:36 -06:00
|
|
|
};
|
|
|
|
|
|
2021-08-07 20:20:11 -06:00
|
|
|
pub Identifier: String = {
|
|
|
|
|
<i:r"[A-Za-z_][A-Za-z0-9_]*"> => i.to_string()
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub LiteralStructField: (ast::Identifier, ast::Expression) = {
|
2021-08-07 20:20:11 -06:00
|
|
|
<field:SpannedIdentifier> ":" <expr:Expression> => (field, expr)
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub LiteralStruct: ast::LiteralStruct = {
|
|
|
|
|
<i:SpannedIdentifier> "{" <field_list:Comma<LiteralStructField>> "}" => ast::LiteralStruct{
|
|
|
|
|
name: i.clone(),
|
|
|
|
|
fields: field_list,
|
2021-08-07 20:20:11 -06:00
|
|
|
type_: ast::TypeUsage::new_named(i.clone()),
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub SpannedIdentifier: ast::Identifier = {
|
2021-08-07 20:20:11 -06:00
|
|
|
<i:Spanned<Identifier>> => ast::Identifier{name: i}
|
2020-04-08 00:04:36 -06:00
|
|
|
};
|
|
|
|
|
|
2020-04-13 23:59:01 -06:00
|
|
|
pub FunctionCall: ast::FunctionCall = {
|
2021-08-08 11:42:26 -06:00
|
|
|
<source:Term> "(" <args:Comma<Expression>> ")" => ast::FunctionCall{source: source, arguments: args, type_: ast::TypeUsage::new_unknown(&id_generator)}
|
2021-08-07 20:20:11 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub StructGetter: ast::StructGetter = {
|
2021-08-08 11:42:26 -06:00
|
|
|
<source:Term> "." <field:SpannedIdentifier> => ast::StructGetter{source: source, attribute: field, type_: ast::TypeUsage::new_unknown(&id_generator)}
|
2021-08-07 20:20:11 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub VariableUsage: ast::VariableUsage = {
|
2021-08-08 11:42:26 -06:00
|
|
|
<identifier:SpannedIdentifier> => ast::VariableUsage{name: identifier, type_: ast::TypeUsage::new_unknown(&id_generator)}
|
2021-08-07 20:20:11 -06:00
|
|
|
};
|
2020-04-13 23:59:01 -06:00
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub Expression: ast::Expression = {
|
|
|
|
|
<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),
|
|
|
|
|
}
|
|
|
|
|
},
|
2020-04-08 00:04:36 -06:00
|
|
|
Factor,
|
2021-08-07 20:20:11 -06:00
|
|
|
};
|
2020-04-08 00:04:36 -06:00
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
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),
|
|
|
|
|
}
|
|
|
|
|
},
|
2020-04-08 00:04:36 -06:00
|
|
|
Term,
|
2021-08-07 20:20:11 -06:00
|
|
|
};
|
2020-04-08 00:04:36 -06:00
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
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,
|
2021-08-07 20:20:11 -06:00
|
|
|
};
|
|
|
|
|
|
2020-07-23 23:54:01 -06:00
|
|
|
|
2021-08-07 20:20:11 -06:00
|
|
|
pub ReturnStatement: ast::ReturnStatement = {
|
|
|
|
|
"return" <e:Expression> => ast::ReturnStatement{source: e}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub LetStatement: ast::LetStatement = {
|
2020-07-23 23:54:01 -06:00
|
|
|
//TODO: support destructuring with tuples, when they exist.
|
|
|
|
|
//TODO: add mut, weak
|
2021-08-08 11:42:26 -06:00
|
|
|
"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},
|
2021-08-07 20:20:11 -06:00
|
|
|
};
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub AssignmentStatement: ast::AssignmentStatement = {
|
2021-08-07 20:20:11 -06:00
|
|
|
<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},
|
2021-08-08 11:42:26 -06:00
|
|
|
};
|
2020-07-23 23:54:01 -06:00
|
|
|
|
|
|
|
|
pub Statement: ast::Statement = {
|
2021-08-07 20:20:11 -06:00
|
|
|
<r:ReturnStatement> ";" => ast::Statement::Return(r),
|
|
|
|
|
<l:LetStatement> ";" => ast::Statement::Let(l),
|
2021-08-08 11:42:26 -06:00
|
|
|
<a:AssignmentStatement> ";" => ast::Statement::Assignment(a),
|
2021-08-07 20:20:11 -06:00
|
|
|
<e:Expression> ";" => ast::Statement::Expression(e),
|
2021-08-08 11:42:26 -06:00
|
|
|
};
|
2020-04-08 00:04:36 -06:00
|
|
|
|
|
|
|
|
pub Block: ast::Block = {
|
2021-08-08 11:42:26 -06:00
|
|
|
"{" <v:(<Statement>)*> <e:Expression?> "}" => match e {
|
2021-09-05 23:58:21 -06:00
|
|
|
None => ast::Block{statements: v, type_: ast::TypeUsage::new_unknown(&id_generator)},
|
2021-08-07 20:20:11 -06:00
|
|
|
Some(e) => {
|
|
|
|
|
let mut v = v;
|
|
|
|
|
v.push(ast::Statement::Expression(e));
|
2021-08-08 11:42:26 -06:00
|
|
|
ast::Block{statements: v, type_: ast::TypeUsage::new_unknown(&id_generator)}
|
2021-08-07 20:20:11 -06:00
|
|
|
}
|
|
|
|
|
}
|
2021-08-08 11:42:26 -06:00
|
|
|
};
|
2020-07-23 23:54:01 -06:00
|
|
|
|
|
|
|
|
pub TypeUsage: ast::TypeUsage = {
|
2021-08-07 20:20:11 -06:00
|
|
|
<n:SpannedIdentifier> => ast::TypeUsage::Named(ast::NamedTypeUsage{name: n}),
|
2021-09-05 23:58:21 -06:00
|
|
|
"fn" "(" <args:Comma<TypeUsage>> ")" => ast::TypeUsage::Function(ast::FunctionTypeUsage{arguments: args, return_type: Box::new(ast::new_unit())}),
|
2021-08-07 20:20:11 -06:00
|
|
|
"fn" "(" <args:Comma<TypeUsage>> ")" ":" <rt:TypeUsage> => ast::TypeUsage::Function(ast::FunctionTypeUsage{arguments: args, return_type: Box::new(rt)}),
|
2021-08-08 11:42:26 -06:00
|
|
|
};
|
2020-04-08 00:04:36 -06:00
|
|
|
|
|
|
|
|
pub VariableDeclaration: ast::VariableDeclaration = {
|
2021-08-08 11:42:26 -06:00
|
|
|
<i:SpannedIdentifier> ":" <t:TypeUsage> => ast::VariableDeclaration{name: i, type_: t},
|
|
|
|
|
};
|
2021-08-07 20:20:11 -06:00
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub FunctionDeclaration: ast::FunctionDeclaration = {
|
2021-09-05 23:58:21 -06:00
|
|
|
"fn" <n:SpannedIdentifier> "(" <args:Comma<VariableDeclaration>> ")" => ast::FunctionDeclaration{name: n, arguments: args, return_type: ast::new_unit()},
|
2021-08-07 20:20:11 -06:00
|
|
|
"fn" <n:SpannedIdentifier> "(" <args:Comma<VariableDeclaration>> ")" ":" <rt:TypeUsage> => ast::FunctionDeclaration{name: n, arguments: args, return_type: rt},
|
2021-08-08 11:42:26 -06:00
|
|
|
};
|
2020-04-08 00:04:36 -06:00
|
|
|
|
|
|
|
|
pub Function: ast::Function = {
|
2021-08-07 20:20:11 -06:00
|
|
|
<d:FunctionDeclaration> <b:Block> => ast::Function{declaration: d, block: b}
|
2021-08-08 11:42:26 -06:00
|
|
|
};
|
2020-04-08 00:04:36 -06:00
|
|
|
|
2021-08-07 20:20:11 -06:00
|
|
|
pub StructField: ast::StructField = {
|
2021-08-08 11:42:26 -06:00
|
|
|
<i:SpannedIdentifier> ":" <t:TypeUsage> => ast::StructField{name: i, type_: t},
|
|
|
|
|
};
|
2020-04-08 00:04:36 -06:00
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub StructTypeDeclaration: ast::StructTypeDeclaration = {
|
|
|
|
|
"type" <i:SpannedIdentifier> "struct" "{" <f:Comma<StructField>> "}" => ast::StructTypeDeclaration{name: i, fields: f}
|
|
|
|
|
};
|
2021-08-07 20:20:11 -06:00
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub AliasTypeDeclaration: ast::AliasTypeDeclaration = {
|
2021-08-07 20:20:11 -06:00
|
|
|
"type" <i:SpannedIdentifier> "=" <t:TypeUsage> ";" => ast::AliasTypeDeclaration{name: i, replaces: t}
|
2021-08-08 11:42:26 -06:00
|
|
|
};
|
2021-08-07 20:20:11 -06:00
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub TypeDeclaration: ast::TypeDeclaration = {
|
2021-08-07 20:20:11 -06:00
|
|
|
<s:StructTypeDeclaration> => ast::TypeDeclaration::Struct(s),
|
|
|
|
|
<a:AliasTypeDeclaration> => ast::TypeDeclaration::Alias(a),
|
2021-08-08 11:42:26 -06:00
|
|
|
};
|
2021-08-07 20:20:11 -06:00
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub Impl: ast::Impl = {
|
|
|
|
|
"impl" <i:SpannedIdentifier> "{" <f:Function*> "}" => ast::Impl{struct_name: i, functions: f}
|
|
|
|
|
};
|
2020-04-08 00:04:36 -06:00
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub ModuleItem: ast::ModuleItem = {
|
2021-08-07 20:20:11 -06:00
|
|
|
<f:Function> => ast::ModuleItem::Function(f),
|
|
|
|
|
<td:TypeDeclaration> => ast::ModuleItem::TypeDeclaration(td),
|
|
|
|
|
<i:Impl> => ast::ModuleItem::Impl(i),
|
2021-08-08 11:42:26 -06:00
|
|
|
};
|
2021-08-07 20:20:11 -06:00
|
|
|
|
|
|
|
|
pub Module: ast::Module = {
|
|
|
|
|
<i:ModuleItem*> => ast::Module{items: i}
|
2021-08-08 11:42:26 -06:00
|
|
|
};
|
2020-04-08 00:04:36 -06:00
|
|
|
|
|
|
|
|
// From https://lalrpop.github.io/lalrpop/tutorial/006_macros.html
|
|
|
|
|
// Comma seperated list of T with optional trailing comma
|
|
|
|
|
Comma<T>: Vec<T> = {
|
|
|
|
|
<v:(<T> ",")*> <e:T?> => match e {
|
|
|
|
|
None => v,
|
|
|
|
|
Some(e) => {
|
|
|
|
|
let mut v = v;
|
|
|
|
|
v.push(e);
|
|
|
|
|
v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-07-23 23:54:01 -06:00
|
|
|
|
|
|
|
|
Spanned<Rule>: ast::Spanned<Rule> = {
|
|
|
|
|
<l: @L> <rule: Rule> <r: @R> => ast::Spanned{span: ast::Span{left: l, right: r}, value: rule}
|
|
|
|
|
};
|