From 48f3a6692ee83e8ddba462361f7ab9ba08bdb0bf Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Mon, 22 Feb 2021 23:51:26 +0100 Subject: [PATCH] chore: extract types into own module --- src/ast/mod.rs | 26 +++-------------------- src/ast/types.rs | 39 ++++++++++++++++++++++++++++++++++ src/generator/c.rs | 1 + src/generator/llvm.rs | 1 + src/generator/tests/c_tests.rs | 2 +- src/parser/infer.rs | 3 ++- src/parser/rules.rs | 1 + src/parser/tests.rs | 2 +- 8 files changed, 49 insertions(+), 26 deletions(-) create mode 100644 src/ast/types.rs diff --git a/src/ast/mod.rs b/src/ast/mod.rs index bef4963..9c0a334 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -18,6 +18,9 @@ use std::collections::HashMap; */ use std::collections::HashSet; +pub mod types; +use types::Type; + /// Table that contains all symbol and its types pub type SymbolTable = HashMap>; @@ -67,29 +70,6 @@ pub struct Variable { pub ty: Option, } -#[derive(Debug, Eq, PartialEq, Clone)] -pub enum Type { - Any, - Int, - Str, - Bool, - Array(Box), - Struct(String), -} - -impl TryFrom for Type { - type Error = String; - fn try_from(s: String) -> Result { - match s.as_ref() { - "int" => Ok(Self::Int), - "string" => Ok(Self::Str), - "any" => Ok(Self::Any), - "bool" => Ok(Self::Bool), - name => Ok(Self::Struct(name.to_string())), - } - } -} - #[derive(Debug, Eq, PartialEq, Clone)] pub enum Statement { /// (Statements, Scoped variables) diff --git a/src/ast/types.rs b/src/ast/types.rs new file mode 100644 index 0000000..1cfbfde --- /dev/null +++ b/src/ast/types.rs @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Garrit Franke + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +use std::convert::TryFrom; + +#[derive(Debug, Eq, PartialEq, Clone)] +pub enum Type { + Any, + Int, + Str, + Bool, + Array(Box), + Struct(String), +} + +impl TryFrom for Type { + type Error = String; + fn try_from(s: String) -> Result { + match s.as_ref() { + "int" => Ok(Self::Int), + "string" => Ok(Self::Str), + "any" => Ok(Self::Any), + "bool" => Ok(Self::Bool), + name => Ok(Self::Struct(name.to_string())), + } + } +} diff --git a/src/generator/c.rs b/src/generator/c.rs index 64e6911..178de85 100644 --- a/src/generator/c.rs +++ b/src/generator/c.rs @@ -1,3 +1,4 @@ +use crate::ast::types::Type; use crate::ast::*; /** * Copyright 2020 Garrit Franke diff --git a/src/generator/llvm.rs b/src/generator/llvm.rs index 17ef78e..06673d8 100644 --- a/src/generator/llvm.rs +++ b/src/generator/llvm.rs @@ -1,3 +1,4 @@ +use crate::ast::types::Type; use crate::ast::*; /** * Copyright 2020 Garrit Franke diff --git a/src/generator/tests/c_tests.rs b/src/generator/tests/c_tests.rs index 3246fb8..003097d 100644 --- a/src/generator/tests/c_tests.rs +++ b/src/generator/tests/c_tests.rs @@ -1,4 +1,4 @@ -use crate::ast::Type; +use crate::ast::types::Type; use crate::generator::c::generate_type; use crate::util::Either; diff --git a/src/parser/infer.rs b/src/parser/infer.rs index 2836a7c..e84b63e 100644 --- a/src/parser/infer.rs +++ b/src/parser/infer.rs @@ -1,4 +1,5 @@ -use crate::ast::{Expression, Module, Statement, SymbolTable, Type}; +use crate::ast::types::Type; +use crate::ast::{Expression, Module, Statement, SymbolTable}; /** * Copyright 2021 Garrit Franke diff --git a/src/parser/rules.rs b/src/parser/rules.rs index 2e8d3f3..92febf2 100644 --- a/src/parser/rules.rs +++ b/src/parser/rules.rs @@ -1,4 +1,5 @@ use super::parser::Parser; +use crate::ast::types::Type; use crate::ast::*; use crate::lexer::Keyword; use crate::lexer::{TokenKind, Value}; diff --git a/src/parser/tests.rs b/src/parser/tests.rs index 51c126d..d485171 100644 --- a/src/parser/tests.rs +++ b/src/parser/tests.rs @@ -1,4 +1,4 @@ -use crate::ast::*; +use crate::ast::types::Type; /** * Copyright 2020 Garrit Franke *