Added methods to check if a type is a number

This commit is contained in:
Sasha Koshka 2022-10-13 20:52:49 -04:00
parent 12755d3f85
commit dd29f69213
2 changed files with 56 additions and 57 deletions

View File

@ -42,23 +42,11 @@ func (literal IntLiteral) What () (what Type) {
// canBePassedAs returns true if this literal can be implicitly cast to the // canBePassedAs returns true if this literal can be implicitly cast to the
// specified type, and false if it can't. // specified type, and false if it can't.
func (literal IntLiteral) canBePassedAs (what Type) (allowed bool) { func (literal IntLiteral) canBePassedAs (what Type) (allowed bool) {
// must be a singlular value // can be passed to singular types that are signed numbers at a
if !what.isSingular() { return } // primitive level.
allowed =
// can be passed to types that are signed numbers at a primitive level. what.isSingular() &&
primitive := what.underlyingPrimitive() what.isSignedNumeric()
switch primitive {
case
&PrimitiveF64,
&PrimitiveF32,
&PrimitiveI64,
&PrimitiveI32,
&PrimitiveI16,
&PrimitiveI8,
&PrimitiveInt:
allowed = true
}
return return
} }
@ -78,28 +66,10 @@ func (literal UIntLiteral) What () (what Type) {
// canBePassedAs returns true if this literal can be implicitly cast to the // canBePassedAs returns true if this literal can be implicitly cast to the
// specified type, and false if it can't. // specified type, and false if it can't.
func (literal UIntLiteral) canBePassedAs (what Type) (allowed bool) { func (literal UIntLiteral) canBePassedAs (what Type) (allowed bool) {
// must be a singlular value // can be passed to singular types that are numbers at a primitive level.
if !what.isSingular() { return } allowed =
what.isSingular() &&
// can be passed to types that are numbers at a primitive level. what.isNumeric()
primitive := what.underlyingPrimitive()
switch primitive {
case
&PrimitiveF64,
&PrimitiveF32,
&PrimitiveI64,
&PrimitiveI32,
&PrimitiveI16,
&PrimitiveI8,
&PrimitiveInt,
&PrimitiveU64,
&PrimitiveU32,
&PrimitiveU16,
&PrimitiveU8,
&PrimitiveUInt:
allowed = true
}
return return
} }
@ -167,23 +137,6 @@ func (literal StringLiteral) canBePassedAs (what Type) (allowed bool) {
what = reduced what = reduced
} }
primitive := what.underlyingPrimitive() allowed = what.isNumeric()
switch primitive {
case
&PrimitiveF64,
&PrimitiveF32,
&PrimitiveI64,
&PrimitiveI32,
&PrimitiveI16,
&PrimitiveI8,
&PrimitiveInt,
&PrimitiveU64,
&PrimitiveU32,
&PrimitiveU16,
&PrimitiveU8,
&PrimitiveUInt:
allowed = true
}
return return
} }

View File

@ -119,6 +119,7 @@ func (what Type) underlyingPrimitive () (underlying *TypeSection) {
underlying = underlying =
actual.(*TypeSection). actual.(*TypeSection).
what.underlyingPrimitive() what.underlyingPrimitive()
// case *FaceSection: // case *FaceSection:
// TODO: depending on if this is an object interface or // TODO: depending on if this is an object interface or
// a function interface, return either Face or Func. // a function interface, return either Face or Func.
@ -139,6 +140,50 @@ func (what Type) underlyingPrimitive () (underlying *TypeSection) {
} }
} }
// isNumeric returns whether or not the type descends from a numeric primitive.
func (what Type) isNumeric () (numeric bool) {
primitive := what.underlyingPrimitive()
switch primitive {
case
&PrimitiveF64,
&PrimitiveF32,
&PrimitiveI64,
&PrimitiveI32,
&PrimitiveI16,
&PrimitiveI8,
&PrimitiveInt,
&PrimitiveU64,
&PrimitiveU32,
&PrimitiveU16,
&PrimitiveU8,
&PrimitiveUInt:
numeric = true
}
return
}
// isSignedNumeric returns whether or not the type descends from a signed
// numeric primitive.
func (what Type) isSignedNumeric () (signedNumeric bool) {
primitive := what.underlyingPrimitive()
switch primitive {
case
&PrimitiveF64,
&PrimitiveF32,
&PrimitiveI64,
&PrimitiveI32,
&PrimitiveI16,
&PrimitiveI8,
&PrimitiveInt:
signedNumeric = true
}
return
}
// isSingular returns whether or not the type is a singular value. this goes // isSingular returns whether or not the type is a singular value. this goes
// all the way up the inheritence chain, only stopping when it hits a non-basic // all the way up the inheritence chain, only stopping when it hits a non-basic
// type because this is about data storage of a value. // type because this is about data storage of a value.
@ -163,6 +208,7 @@ func (what Type) isSingular () (singular bool) {
switch actual.(type) { switch actual.(type) {
case *TypeSection: case *TypeSection:
singular = actual.(*TypeSection).what.isSingular() singular = actual.(*TypeSection).what.isSingular()
// TODO: uncomment this when this section has been implemented // TODO: uncomment this when this section has been implemented
// case *FaceSection: // case *FaceSection:
// singular = true // singular = true