Add Symbol struct to represent symbols

This commit is contained in:
Sameer Rahmani 2020-06-20 22:14:48 +01:00
parent 3454a44f65
commit 08a651019f
6 changed files with 37 additions and 5 deletions

View File

@ -1,8 +1,8 @@
use crate::types::{Expression, List, Number};
use crate::types::{Expression, List, Number, Symbol};
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Expr {
Symbol(String),
Sym(Symbol),
Str(String),
Num(Number),
Comment,
@ -18,7 +18,7 @@ impl Expr {
}
pub fn make_symbol(v: String) -> Expr {
Expr::Symbol(v)
Expr::Sym(Symbol::new(v))
}
pub fn make_string(v: String) -> Expr {

View File

@ -3,7 +3,7 @@ use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::values::{BasicValue, BasicValueEnum, FloatValue, FunctionValue, PointerValue};
use crate::expr::Expression;
use crate::types::Expression;
use std::collections::HashMap;
pub struct Compiler<'a, 'ctx> {

View File

@ -1,4 +1,5 @@
use crate::ast::{Expr, Number};
use crate::ast::Expr;
use crate::types::Number;
use std::io::{BufReader, Read};
pub type ReadResult = Result<Expr, String>;
@ -249,6 +250,9 @@ impl ExprReader {
None => break,
}
}
// TODO: Move this to ast module and use the `new` function on
// Number struct
if is_double {
Ok(Expr::make_number(Number::Float(
string.parse::<f64>().unwrap(),

View File

@ -1,7 +1,9 @@
pub mod core;
pub mod list;
pub mod number;
pub mod symbol;
pub use self::core::Expression;
pub use self::list::List;
pub use self::number::Number;
pub use self::symbol::Symbol;

View File

@ -14,6 +14,7 @@ pub enum Number {
impl PartialEq for Number {
fn eq(&self, other: &Self) -> bool {
// TODO: Eval other
let comb = (&self, &other);
match comb {

25
src/types/symbol.rs Normal file
View File

@ -0,0 +1,25 @@
use crate::types::Expression;
#[derive(Debug, Clone)]
pub struct Symbol {
name: String,
}
impl PartialEq for Symbol {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Eq for Symbol {}
impl Expression for Symbol {
fn eval() {}
fn code_gen() {}
}
impl Symbol {
pub fn new(name: String) -> Self {
Symbol { name }
}
}