about summary refs log tree commit diff
path: root/src/parse/ast/nodes.rs
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2021-10-21 23:46:01 +0200
committerMel <einebeere@gmail.com>2021-10-21 23:46:01 +0200
commitcff714c07e5e2c0f11c121504500a554d60c08cc (patch)
tree6c9e98696530b2661343942948b9f1a50a8665e7 /src/parse/ast/nodes.rs
parenta4980b8dbf1394c2b302f1de7c72d2264426b86e (diff)
downloadrabbithole-cff714c07e5e2c0f11c121504500a554d60c08cc.tar.zst
rabbithole-cff714c07e5e2c0f11c121504500a554d60c08cc.zip
Implement program walking.
Diffstat (limited to 'src/parse/ast/nodes.rs')
-rw-r--r--src/parse/ast/nodes.rs118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/parse/ast/nodes.rs b/src/parse/ast/nodes.rs
new file mode 100644
index 0000000..2347d5b
--- /dev/null
+++ b/src/parse/ast/nodes.rs
@@ -0,0 +1,118 @@
+use crate::lex::token::{Token, TokenVariant::*};
+
+use super::{expression::Expression, statement::Statement};
+
+#[derive(Debug, Clone, Copy)]
+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, Clone, Copy)]
+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, Clone)]
+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, Clone)]
+pub struct TypedIdentifier {
+    pub identifier: Identifier,
+    pub type_constraint: Option<Identifier>,
+}
+
+#[derive(Debug, Clone)]
+pub struct FnNode {
+    pub header: FnHeader,
+    pub body: BlockNode,
+}
+
+#[derive(Debug, Clone)]
+pub struct FnHeader {
+    pub has_self_receiver: bool,
+    pub parameters: Vec<TypedIdentifier>,
+    pub return_type: Option<Identifier>,
+}
+
+#[derive(Debug, Clone)]
+pub struct IfNode {
+    pub conditionals: Vec<ConditionalBlock>,
+    pub else_block: Option<BlockNode>,
+}
+
+#[derive(Debug, Clone)]
+pub struct BlockNode {
+    pub statements: Vec<Statement>,
+    pub tail_expression: Option<Expression>,
+}
+
+#[derive(Debug, Clone)]
+pub struct ConditionalBlock {
+    pub condition: Expression,
+    pub block: BlockNode,
+}