2020-06-05 21:57:20 +02:00
|
|
|
#![allow(dead_code)]
|
|
|
|
use crate::ast::Expr;
|
2021-05-17 20:17:34 +02:00
|
|
|
use crate::ast::Identifier;
|
2020-06-11 22:10:23 +02:00
|
|
|
use crate::ast::Stmt;
|
2020-06-05 21:57:20 +02:00
|
|
|
use crate::lexer::Token;
|
|
|
|
use crate::lexer::TokenKind;
|
|
|
|
|
|
|
|
pub fn token(kind: TokenKind, value: &str) -> Token {
|
|
|
|
Token {
|
|
|
|
kind,
|
|
|
|
value: value.into(),
|
2020-06-06 20:15:32 +02:00
|
|
|
span: (0, 0),
|
2020-06-05 21:57:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 15:52:22 +01:00
|
|
|
pub fn literal(value: f64) -> Box<Expr> {
|
|
|
|
Box::new(Expr::Literal(value))
|
2020-06-05 21:57:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn var(identifier: &str) -> Box<Expr> {
|
2021-05-17 20:17:34 +02:00
|
|
|
Box::new(Expr::Var(Identifier::from_full_name(identifier)))
|
2020-06-05 21:57:20 +02:00
|
|
|
}
|
|
|
|
|
2020-06-11 22:10:23 +02:00
|
|
|
pub fn fn_call(identifier: &str, arguments: Vec<Expr>) -> Box<Expr> {
|
2021-05-17 20:17:34 +02:00
|
|
|
Box::new(Expr::FnCall(
|
|
|
|
Identifier::from_full_name(identifier),
|
|
|
|
arguments,
|
|
|
|
))
|
2020-06-11 22:10:23 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 21:57:20 +02:00
|
|
|
pub fn binary(left: Box<Expr>, op: TokenKind, right: Box<Expr>) -> Box<Expr> {
|
|
|
|
Box::new(Expr::Binary(left, op, right))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unary(op: TokenKind, expr: Box<Expr>) -> Box<Expr> {
|
|
|
|
Box::new(Expr::Unary(op, expr))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn group(expr: Box<Expr>) -> Box<Expr> {
|
|
|
|
Box::new(Expr::Group(expr))
|
|
|
|
}
|
2020-06-11 22:10:23 +02:00
|
|
|
|
2020-06-15 19:10:55 +02:00
|
|
|
pub fn unit(identifier: &str, expr: Box<Expr>) -> Box<Expr> {
|
|
|
|
Box::new(Expr::Unit(identifier.into(), expr))
|
|
|
|
}
|
|
|
|
|
2020-06-11 22:10:23 +02:00
|
|
|
pub fn var_decl(identifier: &str, value: Box<Expr>) -> Stmt {
|
2021-05-17 20:17:34 +02:00
|
|
|
Stmt::VarDecl(Identifier::from_full_name(identifier), value)
|
2020-06-11 22:10:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fn_decl(identifier: &str, parameters: Vec<String>, value: Box<Expr>) -> Stmt {
|
2021-05-17 20:17:34 +02:00
|
|
|
Stmt::FnDecl(Identifier::from_full_name(identifier), parameters, value)
|
2020-06-11 22:10:23 +02:00
|
|
|
}
|
2020-06-15 19:10:55 +02:00
|
|
|
|
|
|
|
pub fn unit_decl(unit: &str, base_unit: &str, expr: Box<Expr>) -> Stmt {
|
|
|
|
Stmt::UnitDecl(unit.into(), base_unit.into(), expr)
|
|
|
|
}
|