Skip to content

Commit

Permalink
Type milestone (#38)
Browse files Browse the repository at this point in the history
* type table, scope table, and linter is busted

* type-milestone paused
  • Loading branch information
coffeebe4code authored Dec 29, 2024
1 parent 85f5b79 commit 5f1e008
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
69 changes: 52 additions & 17 deletions linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use typetable::*;

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

#[derive(Debug)]
pub struct LintSource<'buf, 'ttb, 'sco> {
buffer: &'buf str,
idx: u32,
Expand Down Expand Up @@ -124,7 +125,7 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> {
self.inc_scope_tracker();
if let Some(args) = td.args.as_ref() {
args.iter().for_each(|x| {
let res = self.lint_recurse(x);
let res = self.check_arg_def(&x.into_arg_def());
if let Ok(a) = res {
largs.push(a.0);
largs_curried.push(a.1);
Expand Down Expand Up @@ -311,15 +312,14 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> {
}

pub fn check_value_type(&mut self, _vt: &ValueType) -> ResultTreeType {
let mut curried = Ty::Unknown;
match _vt.val.token {
Token::U64 => curried = Ty::U64,
Token::U32 => curried = Ty::U32,
Token::USize => curried = Ty::USize,
Token::ISize => curried = Ty::ISize,
Token::F64 => curried = Ty::F64,
Token::U8 => curried = Ty::U8,
Token::Char => curried = Ty::Char,
let curried = match _vt.val.token {
Token::U64 => Ty::U64,
Token::U32 => Ty::U32,
Token::USize => Ty::USize,
Token::ISize => Ty::ISize,
Token::F64 => Ty::F64,
Token::U8 => Ty::U8,
Token::Char => Ty::Char,
_ => panic!("type lang issue, unmatched value type: {:?}", _vt.val),
};
let copied = curried.clone();
Expand All @@ -339,37 +339,54 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> {
let mut c_err: Option<Ty> = None;
let mut c_undefined: Option<Ty> = None;
let mut curried = Ty::Unknown;
let mut tag = vec![];

if let Some(right) = &_sig.right_most_type {
c_right = match self.lint_recurse(&right) {
Err(_) => Some(Ty::Unknown),
Ok(v) => Some(v.1),
Err(_) => {
tag.push(Ty::Unknown);
Some(Ty::Unknown)
}
Ok(v) => {
tag.push(v.1.clone());
Some(v.1)
}
}
}
if let Some(left) = &_sig.left_most_type {
c_left = match self.lint_recurse(&left) {
Err(_) => Ty::Unknown,
Ok(v) => v.1,
Err(_) => {
tag.push(Ty::Unknown);
Ty::Unknown
}
Ok(v) => {
tag.push(v.1.clone());
v.1
}
}
}
if let Some(_) = &_sig.err {
c_err = Some(Ty::Error);
tag.push(Ty::Error);
}
if let Some(_) = &_sig.undef {
c_undefined = Some(Ty::Undefined);
tag.push(Ty::Undefined);
}
if c_right.is_some() || c_err.is_some() || c_undefined.is_some() {
sig_info.left = c_left;
sig_info.err = c_err;
sig_info.undefined = c_undefined;
sig_info.right = c_right;
let full = tree!(SigTypes, sig_info);
curried = Ty::Tag(tag);

return Ok((full, curried));
}

let full = tree!(SingleType, c_left);
return Ok((full, curried));
curried = c_left.clone();
let full = tree!(SingleType, curried);
return Ok((full, c_left));
}

pub fn check_self_value(&mut self) -> ResultTreeType {
Expand Down Expand Up @@ -835,7 +852,7 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> {

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

let full: Rc<Box<TypeTree>> = tree!(SelfAccess, a);
let full: Rc<Box<TypeTree>> = tree!(SelfInit, a);
tbl.table.insert("self".to_string(), Rc::clone(&full));

return Ok((full, curried));
Expand Down Expand Up @@ -1237,6 +1254,24 @@ mod tests {
assert!(linter.issues.len() == 0);
}
#[test]
fn it_should_handle_sigs() {
const TEST_STR: &'static str = "
const val: zerror!?usize = 2
const val2: !?usize = 3
const val3: ?usize = 4
const val4: usize = 5
";
let lexer = TLexer::new(TEST_STR);
let mut parser = Parser::new(lexer);
let result = parser.all();
let mut tts = vec![];
let mut scps = vec![];
let mut linter = LintSource::new(TEST_STR, &mut scps, &mut tts);
let _ = linter.lint_check(&result.unwrap());

assert!(linter.issues.len() == 0);
}
#[test]
fn it_should_handle_global_data() {
const TEST_STR: &'static str = "const val: usize = 2
const main = fn() void { return 7 + val }
Expand Down
6 changes: 3 additions & 3 deletions playground.ty
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pub type Motorcycle = struct {
}

// Data has a type. can be manually declared with : <type>
const x = 7;
const y: usize = 7;
const x = 7
const y: usize = 7

// Types have types
// this is usually redundant
Expand All @@ -29,7 +29,7 @@ impl Car = trait(Movable) {

// default implementations can be achieved
pub const set_wheels_default = fn(self: Movable, wheels: u8) void {
self.wheels = wheels;
self.wheels = wheels
}

// default implementation
Expand Down

0 comments on commit 5f1e008

Please sign in to comment.