added warning about shadowing variables

This commit is contained in:
Irmen de Jong 2022-05-22 17:34:08 +02:00
parent 26ea1da146
commit e69aeb8b98
5 changed files with 121 additions and 55 deletions

View File

@ -131,24 +131,24 @@ gfx2 {
monochrome_dont_stipple_flag = not enable monochrome_dont_stipple_flag = not enable
} }
sub rect(uword x, uword y, uword width, uword height, ubyte color) { sub rect(uword x, uword y, uword rwidth, uword rheight, ubyte color) {
if width==0 or height==0 if rwidth==0 or rheight==0
return return
horizontal_line(x, y, width, color) horizontal_line(x, y, rwidth, color)
if height==1 if rheight==1
return return
horizontal_line(x, y+height-1, width, color) horizontal_line(x, y+rheight-1, rwidth, color)
vertical_line(x, y+1, height-2, color) vertical_line(x, y+1, rheight-2, color)
if width==1 if rwidth==1
return 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) { sub fillrect(uword x, uword y, uword rwidth, uword rheight, ubyte color) {
if width==0 if rwidth==0
return return
repeat height { repeat rheight {
horizontal_line(x, y, width, color) horizontal_line(x, y, rwidth, color)
y++ 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 { when active_mode {
1, 5 -> { 1, 5 -> {
; monochrome, lo-res ; monochrome, lo-res
@ -301,7 +301,7 @@ _done
set_both_strides(11) ; 40 increment = 1 line in 320 px monochrome set_both_strides(11) ; 40 increment = 1 line in 320 px monochrome
else else
set_both_strides(12) ; 80 increment = 1 line in 640 px monochrome set_both_strides(12) ; 80 increment = 1 line in 640 px monochrome
repeat height { repeat lheight {
%asm {{ %asm {{
lda cx16.VERA_DATA0 lda cx16.VERA_DATA0
ora cx16.r15L ora cx16.r15L
@ -312,14 +312,14 @@ _done
; draw stippled line. ; draw stippled line.
if x&1 { if x&1 {
y++ y++
height-- lheight--
} }
position2(x,y,true) position2(x,y,true)
if active_mode==1 if active_mode==1
set_both_strides(12) ; 80 increment = 2 line in 320 px monochrome set_both_strides(12) ; 80 increment = 2 line in 320 px monochrome
else else
set_both_strides(13) ; 160 increment = 2 line in 640 px monochrome set_both_strides(13) ; 160 increment = 2 line in 640 px monochrome
repeat height/2 { repeat lheight/2 {
%asm {{ %asm {{
lda cx16.VERA_DATA0 lda cx16.VERA_DATA0
ora cx16.r15L ora cx16.r15L
@ -334,7 +334,7 @@ _done
set_both_strides(11) ; 40 increment = 1 line in 320 px monochrome set_both_strides(11) ; 40 increment = 1 line in 320 px monochrome
else else
set_both_strides(12) ; 80 increment = 1 line in 640 px monochrome set_both_strides(12) ; 80 increment = 1 line in 640 px monochrome
repeat height { repeat lheight {
%asm {{ %asm {{
lda cx16.VERA_DATA0 lda cx16.VERA_DATA0
and cx16.r15L and cx16.r15L
@ -349,7 +349,7 @@ _done
position(x,y) position(x,y)
cx16.VERA_ADDR_H = cx16.VERA_ADDR_H & %00000111 | (14<<4) cx16.VERA_ADDR_H = cx16.VERA_ADDR_H & %00000111 | (14<<4)
%asm {{ %asm {{
ldy height ldy lheight
beq + beq +
lda color lda color
- sta cx16.VERA_DATA0 - sta cx16.VERA_DATA0
@ -361,14 +361,14 @@ _done
6 -> { 6 -> {
; highres 4c ; highres 4c
; use TWO vera adress pointers simultaneously one for reading, one for writing, so auto-increment is possible ; use TWO vera adress pointers simultaneously one for reading, one for writing, so auto-increment is possible
if height==0 if lheight==0
return return
position2(x,y,true) position2(x,y,true)
set_both_strides(13) ; 160 increment = 1 line in 640 px 4c mode set_both_strides(13) ; 160 increment = 1 line in 640 px 4c mode
color &= 3 color &= 3
color <<= gfx2.plot.shift4c[lsb(x) & 3] color <<= gfx2.plot.shift4c[lsb(x) & 3]
ubyte @shared mask = gfx2.plot.mask4c[lsb(x) & 3] ubyte @shared mask = gfx2.plot.mask4c[lsb(x) & 3]
repeat height { repeat lheight {
%asm {{ %asm {{
lda cx16.VERA_DATA0 lda cx16.VERA_DATA0
and mask and mask

View File

@ -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. ; Called when the compiler wants to assign a string value to another string.
while @(source) { while @(source) {
@(target) = @(source) @(tgt) = @(source)
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 { repeat count {
@(target) = @(source) @(tgt) = @(source)
source++ source++
target++ tgt++
} }
} }

View File

@ -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) 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) { override fun visit(block: Block) {
if(block.name in compTarget.machine.opcodeNames) if(block.name in compTarget.machine.opcodeNames)
errors.err("can't use a cpu opcode name as a symbol: '${block.name}'", block.position) 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) if(decl.name in compTarget.machine.opcodeNames)
errors.err("can't use a cpu opcode name as a symbol: '${decl.name}'", decl.position) errors.err("can't use a cpu opcode name as a symbol: '${decl.name}'", decl.position)
val existing = decl.definingScope.lookup(listOf(decl.name)) val existing = decl.parent.definingScope.lookup(listOf(decl.name))
if (existing != null && existing !== decl) if (existing != null && existing !== decl && existing is VarDecl) {
nameError(decl.name, decl.position, existing) if(existing.parent!==decl.parent)
nameShadowWarning(decl.name, decl.position, existing)
else
nameError(decl.name, decl.position, existing)
}
if(decl.definingBlock.name==decl.name) if(decl.definingBlock.name==decl.name)
nameError(decl.name, decl.position, decl.definingBlock) 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)) // checkResult.add(NameError("builtin function name cannot be used as parameter", subroutine.position))
val existing = subroutine.lookup(listOf(subroutine.name)) val existing = subroutine.lookup(listOf(subroutine.name))
if (existing != null && existing !== subroutine) if (existing != null && existing !== subroutine) {
nameError(subroutine.name, subroutine.position, existing) 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. // check that there are no local symbols (variables, labels, subs) that redefine the subroutine's parameters.
val symbolsInSub = subroutine.allDefinedSymbols val symbolsInSub = subroutine.allDefinedSymbols

View File

@ -1,14 +1,69 @@
%import textio
%import math
%import string
%import floats
%zeropage dontuse %zeropage dontuse
; NOTE: meant to test to virtual machine output target (use -target vitual) ; NOTE: meant to test to virtual machine output target (use -target vitual)
main { 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() { sub start() {
ubyte @shared xx = 11
uword addr = $5000 ubyte @shared value1 = inline_candidate()
@(addr) = @(addr) + xx txt.print_ub(value) ; 42
xx = xx ^ 44 txt.spc()
xx = xx | 44 inline_candidate2()
xx = xx & 44 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
; }
} }
} }

View File

@ -771,36 +771,36 @@ planet {
sub random_name() -> str { sub random_name() -> str {
ubyte ii ubyte ii
str name = " " ; 8 chars max str randname = " " ; 8 chars max
ubyte nx = 0 ubyte nx = 0
for ii in 0 to goatsoup_rnd_number() & 3 { for ii in 0 to goatsoup_rnd_number() & 3 {
ubyte x = goatsoup_rnd_number() & $3e ubyte xx = goatsoup_rnd_number() & $3e
if pairs0[x] != '.' { if pairs0[xx] != '.' {
name[nx] = pairs0[x] randname[nx] = pairs0[xx]
nx++ nx++
} }
x++ xx++
if pairs0[x] != '.' { if pairs0[xx] != '.' {
name[nx] = pairs0[x] randname[nx] = pairs0[xx]
nx++ nx++
} }
} }
name[nx] = 0 randname[nx] = 0
name[0] |= 32 ; uppercase first letter randname[0] |= 32 ; uppercase first letter
return name return randname
} }
sub goatsoup_rnd_number() -> ubyte { sub goatsoup_rnd_number() -> ubyte {
ubyte x = goatsoup_rnd[0] * 2 ubyte xx = goatsoup_rnd[0] * 2
uword a = x as uword + goatsoup_rnd[2] uword a = xx as uword + goatsoup_rnd[2]
if goatsoup_rnd[0] > 127 if goatsoup_rnd[0] > 127
a ++ a ++
goatsoup_rnd[0] = lsb(a) goatsoup_rnd[0] = lsb(a)
goatsoup_rnd[2] = x goatsoup_rnd[2] = xx
x = goatsoup_rnd[1] xx = goatsoup_rnd[1]
ubyte ac = x + goatsoup_rnd[3] + msb(a) ubyte ac = xx + goatsoup_rnd[3] + msb(a)
goatsoup_rnd[1] = ac goatsoup_rnd[1] = ac
goatsoup_rnd[3] = x goatsoup_rnd[3] = xx
return ac return ac
} }
@ -1022,7 +1022,6 @@ util {
prefixptr++ prefixptr++
stringptr++ stringptr++
} }
return false
} }
sub print_right(ubyte width, uword s) { sub print_right(ubyte width, uword s) {