Skip to content

Commit

Permalink
Created a new SymbolDecl type (#22)
Browse files Browse the repository at this point in the history
This is working a lot better than refactoring all enums to be more
specific instead of more nested Expr's

Long term, each union should be more succinct and varied, but this is a tedious
problem in rust.

This specific change will allow the separation between symbol declarations and symbol
access
  • Loading branch information
coffeebe4code authored Jul 1, 2024
1 parent f576e7a commit 507545d
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 76 deletions.
32 changes: 18 additions & 14 deletions ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,35 +482,35 @@ impl Symbol {
}

#[derive(Debug, Clone, PartialEq)]
pub struct Never {
pub struct NeverKeyword {
pub val: Lexeme,
}

impl Never {
impl NeverKeyword {
pub fn new(val: Lexeme) -> Self {
Never { val }
NeverKeyword { val }
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct SelfValue {
pub struct SelfKeyword {
pub val: Lexeme,
}

impl SelfValue {
impl SelfKeyword {
pub fn new(val: Lexeme) -> Self {
SelfValue { val }
SelfKeyword { val }
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct UndefinedValue {
pub struct UndefinedKeyword {
pub val: Lexeme,
}

impl UndefinedValue {
impl UndefinedKeyword {
pub fn new(val: Lexeme) -> Self {
UndefinedValue { val }
UndefinedKeyword { val }
}
}

Expand Down Expand Up @@ -628,12 +628,14 @@ pub enum Expr {
CharsValue(CharsValue),
ArrayType(ArrayType),
ValueType(ValueType),
FuncType(FuncType),
ArgDef(ArgDef),
BoolValue(BoolValue),
Symbol(Symbol),
SymbolDecl(Symbol),
AnonFuncDecl(AnonFuncDecl),
FuncDecl(FuncDecl),
FuncType(FuncType),
SelfDecl(SelfKeyword),
TraitDecl(TraitDecl),
StructDecl(StructDecl),
ErrorDecl(ErrorDecl),
Expand All @@ -642,9 +644,9 @@ pub enum Expr {
TopDecl(TopDecl),
Reassignment(Reassignment),
Import(Import),
UndefinedValue(UndefinedValue),
SelfValue(SelfValue),
Never(Never),
UndefinedValue(UndefinedKeyword),
SelfValue(SelfKeyword),
Never(NeverKeyword),
ArrayAccess(ArrayAccess),
PropAccess(PropAccess),
Invoke(Invoke),
Expand All @@ -659,9 +661,10 @@ impl Expr {
_ => panic!("issue no symbol found"),
}
}
pub fn into_self_val(&self) -> SelfValue {
pub fn into_self(&self) -> SelfKeyword {
match self {
Expr::SelfValue(x) => x.to_owned(),
Expr::SelfDecl(x) => x.to_owned(),
_ => panic!("issue no self keyword found"),
}
}
Expand All @@ -674,6 +677,7 @@ impl Expr {
pub fn into_symbol(&self) -> Symbol {
match self {
Expr::Symbol(x) => x.to_owned(),
Expr::SymbolDecl(x) => x.to_owned(),
_ => panic!("issue no symbol found {:?}", self),
}
}
Expand Down
1 change: 1 addition & 0 deletions e2e/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ fn main() {
println!(" [fail]\n issues: {:?}\n", linter.issues);
std::process::exit(1);
}
println!(" [ok] full lint success!");
}
92 changes: 58 additions & 34 deletions linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
},
Expr::TopDecl(top) => self.check_top_decl(&top),
Expr::Symbol(symbol) => self.check_symbol_ref(&symbol),
Expr::SymbolDecl(symbol) => self.check_symbol_decl(&symbol),
Expr::Block(blk) => self.check_block(&blk),
Expr::FuncDecl(fun) => self.check_func_decl(&fun),
Expr::RetOp(ret) => self.check_ret_op(&ret),
Expand All @@ -87,28 +88,30 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
}

pub fn check_func_decl(&mut self, td: &FuncDecl) -> ResultTreeType {
let result = self.lint_recurse(&td.block)?;
let slice = td.identifier.into_symbol().val.slice;

let mut init = FunctionInitialize {
name: slice.clone(),
args: vec![],
args_curried: vec![],
block: result.0,
block_curried: result.1,
};
let mut largs = vec![];
let mut largs_curried = vec![];
if let Some(args) = td.args.as_ref() {
args.iter().for_each(|x| {
let res = self.lint_recurse(x);
if let Ok(a) = res {
init.args.push(a.0);
init.args_curried.push(a.1);
largs.push(a.0);
largs_curried.push(a.1);
return;
}
init.args.push(simple_tree!(UnknownValue));
init.args_curried.push(Ty::Unknown);
largs.push(simple_tree!(UnknownValue));
largs_curried.push(Ty::Unknown);
});
}
let result = self.lint_recurse(&td.block)?;
let slice = td.identifier.into_symbol().val.slice;

let init = FunctionInitialize {
name: slice.clone(),
args: largs,
args_curried: largs_curried,
block: result.0,
block_curried: result.1,
};
let curried = init.block_curried.clone();
let full = tree!(FuncInit, init);

Expand Down Expand Up @@ -185,26 +188,33 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
return ok_tree!(DeclaratorInfo, dec, curried);
}

pub fn check_symbol_ref(&mut self, symbol: &Symbol) -> ResultTreeType {
pub fn check_symbol_decl(&mut self, symbol: &Symbol) -> ResultTreeType {
let sym = SymbolAccess {
ident: symbol.val.slice.clone(),
curried: Ty::Unknown,
};
let curried = sym.curried.clone();
let full = tree!(SymbolInit, sym);
self.ttbl
.table
.insert(symbol.val.slice.clone(), (Rc::clone(&full), 0));
return Ok((full, curried));
}

pub fn check_symbol_ref(&mut self, symbol: &Symbol) -> ResultTreeType {
let sym = SymbolAccess {
ident: symbol.val.slice.clone(),
curried: self
.ttbl
.table
.get(&symbol.val.slice)
.unwrap()
.0
.get_curried()
.clone(),
};
let curried = sym.curried.clone();
return ok_tree!(SymbolAccess, sym, curried);
//let sym = SymbolAccess {
// ident: symbol.val.slice.clone(),
// curried: self
// .ttbl
// .table
// .get(&symbol.val.slice)
// .unwrap()
// .0
// .get_curried()
// .clone(),
//};
//let curried = sym.curried.clone();
//return ok_tree!(SymbolAccess, sym, curried);
}

pub fn check_array_decl(&mut self, arr: &ArrayDecl) -> ResultTreeType {
Expand Down Expand Up @@ -434,22 +444,36 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {
}

pub fn check_anon_func(&mut self, anon: &AnonFuncDecl) -> ResultTreeType {
let mut largs = vec![];
let mut largs_curried = vec![];
if let Some(args) = anon.args.as_ref() {
args.iter().for_each(|x| {
let res = self.lint_recurse(x);
if let Ok(a) = res {
largs.push(a.0);
largs_curried.push(a.1);
return;
}
largs.push(simple_tree!(UnknownValue));
largs_curried.push(Ty::Unknown);
});
}
let result = self.lint_recurse(&anon.block)?;
let slice = format!(":anon_{}", self.idx);
self.idx += 1;

let init = FunctionInitialize {
name: slice.clone(),
args: vec![],
args_curried: vec![],
args: largs,
args_curried: largs_curried,
block: result.0,
block_curried: result.1,
};
let curried = init.block_curried.clone();
let full = tree!(AnonFuncInit, init);
let full = tree!(FuncInit, init);

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

pub fn check_import(&mut self, import: &Import) -> ResultTreeType {
Expand Down Expand Up @@ -590,7 +614,7 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {

pub fn check_arg_def(&mut self, arg: &ArgDef) -> ResultTreeType {
match arg.ident.as_ref() {
Expr::Symbol(x) => {
Expr::SymbolDecl(x) => {
let slice = x.val.slice.clone();
let typ = self.lint_recurse(&arg.typ)?;
let a = NoOp { curried: typ.1 };
Expand All @@ -602,7 +626,7 @@ impl<'buf, 'sym> LintSource<'buf, 'sym> {

return Ok((full, curried));
}
Expr::SelfValue(_) => {
Expr::SelfDecl(_) => {
let typ = self.lint_recurse(&arg.typ)?;
let a = NoOp { curried: typ.1 };

Expand Down
Loading

0 comments on commit 507545d

Please sign in to comment.