Skip to content

Commit

Permalink
Scopes Being Introduced (#25)
Browse files Browse the repository at this point in the history
scopes are needed as a precurser to data layout.
they also will allow for reduced symbol collision and when added at
linter

scopes and type tables have been added and passed into linter.
no usage yet other than the first typetable and setting the first top
level scope
  • Loading branch information
coffeebe4code committed Jul 4, 2024
1 parent 4cf388c commit 769c522
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 45 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ members = [
"linter",
"codelocation",
"types",
"cachectx", "typetable"]
"cachectx",
"typetable",
"scopetable"
]
resolver = "2"

1 change: 1 addition & 0 deletions cachectx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ parser = { path = "../parser" }
ast = { path = "../ast" }
symtable = { path = "../symtable" }
typetable = { path = "../typetable" }
scopetable = { path = "../scopetable" }
types = { path = "../types" }
linter = { path = "../linter" }
ir = { path = "../ir" }
15 changes: 9 additions & 6 deletions cachectx/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use lexer::*;
use linter::*;
use parser::*;
use scopetable::*;
use std::{fs::File, io::Read, path::PathBuf, rc::Rc};
use symtable::*;
use types::*;
Expand All @@ -9,7 +10,8 @@ use typetable::*;
struct InternalContext {
source: PathBuf,
parsed: Option<ResultExpr>,
symtable: Option<TypeTable>,
ttbls: Option<Vec<TypeTable>>,
scopes: Option<Vec<ScopeTable>>,
tree: Option<Vec<Rc<Box<TypeTree>>>>,
}

Expand All @@ -25,7 +27,8 @@ impl CacheContext {
self.files.push(InternalContext {
source,
parsed: None,
symtable: None,
ttbls: None,
scopes: None,
tree: None,
});
}
Expand All @@ -38,14 +41,14 @@ impl CacheContext {
let mut parser = Parser::new(lexer);
match parser.all() {
Ok(mut val) => {
let mut sym = TypeTable::new();
let mut linter = LintSource::new(&contents, &mut sym);
let mut ttbls = vec![];
let mut scopes = vec![];
let mut linter = LintSource::new(&contents, &mut scopes, &mut ttbls);

let analysis = linter.lint_check(&mut val);
println!("debug analysis {:?}", analysis);

ic.tree = Some(analysis);
ic.symtable = Some(sym);
ic.ttbls = Some(ttbls);
}
Err(perr) => ic.parsed = Some(Err(perr)),
}
Expand Down
5 changes: 3 additions & 2 deletions e2e/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ fn main() {
}
}
println!("[run] full linting without cache context");
let mut typ_table = TypeTable::new();
let mut linter = LintSource::new(&contents, &mut typ_table);
let mut ttbls = vec![];
let mut scopes = vec![];
let mut linter = LintSource::new(&contents, &mut scopes, &mut ttbls);
let borrow = res.unwrap();
let result = linter.lint_check(&mut borrow.to_owned());

Expand Down
7 changes: 4 additions & 3 deletions ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,11 @@ mod tests {
),
None,
);
let mut type_table = TypeTable::new();
let mut linter = LintSource::new("test", &mut type_table);
let mut tt = vec![];
let mut scp = vec![];
let mut linter = LintSource::new("test", &mut scp, &mut tt);
let linter_result = linter.check_func_decl(&func_def).unwrap();
let mut fir = IRSource::new(0, SymTable::new(), &linter.ttbl);
let mut fir = IRSource::new(0, SymTable::new(), &linter.ttbls.get(0).unwrap());
let result = fir.begin(linter_result.0);
/*
* function u0:0() -> i64 system_v
Expand Down
1 change: 1 addition & 0 deletions linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
ast = { path = "../ast" }
types = { path = "../types" }
typetable = { path = "../typetable" }
scopetable = { path = "../scopetable" }
parser = { path = "../parser" }
token = { path = "../token" }
lexer = { path = "../lexer" }
Expand Down
94 changes: 67 additions & 27 deletions linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,39 @@ use codelocation::*;
use lexer::*;
use perror::LinterError;
use perror::LinterErrorPoint;
use scopetable::ScopeTable;
use std::rc::Rc;
use token::Token;
use types::*;
use typetable::*;

type ResultTreeType = Result<(Rc<Box<TypeTree>>, Ty), usize>;

pub struct LintSource<'buf, 'sym> {
pub struct LintSource<'buf, 'ttb, 'sco> {
buffer: &'buf str,
idx: usize,
curr_scope: usize,
curr_self: Option<Ty>,
pub ttbl: &'sym mut TypeTable,
pub scopes: &'sco mut Vec<ScopeTable>,
pub ttbls: &'ttb mut Vec<TypeTable>,
pub issues: Vec<LinterError>,
}

impl<'buf, 'sym> LintSource<'buf, 'sym> {
pub fn new(buffer: &'buf str, slt: &'sym mut TypeTable) -> Self {
impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> {
pub fn new(
buffer: &'buf str,
scopes: &'sco mut Vec<ScopeTable>,
ttbls: &'ttb mut Vec<TypeTable>,
) -> Self {
ttbls.push(TypeTable::new());
scopes.push(ScopeTable::new(0, 0));
LintSource {
buffer,
idx: 0,
curr_scope: 0,
curr_self: None,
ttbl: slt,
scopes,
ttbls,
issues: vec![],
}
}
Expand Down Expand Up @@ -114,8 +125,9 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
};
let curried = init.block_curried.clone();
let full = tree!(FuncInit, init);
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

self.ttbl.table.insert(slice, Rc::clone(&full));
tbl.table.insert(slice, Rc::clone(&full));
Ok((full, curried))
}

Expand Down Expand Up @@ -191,17 +203,18 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
};
let curried = sym.curried.clone();
let full = tree!(SymbolInit, sym);
self.ttbl
.table
.insert(symbol.val.slice.clone(), Rc::clone(&full));

let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

tbl.table.insert(symbol.val.slice.clone(), Rc::clone(&full));
return Ok((full, curried));
}

pub fn check_symbol_ref(&mut self, symbol: &Symbol) -> ResultTreeType {
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();
let sym = SymbolAccess {
ident: symbol.val.slice.clone(),
curried: self
.ttbl
curried: tbl
.table
.get(&symbol.val.slice)
.unwrap()
Expand Down Expand Up @@ -245,7 +258,10 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
let curried = err_info.curried.clone();
let full = tree!(ErrorInfo, err_info);

self.ttbl.table.insert(slice.clone(), Rc::clone(&full));
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

tbl.table.insert(slice, Rc::clone(&full));

return Ok((full, curried));
}

Expand Down Expand Up @@ -342,7 +358,9 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
let curried = obj_info.curried.clone();
let full = tree!(StructInfo, obj_info);

self.ttbl.table.insert(slice, Rc::clone(&full));
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

tbl.table.insert(slice, Rc::clone(&full));
return Ok((full, curried));
}
Err(self.set_error(
Expand All @@ -363,7 +381,9 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
let curried = init.curried.clone();
let full = tree!(PropInit, init);

self.ttbl.table.insert(slice.clone(), Rc::clone(&full));
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

tbl.table.insert(slice, Rc::clone(&full));
return Ok((full, curried));
}

Expand Down Expand Up @@ -393,8 +413,9 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
let curried = struct_init.curried.clone();
let full = tree!(StructInit, struct_init);

self.ttbl
.table
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

tbl.table
.insert(prev.0.into_symbol_access().ident.clone(), Rc::clone(&full));
return Ok((full, curried));
}
Expand Down Expand Up @@ -432,7 +453,9 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
let curried = tag_info.curried.clone();
let full = tree!(TagInfo, tag_info);

self.ttbl.table.insert(copy, Rc::clone(&full));
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

tbl.table.insert(copy, Rc::clone(&full));
return Ok((full, curried));
}

Expand Down Expand Up @@ -465,7 +488,9 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
let curried = init.block_curried.clone();
let full = tree!(FuncInit, init);

self.ttbl.table.insert(slice, Rc::clone(&full));
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

tbl.table.insert(slice, Rc::clone(&full));
Ok((full, curried))
}

Expand All @@ -481,11 +506,15 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
let curried = init.curried.clone();
if import.mutability.token == Token::Const {
let full: Rc<Box<TypeTree>> = tree!(ConstInit, init);
self.ttbl.table.insert(slice, Rc::clone(&full));
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

tbl.table.insert(slice, Rc::clone(&full));
return Ok((full, Ty::Const(Box::new(curried))));
}
let full: Rc<Box<TypeTree>> = tree!(MutInit, init);
self.ttbl.table.insert(slice, Rc::clone(&full));
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

tbl.table.insert(slice, Rc::clone(&full));
return Ok((full, Ty::Mut(Box::new(curried))));
}

Expand All @@ -502,12 +531,16 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
if inner.mutability.token == Token::Const {
init.curried = Ty::Const(Box::new(init.curried));
let full: Rc<Box<TypeTree>> = tree!(ConstInit, init);
self.ttbl.table.insert(slice, Rc::clone(&full));
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

tbl.table.insert(slice, Rc::clone(&full));
return Ok((full, Ty::Const(Box::new(curried))));
}
init.curried = Ty::Mut(Box::new(init.curried));
let full: Rc<Box<TypeTree>> = tree!(MutInit, init);
self.ttbl.table.insert(slice, Rc::clone(&full));
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

tbl.table.insert(slice, Rc::clone(&full));
return Ok((full, Ty::Mut(Box::new(curried))));
}

Expand All @@ -523,11 +556,15 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
let curried = init.curried.clone();
if td.mutability.token == Token::Const {
let full: Rc<Box<TypeTree>> = tree!(ConstInit, init);
self.ttbl.table.insert(slice, Rc::clone(&full));
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

tbl.table.insert(slice, Rc::clone(&full));
return Ok((full, Ty::Const(Box::new(curried))));
}
let full: Rc<Box<TypeTree>> = tree!(MutInit, init);
self.ttbl.table.insert(slice, Rc::clone(&full));
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

tbl.table.insert(slice, Rc::clone(&full));
return Ok((full, Ty::Mut(Box::new(curried))));
}

Expand Down Expand Up @@ -615,7 +652,9 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
let curried = a.curried.clone();
self.curr_self = Some(a.curried.clone());
let full: Rc<Box<TypeTree>> = tree!(ArgInit, a);
self.ttbl.table.insert(slice, Rc::clone(&full));
let tbl = self.ttbls.get_mut(self.curr_scope).unwrap();

tbl.table.insert(slice, Rc::clone(&full));

return Ok((full, curried));
}
Expand Down Expand Up @@ -893,8 +932,9 @@ mod tests {
let lexer = TLexer::new("8 + --5");
let mut parser = Parser::new(lexer);
let result = parser.low_bin();
let mut tt = TypeTable::new();
let mut linter = LintSource::new("8 + --5", &mut tt);
let mut tts = vec![];
let mut scps = vec![];
let mut linter = LintSource::new("8 + --5", &mut scps, &mut tts);
let test = linter.lint_recurse(&result.unwrap());

assert!(test.is_err_and(|x| { x == 0 }));
Expand Down
1 change: 1 addition & 0 deletions objmaker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
ast = { path = "../ast" }
symtable = { path = "../symtable" }
scopetable = { path = "../scopetable" }
typetable = { path = "../typetable" }
lexer = { path = "../lexer" }
linter = { path = "../linter" }
Expand Down
14 changes: 8 additions & 6 deletions objmaker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ pub fn from_buffer(contents: &str, path: &Path) -> () {
let lex = TLexer::new(&contents);
let mut parse = Parser::new(lex);
let ast_parsed = parse.all().unwrap();
let mut type_table = TypeTable::new();
let mut linter = LintSource::new(path.to_str().unwrap(), &mut type_table);
let mut type_tables = vec![];
let mut scopes = vec![];
let mut linter = LintSource::new(path.to_str().unwrap(), &mut scopes, &mut type_tables);
let lint_res = linter.lint_check(&ast_parsed);
let mut ir = IRSource::new(0, SymTable::new(), linter.ttbl);
let mut ir = IRSource::new(0, SymTable::new(), linter.ttbls.get(0).unwrap());
if linter.issues.len() > 0 {
for x in linter.issues {
println!("{}", x);
Expand Down Expand Up @@ -51,10 +52,11 @@ pub fn from_file(input_path: &PathBuf) -> () {
let lex = TLexer::new(&contents);
let mut parse = Parser::new(lex);
let ast_parsed = parse.all().unwrap();
let mut type_table = TypeTable::new();
let mut linter = LintSource::new(input_path.to_str().unwrap(), &mut type_table);
let mut type_tables = vec![];
let mut scopes = vec![];
let mut linter = LintSource::new(input_path.to_str().unwrap(), &mut scopes, &mut type_tables);
let lint_res = linter.lint_check(&ast_parsed);
let mut ir = IRSource::new(0, SymTable::new(), linter.ttbl);
let mut ir = IRSource::new(0, SymTable::new(), linter.ttbls.get(0).unwrap());
if linter.issues.len() > 0 {
panic!("linter issues exist");
}
Expand Down
8 changes: 8 additions & 0 deletions scopetable/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "scopetable"
version = "0.1.0"
edition = "2021"

[dependencies]
typetable = { path = "../typetable" }
types = { path = "../types" }
Loading

0 comments on commit 769c522

Please sign in to comment.