Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ianyourgod committed Jul 8, 2024
1 parent 32ef4b2 commit 0435179
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 23 deletions.
6 changes: 2 additions & 4 deletions main.ag
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
fn main() -> u32 {
let unsigned1: u32 = 8;
let unsigned2: u32 = 3;
let unsigned1: u32 = -9223372036854775809;
}

return unsigned1 / unsigned2 % unsigned2;
}
62 changes: 50 additions & 12 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,23 +528,35 @@ impl Parser {
}

fn parse_int_literal(&mut self) -> Box<nodes::Expression> {
let int = self.cur_token.literal.parse::<i64>().unwrap();
let u_int = self.cur_token.literal.parse::<i128>();

let int = match u_int {
Result::Ok(val) => {
val
},
_ => panic!("Invalid integer literal")
};

self.next_token();

let i32_max = i32::MAX as i64;
let i32_min = i32::MIN as i64;
let u32_max = u32::MAX as i64;
let i32_max = i32::MAX as i128;
let i32_min = i32::MIN as i128;
let i64_max = i64::MAX as i128;
let i64_min = i64::MIN as i128;
let u64_max = u64::MAX as i128;

if int > u32_max {
Box::new(nodes::Expression::Literal(nodes::Literal::Generic64(int as u64), Some(nodes::Type::Generic64)))
if int > i64_max {
Box::new(nodes::Expression::Literal(nodes::Literal::U64(int as u64), Some(nodes::Type::U64)))
} else if int > i32_max {
Box::new(nodes::Expression::Literal(nodes::Literal::Generic32(int as u32), Some(nodes::Type::U32)))
} else if int < i32_min {
Box::new(nodes::Expression::Literal(nodes::Literal::I64(int), Some(nodes::Type::I64)))
} else if int < 0 {
Box::new(nodes::Expression::Literal(nodes::Literal::I32(int as i32), Some(nodes::Type::GenericInt)))
} else {
Box::new(nodes::Expression::Literal(nodes::Literal::Generic64(int as u64), Some(nodes::Type::Generic64)))
} else if int >= 0 {
Box::new(nodes::Expression::Literal(nodes::Literal::Generic32(int as u32), Some(nodes::Type::Generic32)))
} else if int >= i32_min {
Box::new(nodes::Expression::Literal(nodes::Literal::I32(int as i32), Some(nodes::Type::I32)))
} else if int >= i64_min {
Box::new(nodes::Expression::Literal(nodes::Literal::I64(int as i64), Some(nodes::Type::I64)))
} else {
panic!("Integer literal out of range")
}
}

Expand All @@ -560,6 +572,32 @@ impl Parser {
lexer::TokenType::Subtract => {
let op = self.parse_unop();
self.next_token();

if self.cur_token.kind == lexer::TokenType::Int {
let int_lit = *self.parse_int_literal();

match int_lit {
nodes::Expression::Literal(lit, _) => {
return match lit {
nodes::Literal::Generic32(val) => {
Box::new(nodes::Expression::Literal(nodes::Literal::I32(-(val as i32)), Some(nodes::Type::GenericInt)))
},
nodes::Literal::I32(val) => {
Box::new(nodes::Expression::Literal(nodes::Literal::I32(-val), Some(nodes::Type::GenericInt)))
},
nodes::Literal::Generic64(val) => {
Box::new(nodes::Expression::Literal(nodes::Literal::I64(-(val as i64)), Some(nodes::Type::I64)))
},
nodes::Literal::I64(val) => {
Box::new(nodes::Expression::Literal(nodes::Literal::I64(-val), Some(nodes::Type::I64)))
},
nodes::Literal::U64(val) => panic!("Cannot negate unsigned integer.\nYour number is too big to fit into an integer type and therefor is forced into an unsigned type."),
}
},
_ => panic!("")
}
}

let expr = self.parse_factor();
Box::new(nodes::Expression::UnaryOp(op, expr, None))
},
Expand Down
11 changes: 6 additions & 5 deletions src/parser/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ pub enum Expression {

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Literal {
Generic32(u32),
Generic64(u64),
I32(i32),
I64(i64),
U64(u64),
Generic32(u32), // 0 <-> i32::MAX
Generic64(u64), // i32::MAX + 1 <-> I64::MAX
I32(i32), // i32::MIN <-> -1
I64(i64), // i64::MIN <-> i32::MIN - 1
U64(u64), // i64::MAX + 1 <-> u64::MAX
// no u32 as its just in the generic64 range
}

impl Literal {
Expand Down
5 changes: 3 additions & 2 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ wow todos for the argent language
- [x] Rewrite to use tacky and a lot of compiler passes
- [ ] Pattern matching
- [ ] Do while?
- [ ] Have an actual compile time error thing instead of just panicking
- [x] Type checking
- [ ] Structs
- [ ] Enums
Expand All @@ -26,8 +27,8 @@ wow todos for the argent language
- [ ] isize
- [ ] u8
- [ ] u16
- [ ] uint (can also be u32)
- [ ] u64
- [x] uint (can also be u32)
- [x] u64
- [ ] usize
- [ ] float
- [ ] double
Expand Down

0 comments on commit 0435179

Please sign in to comment.