From a4980b8dbf1394c2b302f1de7c72d2264426b86e Mon Sep 17 00:00:00 2001 From: Mel Date: Wed, 20 Oct 2021 22:15:08 +0200 Subject: Statement parsing. --- src/parse/ast/value.rs | 106 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/parse/ast/value.rs (limited to 'src/parse/ast/value.rs') diff --git a/src/parse/ast/value.rs b/src/parse/ast/value.rs new file mode 100644 index 0000000..263cb58 --- /dev/null +++ b/src/parse/ast/value.rs @@ -0,0 +1,106 @@ +use crate::lex::token::{Token, TokenVariant::*}; + +use super::{expression::Expression, statement::Statement}; + +#[derive(Debug)] +pub enum BinaryOperator { + Plus, + Minus, + Star, + Slash, + Eq, + Neq, + Gt, + Gte, + Lt, + Lte, + Assign, + ConstAssign, + Dot, +} + +impl BinaryOperator { + pub fn from_token(token: Token) -> Self { + match token.variant { + OpPlus => Self::Plus, + OpMinus => Self::Minus, + OpStar => Self::Star, + OpSlash => Self::Slash, + OpEq => Self::Eq, + OpNeq => Self::Neq, + OpLt => Self::Lt, + OpGt => Self::Gt, + OpLte => Self::Lte, + OpGte => Self::Gte, + Assign => Self::Assign, + ConstAssign => Self::ConstAssign, + Dot => Self::Dot, + _ => panic!("Can't create binary operator from '{:?}'.", token.variant) + } + } +} + +#[derive(Debug)] +pub enum UnaryOperator { + Plus, + Minus, + Not, +} + +impl UnaryOperator { + pub fn from_token(token: Token) -> Self { + match token.variant { + OpPlus => Self::Plus, + OpMinus => Self::Minus, + OpNot => Self::Not, + _ => panic!("Can't create unary operator from '{:?}'.", token.variant) + } + } +} + +#[derive(Debug)] +pub enum Literal { + Int(u32), + Float(f32), + Str(String), +} + +impl Literal { + pub fn from_token(token: Token) -> Self { + match token.variant { + Int(int) => Self::Int(int), + Float(float) => Self::Float(float), + Str(string) => Self::Str(string), + _ => panic!("Can't create literal from '{:?}'.", token.variant) + } + } +} + +pub type Identifier = String; + +// If the contraint is None the type will have to be inferred +// during analysis. +#[derive(Debug)] +pub struct TypedIdentifier { + pub identifier: Identifier, + pub type_constraint: Option, +} + +#[derive(Debug)] +pub struct FnHeader { + pub has_self_receiver: bool, + pub parameters: Vec, + pub return_type: Option, +} + +#[derive(Debug)] +pub struct Block { + pub statements: Vec, + pub tail_expression: Option, +} + +#[derive(Debug)] +pub struct ConditionalBlock { + pub condition: Expression, + pub block: Block, +} -- cgit 1.4.1