making InferredType easier to use

This commit is contained in:
Irmen de Jong 2021-10-15 00:18:13 +02:00
parent 16ed68c1ec
commit 15a02d7664
3 changed files with 7 additions and 3 deletions

View File

@ -741,7 +741,7 @@ internal class AstChecker(private val program: Program,
override fun visit(array: ArrayLiteralValue) {
if(array.type.isKnown) {
if (!compilerOptions.floats && array.type.getOr(DataType.UNDEFINED) in setOf(DataType.FLOAT, DataType.ARRAY_F)) {
if (!compilerOptions.floats && array.type.oneOf(DataType.FLOAT, DataType.ARRAY_F)) {
errors.err("floating point used, but that is not enabled via options", array.position)
}
val arrayspec = ArrayIndex.forArray(array)
@ -1157,7 +1157,7 @@ internal class AstChecker(private val program: Program,
when {
constvalue == null -> errors.err("choice value must be a constant", whenChoice.position)
constvalue.type !in IntegerDatatypes -> errors.err("choice value must be a byte or word", whenChoice.position)
constvalue.type != conditionType.getOr(DataType.UNDEFINED) -> errors.err("choice value datatype differs from condition value", whenChoice.position)
!conditionType.istype(constvalue.type) -> errors.err("choice value datatype differs from condition value", whenChoice.position)
}
}
} else {

View File

@ -15,6 +15,8 @@ object InferredTypes {
fun getOrElse(transform: (InferredType) -> DataType): DataType =
if(isUnknown || isVoid) transform(this) else datatype!!
infix fun istype(type: DataType): Boolean = if(isUnknown || isVoid) false else this.datatype==type
infix fun isnot(type: DataType): Boolean = if(isUnknown || isVoid) true else this.datatype!=type
fun oneOf(vararg types: DataType) = if(isUnknown || isVoid) false else this.datatype in types
companion object {
fun unknown() = InferredType(isUnknown = true, isVoid = false, datatype = null)

View File

@ -3,7 +3,9 @@ TODO
For next compiler release
^^^^^^^^^^^^^^^^^^^^^^^^^
...
replace uses of inferredType.istype(..) by infix form
replace checks against multiple types with .oneOf(...)
replace certain uses of inferredType.getOr(UNKNOWN) by i.getOrElse({ errorhandler })
Blocked by Commander-x16 v39 release