From e69aeb8b9842438673b4555c9f4033fcd7adc087 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 22 May 2022 17:34:08 +0200 Subject: [PATCH] added warning about shadowing variables --- compiler/res/prog8lib/cx16/gfx2.p8 | 40 +++++------ compiler/res/prog8lib/virtual/syslib.p8 | 14 ++-- .../astprocessing/AstIdentifiersChecker.kt | 22 ++++-- examples/test.p8 | 67 +++++++++++++++++-- examples/textelite.p8 | 33 +++++---- 5 files changed, 121 insertions(+), 55 deletions(-) diff --git a/compiler/res/prog8lib/cx16/gfx2.p8 b/compiler/res/prog8lib/cx16/gfx2.p8 index 519dffeb7..9fdac2118 100644 --- a/compiler/res/prog8lib/cx16/gfx2.p8 +++ b/compiler/res/prog8lib/cx16/gfx2.p8 @@ -131,24 +131,24 @@ gfx2 { monochrome_dont_stipple_flag = not enable } - sub rect(uword x, uword y, uword width, uword height, ubyte color) { - if width==0 or height==0 + sub rect(uword x, uword y, uword rwidth, uword rheight, ubyte color) { + if rwidth==0 or rheight==0 return - horizontal_line(x, y, width, color) - if height==1 + horizontal_line(x, y, rwidth, color) + if rheight==1 return - horizontal_line(x, y+height-1, width, color) - vertical_line(x, y+1, height-2, color) - if width==1 + horizontal_line(x, y+rheight-1, rwidth, color) + vertical_line(x, y+1, rheight-2, color) + if rwidth==1 return - vertical_line(x+width-1, y+1, height-2, color) + vertical_line(x+rwidth-1, y+1, rheight-2, color) } - sub fillrect(uword x, uword y, uword width, uword height, ubyte color) { - if width==0 + sub fillrect(uword x, uword y, uword rwidth, uword rheight, ubyte color) { + if rwidth==0 return - repeat height { - horizontal_line(x, y, width, color) + repeat rheight { + horizontal_line(x, y, rwidth, color) y++ } } @@ -288,7 +288,7 @@ _done } } - sub vertical_line(uword x, uword y, uword height, ubyte color) { + sub vertical_line(uword x, uword y, uword lheight, ubyte color) { when active_mode { 1, 5 -> { ; monochrome, lo-res @@ -301,7 +301,7 @@ _done set_both_strides(11) ; 40 increment = 1 line in 320 px monochrome else set_both_strides(12) ; 80 increment = 1 line in 640 px monochrome - repeat height { + repeat lheight { %asm {{ lda cx16.VERA_DATA0 ora cx16.r15L @@ -312,14 +312,14 @@ _done ; draw stippled line. if x&1 { y++ - height-- + lheight-- } position2(x,y,true) if active_mode==1 set_both_strides(12) ; 80 increment = 2 line in 320 px monochrome else set_both_strides(13) ; 160 increment = 2 line in 640 px monochrome - repeat height/2 { + repeat lheight/2 { %asm {{ lda cx16.VERA_DATA0 ora cx16.r15L @@ -334,7 +334,7 @@ _done set_both_strides(11) ; 40 increment = 1 line in 320 px monochrome else set_both_strides(12) ; 80 increment = 1 line in 640 px monochrome - repeat height { + repeat lheight { %asm {{ lda cx16.VERA_DATA0 and cx16.r15L @@ -349,7 +349,7 @@ _done position(x,y) cx16.VERA_ADDR_H = cx16.VERA_ADDR_H & %00000111 | (14<<4) %asm {{ - ldy height + ldy lheight beq + lda color - sta cx16.VERA_DATA0 @@ -361,14 +361,14 @@ _done 6 -> { ; highres 4c ; use TWO vera adress pointers simultaneously one for reading, one for writing, so auto-increment is possible - if height==0 + if lheight==0 return position2(x,y,true) set_both_strides(13) ; 160 increment = 1 line in 640 px 4c mode color &= 3 color <<= gfx2.plot.shift4c[lsb(x) & 3] ubyte @shared mask = gfx2.plot.mask4c[lsb(x) & 3] - repeat height { + repeat lheight { %asm {{ lda cx16.VERA_DATA0 and mask diff --git a/compiler/res/prog8lib/virtual/syslib.p8 b/compiler/res/prog8lib/virtual/syslib.p8 index 34c16a24b..b78844c26 100644 --- a/compiler/res/prog8lib/virtual/syslib.p8 +++ b/compiler/res/prog8lib/virtual/syslib.p8 @@ -30,21 +30,21 @@ sys { }} } - sub internal_stringcopy(uword source, uword target) { + sub internal_stringcopy(uword source, uword tgt) { ; Called when the compiler wants to assign a string value to another string. while @(source) { - @(target) = @(source) + @(tgt) = @(source) source++ - target++ + tgt++ } - @(target)=0 + @(tgt)=0 } - sub memcopy(uword source, uword target, uword count) { + sub memcopy(uword source, uword tgt, uword count) { repeat count { - @(target) = @(source) + @(tgt) = @(source) source++ - target++ + tgt++ } } diff --git a/compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt index d7bcd30ac..b7cfe8c37 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt @@ -24,6 +24,10 @@ internal class AstIdentifiersChecker(private val errors: IErrorReporter, errors.err("name conflict '$name', also defined in ${existing.position.file} line ${existing.position.line}", position) } + private fun nameShadowWarning(name: String, position: Position, existing: Statement) { + errors.warn("name '$name' shadows occurrence at ${existing.position.file} line ${existing.position.line}", position) + } + override fun visit(block: Block) { if(block.name in compTarget.machine.opcodeNames) errors.err("can't use a cpu opcode name as a symbol: '${block.name}'", block.position) @@ -50,9 +54,13 @@ internal class AstIdentifiersChecker(private val errors: IErrorReporter, if(decl.name in compTarget.machine.opcodeNames) errors.err("can't use a cpu opcode name as a symbol: '${decl.name}'", decl.position) - val existing = decl.definingScope.lookup(listOf(decl.name)) - if (existing != null && existing !== decl) - nameError(decl.name, decl.position, existing) + val existing = decl.parent.definingScope.lookup(listOf(decl.name)) + if (existing != null && existing !== decl && existing is VarDecl) { + if(existing.parent!==decl.parent) + nameShadowWarning(decl.name, decl.position, existing) + else + nameError(decl.name, decl.position, existing) + } if(decl.definingBlock.name==decl.name) nameError(decl.name, decl.position, decl.definingBlock) @@ -74,8 +82,12 @@ internal class AstIdentifiersChecker(private val errors: IErrorReporter, // checkResult.add(NameError("builtin function name cannot be used as parameter", subroutine.position)) val existing = subroutine.lookup(listOf(subroutine.name)) - if (existing != null && existing !== subroutine) - nameError(subroutine.name, subroutine.position, existing) + if (existing != null && existing !== subroutine) { + if(existing.parent!==existing.parent) + nameShadowWarning(subroutine.name, subroutine.position, existing) + else + nameError(subroutine.name, subroutine.position, existing) + } // check that there are no local symbols (variables, labels, subs) that redefine the subroutine's parameters. val symbolsInSub = subroutine.allDefinedSymbols diff --git a/examples/test.p8 b/examples/test.p8 index 7ed84b973..e660ea3c6 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,14 +1,69 @@ +%import textio +%import math +%import string +%import floats %zeropage dontuse + ; NOTE: meant to test to virtual machine output target (use -target vitual) main { + ubyte value = 42 + + sub inline_candidate() -> ubyte { + return math.sin8u(value) + } + + sub inline_candidate2() { + value++ + return + } + + sub add(ubyte first, ubyte second) -> ubyte { + return first + second + } + + sub mul(ubyte first, ubyte second) -> ubyte { + return first * second + } + + ubyte ix + sub start() { - ubyte @shared xx = 11 - uword addr = $5000 - @(addr) = @(addr) + xx - xx = xx ^ 44 - xx = xx | 44 - xx = xx & 44 + + ubyte @shared value1 = inline_candidate() + txt.print_ub(value) ; 42 + txt.spc() + inline_candidate2() + inline_candidate2() + inline_candidate2() + txt.print_ub(value) ; 45 + txt.nl() + txt.print_ub(inline_candidate()) + txt.nl() + + ubyte @shared value=99 ; TODO compiler warning about shadowing + txt.print_ub(value) + txt.nl() + + ubyte @shared add=99 ; TODO compiler warning about shadowing + +; ; a "pixelshader": +; sys.gfx_enable(0) ; enable lo res screen +; ubyte shifter +; +; repeat { +; uword xx +; uword yy = 0 +; repeat 240 { +; xx = 0 +; repeat 320 { +; sys.gfx_plot(xx, yy, xx*yy + shifter as ubyte) +; xx++ +; } +; yy++ +; } +; shifter+=4 +; } } } diff --git a/examples/textelite.p8 b/examples/textelite.p8 index b498d5ca9..52a642b0f 100644 --- a/examples/textelite.p8 +++ b/examples/textelite.p8 @@ -771,36 +771,36 @@ planet { sub random_name() -> str { ubyte ii - str name = " " ; 8 chars max + str randname = " " ; 8 chars max ubyte nx = 0 for ii in 0 to goatsoup_rnd_number() & 3 { - ubyte x = goatsoup_rnd_number() & $3e - if pairs0[x] != '.' { - name[nx] = pairs0[x] + ubyte xx = goatsoup_rnd_number() & $3e + if pairs0[xx] != '.' { + randname[nx] = pairs0[xx] nx++ } - x++ - if pairs0[x] != '.' { - name[nx] = pairs0[x] + xx++ + if pairs0[xx] != '.' { + randname[nx] = pairs0[xx] nx++ } } - name[nx] = 0 - name[0] |= 32 ; uppercase first letter - return name + randname[nx] = 0 + randname[0] |= 32 ; uppercase first letter + return randname } sub goatsoup_rnd_number() -> ubyte { - ubyte x = goatsoup_rnd[0] * 2 - uword a = x as uword + goatsoup_rnd[2] + ubyte xx = goatsoup_rnd[0] * 2 + uword a = xx as uword + goatsoup_rnd[2] if goatsoup_rnd[0] > 127 a ++ goatsoup_rnd[0] = lsb(a) - goatsoup_rnd[2] = x - x = goatsoup_rnd[1] - ubyte ac = x + goatsoup_rnd[3] + msb(a) + goatsoup_rnd[2] = xx + xx = goatsoup_rnd[1] + ubyte ac = xx + goatsoup_rnd[3] + msb(a) goatsoup_rnd[1] = ac - goatsoup_rnd[3] = x + goatsoup_rnd[3] = xx return ac } @@ -1022,7 +1022,6 @@ util { prefixptr++ stringptr++ } - return false } sub print_right(ubyte width, uword s) {