From e62aab9b4b0b6e5c7f7ec1946f92877baf3d155f Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Fri, 25 Oct 2024 15:40:23 +0200 Subject: [PATCH] adding better binaro operator errors positions --- lang/src/codegen.rs | 10 +++++----- lang/src/fmt.rs | 5 +++-- lang/src/parser.rs | 14 +++++++++---- lang/src/son.rs | 18 ++++++++--------- lang/tests/son_tests_arrays.txt | 16 +++++++-------- lang/tests/son_tests_c_strings.txt | 4 ++-- lang/tests/son_tests_different_types.txt | 10 +++++----- lang/tests/son_tests_generic_functions.txt | 4 ++-- lang/tests/son_tests_idk.txt | 20 +++++++++---------- lang/tests/son_tests_intcast_store.txt | 6 +++--- lang/tests/son_tests_request_page.txt | 4 ++-- lang/tests/son_tests_struct_operators.txt | 8 ++++---- lang/tests/son_tests_struct_patterns.txt | 4 ++-- ...sts_struct_return_from_module_function.txt | 4 ++-- .../tests/son_tests_tests_ptr_to_ptr_copy.txt | 4 ++-- lang/tests/son_tests_wide_ret.txt | 16 +++++++-------- 16 files changed, 77 insertions(+), 70 deletions(-) diff --git a/lang/src/codegen.rs b/lang/src/codegen.rs index cda4d2b..bbca41f 100644 --- a/lang/src/codegen.rs +++ b/lang/src/codegen.rs @@ -1352,7 +1352,7 @@ impl Codegen { ), } } - E::BinOp { left, op: T::Decl, right } if self.has_ct(left) => { + E::BinOp { left, op: T::Decl, right, .. } if self.has_ct(left) => { let slot_base = self.ct.vm.read_reg(reg::STACK_PTR).0; let (cnt, ty) = self.eval_const_low(self.ci.file, right, None); if self.assign_ct_pattern(left, ty, cnt as _) { @@ -1360,7 +1360,7 @@ impl Codegen { } Some(Value::void()) } - E::BinOp { left, op: T::Decl, right } => { + E::BinOp { left, op: T::Decl, right, .. } => { let value = self.expr(right)?; self.assign_pattern(left, value) } @@ -1506,7 +1506,7 @@ impl Codegen { E::If { cond, then, mut else_, .. } => { let mut then = Some(then); let jump_offset; - if let &E::BinOp { left, op, right } = cond + if let &E::BinOp { left, op, right, .. } = cond && let ty = self.infer_type(left) && let Some((op, swapped)) = op.cond_op(ty.is_signed()) { @@ -1605,7 +1605,7 @@ impl Codegen { self.ci.emit(jmp(loop_.offset as i32 - offset as i32)); None } - E::BinOp { left, op: op @ (T::And | T::Or), right } => { + E::BinOp { left, op: op @ (T::And | T::Or), right, .. } => { let lhs = self.expr_ctx(left, Ctx::default().with_ty(ty::BOOL))?; let lhs = self.loc_to_reg(lhs.loc, 1); let jump_offset = self.ci.code.len() + 3; @@ -1622,7 +1622,7 @@ impl Codegen { Some(Value { ty: ty::Id::BOOL, loc: Loc::reg(lhs) }) } - E::BinOp { left, op, right } if op != T::Decl => 'ops: { + E::BinOp { left, op, right, .. } if op != T::Decl => 'ops: { let left = self.expr_ctx(left, Ctx { ty: ctx.ty.filter(|_| op.is_homogenous()), check: ctx.check, diff --git a/lang/src/fmt.rs b/lang/src/fmt.rs index 05ade5c..54c3f4f 100644 --- a/lang/src/fmt.rs +++ b/lang/src/fmt.rs @@ -358,7 +358,8 @@ impl<'a> Formatter<'a> { Expr::BinOp { left, op: TokenKind::Assign, - right: &Expr::BinOp { left: lleft, op, right }, + right: &Expr::BinOp { left: lleft, op, right, .. }, + .. } if left.pos() == lleft.pos() => { self.fmt(left, f)?; f.write_str(" ")?; @@ -366,7 +367,7 @@ impl<'a> Formatter<'a> { f.write_str("= ")?; self.fmt(right, f) } - Expr::BinOp { right, op, left } => { + Expr::BinOp { right, op, left, .. } => { let prec_miss_left = |e: &Expr| { matches!( e, Expr::BinOp { op: lop, .. } if op.precedence() > lop.precedence() diff --git a/lang/src/parser.rs b/lang/src/parser.rs index 1032164..de3b1d5 100644 --- a/lang/src/parser.rs +++ b/lang/src/parser.rs @@ -159,7 +159,7 @@ impl<'a, 'b> Parser<'a, 'b> { break; } - let op = self.next().kind; + let Token { kind: op, start: pos, .. } = self.next(); if op == TokenKind::Decl { self.declare_rec(&fold, top_level); @@ -172,10 +172,15 @@ impl<'a, 'b> Parser<'a, 'b> { if let Some(op) = op.ass_op() { self.flag_idents(*left, idfl::MUTABLE); - let right = Expr::BinOp { left: self.arena.alloc(fold), op, right }; - fold = Expr::BinOp { left, op: TokenKind::Assign, right: self.arena.alloc(right) }; + let right = Expr::BinOp { left: self.arena.alloc(fold), pos, op, right }; + fold = Expr::BinOp { + left, + pos, + op: TokenKind::Assign, + right: self.arena.alloc(right), + }; } else { - fold = Expr::BinOp { left, right, op }; + fold = Expr::BinOp { left, right, pos, op }; if op == TokenKind::Assign { self.flag_idents(*left, idfl::MUTABLE); } @@ -762,6 +767,7 @@ generate_expr! { /// `Expr OP Expr` BinOp { left: &'a Self, + pos: Pos, op: TokenKind, right: &'a Self, }, diff --git a/lang/src/son.rs b/lang/src/son.rs index dcad7bd..46fb4e1 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -2265,7 +2265,7 @@ impl<'a> Codegen<'a> { } Some(self.ci.nodes.new_node_lit(val.ty, Kind::UnOp { op }, [VOID, val.id])) } - Expr::BinOp { left, op: TokenKind::Decl, right } => { + Expr::BinOp { left, op: TokenKind::Decl, right, .. } => { let mut right = self.expr(right)?; if right.ty.loc(&self.tys) == Loc::Stack { let stck = self.ci.nodes.new_node_nop(right.ty, Kind::Stck, [VOID, MEM]); @@ -2276,11 +2276,11 @@ impl<'a> Codegen<'a> { self.assign_pattern(left, right); Some(Value::VOID) } - Expr::BinOp { left, op: TokenKind::Assign, right } => { + Expr::BinOp { left, pos, op: TokenKind::Assign, right } => { let dest = self.raw_expr(left)?; let mut value = self.expr_ctx(right, Ctx::default().with_ty(dest.ty))?; - self.assert_ty(left.pos(), &mut value, dest.ty, "assignment source"); + self.assert_ty(pos, &mut value, dest.ty, "assignment source"); if dest.var { let var = &mut self.ci.scope.vars[(u16::MAX - dest.id) as usize]; @@ -2295,12 +2295,12 @@ impl<'a> Codegen<'a> { } else if dest.ptr { self.store_mem(dest.id, dest.ty, value.id); } else { - self.report(left.pos(), "cannot assign to this expression"); + self.report(pos, "cannot assign to this expression"); } Some(Value::VOID) } - Expr::BinOp { left, op, right } + Expr::BinOp { left, pos, op, right } if !matches!(op, TokenKind::Assign | TokenKind::Decl) => { let mut lhs = self.raw_expr_ctx(left, ctx)?; @@ -2316,7 +2316,7 @@ impl<'a> Codegen<'a> { self.ci.nodes.unlock(lhs.id); let mut rhs = rhs?; self.strip_var(&mut rhs); - let ty = self.binop_ty(right.pos(), &mut lhs, &mut rhs, op); + let ty = self.binop_ty(pos, &mut lhs, &mut rhs, op); let inps = [VOID, lhs.id, rhs.id]; Some(self.ci.nodes.new_node_lit( ty::bin_ret(ty, op), @@ -2330,15 +2330,15 @@ impl<'a> Codegen<'a> { self.ci.nodes.unlock(lhs.id); let mut rhs = rhs?; self.strip_var(&mut rhs); - self.assert_ty(right.pos(), &mut rhs, lhs.ty, "struct operand"); + self.assert_ty(pos, &mut rhs, lhs.ty, "struct operand"); let dst = self.ci.nodes.new_node(lhs.ty, Kind::Stck, [VOID, MEM]); self.struct_op(left.pos(), op, s, dst, lhs.id, rhs.id); Some(Value::ptr(dst).ty(lhs.ty)) } _ => { self.report( - left.pos(), - fa!("'{0} {op} {0}' is not supported", self.ty_display(lhs.ty),), + pos, + fa!("'{} {op} _' is not supported", self.ty_display(lhs.ty)), ); Value::NEVER } diff --git a/lang/tests/son_tests_arrays.txt b/lang/tests/son_tests_arrays.txt index b1783fe..69efca3 100644 --- a/lang/tests/son_tests_arrays.txt +++ b/lang/tests/son_tests_arrays.txt @@ -4,9 +4,9 @@ main: LI64 r7, 4d LI64 r8, 2d LI64 r9, 1d - LI64 r6, 1d - LI64 r10, 255d - LI64 r11, 0d + LI8 r6, 1b + LI8 r10, 255b + LI8 r11, 0b ADDI64 r12, r254, 0d ST r11, r254, 0a, 1h ST r11, r254, 1a, 1h @@ -24,14 +24,14 @@ main: ADDI64 r254, r254, 36d JALA r0, r31, 0a pass: - LD r3, r2, 8a, 8h - MULI64 r8, r3, 8d + LD r4, r2, 8a, 8h + MULI64 r8, r4, 8d ADD64 r10, r8, r2 LD r8, r10, 0a, 8h - LD r9, r2, 0a, 8h - ADD64 r11, r3, r9 + LD r7, r2, 0a, 8h + ADD64 r11, r4, r7 ADD64 r1, r8, r11 JALA r0, r31, 0a -code size: 348 +code size: 327 ret: 8 status: Ok(()) diff --git a/lang/tests/son_tests_c_strings.txt b/lang/tests/son_tests_c_strings.txt index 34feef6..782ffb0 100644 --- a/lang/tests/son_tests_c_strings.txt +++ b/lang/tests/son_tests_c_strings.txt @@ -12,7 +12,7 @@ main: ADDI64 r254, r254, 16d JALA r0, r31, 0a str_len: - LI64 r6, 0d + LI8 r6, 0b LI64 r1, 0d 2: LD r8, r2, 0a, 1h ANDI r8, r8, 255d @@ -23,6 +23,6 @@ str_len: ADDI64 r1, r1, 1d JMP :2 1: JALA r0, r31, 0a -code size: 226 +code size: 219 ret: 16 status: Ok(()) diff --git a/lang/tests/son_tests_different_types.txt b/lang/tests/son_tests_different_types.txt index 45dfca4..ae9a9db 100644 --- a/lang/tests/son_tests_different_types.txt +++ b/lang/tests/son_tests_different_types.txt @@ -1,9 +1,9 @@ main: ADDI64 r254, r254, -24d - LI64 r6, 2d - LI64 r5, 0d - LI64 r8, 0d - LI64 r9, 255d + LI32 r6, 2w + LI32 r5, 0w + LI8 r8, 0b + LI8 r9, 255b ADDI64 r10, r254, 0d ADDI64 r7, r254, 12d ST r9, r254, 12a, 1h @@ -43,6 +43,6 @@ main: ANDI r1, r12, 4294967295d 1: ADDI64 r254, r254, 24d JALA r0, r31, 0a -code size: 449 +code size: 427 ret: 512 status: Ok(()) diff --git a/lang/tests/son_tests_generic_functions.txt b/lang/tests/son_tests_generic_functions.txt index c2b5557..eb53bd7 100644 --- a/lang/tests/son_tests_generic_functions.txt +++ b/lang/tests/son_tests_generic_functions.txt @@ -7,7 +7,7 @@ add: main: ADDI64 r254, r254, -16d ST r31, r254, 0a, 16h - LI64 r3, 2d + LI32 r3, 2w CP r2, r3 JAL r31, r0, :add CP r32, r1 @@ -20,6 +20,6 @@ main: LD r31, r254, 0a, 16h ADDI64 r254, r254, 16d JALA r0, r31, 0a -code size: 165 +code size: 161 ret: 0 status: Ok(()) diff --git a/lang/tests/son_tests_idk.txt b/lang/tests/son_tests_idk.txt index ed973f3..f9b3b0f 100644 --- a/lang/tests/son_tests_idk.txt +++ b/lang/tests/son_tests_idk.txt @@ -1,20 +1,20 @@ main: ADDI64 r254, r254, -128d - LI64 r6, 69d + LI8 r7, 69b LI64 r5, 128d - LI64 r7, 0d + LI64 r6, 0d ADDI64 r4, r254, 0d - 2: JLTU r7, r5, :0 - LD r2, r254, 42a, 1h - ANDI r1, r2, 255d + 2: JLTU r6, r5, :0 + LD r3, r254, 42a, 1h + ANDI r1, r3, 255d JMP :1 - 0: ADDI64 r8, r7, 1d - ADD64 r3, r7, r4 - ST r6, r3, 0a, 1h - CP r7, r8 + 0: ADDI64 r8, r6, 1d + ADD64 r6, r6, r4 + ST r7, r6, 0a, 1h + CP r6, r8 JMP :2 1: ADDI64 r254, r254, 128d JALA r0, r31, 0a -code size: 152 +code size: 145 ret: 69 status: Ok(()) diff --git a/lang/tests/son_tests_intcast_store.txt b/lang/tests/son_tests_intcast_store.txt index d051b3f..2b76f16 100644 --- a/lang/tests/son_tests_intcast_store.txt +++ b/lang/tests/son_tests_intcast_store.txt @@ -15,8 +15,8 @@ main: JALA r0, r31, 0a set: ADDI64 r254, r254, -25d - LI64 r9, 8d - LI64 r8, 5d + LI32 r9, 8w + LI8 r8, 5b ANDI r10, r4, 4294967295d ADDI64 r1, r254, 0d ST r8, r254, 0a, 1h @@ -27,6 +27,6 @@ set: LD r1, r254, 1a, 4h ADDI64 r254, r254, 25d JALA r0, r31, 0a -code size: 309 +code size: 298 ret: 1024 status: Ok(()) diff --git a/lang/tests/son_tests_request_page.txt b/lang/tests/son_tests_request_page.txt index 38d1f4a..1c088ce 100644 --- a/lang/tests/son_tests_request_page.txt +++ b/lang/tests/son_tests_request_page.txt @@ -7,7 +7,7 @@ create_back_buffer: JAL r31, r0, :request_page JMP :1 0: CP r33, r2 - LI64 r34, 255d + LI8 r34, 255b CP r2, r34 JAL r31, r0, :request_page CP r35, r1 @@ -45,6 +45,6 @@ request_page: ST r12, r4, 1a, 1h ECA JALA r0, r31, 0a -code size: 346 +code size: 339 ret: 42 status: Ok(()) diff --git a/lang/tests/son_tests_struct_operators.txt b/lang/tests/son_tests_struct_operators.txt index d48dd36..df05924 100644 --- a/lang/tests/son_tests_struct_operators.txt +++ b/lang/tests/son_tests_struct_operators.txt @@ -1,8 +1,8 @@ main: ADDI64 r254, r254, -168d - LI64 r11, 4d - LI64 r5, 1d - LI64 r3, 0d + LI8 r11, 4b + LI8 r5, 1b + LI8 r3, 0b ADDI64 r6, r254, 144d ADDI64 r6, r254, 148d ST r3, r254, 148a, 1h @@ -115,6 +115,6 @@ main: ADD64 r1, r5, r3 1: ADDI64 r254, r254, 168d JALA r0, r31, 0a -code size: 1247 +code size: 1226 ret: 10 status: Ok(()) diff --git a/lang/tests/son_tests_struct_patterns.txt b/lang/tests/son_tests_struct_patterns.txt index cebce40..7b19b93 100644 --- a/lang/tests/son_tests_struct_patterns.txt +++ b/lang/tests/son_tests_struct_patterns.txt @@ -34,7 +34,7 @@ fib_iter: main: ADDI64 r254, r254, -18d ST r31, r254, 2a, 16h - LI64 r1, 10d + LI8 r1, 10b ADDI64 r3, r254, 0d ST r1, r254, 0a, 1h ST r1, r254, 1a, 1h @@ -50,6 +50,6 @@ main: LD r31, r254, 2a, 16h ADDI64 r254, r254, 18d JALA r0, r31, 0a -code size: 382 +code size: 375 ret: 0 status: Ok(()) diff --git a/lang/tests/son_tests_struct_return_from_module_function.txt b/lang/tests/son_tests_struct_return_from_module_function.txt index f0fa334..cf37ae2 100644 --- a/lang/tests/son_tests_struct_return_from_module_function.txt +++ b/lang/tests/son_tests_struct_return_from_module_function.txt @@ -1,6 +1,6 @@ foo: ADDI64 r254, r254, -16d - LI64 r3, 2d + LI32 r3, 2w LI64 r5, 3d ADDI64 r4, r254, 0d ST r5, r254, 0a, 8h @@ -33,6 +33,6 @@ main: LD r31, r254, 48a, 8h ADDI64 r254, r254, 56d JALA r0, r31, 0a -code size: 359 +code size: 355 ret: 0 status: Ok(()) diff --git a/lang/tests/son_tests_tests_ptr_to_ptr_copy.txt b/lang/tests/son_tests_tests_ptr_to_ptr_copy.txt index 0c9f616..ce6a245 100644 --- a/lang/tests/son_tests_tests_ptr_to_ptr_copy.txt +++ b/lang/tests/son_tests_tests_ptr_to_ptr_copy.txt @@ -1,6 +1,6 @@ main: ADDI64 r254, r254, -10240d - LI64 r6, 64d + LI8 r6, 64b LI64 r7, 1024d LI64 r9, 1d LI64 r8, 0d @@ -25,6 +25,6 @@ main: JMP :4 2: ADDI64 r254, r254, 10240d JALA r0, r31, 0a -code size: 205 +code size: 198 ret: 64 status: Ok(()) diff --git a/lang/tests/son_tests_wide_ret.txt b/lang/tests/son_tests_wide_ret.txt index 55cd32a..3b6709a 100644 --- a/lang/tests/son_tests_wide_ret.txt +++ b/lang/tests/son_tests_wide_ret.txt @@ -8,10 +8,10 @@ main: ST r1, r254, 16a, 16h ADDI64 r7, r254, 0d BMC r32, r7, 16h - LD r1, r254, 12a, 1h + LD r12, r254, 12a, 1h LD r11, r254, 3a, 1h - SUB8 r2, r11, r1 - ANDI r1, r2, 255d + SUB8 r3, r11, r12 + ANDI r1, r3, 255d LD r31, r254, 32a, 16h ADDI64 r254, r254, 48d JALA r0, r31, 0a @@ -21,9 +21,9 @@ maina: ADDI64 r32, r254, 36d JAL r31, r0, :small_struct ST r1, r254, 36a, 4h - LI64 r2, 1d - LI64 r3, 3d - LI64 r1, 0d + LI8 r2, 1b + LI8 r3, 3b + LI8 r1, 0b ADDI64 r6, r254, 0d ADDI64 r8, r6, 8d ADDI64 r7, r254, 16d @@ -47,13 +47,13 @@ maina: JALA r0, r31, 0a small_struct: ADDI64 r254, r254, -4d - LI64 r2, 0d + LI16 r2, 0h ADDI64 r3, r254, 0d ST r2, r254, 0a, 2h ST r2, r254, 2a, 2h LD r1, r3, 0a, 4h ADDI64 r254, r254, 4d JALA r0, r31, 0a -code size: 573 +code size: 546 ret: 2 status: Ok(())