From d28c994ecd86efbe1b049f76c74d2a2e352b44eb Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 29 Dec 2023 03:39:12 +0100 Subject: [PATCH] directive really needs to be listed out in the parser otherwise it confuses it with % modulo :-( Also fix missing const fold pass in optimizer --- compiler/src/prog8/compiler/Compiler.kt | 7 ++++++- compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt | 2 +- docs/source/todo.rst | 1 - parser/antlr/Prog8ANTLR.g4 | 8 +++++--- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 3551ebbe1..a5a5c16f4 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -386,13 +386,18 @@ private fun optimizeAst(program: Program, compilerOptions: CompilationOptions, e val optsDone2 = program.optimizeStatements(errors, functions, compilerOptions) val optsDone3 = program.inlineSubroutines(compilerOptions) program.constantFold(errors, compTarget) // because simplified statements and expressions can result in more constants that can be folded away - errors.report() + if(!errors.noErrors()) { + errors.report() + break + } if (optsDone1 + optsDone2 + optsDone3 == 0) break } val remover2 = UnusedCodeRemover(program, errors, compTarget) remover2.visit(program) remover2.applyModifications() + if(errors.noErrors()) + program.constantFold(errors, compTarget) // because simplified statements and expressions can result in more constants that can be folded away errors.report() } diff --git a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt index 2696ccaf1..915c92e3f 100644 --- a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt +++ b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt @@ -361,7 +361,7 @@ private fun ArrayindexContext.toAst() : ArrayIndex = ArrayIndex(expression().toAst(), toPosition()) internal fun DirectiveContext.toAst() : Directive = - Directive(DIRECTIVE().text, directivearg().map { it.toAst() }, toPosition()) + Directive(directivename.text, directivearg().map { it.toAst() }, toPosition()) private fun DirectiveargContext.toAst() : DirectiveArg { val str = stringliteral() diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 4bfa84157..b0621e4df 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,7 +2,6 @@ TODO ==== -- fix a1%a2 being parsed as directive - fix bitshift.p8 - add crc8 and crc16 and crc32 to math - fix crc* bench routines to no longer depend on the kernal rom version (use a bin file) diff --git a/parser/antlr/Prog8ANTLR.g4 b/parser/antlr/Prog8ANTLR.g4 index 0128dfb43..c202dd092 100644 --- a/parser/antlr/Prog8ANTLR.g4 +++ b/parser/antlr/Prog8ANTLR.g4 @@ -34,8 +34,6 @@ INVALID_AND_COMPOSITE: '&&' ; fragment HEX_DIGIT: ('a'..'f') | ('A'..'F') | ('0'..'9') ; fragment BIN_DIGIT: ('0' | '1') ; fragment DEC_DIGIT: ('0'..'9') ; -fragment LOWERCASE: ('a'..'z') ; -DIRECTIVE: '%' LOWERCASE+ ; FLOAT_NUMBER : FNUMBER (('E'|'e') ('+' | '-')? DEC_INTEGER)? ; // sign comes later from unary expression FNUMBER : FDOTNUMBER | FNUMDOTNUMBER ; @@ -130,7 +128,11 @@ labeldef : identifier ':' ; unconditionaljump : 'goto' (integerliteral | scoped_identifier) ; -directive : DIRECTIVE (directivearg? | directivearg (',' directivearg)*) ; +directive : + directivename=('%output' | '%launcher' | '%zeropage' | '%zpreserved' | '%zpallowed' | '%address' | '%import' | + '%breakpoint' | '%asminclude' | '%asmbinary' | '%option' | '%encoding' ) + (directivearg? | directivearg (',' directivearg)*) + ; directivearg : stringliteral | identifier | integerliteral ;