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

View File

@ -119,6 +119,7 @@ func (what Type) underlyingPrimitive () (underlying *TypeSection) {
underlying =
actual.(*TypeSection).
what.underlyingPrimitive()
// case *FaceSection:
// TODO: depending on if this is an object interface or
// 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
// 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.
@ -163,6 +208,7 @@ func (what Type) isSingular () (singular bool) {
switch actual.(type) {
case *TypeSection:
singular = actual.(*TypeSection).what.isSingular()
// TODO: uncomment this when this section has been implemented
// case *FaceSection:
// singular = true