Skip to content

Commit

Permalink
slight parser rule tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
irmen committed Oct 29, 2024
1 parent bbba4b3 commit 518e5a3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 0 additions & 2 deletions compiler/src/prog8/compiler/ModuleImporter.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package prog8.compiler

import com.github.michaelbull.result.*
import prog8.ast.IStatementContainer
import prog8.ast.Module
import prog8.ast.Program
import prog8.ast.base.SyntaxError
import prog8.ast.statements.Block
import prog8.ast.statements.Directive
import prog8.ast.statements.DirectiveArg
import prog8.code.core.IErrorReporter
Expand Down
4 changes: 4 additions & 0 deletions compilerAst/src/prog8/ast/statements/AstStatements.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class BuiltinFunctionPlaceholder(override val name: String, override val positio
override fun referencesIdentifier(nameInSource: List<String>): Boolean = nameInSource.size==1 && name==nameInSource[0]
}

// note: a Block is not strictly a Statement (but a Module Element rather)
// however by making it a statement we can reuse the name lookup logic for them (a module *is* name scope that has to do lookups)
class Block(override val name: String,
val address: UInt?,
override val statements: MutableList<Statement>,
Expand Down Expand Up @@ -95,6 +97,8 @@ class Block(override val name: String,
fun options() = statements.filter { it is Directive && it.directive == "%option" }.flatMap { (it as Directive).args }.map {it.name!!}.toSet()
}

// note: a Directive is not strictly always Statement (in module scope, it's a Module Element rather)
// however by making it a statement we can reuse the name lookup logic for them (a module *is* name scope that has to do lookups)
data class Directive(val directive: String, val args: List<DirectiveArg>, override val position: Position) : Statement() {
override lateinit var parent: Node

Expand Down
8 changes: 6 additions & 2 deletions compilerAst/src/prog8/parser/Prog8Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ object Prog8Parser {
val parseTree = parser.module()
val module = ParsedModule(src)

parseTree.directive().forEach { module.add(it.toAst()) }
parseTree.block().forEach { module.add(it.toAst(module.isLibrary)) }
parseTree.module_element().forEach {
val block = it.block()?.toAst(module.isLibrary)
val directive = it.directive()?.toAst()
if(directive != null) module.add(directive)
if(block != null) module.add(block)
}

return module
}
Expand Down
2 changes: 0 additions & 2 deletions docs/source/todo.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
TODO
====

Block node is not a Statement

Improve register load order in subroutine call args assignments:
in certain situations, the "wrong" order of evaluation of function call arguments is done which results
in overwriting registers that already got their value, which requires a lot of stack juggling (especially on plain 6502 cpu!)
Expand Down
6 changes: 5 additions & 1 deletion parser/antlr/Prog8ANTLR.g4
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ NOT_IN: 'not' [ \t]+ 'in' [ \t] ;
// If there are more than one, then they must be separated by EOL (one or more newlines).
// However, trailing EOL is NOT required.
// Note: the parser may see *several* consecutive EOLs - this happens when EOL and comments are interleaved (see #47)
module: EOL* ((directive | block) (EOL+ (directive | block))*)? EOL* EOF;
module: EOL* (module_element (EOL+ module_element)*)? EOL* EOF;

module_element:
directive | block ;


block: identifier integerliteral? EOL? '{' EOL? (block_statement | EOL)* '}';

Expand Down

0 comments on commit 518e5a3

Please sign in to comment.