Add ability to specify variable types in let

This commit is contained in:
Sofia 2025-07-25 00:40:58 +03:00
parent ea8a833bdf
commit 6634597c92
5 changed files with 18 additions and 8 deletions

View File

@ -45,12 +45,11 @@ Currently missing big features (TODOs) are:
- ~~Built-in Int/Float division and modulo~~ (DONE)
- ~~Loops~~ (DONE)
- ~~Intrinsic functions~~ (DONE)
- ~~Ability to specify types in literals~~ (DONE)
- ~~Ability to specify types in literals and variable definitions~~ (DONE)
- Debug Information (PARTIALLY DONE)
- Not-Unary
- Importing types from other modules
- Importable binops?
- Ability to specify types in variable definitions
Big features that I want later but are not necessary:
- Associated functions

View File

@ -17,7 +17,7 @@ fn main() -> u32 {
let mut value = test();
let beep = [2, 3, 4];
let boop = 3;
let boop: f32 = 3;
let mut a = &mut value;

View File

@ -1,9 +1,6 @@
// Arithmetic, function calls and imports!
pub fn test() -> u8 {
return 5;
}
pub fn main() -> bool {
return 7.5 > (test() as f16);
return 7.5 > 5.001;
}

View File

@ -531,6 +531,13 @@ impl Parse for LetStatement {
let mutability = stream.expect(Token::MutKeyword).is_ok();
if let Some(Token::Identifier(variable)) = stream.next() {
let ty = if let Some(Token::Colon) = stream.peek() {
stream.next(); // Consume colon
Some(stream.parse()?)
} else {
None
};
let range = stream.get_range_prev().unwrap();
stream.expect(Token::Equals)?;
@ -538,7 +545,7 @@ impl Parse for LetStatement {
stream.expect(Token::Semi)?;
Ok(LetStatement {
name: variable,
ty: None,
ty,
mutable: mutability,
value: expression,
name_range: range,

View File

@ -900,6 +900,13 @@ impl Literal {
(L::Vague(VagueL::Number(v)), TypeKind::U32) => L::U32(v as u32),
(L::Vague(VagueL::Number(v)), TypeKind::U64) => L::U64(v as u64),
(L::Vague(VagueL::Number(v)), TypeKind::U128) => L::U128(v as u128),
(L::Vague(VagueL::Number(v)), TypeKind::F16) => L::F16(v as f32),
(L::Vague(VagueL::Number(v)), TypeKind::F32) => L::F32(v as f32),
(L::Vague(VagueL::Number(v)), TypeKind::F32B) => L::F32B(v as f32),
(L::Vague(VagueL::Number(v)), TypeKind::F64) => L::F64(v as f64),
(L::Vague(VagueL::Number(v)), TypeKind::F80) => L::F80(v as f64),
(L::Vague(VagueL::Number(v)), TypeKind::F128) => L::F128(v as f64),
(L::Vague(VagueL::Number(v)), TypeKind::F128PPC) => L::F128PPC(v as f64),
(L::Vague(VagueL::Decimal(v)), TypeKind::F16) => L::F16(v as f32),
(L::Vague(VagueL::Decimal(v)), TypeKind::F32) => L::F32(v as f32),
(L::Vague(VagueL::Decimal(v)), TypeKind::F32B) => L::F32B(v as f32),