range initializers

This commit is contained in:
Irmen de Jong 2018-08-14 16:46:34 +02:00
parent 4b7d656a2f
commit 50bbbc67e2
2 changed files with 15 additions and 11 deletions

View File

@ -1,5 +1,5 @@
~ main $c003 { ~ main $c003 {
word[100] ascending = 100 to 199 word[1000] ascending = 10 to 1009
word[100] ascending2 = "a" to "z" word[100] ascending2 = "a" to "z"
str ascending3 = "a" to "z" str ascending3 = "a" to "z"
str ascending5 = "z" to "z" str ascending5 = "z" to "z"

View File

@ -281,8 +281,9 @@ class AstChecker(private val globalNamespace: INameScope) : IAstProcessor {
if (value.arrayvalue.size != expected) if (value.arrayvalue.size != expected)
checkResult.add(SyntaxError("initializer array size mismatch (expecting $expected, got ${value.arrayvalue.size})", decl.position)) checkResult.add(SyntaxError("initializer array size mismatch (expecting $expected, got ${value.arrayvalue.size})", decl.position))
else { else {
value.arrayvalue.forEach { for(v in value.arrayvalue) {
checkValueRange(decl.datatype, it.constValue(globalNamespace)!!, it.position) if(!checkValueRange(decl.datatype, v.constValue(globalNamespace)!!, v.position))
break
} }
} }
} }
@ -297,8 +298,9 @@ class AstChecker(private val globalNamespace: INameScope) : IAstProcessor {
if (value.arrayvalue.size != expected) if (value.arrayvalue.size != expected)
checkResult.add(SyntaxError("initializer array size mismatch (expecting $expected, got ${value.arrayvalue.size})", decl.position)) checkResult.add(SyntaxError("initializer array size mismatch (expecting $expected, got ${value.arrayvalue.size})", decl.position))
else { else {
value.arrayvalue.forEach { for(v in value.arrayvalue) {
checkValueRange(decl.datatype, it.constValue(globalNamespace)!!, it.position) if(!checkValueRange(decl.datatype, v.constValue(globalNamespace)!!, v.position))
break
} }
} }
} }
@ -330,32 +332,34 @@ class AstChecker(private val globalNamespace: INameScope) : IAstProcessor {
return range return range
} }
private fun checkValueRange(datatype: DataType, value: LiteralValue, position: Position?) { private fun checkValueRange(datatype: DataType, value: LiteralValue, position: Position?) : Boolean {
fun err(msg: String) { fun err(msg: String) : Boolean {
checkResult.add(SyntaxError(msg, position)) checkResult.add(SyntaxError(msg, position))
return false
} }
when (datatype) { when (datatype) {
DataType.FLOAT -> { DataType.FLOAT -> {
val number = value.asFloat(false) val number = value.asFloat(false)
if (number!=null && (number > 1.7014118345e+38 || number < -1.7014118345e+38)) if (number!=null && (number > 1.7014118345e+38 || number < -1.7014118345e+38))
err("floating point value out of range for MFLPT format") return err("floating point value out of range for MFLPT format")
} }
DataType.BYTE -> { DataType.BYTE -> {
val number = value.asInt(false) val number = value.asInt(false)
if (number!=null && (number < 0 || number > 255)) if (number!=null && (number < 0 || number > 255))
err("value out of range for unsigned byte") return err("value out of range for unsigned byte")
} }
DataType.WORD -> { DataType.WORD -> {
val number = value.asInt(false) val number = value.asInt(false)
if (number!=null && (number < 0 || number > 65535)) if (number!=null && (number < 0 || number > 65535))
err("value out of range for unsigned word") return err("value out of range for unsigned word")
} }
DataType.STR, DataType.STR_P, DataType.STR_S, DataType.STR_PS -> { DataType.STR, DataType.STR_P, DataType.STR_S, DataType.STR_PS -> {
val str = value.strvalue val str = value.strvalue
if (str!=null && (str.isEmpty() || str.length > 65535)) if (str!=null && (str.isEmpty() || str.length > 65535))
err("string length must be 1..65535") return err("string length must be 1..65535")
} }
} }
return true
} }
private fun checkConstInitializerValueScalar(decl: VarDecl) { private fun checkConstInitializerValueScalar(decl: VarDecl) {