2020-04-08 00:04:36 -06:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
use crate::ast;
|
2021-04-18 08:51:21 -06:00
|
|
|
use crate::types;
|
2020-04-08 00:04:36 -06:00
|
|
|
|
2021-07-23 08:58:17 -06:00
|
|
|
grammar(id_generator: ast::IdGenerator);
|
2020-04-08 00:04:36 -06:00
|
|
|
|
|
|
|
|
|
2021-07-23 08:58:17 -06:00
|
|
|
pub TypeUsage: ast::TypeUsage = {
|
|
|
|
|
"fn" "(" <args:Comma<TypeUsage>> ")" => ast::TypeUsage::Function(ast::FunctionTypeUsage{arguments: args, return_type: ast::new_unit()}),
|
|
|
|
|
"fn" "(" <args:Comma<TypeUsage>> ")" ":" <return_type:TypeUsage> => ast::TypeUsage::Function(ast::FunctionTypeUsage{arguments: args, return_type: return_type),
|
|
|
|
|
<name:Identifier> => ast::TypeUsage::Named(ast::NamedTypeUsage{name: name})
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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::new_named("i64")},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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::new_named("f64")},
|
2020-04-08 00:04:36 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub Identifier: ast::Identifier = {
|
2020-07-23 23:54:01 -06:00
|
|
|
<i:r"[A-Za-z][A-Za-z0-9_]*"> => ast::Identifier{name: i.to_string()}
|
2020-04-08 00:04:36 -06:00
|
|
|
};
|
|
|
|
|
|
2020-04-13 23:59:01 -06:00
|
|
|
pub FunctionCall: ast::FunctionCall = {
|
2020-07-23 23:54:01 -06:00
|
|
|
<i:Spanned<Identifier>> "(" <args:Comma<Spanned<Expression>>> ")" => ast::FunctionCall{name:i, arguments: args}
|
2020-04-13 23:59:01 -06:00
|
|
|
}
|
|
|
|
|
|
2020-04-08 00:04:36 -06:00
|
|
|
pub Expression: Box<ast::Expression> = {
|
2020-07-23 23:54:01 -06:00
|
|
|
<l:Spanned<Expression>> "+" <r:Spanned<Factor>> => Box::new(ast::Expression::Op(ast::Operation{left: l, op: ast::Operator::Plus, right: r})),
|
|
|
|
|
<l:Spanned<Expression>> "-" <r:Spanned<Factor>> => Box::new(ast::Expression::Op(ast::Operation{left: l, op: ast::Operator::Minus, right: r})),
|
2020-04-08 00:04:36 -06:00
|
|
|
Factor,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub Factor: Box<ast::Expression> = {
|
2020-07-23 23:54:01 -06:00
|
|
|
<l:Spanned<Factor>> "*" <r:Spanned<Term>> => Box::new(ast::Expression::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})),
|
2020-04-08 00:04:36 -06:00
|
|
|
Term,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub Term: Box<ast::Expression> = {
|
2020-07-23 23:54:01 -06:00
|
|
|
Spanned<LiteralInt> => Box::new(ast::Expression::LiteralInt(<>)),
|
|
|
|
|
Spanned<Identifier> => Box::new(ast::Expression::Identifier(<>)),
|
|
|
|
|
Spanned<FunctionCall> => Box::new(ast::Expression::FunctionCall(<>)),
|
|
|
|
|
"(" <e:Expression> ")" => e
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub LetStatement: ast::Assignment = {
|
|
|
|
|
//TODO: support destructuring with tuples, when they exist.
|
|
|
|
|
//TODO: add mut, weak
|
|
|
|
|
"let" <n:Spanned<Identifier>> "=" <e:Spanned<Expression>> => ast::Assignment{name: n, type_usage: None, expression: e},
|
|
|
|
|
"let" <n:Spanned<Identifier>> ":" <t:Spanned<TypeUsage>> "=" <e:Spanned<Expression>> => ast::Assignment{name: n, type_usage: Some(t), expression: e},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub Statement: ast::Statement = {
|
|
|
|
|
<l:Spanned<LetStatement>> => ast::Statement::Assignment(l),
|
|
|
|
|
<e:Spanned<Expression>> => ast::Statement::Expression(e),
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub Block: ast::Block = {
|
2020-07-23 23:54:01 -06:00
|
|
|
"{" <s:LineDelimiter<Spanned<Statement>>> "}" => ast::Block{statements: s}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub TypeUsage: ast::TypeUsage = {
|
2021-04-18 08:51:21 -06:00
|
|
|
<n:Spanned<Identifier>> => ast::TypeUsage{name: n, ty: types::SpecifiedType::Unknown}
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub VariableDeclaration: ast::VariableDeclaration = {
|
2020-07-23 23:54:01 -06:00
|
|
|
<i:Spanned<Identifier>> ":" <t:Spanned<TypeUsage>> => ast::VariableDeclaration{name: i, type_usage: t},
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub Function: ast::Function = {
|
2020-07-23 23:54:01 -06:00
|
|
|
"fn" <n:Spanned<Identifier>> "(" <args:Comma<VariableDeclaration>> ")" <rt:Spanned<TypeUsage>> <b:Block> => ast::Function{name: n, arguments: args, block: b, return_type: rt}
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-04-13 23:59:01 -06:00
|
|
|
pub Module: ast::Module = {
|
|
|
|
|
<fs:Function*> => ast::Module{functions: fs}
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
LineDelimiter<T>: Vec<T> = {
|
|
|
|
|
<v:(<T> r"\n|;")*> <e:T> => {
|
|
|
|
|
let mut v = v;
|
|
|
|
|
v.push(e);
|
|
|
|
|
v
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Spanned<Rule>: ast::Spanned<Rule> = {
|
|
|
|
|
<l: @L> <rule: Rule> <r: @R> => ast::Spanned{span: ast::Span{left: l, right: r}, value: rule}
|
|
|
|
|
};
|