2022-09-22 15:51:45 -06:00
|
|
|
package analyzer
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2022-10-11 22:48:55 -06:00
|
|
|
// IntLiteral represents a constant signed integer value.
|
2022-10-11 16:31:37 -06:00
|
|
|
type IntLiteral struct {
|
|
|
|
locatable
|
|
|
|
value int64
|
|
|
|
}
|
|
|
|
|
2022-10-11 22:48:55 -06:00
|
|
|
// UIntLiteral represents a constant unsigned itneger value.
|
2022-10-11 16:31:37 -06:00
|
|
|
type UIntLiteral struct {
|
|
|
|
locatable
|
|
|
|
value uint64
|
|
|
|
}
|
|
|
|
|
2022-10-11 22:48:55 -06:00
|
|
|
// FloatLiteral represents a constant floating point value.
|
2022-10-11 16:31:37 -06:00
|
|
|
type FloatLiteral struct {
|
|
|
|
locatable
|
|
|
|
value float64
|
|
|
|
}
|
|
|
|
|
2022-10-11 22:48:55 -06:00
|
|
|
// StringLiteral represents a constant text value.
|
2022-10-11 16:31:37 -06:00
|
|
|
type StringLiteral struct {
|
|
|
|
locatable
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:51:45 -06:00
|
|
|
// ToString outputs the data in the argument as a string.
|
|
|
|
func (literal IntLiteral) ToString (indent int) (output string) {
|
2022-10-11 16:31:37 -06:00
|
|
|
output += doIndent(indent, fmt.Sprint("arg int ", literal.value, "\n"))
|
2022-09-22 15:51:45 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-11 15:03:19 -06:00
|
|
|
// What returns the type of the argument
|
|
|
|
func (literal IntLiteral) What () (what Type) {
|
|
|
|
what.actual = &PrimitiveI64
|
|
|
|
what.length = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-04 14:19:26 -06:00
|
|
|
// 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
|
2022-10-11 14:20:12 -06:00
|
|
|
if !what.isSingular() { return }
|
2022-10-04 14:19:26 -06:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:51:45 -06:00
|
|
|
// ToString outputs the data in the argument as a string.
|
|
|
|
func (literal UIntLiteral) ToString (indent int) (output string) {
|
2022-10-11 16:31:37 -06:00
|
|
|
output += doIndent(indent, fmt.Sprint("arg uint ", literal.value, "\n"))
|
2022-09-22 15:51:45 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-11 15:03:19 -06:00
|
|
|
// What returns the type of the argument
|
|
|
|
func (literal UIntLiteral) What () (what Type) {
|
|
|
|
what.actual = &PrimitiveU64
|
|
|
|
what.length = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-04 14:19:26 -06:00
|
|
|
// 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
|
2022-10-11 14:20:12 -06:00
|
|
|
if !what.isSingular() { return }
|
2022-10-04 14:19:26 -06:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-11 15:03:19 -06:00
|
|
|
// What returns the type of the argument
|
|
|
|
func (literal FloatLiteral) What () (what Type) {
|
|
|
|
what.actual = &PrimitiveF64
|
|
|
|
what.length = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:51:45 -06:00
|
|
|
// ToString outputs the data in the argument as a string.
|
|
|
|
func (literal FloatLiteral) ToString (indent int) (output string) {
|
2022-10-11 16:31:37 -06:00
|
|
|
output += doIndent(indent, fmt.Sprint("arg float ", literal.value, "\n"))
|
2022-09-22 15:51:45 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-04 14:19:26 -06:00
|
|
|
// canBePassedAs returns true if this literal can be implicitly cast to the
|
|
|
|
// specified type, and false if it can't.
|
|
|
|
func (literal FloatLiteral) canBePassedAs (what Type) (allowed bool) {
|
|
|
|
// must be a singlular value
|
2022-10-11 14:20:12 -06:00
|
|
|
if !what.isSingular() { return }
|
2022-10-04 14:19:26 -06:00
|
|
|
|
|
|
|
// can be passed to types that are floats at a primitive level.
|
|
|
|
primitive := what.underlyingPrimitive()
|
|
|
|
switch primitive {
|
|
|
|
case
|
|
|
|
&PrimitiveF64,
|
|
|
|
&PrimitiveF32:
|
|
|
|
|
|
|
|
allowed = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-11 15:03:19 -06:00
|
|
|
// What returns the type of the argument
|
|
|
|
func (literal StringLiteral) What () (what Type) {
|
|
|
|
what.actual = &BuiltInString
|
|
|
|
what.length = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:51:45 -06:00
|
|
|
// ToString outputs the data in the argument as a string.
|
|
|
|
func (literal StringLiteral) ToString (indent int) (output string) {
|
2022-10-11 16:31:37 -06:00
|
|
|
output += doIndent(indent, fmt.Sprint("arg string '", literal.value, "'\n"))
|
2022-09-22 15:51:45 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-04 14:19:26 -06:00
|
|
|
// canBePassedAs returns true if this literal can be implicitly cast to the
|
|
|
|
// specified type, and false if it can't.
|
|
|
|
func (literal StringLiteral) canBePassedAs (what Type) (allowed bool) {
|
2022-10-11 13:09:44 -06:00
|
|
|
// can be passed to types that are numbers at a primitive level, or
|
|
|
|
// types that can be reduced to a variable array pointing to numbers at
|
|
|
|
// a primitive level.
|
|
|
|
|
2022-10-11 16:03:44 -06:00
|
|
|
// we don't check the length of what, becasue when setting a static
|
|
|
|
// array to a string literal, excess data will be cut off (and if it is
|
|
|
|
// shorter, the excess space will be filled with zeros).
|
|
|
|
|
2022-10-11 14:20:12 -06:00
|
|
|
reduced, worked := what.reduce()
|
2022-10-11 13:09:44 -06:00
|
|
|
if worked {
|
2022-10-11 14:20:12 -06:00
|
|
|
if !what.isSingular() { return }
|
|
|
|
if reduced.kind != TypeKindVariableArray { return }
|
|
|
|
what = reduced
|
2022-10-11 13:09:44 -06:00
|
|
|
}
|
|
|
|
|
2022-10-04 14:19:26 -06:00
|
|
|
primitive := what.underlyingPrimitive()
|
|
|
|
switch primitive {
|
|
|
|
case
|
|
|
|
&PrimitiveF64,
|
|
|
|
&PrimitiveF32,
|
|
|
|
&PrimitiveI64,
|
|
|
|
&PrimitiveI32,
|
|
|
|
&PrimitiveI16,
|
|
|
|
&PrimitiveI8,
|
|
|
|
&PrimitiveInt,
|
|
|
|
&PrimitiveU64,
|
|
|
|
&PrimitiveU32,
|
|
|
|
&PrimitiveU16,
|
|
|
|
&PrimitiveU8,
|
|
|
|
&PrimitiveUInt:
|
|
|
|
|
|
|
|
allowed = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|