diff --git a/codeAst/src/prog8/code/ast/AstBase.kt b/codeAst/src/prog8/code/ast/AstBase.kt index e4b0c7412..b68b85572 100644 --- a/codeAst/src/prog8/code/ast/AstBase.kt +++ b/codeAst/src/prog8/code/ast/AstBase.kt @@ -30,6 +30,10 @@ sealed class PtNode(val position: Position) { children.add(index, child) child.parent = this } + + fun definingBlock() = findParentNode(this) + fun definingSub() = findParentNode(this) + fun definingAsmSub() = findParentNode(this) } @@ -116,3 +120,17 @@ class PtNop(position: Position): PtNode(position) { class PtScopeVarsDecls(position: Position): PtNode(position) { override fun printProperties() {} } + + + +// find the parent node of a specific type or interface +// (useful to figure out in what namespace/block something is defined, etc.) +inline fun findParentNode(node: PtNode): T? { + var candidate = node.parent + while(candidate !is T && candidate !is PtProgram) + candidate = candidate.parent + return if(candidate is PtProgram) + null + else + candidate as T +} \ No newline at end of file diff --git a/examples/test.p8 b/examples/test.p8 index 94fd1376a..06cf40567 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -6,31 +6,24 @@ main { sub start() { - - txt.chrout('\x40') - txt.chrout('\x41') - txt.chrout('\x42') - txt.chrout('\n') - txt.print("Hello\n\"quotes\"\n") - ; a "pixelshader": -; void syscall1(8, 0) ; enable lo res creen -; ubyte shifter -; -; shifter >>= 1 -; -; repeat { -; uword xx -; uword yy = 0 -; repeat 240 { -; xx = 0 -; repeat 320 { -; syscall3(10, xx, yy, xx*yy + shifter) ; plot pixel -; xx++ -; } -; yy++ -; } -; shifter+=4 -; } + void syscall1(8, 0) ; enable lo res creen + ubyte shifter + + shifter >>= 1 + + repeat { + uword xx + uword yy = 0 + repeat 240 { + xx = 0 + repeat 320 { + syscall3(10, xx, yy, xx*yy + shifter) ; plot pixel + xx++ + } + yy++ + } + shifter+=4 + } } }