Skip to content

Commit 1860f66

Browse files
committed
allow "x not in array" as equivalent to "not x in array"
update antlr parsing lib
1 parent ded9ada commit 1860f66

File tree

9 files changed

+41
-14
lines changed

9 files changed

+41
-14
lines changed

.idea/libraries/antlr_antlr4.xml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dependencies {
3434
implementation project(':codeGenIntermediate')
3535
implementation project(':codeGenExperimental')
3636
implementation project(':virtualmachine')
37-
implementation 'org.antlr:antlr4-runtime:4.10.1'
37+
implementation 'org.antlr:antlr4-runtime:4.11.1'
3838
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
3939
// implementation "org.jetbrains.kotlin:kotlin-reflect"
4040
implementation 'org.jetbrains.kotlinx:kotlinx-cli:0.3.4'

compiler/res/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8.8
1+
8.8.1

compiler/src/prog8/compiler/astprocessing/AstPreprocessor.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ class AstPreprocessor(val program: Program,
145145
val containment = ContainmentCheck(expr.left, expr.right, expr.position)
146146
return listOf(IAstModification.ReplaceNode(expr, containment, parent))
147147
}
148+
if(expr.operator=="not in") {
149+
val containment = ContainmentCheck(expr.left, expr.right, expr.position)
150+
val notContainment = PrefixExpression("not", containment, expr.position)
151+
return listOf(IAstModification.ReplaceNode(expr, notContainment, parent))
152+
}
148153
return noModifications
149154
}
150155

compiler/test/ast/TestVarious.kt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ package prog8tests.ast
33
import io.kotest.core.spec.style.FunSpec
44
import io.kotest.matchers.shouldBe
55
import io.kotest.matchers.shouldNotBe
6+
import io.kotest.matchers.types.instanceOf
67
import prog8.ast.IFunctionCall
7-
import prog8.ast.expressions.BinaryExpression
8-
import prog8.ast.expressions.IdentifierReference
9-
import prog8.ast.expressions.StringLiteral
8+
import prog8.ast.expressions.*
109
import prog8.ast.statements.Assignment
1110
import prog8.ast.statements.InlineAssembly
1211
import prog8.ast.statements.VarDecl
@@ -179,5 +178,28 @@ main {
179178
val stmts = result.program.entrypoint.statements
180179
stmts.size shouldBe 9
181180
}
181+
182+
test("alternative notation for negative containment check") {
183+
val src="""
184+
main {
185+
sub start() {
186+
ubyte[] array=[1,2,3]
187+
cx16.r0L = not (3 in array)
188+
cx16.r1L = 3 not in array
189+
}
190+
}
191+
"""
192+
val result = compileText(C64Target(), optimize=false, src, writeAssembly=false)!!
193+
val stmts = result.program.entrypoint.statements
194+
stmts.size shouldBe 3
195+
val value1 = (stmts[1] as Assignment).value as BinaryExpression
196+
val value2 = (stmts[2] as Assignment).value as BinaryExpression
197+
value1.operator shouldBe "=="
198+
value1.left shouldBe instanceOf<ContainmentCheck>()
199+
(value1.right as NumericLiteral).number shouldBe 0.0
200+
value2.operator shouldBe "=="
201+
value2.left shouldBe instanceOf<ContainmentCheck>()
202+
(value2.right as NumericLiteral).number shouldBe 0.0
203+
}
182204
})
183205

compilerAst/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ compileTestKotlin {
2424
dependencies {
2525
implementation project(':codeCore')
2626
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
27-
implementation 'org.antlr:antlr4-runtime:4.10.1'
27+
implementation 'org.antlr:antlr4-runtime:4.11.1'
2828
implementation "com.michael-bull.kotlin-result:kotlin-result-jvm:1.1.16"
2929
implementation project(':parser')
3030
}

docs/source/todo.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ TODO
33

44
For next release
55
^^^^^^^^^^^^^^^^
6-
- allow "xx not in array" and rewrite it into "not xx in array"
76
- make sure bool value is always 0 or 1 (all casts should convert), then:
87
- rewrite bool=bool^1 into bool=not bool
98
- should solve: bool bb = not bb -> larger code than bool bb ^= 1

parser/antlr/Prog8ANTLR.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ expression :
170170
| left = expression EOL? bop = ('==' | '!=') EOL? right = expression
171171
| rangefrom = expression rto = ('to'|'downto') rangeto = expression ('step' rangestep = expression)? // can't create separate rule due to mutual left-recursion
172172
| left = expression EOL? bop = 'in' EOL? right = expression
173+
| left = expression EOL? bop = 'not in' EOL? right = expression
173174
| prefix = 'not' expression
174175
| left = expression EOL? bop = 'and' EOL? right = expression
175176
| left = expression EOL? bop = 'or' EOL? right = expression

parser/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ java {
1010
}
1111

1212
dependencies {
13-
antlr 'org.antlr:antlr4:4.10.1'
14-
implementation 'org.antlr:antlr4-runtime:4.10.1'
13+
antlr 'org.antlr:antlr4:4.11.1'
14+
implementation 'org.antlr:antlr4-runtime:4.11.1'
1515
}
1616

1717
configurations.all {

0 commit comments

Comments
 (0)