From 95e9270fef80f827325acc16c21d56ff73380551 Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Sun, 17 Nov 2024 20:04:53 +0100 Subject: [PATCH] adding case checking --- lang/README.md | 54 +++++++++--------- lang/src/lib.rs | 55 ++++++++++++++++--- lang/src/son.rs | 18 +++--- lang/src/utils.rs | 19 +++++++ ...sts_advanced_floating_point_arithmetic.txt | 2 +- lang/tests/son_tests_big_array_crash.txt | 2 +- lang/tests/son_tests_generic_types.txt | 4 +- ...ests_null_check_returning_small_global.txt | 4 +- .../son_tests_returning_optional_issues.txt | 2 +- lang/tests/son_tests_struct_in_register.txt | 2 +- 10 files changed, 112 insertions(+), 50 deletions(-) diff --git a/lang/README.md b/lang/README.md index 153b247..9c6b2ed 100644 --- a/lang/README.md +++ b/lang/README.md @@ -358,10 +358,10 @@ note: values of global variables are evaluated at compile time #### constants ```hb main := fn(): u32 { - return some_const + 35 + return SOME_CONST + 35 } -$some_const := 34 +$SOME_CONST := 34 ``` #### directives @@ -547,11 +547,11 @@ modify := fn($num: ^uint): void { #### generic_types ```hb -MALLOC_SYS_CALL := 69 -FREE_SYS_CALL := 96 +malloc_sys_call := 69 +free_sys_call := 96 -malloc := fn(size: uint, align: uint): ?^void return @eca(MALLOC_SYS_CALL, size, align) -free := fn(ptr: ^void, size: uint, align: uint): void return @eca(FREE_SYS_CALL, ptr, size, align) +malloc := fn(size: uint, align: uint): ?^void return @eca(malloc_sys_call, size, align) +free := fn(ptr: ^void, size: uint, align: uint): void return @eca(free_sys_call, ptr, size, align) Vec := fn($Elem: type): type { return struct { @@ -844,10 +844,10 @@ main := fn(): void { #### null_check_returning_small_global ```hb -MAGIC := 127 +magic := 127 get := fn(file: ^u8): ?uint { - if *file == MAGIC { - return MAGIC + if *file == magic { + return magic } else { return null } @@ -861,7 +861,7 @@ foo := fn(): ?uint { return null } else if gotten == 4 { return 2 - } else if gotten == MAGIC { + } else if gotten == magic { return 0 } @@ -909,7 +909,7 @@ dangle := fn(): ^uint return &0 #### advanced_floating_point_arithmetic ```hb -SIN_TABLE := [f32].(0.0, 0.02454122852291229, 0.04906767432741801, 0.07356456359966743, 0.0980171403295606, 0.1224106751992162, 0.1467304744553617, 0.1709618887603012, 0.1950903220161282, 0.2191012401568698, 0.2429801799032639, 0.2667127574748984, 0.2902846772544623, 0.3136817403988915, 0.3368898533922201, 0.3598950365349881, 0.3826834323650898, 0.4052413140049899, 0.4275550934302821, 0.4496113296546065, 0.4713967368259976, 0.492898192229784, 0.5141027441932217, 0.5349976198870972, 0.5555702330196022, 0.5758081914178453, 0.5956993044924334, 0.6152315905806268, 0.6343932841636455, 0.6531728429537768, 0.6715589548470183, 0.6895405447370668, 0.7071067811865475, 0.7242470829514669, 0.7409511253549591, 0.7572088465064845, 0.773010453362737, 0.7883464276266062, 0.8032075314806448, 0.8175848131515837, 0.8314696123025452, 0.844853565249707, 0.8577286100002721, 0.8700869911087113, 0.8819212643483549, 0.8932243011955153, 0.9039892931234433, 0.9142097557035307, 0.9238795325112867, 0.9329927988347388, 0.9415440651830208, 0.9495281805930367, 0.9569403357322089, 0.9637760657954398, 0.970031253194544, 0.9757021300385286, 0.9807852804032304, 0.9852776423889412, 0.989176509964781, 0.99247953459871, 0.9951847266721968, 0.9972904566786902, 0.9987954562051724, 0.9996988186962042, 1.0, 0.9996988186962042, 0.9987954562051724, 0.9972904566786902, 0.9951847266721969, 0.99247953459871, 0.989176509964781, 0.9852776423889412, 0.9807852804032304, 0.9757021300385286, 0.970031253194544, 0.9637760657954398, 0.9569403357322089, 0.9495281805930367, 0.9415440651830208, 0.9329927988347388, 0.9238795325112867, 0.9142097557035307, 0.9039892931234434, 0.8932243011955152, 0.881921264348355, 0.8700869911087115, 0.8577286100002721, 0.8448535652497072, 0.8314696123025455, 0.8175848131515837, 0.8032075314806449, 0.7883464276266063, 0.7730104533627371, 0.7572088465064847, 0.740951125354959, 0.7242470829514669, 0.7071067811865476, 0.6895405447370671, 0.6715589548470186, 0.6531728429537766, 0.6343932841636455, 0.6152315905806269, 0.5956993044924335, 0.5758081914178454, 0.5555702330196022, 0.5349976198870972, 0.5141027441932218, 0.4928981922297841, 0.4713967368259979, 0.4496113296546069, 0.427555093430282, 0.4052413140049899, 0.3826834323650899, 0.3598950365349883, 0.3368898533922203, 0.3136817403988914, 0.2902846772544624, 0.2667127574748985, 0.2429801799032641, 0.21910124015687, 0.1950903220161286, 0.1709618887603012, 0.1467304744553618, 0.1224106751992163, 0.09801714032956083, 0.07356456359966773, 0.04906767432741797, 0.02454122852291233, 0.0, -0.02454122852291208, -0.04906767432741772, -0.0735645635996675, -0.09801714032956059, -0.1224106751992161, -0.1467304744553616, -0.170961888760301, -0.1950903220161284, -0.2191012401568698, -0.2429801799032638, -0.2667127574748983, -0.2902846772544621, -0.3136817403988912, -0.3368898533922201, -0.3598950365349881, -0.3826834323650897, -0.4052413140049897, -0.4275550934302818, -0.4496113296546067, -0.4713967368259976, -0.4928981922297839, -0.5141027441932216, -0.5349976198870969, -0.555570233019602, -0.5758081914178453, -0.5956993044924332, -0.6152315905806267, -0.6343932841636453, -0.6531728429537765, -0.6715589548470184, -0.6895405447370668, -0.7071067811865475, -0.7242470829514668, -0.7409511253549589, -0.7572088465064842, -0.7730104533627367, -0.7883464276266059, -0.8032075314806451, -0.8175848131515838, -0.8314696123025452, -0.844853565249707, -0.857728610000272, -0.8700869911087113, -0.8819212643483549, -0.8932243011955152, -0.9039892931234431, -0.9142097557035305, -0.9238795325112865, -0.932992798834739, -0.9415440651830208, -0.9495281805930367, -0.9569403357322088, -0.9637760657954398, -0.970031253194544, -0.9757021300385285, -0.9807852804032303, -0.9852776423889411, -0.9891765099647809, -0.9924795345987101, -0.9951847266721969, -0.9972904566786902, -0.9987954562051724, -0.9996988186962042, -1.0, -0.9996988186962042, -0.9987954562051724, -0.9972904566786902, -0.9951847266721969, -0.9924795345987101, -0.9891765099647809, -0.9852776423889412, -0.9807852804032304, -0.9757021300385286, -0.970031253194544, -0.96377606579544, -0.9569403357322089, -0.9495281805930368, -0.9415440651830209, -0.9329927988347391, -0.9238795325112866, -0.9142097557035306, -0.9039892931234433, -0.8932243011955153, -0.881921264348355, -0.8700869911087115, -0.8577286100002722, -0.8448535652497072, -0.8314696123025455, -0.817584813151584, -0.8032075314806453, -0.7883464276266061, -0.7730104533627369, -0.7572088465064846, -0.7409511253549591, -0.724247082951467, -0.7071067811865477, -0.6895405447370672, -0.6715589548470187, -0.6531728429537771, -0.6343932841636459, -0.6152315905806274, -0.5956993044924332, -0.5758081914178452, -0.5555702330196022, -0.5349976198870973, -0.5141027441932219, -0.4928981922297843, -0.4713967368259979, -0.449611329654607, -0.4275550934302825, -0.4052413140049904, -0.3826834323650904, -0.359895036534988, -0.33688985339222, -0.3136817403988915, -0.2902846772544625, -0.2667127574748986, -0.2429801799032642, -0.2191012401568702, -0.1950903220161287, -0.1709618887603018, -0.1467304744553624, -0.122410675199216, -0.09801714032956051, -0.07356456359966741, -0.04906767432741809, -0.02454122852291245) +sin_table := [f32].(0.0, 0.02454122852291229, 0.04906767432741801, 0.07356456359966743, 0.0980171403295606, 0.1224106751992162, 0.1467304744553617, 0.1709618887603012, 0.1950903220161282, 0.2191012401568698, 0.2429801799032639, 0.2667127574748984, 0.2902846772544623, 0.3136817403988915, 0.3368898533922201, 0.3598950365349881, 0.3826834323650898, 0.4052413140049899, 0.4275550934302821, 0.4496113296546065, 0.4713967368259976, 0.492898192229784, 0.5141027441932217, 0.5349976198870972, 0.5555702330196022, 0.5758081914178453, 0.5956993044924334, 0.6152315905806268, 0.6343932841636455, 0.6531728429537768, 0.6715589548470183, 0.6895405447370668, 0.7071067811865475, 0.7242470829514669, 0.7409511253549591, 0.7572088465064845, 0.773010453362737, 0.7883464276266062, 0.8032075314806448, 0.8175848131515837, 0.8314696123025452, 0.844853565249707, 0.8577286100002721, 0.8700869911087113, 0.8819212643483549, 0.8932243011955153, 0.9039892931234433, 0.9142097557035307, 0.9238795325112867, 0.9329927988347388, 0.9415440651830208, 0.9495281805930367, 0.9569403357322089, 0.9637760657954398, 0.970031253194544, 0.9757021300385286, 0.9807852804032304, 0.9852776423889412, 0.989176509964781, 0.99247953459871, 0.9951847266721968, 0.9972904566786902, 0.9987954562051724, 0.9996988186962042, 1.0, 0.9996988186962042, 0.9987954562051724, 0.9972904566786902, 0.9951847266721969, 0.99247953459871, 0.989176509964781, 0.9852776423889412, 0.9807852804032304, 0.9757021300385286, 0.970031253194544, 0.9637760657954398, 0.9569403357322089, 0.9495281805930367, 0.9415440651830208, 0.9329927988347388, 0.9238795325112867, 0.9142097557035307, 0.9039892931234434, 0.8932243011955152, 0.881921264348355, 0.8700869911087115, 0.8577286100002721, 0.8448535652497072, 0.8314696123025455, 0.8175848131515837, 0.8032075314806449, 0.7883464276266063, 0.7730104533627371, 0.7572088465064847, 0.740951125354959, 0.7242470829514669, 0.7071067811865476, 0.6895405447370671, 0.6715589548470186, 0.6531728429537766, 0.6343932841636455, 0.6152315905806269, 0.5956993044924335, 0.5758081914178454, 0.5555702330196022, 0.5349976198870972, 0.5141027441932218, 0.4928981922297841, 0.4713967368259979, 0.4496113296546069, 0.427555093430282, 0.4052413140049899, 0.3826834323650899, 0.3598950365349883, 0.3368898533922203, 0.3136817403988914, 0.2902846772544624, 0.2667127574748985, 0.2429801799032641, 0.21910124015687, 0.1950903220161286, 0.1709618887603012, 0.1467304744553618, 0.1224106751992163, 0.09801714032956083, 0.07356456359966773, 0.04906767432741797, 0.02454122852291233, 0.0, -0.02454122852291208, -0.04906767432741772, -0.0735645635996675, -0.09801714032956059, -0.1224106751992161, -0.1467304744553616, -0.170961888760301, -0.1950903220161284, -0.2191012401568698, -0.2429801799032638, -0.2667127574748983, -0.2902846772544621, -0.3136817403988912, -0.3368898533922201, -0.3598950365349881, -0.3826834323650897, -0.4052413140049897, -0.4275550934302818, -0.4496113296546067, -0.4713967368259976, -0.4928981922297839, -0.5141027441932216, -0.5349976198870969, -0.555570233019602, -0.5758081914178453, -0.5956993044924332, -0.6152315905806267, -0.6343932841636453, -0.6531728429537765, -0.6715589548470184, -0.6895405447370668, -0.7071067811865475, -0.7242470829514668, -0.7409511253549589, -0.7572088465064842, -0.7730104533627367, -0.7883464276266059, -0.8032075314806451, -0.8175848131515838, -0.8314696123025452, -0.844853565249707, -0.857728610000272, -0.8700869911087113, -0.8819212643483549, -0.8932243011955152, -0.9039892931234431, -0.9142097557035305, -0.9238795325112865, -0.932992798834739, -0.9415440651830208, -0.9495281805930367, -0.9569403357322088, -0.9637760657954398, -0.970031253194544, -0.9757021300385285, -0.9807852804032303, -0.9852776423889411, -0.9891765099647809, -0.9924795345987101, -0.9951847266721969, -0.9972904566786902, -0.9987954562051724, -0.9996988186962042, -1.0, -0.9996988186962042, -0.9987954562051724, -0.9972904566786902, -0.9951847266721969, -0.9924795345987101, -0.9891765099647809, -0.9852776423889412, -0.9807852804032304, -0.9757021300385286, -0.970031253194544, -0.96377606579544, -0.9569403357322089, -0.9495281805930368, -0.9415440651830209, -0.9329927988347391, -0.9238795325112866, -0.9142097557035306, -0.9039892931234433, -0.8932243011955153, -0.881921264348355, -0.8700869911087115, -0.8577286100002722, -0.8448535652497072, -0.8314696123025455, -0.817584813151584, -0.8032075314806453, -0.7883464276266061, -0.7730104533627369, -0.7572088465064846, -0.7409511253549591, -0.724247082951467, -0.7071067811865477, -0.6895405447370672, -0.6715589548470187, -0.6531728429537771, -0.6343932841636459, -0.6152315905806274, -0.5956993044924332, -0.5758081914178452, -0.5555702330196022, -0.5349976198870973, -0.5141027441932219, -0.4928981922297843, -0.4713967368259979, -0.449611329654607, -0.4275550934302825, -0.4052413140049904, -0.3826834323650904, -0.359895036534988, -0.33688985339222, -0.3136817403988915, -0.2902846772544625, -0.2667127574748986, -0.2429801799032642, -0.2191012401568702, -0.1950903220161287, -0.1709618887603018, -0.1467304744553624, -0.122410675199216, -0.09801714032956051, -0.07356456359966741, -0.04906767432741809, -0.02454122852291245) sin := fn(theta: f32): f32 { PI := 3.14159265358979323846 @@ -918,7 +918,7 @@ sin := fn(theta: f32): f32 { d := theta - @floatcast(@itf(si)) * 2.0 * PI / @itf(TABLE_SIZE) ci := si + TABLE_SIZE / 4 & TABLE_SIZE - 1 si &= TABLE_SIZE - 1 - return SIN_TABLE[@bitcast(si)] + (SIN_TABLE[@bitcast(ci)] - 0.5 * SIN_TABLE[@bitcast(si)] * d) * d + return sin_table[@bitcast(si)] + (sin_table[@bitcast(ci)] - 0.5 * sin_table[@bitcast(si)] * d) * d } main := fn(): int { @@ -982,10 +982,10 @@ main := fn(): uint { #### returning_optional_issues ```hb -BMP := 0 +bmp := 0 get_format := fn(): ?uint { - return BMP + return bmp } main := fn(): uint { @@ -1102,9 +1102,9 @@ main := fn(): void { #### big_array_crash ```hb -SIN_TABLE := [int].(0, 174, 348, 523, 697, 871, 1045, 1218, 1391, 1564, 1736, 1908, 2079, 2249, 2419, 2588, 2756, 2923, 3090, 3255, 3420, 3583, 3746, 3907, 4067, 4226, 4384, 4540, 4695, 4848, 5000, 5150, 5299, 5446, 5591, 5735, 5877, 6018, 6156, 6293, 6427, 6560, 6691, 6819, 6946, 7071, 7193, 7313, 7431, 7547, 7660, 7771, 7880, 7986, 8090, 8191, 8290, 8386, 8480, 8571, 8660, 8746, 8829, 8910, 8987, 9063, 9135, 9205, 9271, 9335, 9396, 9455, 9510, 9563, 9612, 9659, 9702, 9743, 9781, 9816, 9848, 9877, 9902, 9925, 9945, 9961, 9975, 9986, 9993, 9998, 10000) +sin_table := [int].(0, 174, 348, 523, 697, 871, 1045, 1218, 1391, 1564, 1736, 1908, 2079, 2249, 2419, 2588, 2756, 2923, 3090, 3255, 3420, 3583, 3746, 3907, 4067, 4226, 4384, 4540, 4695, 4848, 5000, 5150, 5299, 5446, 5591, 5735, 5877, 6018, 6156, 6293, 6427, 6560, 6691, 6819, 6946, 7071, 7193, 7313, 7431, 7547, 7660, 7771, 7880, 7986, 8090, 8191, 8290, 8386, 8480, 8571, 8660, 8746, 8829, 8910, 8987, 9063, 9135, 9205, 9271, 9335, 9396, 9455, 9510, 9563, 9612, 9659, 9702, 9743, 9781, 9816, 9848, 9877, 9902, 9925, 9945, 9961, 9975, 9986, 9993, 9998, 10000) -main := fn(): int return SIN_TABLE[10] +main := fn(): int return sin_table[10] ``` #### returning_global_struct @@ -1241,11 +1241,11 @@ main := fn(): uint { // in module: math.hb -SIZEOF_uint := 32 -SHIFT := SIZEOF_uint - 1 +sizeof_uint := 32 +shift := sizeof_uint - 1 min := fn(a: uint, b: uint): uint { c := a - b - return b + (c & c >> SHIFT) + return b + (c & c >> shift) } ``` @@ -1344,10 +1344,10 @@ sqrt := fn(x: uint): uint { #### struct_in_register ```hb ColorBGRA := struct {b: u8, g: u8, r: u8, a: u8} -MAGENTA := ColorBGRA.{b: 205, g: 0, r: 205, a: 255} +magenta := ColorBGRA.{b: 205, g: 0, r: 205, a: 255} main := fn(): uint { - color := MAGENTA + color := magenta return color.r } ``` @@ -1356,9 +1356,9 @@ main := fn(): uint { ```hb stn := @use("stn.hb") -CONST_A := 100 -CONST_B := 50 -a := stn.math.min(CONST_A, CONST_B) +const_a := 100 +const_b := 50 +a := stn.math.min(const_a, const_b) main := fn(): uint { return a @@ -1368,11 +1368,11 @@ main := fn(): uint { math := @use("math.hb") // in module: math.hb -SIZEOF_uint := 32 -SHIFT := SIZEOF_uint - 1 +sizeof_uint := 32 +shift := sizeof_uint - 1 min := fn(a: uint, b: uint): uint { c := a - b - return b + (c & c >> SHIFT) + return b + (c & c >> shift) } ``` diff --git a/lang/src/lib.rs b/lang/src/lib.rs index 971701d..bf021a0 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -548,6 +548,10 @@ pub mod ty { };)* } + impl Builtin { + $(pub const $name: Self = Builtin($name);)* + } + impl Id { $(pub const $name: Self = Kind::Builtin(Builtin($name)).compress();)* } @@ -755,7 +759,8 @@ pub mod ty { TK::Global(idx) => { let global = &self.tys.ins.globals[idx]; let file = &self.files[global.file.index()]; - f.write_str(file.ident_str(global.name)) + f.write_str(file.ident_str(global.name))?; + f.write_str(" (global)") } TK::Slice(idx) => { let array = self.tys.ins.slices[idx]; @@ -770,7 +775,8 @@ pub mod ty { TK::Const(idx) => { let cnst = &self.tys.ins.consts[idx]; let file = &self.files[cnst.file.index()]; - f.write_str(file.ident_str(cnst.name)) + f.write_str(file.ident_str(cnst.name))?; + f.write_str(" (const)") } } } @@ -806,6 +812,7 @@ struct Func { expr: ExprRef, sig: Option, is_inline: bool, + returns_type: bool, comp_state: [CompState; 2], } @@ -1029,12 +1036,13 @@ pub struct Types { trait TypeParser { fn tys(&mut self) -> &mut Types; + fn ty_display(&self, of: ty::Id) -> ty::Display; fn on_reuse(&mut self, existing: ty::Id); fn find_local_ty(&mut self, name: Ident) -> Option; fn eval_const(&mut self, file: Module, expr: &Expr, ty: ty::Id) -> u64; fn eval_global(&mut self, file: Module, name: Ident, expr: &Expr) -> ty::Id; fn infer_type(&mut self, expr: &Expr) -> ty::Id; - fn report(&self, file: Module, pos: Pos, msg: impl Display) -> ty::Id; + fn error(&self, file: Module, pos: Pos, msg: impl Display) -> ty::Id; fn find_type( &mut self, @@ -1061,9 +1069,9 @@ trait TypeParser { return match id { Ok(_) => { debug_assert_eq!(from_file, file); - self.report(file, pos, "somehow this was not found") + self.error(file, pos, "somehow this was not found") } - Err("main") => self.report( + Err("main") => self.error( from_file, pos, format_args!( @@ -1074,13 +1082,13 @@ trait TypeParser { ), ), Err(name) => { - self.report(from_file, pos, format_args!("undefined indentifier: {name}")) + self.error(from_file, pos, format_args!("undefined indentifier: {name}")) } }; }; let tys = self.tys(); - if let Some(&ty) = tys.syms.get(SymKey::Decl(file, name), &tys.ins) { + let ty = if let Some(&ty) = tys.syms.get(SymKey::Decl(file, name), &tys.ins) { ty } else { let (is_ct, ty) = left @@ -1107,7 +1115,22 @@ trait TypeParser { } tys.syms.insert(SymKey::Decl(file, name), ty, &tys.ins); ty + }; + + if let Err(proper_case) = self.tys().case(ty)(f.ident_str(name)) { + self.error( + from_file, + pos, + format_args!( + "the declaration does not have conventional \ + casing, expected '{proper_case}', \ + because the declared type is '{}'", + self.ty_display(ty), + ), + ); } + + ty }; let tys = self.tys(); @@ -1250,13 +1273,14 @@ trait TypeParser { } let Some(args) = self.tys().pack_args(arg_base) else { - return self.report(file, pos, "function has too many argumnets"); + return self.error(file, pos, "function has too many argumnets"); }; let ret = self.parse_ty(file, ret, None, files); Some(Sig { args, ret }) }, expr: ExprRef::new(expr), + returns_type: matches!(ret, &Expr::Ident { id, .. } if ty::Builtin::try_from(id) == Ok(ty::Builtin::TYPE)), ..Default::default() }; @@ -1269,6 +1293,21 @@ trait TypeParser { } impl Types { + pub fn case(&self, ty: ty::Id) -> fn(&str) -> Result<(), &'static str> { + match ty.expand() { + ty::Kind::NEVER => |_| Ok(()), + ty::Kind::Enum(_) + | ty::Kind::Struct(_) + | ty::Kind::Builtin(_) + | ty::Kind::Ptr(_) + | ty::Kind::Slice(_) + | ty::Kind::Opt(_) => utils::is_pascal_case, + ty::Kind::Func(f) if self.ins.funcs[f].returns_type => utils::is_pascal_case, + ty::Kind::Func(_) | ty::Kind::Global(_) | ty::Kind::Module(_) => utils::is_snake_case, + ty::Kind::Const(_) => utils::is_screaming_case, + } + } + fn pack_args(&mut self, arg_base: usize) -> Option { let base = self.ins.args.len(); self.ins.args.extend(self.tmp.args.drain(arg_base..)); diff --git a/lang/src/son.rs b/lang/src/son.rs index 7352959..707d46b 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -3597,7 +3597,7 @@ impl<'a> Codegen<'a> { ret = ret.and(self.expr(stmt)); if let Some(mut id) = ret { if id.ty != ty::Id::VOID { - self.error( + self.warn( stmt.pos(), fa!( "statements need to evaluate to 'void', \ @@ -4967,11 +4967,11 @@ impl<'a> Codegen<'a> { value.ty = to; } - //#[track_caller] - //fn warn(&self, pos: Pos, msg: impl core::fmt::Display) { - // let mut buf = self.warnings.borrow_mut(); - // write!(buf, "{}", self.file().report(pos, msg)).unwrap(); - //} + #[track_caller] + fn warn(&self, pos: Pos, msg: impl core::fmt::Display) { + let mut buf = self.errors.borrow_mut(); + write!(buf, "(W) {}", self.file().report(pos, msg)).unwrap(); + } #[track_caller] fn error(&self, pos: Pos, msg: impl core::fmt::Display) { @@ -4995,6 +4995,10 @@ impl TypeParser for Codegen<'_> { self.tys } + fn ty_display(&self, of: ty::Id) -> ty::Display { + self.ty_display(of) + } + fn eval_const(&mut self, file: Module, expr: &Expr, ret: ty::Id) -> u64 { self.ct.activate(); let mut scope = mem::take(&mut self.ci.scope.vars); @@ -5062,7 +5066,7 @@ impl TypeParser for Codegen<'_> { gid.into() } - fn report(&self, file: Module, pos: Pos, msg: impl Display) -> ty::Id { + fn error(&self, file: Module, pos: Pos, msg: impl Display) -> ty::Id { let mut buf = self.errors.borrow_mut(); write!(buf, "{}", self.files[file.index()].report(pos, msg)).unwrap(); ty::Id::NEVER diff --git a/lang/src/utils.rs b/lang/src/utils.rs index 8609894..40da23f 100644 --- a/lang/src/utils.rs +++ b/lang/src/utils.rs @@ -12,6 +12,25 @@ use { }, }; +fn decide(b: bool, name: &'static str) -> Result<(), &'static str> { + b.then_some(()).ok_or(name) +} + +pub fn is_snake_case(str: &str) -> Result<(), &'static str> { + decide(str.bytes().all(|c| matches!(c, b'a'..=b'z' | b'0'..=b'9' | b'_')), "snake_case") +} + +pub fn is_pascal_case(str: &str) -> Result<(), &'static str> { + decide( + str.as_bytes()[0].is_ascii_uppercase() && str.bytes().all(|c| c.is_ascii_alphanumeric()), + "PascalCase", + ) +} + +pub fn is_screaming_case(str: &str) -> Result<(), &'static str> { + decide(str.bytes().all(|c| matches!(c, b'A'..=b'Z' | b'0'..=b'9' | b'_')), "SCREAMING_CASE") +} + type Nid = u16; pub union BitSet { diff --git a/lang/tests/son_tests_advanced_floating_point_arithmetic.txt b/lang/tests/son_tests_advanced_floating_point_arithmetic.txt index 076c2e7..f46cbd0 100644 --- a/lang/tests/son_tests_advanced_floating_point_arithmetic.txt +++ b/lang/tests/son_tests_advanced_floating_point_arithmetic.txt @@ -21,7 +21,7 @@ sin: ANDI r15, r14, 255d ITF64 r16, r14 MULI64 r15, r15, 4d - LRA r17, r0, :SIN_TABLE + LRA r17, r0, :sin_table LI32 r18, 1086918619w FC64T32 r16, r16, 1b ADDI64 r14, r14, 64d diff --git a/lang/tests/son_tests_big_array_crash.txt b/lang/tests/son_tests_big_array_crash.txt index 61ce17b..693f648 100644 --- a/lang/tests/son_tests_big_array_crash.txt +++ b/lang/tests/son_tests_big_array_crash.txt @@ -1,5 +1,5 @@ main: - LRA r13, r0, :SIN_TABLE + LRA r13, r0, :sin_table LD r13, r13, 80a, 8h CP r1, r13 JALA r0, r31, 0a diff --git a/lang/tests/son_tests_generic_types.txt b/lang/tests/son_tests_generic_types.txt index b71e0bb..4b6101d 100644 --- a/lang/tests/son_tests_generic_types.txt +++ b/lang/tests/son_tests_generic_types.txt @@ -19,7 +19,7 @@ free: CP r13, r2 CP r14, r3 CP r15, r4 - LRA r16, r0, :FREE_SYS_CALL + LRA r16, r0, :free_sys_call LD r16, r16, 0a, 8h CP r2, r16 CP r3, r13 @@ -49,7 +49,7 @@ main: malloc: CP r13, r2 CP r14, r3 - LRA r15, r0, :MALLOC_SYS_CALL + LRA r15, r0, :malloc_sys_call LD r15, r15, 0a, 8h CP r2, r15 CP r3, r13 diff --git a/lang/tests/son_tests_null_check_returning_small_global.txt b/lang/tests/son_tests_null_check_returning_small_global.txt index fc8fe55..deca828 100644 --- a/lang/tests/son_tests_null_check_returning_small_global.txt +++ b/lang/tests/son_tests_null_check_returning_small_global.txt @@ -23,7 +23,7 @@ foo: ST r33, r254, 40a, 8h LD r1, r32, 0a, 16h JMP :1 - 2: LRA r34, r0, :MAGIC + 2: LRA r34, r0, :magic LD r34, r34, 0a, 8h JNE r34, r32, :3 ADDI64 r32, r254, 16d @@ -41,7 +41,7 @@ get: ADDI64 r254, r254, -32d CP r13, r3 LD r13, r13, 0a, 1h - LRA r14, r0, :MAGIC + LRA r14, r0, :magic ANDI r13, r13, 255d LD r14, r14, 0a, 8h JNE r14, r13, :0 diff --git a/lang/tests/son_tests_returning_optional_issues.txt b/lang/tests/son_tests_returning_optional_issues.txt index 5b0422d..4e99b5f 100644 --- a/lang/tests/son_tests_returning_optional_issues.txt +++ b/lang/tests/son_tests_returning_optional_issues.txt @@ -2,7 +2,7 @@ get_format: ADDI64 r254, r254, -16d LI8 r13, 1b ADDI64 r14, r254, 0d - LRA r15, r0, :BMP + LRA r15, r0, :bmp ST r13, r254, 0a, 1h LD r13, r15, 0a, 8h ST r13, r254, 8a, 8h diff --git a/lang/tests/son_tests_struct_in_register.txt b/lang/tests/son_tests_struct_in_register.txt index ffac3b1..25fcaa7 100644 --- a/lang/tests/son_tests_struct_in_register.txt +++ b/lang/tests/son_tests_struct_in_register.txt @@ -1,6 +1,6 @@ main: ADDI64 r254, r254, -4d - LRA r13, r0, :MAGENTA + LRA r13, r0, :magenta ADDI64 r14, r254, 0d BMC r13, r14, 4h LD r13, r254, 2a, 1h