Skip to content

Commit

Permalink
purge SymbolMap
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Aug 15, 2024
1 parent e9c6216 commit 6ff346f
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 105 deletions.
41 changes: 13 additions & 28 deletions src/compile_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashMap;
use index_vec::IndexVec;

use crate::{
compiler::{Compiler, Symbol},
repr::{
ast::typed::Function,
identifier::{FunctionIdx, ScopedBinding},
Expand All @@ -14,10 +15,7 @@ use crate::{
lower_ir::{FunctionBuilder as FunctionBuilderTrait, IRCtx},
type_check::{FunctionSignature, TypeCheckCtx},
},
util::{
scope::Scope,
symbol_map::{interner_symbol_map::*, SymbolMap},
},
util::scope::Scope,
};

struct FunctionInfo {
Expand All @@ -28,8 +26,7 @@ struct FunctionInfo {
}

pub struct CompilePass {
/// Backing symbol store for entire compile.
pub symbols: InternerSymbolMap,
pub compiler: Compiler,

/// All available functions within this pass
functions: IndexVec<FunctionIdx, FunctionInfo>,
Expand All @@ -41,7 +38,7 @@ pub struct CompilePass {
impl CompilePass {
pub fn new() -> Self {
Self {
symbols: InternerSymbolMap::new(),
compiler: Compiler::default(),
functions: IndexVec::new(),
function_symbols: HashMap::new(),
}
Expand All @@ -54,25 +51,6 @@ impl Default for CompilePass {
}
}

impl SymbolMap for CompilePass {
type Symbol = Symbol;

fn intern<T>(&mut self, s: T) -> Symbol
where
T: AsRef<str>,
{
SymbolMap::intern(&mut self.symbols, s)
}

fn get(&self, s: Symbol) -> String {
SymbolMap::get(&self.symbols, s)
}

fn dump_symbols(&self) -> InternerSymbolMap {
SymbolMap::dump_symbols(&self.symbols)
}
}

impl TypeCheckCtx for CompilePass {
fn register_function(&mut self, symbol: Symbol, signature: FunctionSignature) -> FunctionIdx {
let idx = self.functions.push(FunctionInfo {
Expand Down Expand Up @@ -214,7 +192,11 @@ impl LLVMCtx for CompilePass {
.as_ref()
.unwrap()
.get_binding(binding);
self.get(symbol)

self.compiler
.get_interned_string(symbol)
.unwrap()
.to_string()
}

fn get_scoped_binding_ty(&self, function: &FunctionIdx, binding: &ScopedBinding) -> Ty {
Expand All @@ -227,6 +209,9 @@ impl LLVMCtx for CompilePass {
}

fn get_function_name(&self, function: &FunctionIdx) -> String {
self.get(self.functions[*function].symbol)
self.compiler
.get_interned_string(self.functions[*function].symbol)
.unwrap()
.to_string()
}
}
4 changes: 2 additions & 2 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::compile_pass::CompilePass;
pub type Symbol = DefaultSymbol;

/// Contains all of the state required for a compiler pass.
#[derive(Default)]
#[derive(Default, Clone)]
pub struct Compiler {
/// Symbols interned by the compiler.
symbols: StringInterner<DefaultBackend>,
Expand All @@ -33,7 +33,7 @@ impl From<Compiler> for CompilePass {
fn from(compiler: Compiler) -> Self {
let mut ctx = Self::default();

ctx.symbols = compiler.symbols;
ctx.compiler = compiler;

ctx
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn compile_and_run(source: &'static str, debug: bool) -> i64 {

let program = parse(&mut compiler, source).unwrap();

let mut ctx: CompilePass = compiler.into();
let mut ctx = CompilePass::from(compiler.clone());

let program = program.ty_solve(&mut ctx).unwrap();

Expand Down Expand Up @@ -79,6 +79,7 @@ pub fn compile_and_run(source: &'static str, debug: bool) -> i64 {

for (idx, function) in ctx.all_functions() {
FunctionGenerator::new(
&mut compiler,
&mut ctx,
&llvm_ctx,
function_map.clone(),
Expand Down
2 changes: 1 addition & 1 deletion src/repr/ast/untyped.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{generate_ast, repr::ty::Ty, util::symbol_map::interner_symbol_map::Symbol};
use crate::{compiler::Symbol, generate_ast, repr::ty::Ty};

generate_ast! {
TyInfo: Option<Ty>,
Expand Down
11 changes: 4 additions & 7 deletions src/stage/codegen/ctx.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use crate::{
repr::{
identifier::{FunctionIdx, ScopedBinding},
ty::Ty,
},
util::symbol_map::{interner_symbol_map::Symbol, SymbolMap},
use crate::repr::{
identifier::{FunctionIdx, ScopedBinding},
ty::Ty,
};

pub trait LLVMCtx: SymbolMap<Symbol = Symbol> {
pub trait LLVMCtx {
/// Produce the original name for a scoped binding.
fn get_scoped_binding_name(&self, function: &FunctionIdx, binding: &ScopedBinding) -> String;

Expand Down
20 changes: 14 additions & 6 deletions src/stage/codegen/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ use inkwell::{
IntPredicate,
};

use crate::repr::{
identifier::{FunctionIdx, ScopedBinding},
ir::{
BasicBlockIdx, BinaryOp, ConstantValue, Function, Terminator, Triple, TripleRef, UnaryOp,
Value,
use crate::{
compiler::Compiler,
repr::{
identifier::{FunctionIdx, ScopedBinding},
ir::{
BasicBlockIdx, BinaryOp, ConstantValue, Function, Terminator, Triple, TripleRef,
UnaryOp, Value,
},
ty::Ty,
},
ty::Ty,
};

use super::ctx::LLVMCtx;

pub struct FunctionGenerator<'ctx, 'ink, Ctx> {
#[allow(dead_code)]
compiler: &'ctx mut Compiler,

ctx: &'ctx mut Ctx,
llvm_ctx: &'ink Context,

Expand All @@ -41,6 +47,7 @@ pub struct FunctionGenerator<'ctx, 'ink, Ctx> {

impl<'ctx, 'ink, Ctx: LLVMCtx> FunctionGenerator<'ctx, 'ink, Ctx> {
pub fn new(
compiler: &'ctx mut Compiler,
ctx: &'ctx mut Ctx,
llvm_ctx: &'ink Context,
functions: HashMap<FunctionIdx, FunctionValue<'ink>>,
Expand All @@ -61,6 +68,7 @@ impl<'ctx, 'ink, Ctx: LLVMCtx> FunctionGenerator<'ctx, 'ink, Ctx> {
builder.position_at_end(entry);

Self {
compiler,
ctx,
llvm_ctx,
llvm_function,
Expand Down
5 changes: 1 addition & 4 deletions src/stage/type_check/ctx/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
repr::identifier::FunctionIdx,
util::{scope::*, symbol_map::interner_symbol_map::Symbol},
};
use crate::{compiler::Symbol, repr::identifier::FunctionIdx, util::scope::*};

use super::FunctionSignature;

Expand Down
6 changes: 2 additions & 4 deletions src/stage/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ mod statement;

use itertools::Itertools;

use crate::compiler::Symbol;
use crate::repr::ast::{base as base_ast, untyped as parse_ast};
use crate::repr::ty::Ty;
use crate::{
repr::ast::{base as base_ast, untyped as parse_ast},
util::symbol_map::interner_symbol_map::Symbol,
};

pub use ctx::TypeCheckCtx;

Expand Down
3 changes: 2 additions & 1 deletion src/stage/type_check/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ mod test_statement {
use string_interner::Symbol as _;

use crate::{
compiler::Symbol,
repr::ast::untyped::*,
stage::type_check::ctx::MockTypeCheckCtx,
util::{scope::Scope, span::Span, symbol_map::interner_symbol_map::Symbol},
util::{scope::Scope, span::Span},
};

use super::Ty;
Expand Down
1 change: 0 additions & 1 deletion src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod scope;
pub mod span;
pub mod symbol_map;
2 changes: 1 addition & 1 deletion src/util/scope.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use index_vec::IndexVec;

use crate::{
compiler::Symbol,
repr::{identifier::*, ty::Ty},
util::symbol_map::interner_symbol_map::Symbol,
};

pub struct ScopePart {
Expand Down
49 changes: 0 additions & 49 deletions src/util/symbol_map.rs

This file was deleted.

0 comments on commit 6ff346f

Please sign in to comment.