More entities fixed

This commit is contained in:
barsdeveloper
2024-06-04 01:00:41 +02:00
parent 1a8636bb5d
commit c05e6d3cc9
15 changed files with 913 additions and 456 deletions

View File

@@ -27,6 +27,7 @@ export default class ArrayEntity extends IEntity {
values = values instanceof Array ? values : []
const result = new this(values)
result.trailing = trailing !== undefined
return result
}).label(`ArrayEntity of ${this.type?.className() ?? "unknown values"}`)
}
@@ -43,6 +44,19 @@ export default class ArrayEntity extends IEntity {
return result
}
/** @param {IEntity} other */
equals(other) {
if (!(other instanceof ArrayEntity) || this.values.length !== other.values.length) {
return false
}
for (let i = 0; i < this.values.length; ++i) {
if (!this.values[i].equals(other.values[i])) {
return false
}
}
return true
}
valueOf() {
return this.values
}
@@ -53,7 +67,7 @@ export default class ArrayEntity extends IEntity {
printKey = this.Self().printKey,
) {
let result = this.values.map(v => v?.toString()).join(this.Self().attributeSeparator)
if (this.Self().trailing) {
if (this.trailing) {
result += this.Self().attributeSeparator
}
return `(${result})`

View File

@@ -9,7 +9,8 @@ export default class ByteEntity extends IntegerEntity {
return super.value
}
set value(value) {
if (value % 1 == 0 && value >= 0 && value < 1 << 8) {
value = Math.trunc(value)
if (value >= 0 && value < 1 << 8) {
super.value = value
}
}

View File

@@ -238,17 +238,25 @@ export default class IEntity {
/** @param {IEntity} other */
equals(other) {
const thisKeys = Object.keys(this)
const otherKeys = Object.keys(other)
if (thisKeys.length != otherKeys.length) {
if (!(other instanceof IEntity)) {
return false
}
if (this.valueOf && other.valueOf) {
return this.valueOf() === other.valueOf()
const thisKeys = Object.keys(this)
const otherKeys = Object.keys(other)
if (thisKeys.length != otherKeys.length || !(this instanceof other.constructor) && !(other instanceof this.constructor)) {
return false
}
for (let i = 0; i < thisKeys.length; ++i) {
if (!(this[thisKeys[i]] instanceof IEntity && this[thisKeys[i]].equals(other[otherKeys[i]]))) {
return false
const a = this[thisKeys[i]]
const b = other[otherKeys[i]]
if (a instanceof IEntity) {
if (!a.equals(b)) {
return false
}
} else {
if (a !== b) {
return false
}
}
}
return true

View File

@@ -9,6 +9,7 @@ export default class IntegerEntity extends NumberEntity {
return super.value
}
set value(value) {
value = Math.trunc(value)
if (value >= 1 << 31 && value < -(1 << 31)) {
value = Math.floor(value)
super.value = value

View File

@@ -16,6 +16,7 @@ export default class KeyBindingEntity extends IEntity {
bCmd: BooleanEntity,
Key: SymbolEntity,
}
/** @type {P<KeyBindingEntity>} */
static grammar = P.alt(
SymbolEntity.grammar.map(identifier => new this({ Key: identifier })),
Grammar.createEntityGrammar(this)

View File

@@ -4,40 +4,59 @@ import IEntity from "./IEntity.js"
export default class NumberEntity extends IEntity {
static numberRegexSource = String.raw`${Grammar.numberRegexSource}(?<=(?:\.(\d*0+))?)`
static grammar = P.regArray(
new RegExp(`(${Grammar.numberRegexSource})|(\\+?inf)|(-inf)`)
).map(([_0, n, plusInf, minusInf]) => new this(
n ? Number(n) : plusInf ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY
)).label("NumberEntity")
new RegExp(`(?<n>${this.numberRegexSource})|(?<posInf>\\+?inf)|(?<negInf>-inf)`)
).map(({ 2: precision, groups: { n, posInf, negInf } }) => new this(
n ? Number(n) : posInf ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY,
precision?.length
)
).label("NumberEntity")
/** @type {Number} */
#value
#precision = 0
get precision() {
return this.#precision
}
set precision(value) {
this.#precision = value
}
/**
* @protected
* @type {Number}
*/
_value
get value() {
return this.#value
return this._value
}
set value(value) {
if (value === -0) {
value = 0
}
this.#value = value
this._value = value
}
constructor(value = 0) {
constructor(value = 0, precision = 0) {
super()
this.value = value
this.value = Number(value)
this.#precision = Number(precision)
}
valueOf() {
return this.value
}
toString() {
toString(
insideString = false,
indentation = "",
printKey = this.Self().printKey,
) {
if (this.value === Number.POSITIVE_INFINITY) {
return "+inf"
}
if (this.value === Number.NEGATIVE_INFINITY) {
return "-inf"
}
return this.value.toString()
return this.#precision ? this.value.toFixed(this.#precision) : this.value.toString()
}
}

View File

@@ -29,6 +29,7 @@ export default class ObjectReferenceEntity extends IEntity {
)
).map(([full, type, path]) => new this(type, path, full))
static typeReferenceGrammar = this.typeReference.map(v => new this(v, "", v))
/** @type {P<ObjectReferenceEntity>} */
static grammar = P.alt(
this.fullReferenceSerializedGrammar,
this.fullReferenceGrammar,
@@ -51,6 +52,8 @@ export default class ObjectReferenceEntity extends IEntity {
this.#path = value
}
#fullEscaped
/** @type {String} */
#full
get full() {
return this.#full
@@ -75,11 +78,25 @@ export default class ObjectReferenceEntity extends IEntity {
return Utility.getNameFromPath(this.path.replace(/_C$/, ""), dropCounter)
}
/** @param {IEntity} other */
equals(other) {
if (!(other instanceof ObjectReferenceEntity)) {
return false
}
return this.type == other.type && this.path == other.path
}
toString(
insideString = false,
indentation = "",
printKey = this.Self().printKey,
) {
if (insideString) {
if (this.#fullEscaped === undefined) {
this.#fullEscaped = Utility.escapeString(this.#full, false)
}
return this.#fullEscaped
}
return this.full
}
}

View File

@@ -1,21 +1,39 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import RotatorEntity from "./RotatorEntity.js"
import NumberEntity from "./NumberEntity.js"
export default class SimpleSerializationRotatorEntity extends RotatorEntity {
static attributeSeparator = ", "
/** @type {P<SimpleSerializationRotatorEntity>} */
static grammar = P.alt(
P.regArray(new RegExp(
`(${Grammar.numberRegexSource})`
`(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${Grammar.numberRegexSource})`
+ `(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${Grammar.numberRegexSource})`
)).map(([_, p, y, r]) => new this({
R: Number(r),
P: Number(p),
Y: Number(y),
+ `(${NumberEntity.numberRegexSource})`
)).map(([_, p, pPrecision, y, yPrecision, r, rPrecision]) => new this({
R: new NumberEntity(r, rPrecision?.length),
P: new NumberEntity(p, pPrecision?.length),
Y: new NumberEntity(y, yPrecision?.length),
})),
RotatorEntity.grammar
)
RotatorEntity.grammar.map(v => new this({
R: v.R,
P: v.P,
Y: v.Y,
}))
).label("SimpleSerializationRotatorEntity")
toString(
insideString = false,
indentation = "",
printKey = this.Self().printKey,
) {
const Self = this.Self()
return this.P.toString(insideString) + Self.attributeSeparator
+ this.Y.toString(insideString) + Self.attributeSeparator
+ this.R.toString(insideString) + (this.trailing ? Self.attributeSeparator : "")
}
}

View File

@@ -1,18 +1,34 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import Vector2DEntity from "./Vector2DEntity.js"
import NumberEntity from "./NumberEntity.js"
export default class SimpleSerializationVector2DEntity extends Vector2DEntity {
static attributeSeparator = ", "
/** @type {P<SimpleSerializationVector2DEntity>} */
static grammar = P.alt(
P.regArray(new RegExp(
`(${Grammar.numberRegexSource})`
`(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${Grammar.numberRegexSource})`
)).map(([_, x, y]) => new this({
X: Number(x),
Y: Number(y),
+ `(${NumberEntity.numberRegexSource})`
)).map(([_, x, xPrecision, y, yPrecision]) => new this({
X: new NumberEntity(x, xPrecision?.length),
Y: new NumberEntity(y, yPrecision?.length),
})),
Vector2DEntity.grammar
)
Vector2DEntity.grammar.map(v => new this({
X: v.X,
Y: v.Y,
}))
).label("SimpleSerializationVector2DEntity")
toString(
insideString = false,
indentation = "",
printKey = this.Self().printKey,
) {
const Self = this.Self()
return this.X.toString(insideString) + Self.attributeSeparator
+ this.Y.toString(insideString) + (this.trailing ? Self.attributeSeparator : "")
}
}

View File

@@ -1,22 +1,40 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import VectorEntity from "./VectorEntity.js"
import NumberEntity from "./NumberEntity.js"
export default class SimpleSerializationVectorEntity extends VectorEntity {
static attributeSeparator = ", "
/** @type {P<SimpleSerializationVectorEntity>} */
static grammar = P.alt(
P.regArray(new RegExp(
`(${Grammar.numberRegexSource})`
`(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${Grammar.numberRegexSource})`
+ `(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${Grammar.numberRegexSource})`
+ `(${NumberEntity.numberRegexSource})`
))
.map(([_0, x, y, z]) => new this({
X: Number(x),
Y: Number(y),
Z: Number(z),
.map(([_, x, xPrecision, y, yPrecision, z, zPrecision]) => new this({
X: new NumberEntity(x, xPrecision?.length),
Y: new NumberEntity(y, yPrecision?.length),
Z: new NumberEntity(z, zPrecision?.length),
})),
VectorEntity.grammar
VectorEntity.grammar.map(v => new this({
X: v.X,
Y: v.Y,
Z: v.Z,
}))
)
toString(
insideString = false,
indentation = "",
printKey = this.Self().printKey,
) {
const Self = this.Self()
return this.X.toString(insideString) + Self.attributeSeparator
+ this.Y.toString(insideString) + Self.attributeSeparator
+ this.Z.toString(insideString) + (this.trailing ? Self.attributeSeparator : "")
}
}

View File

@@ -23,9 +23,9 @@ export default class StringEntity extends IPrintableEntity {
}
toString(insideString = false) {
let result = Utility.escapeString(this.value)
if (!insideString) {
result = `"${result}"`
let result = `"${Utility.escapeString(this.value)}"`
if (insideString) {
result = Utility.escapeString(result, false)
}
return result
}