Browse Source

chore: extract types into own module

master
Garrit Franke 9 months ago
parent
commit
48f3a6692e
  1. 26
      src/ast/mod.rs
  2. 39
      src/ast/types.rs
  3. 1
      src/generator/c.rs
  4. 1
      src/generator/llvm.rs
  5. 2
      src/generator/tests/c_tests.rs
  6. 3
      src/parser/infer.rs
  7. 1
      src/parser/rules.rs
  8. 2
      src/parser/tests.rs

26
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<String, Option<Type>>;
@ -67,29 +70,6 @@ pub struct Variable {
pub ty: Option<Type>,
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Type {
Any,
Int,
Str,
Bool,
Array(Box<Type>),
Struct(String),
}
impl TryFrom<String> for Type {
type Error = String;
fn try_from(s: String) -> Result<Self, Self::Error> {
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)

39
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<Type>),
Struct(String),
}
impl TryFrom<String> for Type {
type Error = String;
fn try_from(s: String) -> Result<Self, Self::Error> {
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())),
}
}
}

1
src/generator/c.rs

@ -1,3 +1,4 @@
use crate::ast::types::Type;
use crate::ast::*;
/**
* Copyright 2020 Garrit Franke

1
src/generator/llvm.rs

@ -1,3 +1,4 @@
use crate::ast::types::Type;
use crate::ast::*;
/**
* Copyright 2020 Garrit Franke

2
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;

3
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

1
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};

2
src/parser/tests.rs

@ -1,4 +1,4 @@
use crate::ast::*;
use crate::ast::types::Type;
/**
* Copyright 2020 Garrit Franke
*

Loading…
Cancel
Save