Refactor retrieving DataType's name

This commit is contained in:
2020-03-14 13:18:01 +01:00
parent eb893c28f3
commit d8744670ed
13 changed files with 97 additions and 93 deletions

View File

@@ -18,7 +18,7 @@ object ActualSignatureFormatter {
DataType.MAP -> mapTypes(
argument.value as Map<Value, Value>
)
else -> argument.type.name.toLowerCase()
else -> argument.typeName
})
}
@@ -34,7 +34,7 @@ object ActualSignatureFormatter {
DataType.MAP -> mapTypes(
item.value as Map<Value, Value>
)
else -> item.type.name.toLowerCase()
else -> item.typeName
})
}
@@ -43,14 +43,14 @@ object ActualSignatureFormatter {
private fun mapTypes(map: Map<Value, Value>, output: MutableMap<String, String> = mutableMapOf()): String {
for ((k, v) in map) {
output[k.type.toString()] = when (v.type) {
output[k.typeName] = when (v.type) {
DataType.LIST -> listTypes(
v.value as List<Value>
)
DataType.MAP -> mapTypes(
v.value as Map<Value, Value>
)
else -> v.type.toString()
else -> v.typeName
}
}

View File

@@ -100,7 +100,7 @@ class Matcher(val type: DataType?, private val matcher: (Value) -> Boolean, priv
return Matcher(
DataType.LIST,
{ list -> (list.value as List<Value>).all { it.type in types } },
"list<${types.joinToString(", ") { it.name.toLowerCase() }}>"
"list<${types.joinToString(", ") { it.toString() }}>"
)
}
@@ -116,12 +116,12 @@ class Matcher(val type: DataType?, private val matcher: (Value) -> Boolean, priv
return Matcher(
null,
{ it.type in types },
"<${types.joinToString(", ") { it.name.toLowerCase() }}>"
"<${types.joinToString(", ") { it.toString() }}>"
)
}
fun ofType(type: DataType): Matcher {
return Matcher(null, { it.type == type }, type.name.toLowerCase())
return Matcher(null, { it.type == type }, type.toString())
}
fun oneOf(vararg matchers: Matcher): Matcher {

View File

@@ -5,87 +5,88 @@ import io.smnp.error.ShouldNeverReachThisLineException
import io.smnp.type.enumeration.DataType
data class Value(val type: DataType, val value: Any?, val properties: Map<String, Value> = emptyMap()) {
init {
if(!type.isInstance(value)) {
throw RuntimeException("'$value' is not of type $type")
}
}
init {
if (!type.isInstance(value)) {
throw RuntimeException("'$value' is not of type $type")
}
}
fun stringify() = type.stringifier(value)
fun stringify() = type.stringifier(value)
fun typeName() = type.toString()
val typeName: String
get() = type.toString()
override fun toString(): String {
return "$type($value)"
}
override fun toString(): String {
return "$type($value)"
}
companion object {
fun int(value: Int): Value {
return Value(DataType.INT, value)
}
companion object {
fun int(value: Int): Value {
return Value(DataType.INT, value)
}
fun float(value: Float): Value {
return Value(
DataType.FLOAT,
value
fun float(value: Float): Value {
return Value(
DataType.FLOAT,
value
)
}
fun numeric(value: Number): Value {
return when (value::class) {
Int::class -> int(value.toInt())
Float::class -> float(value.toFloat())
else -> throw ShouldNeverReachThisLineException()
}
}
fun string(value: String): Value {
return Value(
DataType.STRING, value, hashMapOf(
Pair("length", int(value.length))
)
}
)
}
fun numeric(value: Number): Value {
return when(value::class) {
Int::class -> int(value.toInt())
Float::class -> float(value.toFloat())
else -> throw ShouldNeverReachThisLineException()
}
}
fun string(value: String): Value {
return Value(
DataType.STRING, value, hashMapOf(
Pair("length", int(value.length))
)
fun list(value: List<Value>): Value {
return Value(
DataType.LIST, value, hashMapOf(
Pair("size", int(value.size))
)
}
)
}
fun list(value: List<Value>): Value {
return Value(
DataType.LIST, value, hashMapOf(
Pair("size", int(value.size))
)
fun map(value: Map<Value, Value>): Value {
return Value(
DataType.MAP, value, hashMapOf(
Pair("size", int(value.size)),
Pair("keys", list(value.keys.toList())),
Pair("values", list(value.values.toList()))
)
}
)
}
fun map(value: Map<Value, Value>): Value {
return Value(
DataType.MAP, value, hashMapOf(
Pair("size", int(value.size)),
Pair("keys", list(value.keys.toList())),
Pair("values", list(value.values.toList()))
)
fun note(value: Note): Value {
return Value(
DataType.NOTE, value, hashMapOf(
Pair("pitch", string(value.pitch.toString())),
Pair("octave", int(value.octave)),
Pair("duration", int(value.duration)),
Pair("dot", bool(value.dot))
)
}
)
}
fun note(value: Note): Value {
return Value(
DataType.NOTE, value, hashMapOf(
Pair("pitch", string(value.pitch.toString())),
Pair("octave", int(value.octave)),
Pair("duration", int(value.duration)),
Pair("dot", bool(value.dot))
)
)
}
fun bool(value: Boolean): Value {
return Value(DataType.BOOL, value)
}
fun bool(value: Boolean): Value {
return Value(DataType.BOOL, value)
}
fun type(value: DataType): Value {
return Value(DataType.TYPE, value)
}
fun type(value: DataType): Value {
return Value(DataType.TYPE, value)
}
fun void(): Value {
return Value(DataType.VOID, null)
}
}
fun void(): Value {
return Value(DataType.VOID, null)
}
}
}

View File

@@ -14,6 +14,6 @@ data class Token(val type: TokenType, val value: Any, val rawValue: String, val
}
override fun toString(): String {
return "(${type.name.toLowerCase()}, »${rawValue}«, ${position.short()})"
return "($type, »$rawValue«, ${position.short})"
}
}

View File

@@ -1,15 +1,14 @@
package io.smnp.dsl.token.model.entity
data class TokenPosition(val line: Int, val beginCol: Int, val endCol: Int) {
companion object {
val NONE = TokenPosition(-1, -1, -1)
}
companion object {
val NONE = TokenPosition(-1, -1, -1)
}
override fun toString(): String {
return "[line ${line+1}, col ${beginCol+1}]"
}
override fun toString(): String {
return "[line ${line + 1}, col ${beginCol + 1}]"
}
fun short(): String {
return "${line+1}:${beginCol+1}"
}
val short: String
get() = "${line + 1}:${beginCol + 1}"
}

View File

@@ -46,5 +46,9 @@ enum class TokenType(val token: String) {
IF("if"),
AS("as"),
IDENTIFIER("identifier"),
COMMENT("comment")
COMMENT("comment");
override fun toString(): String {
return super.name.toLowerCase()
}
}

View File

@@ -22,7 +22,7 @@ class AccessOperatorEvaluator : Evaluator() {
EvaluatorOutput.value(
lhs.properties[rhs] ?: throw PositionException(
EnvironmentException(
EvaluationException("Unknown property $rhs of type ${lhs.type.name.toLowerCase()}"),
EvaluationException("Unknown property $rhs of type ${lhs.typeName}"),
environment
),
rhsNode.position

View File

@@ -31,7 +31,7 @@ class LoopEvaluator : Evaluator() {
else -> throw PositionException(
EnvironmentException(
EvaluationException(
"Expected for-loop with int iterator or foreach-loop with string, list or map iterator or while-loop with bool iterator, found ${iterator.type.name.toLowerCase()}"
"Expected for-loop with int iterator or foreach-loop with string, list or map iterator or while-loop with bool iterator, found ${iterator.typeName}"
),
environment
), iteratorNode.position

View File

@@ -35,7 +35,7 @@ class MapEvaluator : Evaluator() {
if (key.type !in listOf(BOOL, INT, NOTE, STRING)) {
throw PositionException(
EnvironmentException(
EvaluationException("Invalid map key's type ${key.type.name.toLowerCase()}"),
EvaluationException("Invalid map key's type ${key.typeName}"),
environment
),
keyNode.position

View File

@@ -26,7 +26,7 @@ class MinusOperatorEvaluator : Evaluator() {
DataType.LIST -> Value.list((operand.value.value as List<Value>).reversed())
else -> throw PositionException(
EnvironmentException(
EvaluationException("Type ${operand.value.type.name.toLowerCase()} does not support minus operator"),
EvaluationException("Type ${operand.value.typeName} does not support minus operator"),
environment
),
node.position

View File

@@ -43,7 +43,7 @@ class SumOperatorEvaluator : Evaluator() {
else throw PositionException(
EnvironmentException(
EvaluationException(
"The ${lhs.type.name.toLowerCase()} and ${rhs.type.name.toLowerCase()} are not supported by + operator"
"The ${lhs.typeName} and ${rhs.typeName} are not supported by + operator"
),
environment
), plusNode.position

View File

@@ -6,7 +6,7 @@ import io.smnp.type.model.Value
class EvaluatorOutput private constructor(val result: EvaluationResult, val value: Value?) {
override fun toString(): String {
return "${result.name.toLowerCase()}(${value ?: ""})"
return "$result(${value ?: ""})"
}
companion object {

View File

@@ -9,7 +9,7 @@ import io.smnp.type.model.Value
class TypeOfFunction : Function("typeOf") {
override fun define(new: FunctionDefinitionTool) {
new function simple(allTypes()) body { _, (obj) ->
Value.string(obj.type.toString()) // TODO
Value.string(obj.typeName)
}
}
}