Skip to content

Commit

Permalink
refactor(types): remove types.Primitives array
Browse files Browse the repository at this point in the history
  • Loading branch information
saffage committed May 8, 2024
1 parent be5578b commit 6dd72d3
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 90 deletions.
4 changes: 2 additions & 2 deletions checker/builtin_fns.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ func (check *Checker) builtInMagic(node *ast.ParenList, args []*TypedValue) *Typ

switch *strval {
case "Bool":
return &TypedValue{types.NewTypeDesc(types.Primitives[types.Bool]), nil}
return &TypedValue{types.NewTypeDesc(types.Bool), nil}

case "I32":
return &TypedValue{types.NewTypeDesc(types.Primitives[types.I32]), nil}
return &TypedValue{types.NewTypeDesc(types.I32), nil}

default:
check.errorf(node.Exprs[0], "unknown magic '%s'", *strval)
Expand Down
12 changes: 6 additions & 6 deletions checker/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,32 @@ func (check *Checker) defBuiltIns() {
name: "magic",
f: check.builtInMagic,
t: types.NewFunc(
types.NewTuple(types.Primitives[types.AnyTypeDesc]),
types.NewTuple(types.Primitives[types.UntypedString]),
types.NewTuple(types.AnyTypeDesc),
types.NewTuple(types.UntypedString),
),
},
{
name: "type_of",
f: check.builtInTypeOf,
t: types.NewFunc(
types.NewTuple(types.Primitives[types.AnyTypeDesc]),
types.NewTuple(types.Primitives[types.Any]),
types.NewTuple(types.AnyTypeDesc),
types.NewTuple(types.Any),
),
},
{
name: "print",
f: check.builtInPrint,
t: types.NewFunc(
types.Unit,
types.NewTuple(types.Primitives[types.Any]),
types.NewTuple(types.Any),
),
},
{
name: "assert",
f: check.builtInAssert,
t: types.NewFunc(
types.Unit,
types.NewTuple(types.Primitives[types.Bool]),
types.NewTuple(types.Bool),
),
},
}
Expand Down
10 changes: 5 additions & 5 deletions checker/global_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,34 @@ var Global = NewScope(nil)
var primitives = [...]Symbol{
&TypeAlias{
owner: nil,
t: types.NewAlias(types.Primitives[types.Bool], "bool"),
t: types.NewAlias(types.Bool, "bool"),
node: nil,
name: &ast.Ident{Name: "bool"},
},
&TypeAlias{
owner: nil,
t: types.NewAlias(types.Primitives[types.I32], "i32"),
t: types.NewAlias(types.I32, "i32"),
node: nil,
name: &ast.Ident{Name: "i32"},
},
&TypeAlias{
owner: nil,
t: types.NewAlias(types.Primitives[types.I32], "int"),
t: types.NewAlias(types.I32, "int"),
node: nil,
name: &ast.Ident{Name: "int"},
},
NewConst(
nil,
&TypedValue{
Type: types.Primitives[types.UntypedBool],
Type: types.UntypedBool,
Value: constant.NewBool(true),
},
&ast.Ident{Name: "true"},
),
NewConst(
nil,
&TypedValue{
Type: types.Primitives[types.UntypedBool],
Type: types.UntypedBool,
Value: constant.NewBool(false),
},
&ast.Ident{Name: "false"},
Expand Down
22 changes: 11 additions & 11 deletions checker/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ func (check *Checker) prefix(node *ast.PrefixOp, tOperand types.Type) types.Type
case ast.OperatorNeg:
if p := types.AsPrimitive(tOperand); p != nil {
switch p.Kind() {
case types.UntypedInt, types.UntypedFloat, types.I32:
case types.KindUntypedInt, types.KindUntypedFloat, types.KindI32:
return tOperand
}
}

case ast.OperatorNot:
if p := types.AsPrimitive(tOperand); p != nil {
switch p.Kind() {
case types.UntypedBool, types.Bool:
case types.KindUntypedBool, types.KindBool:
return tOperand
}
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func (check *Checker) infix(node *ast.InfixOp, tOperandX, tOperandY types.Type)
ast.OperatorBitShl,
ast.OperatorBitShr:
switch primitive.Kind() {
case types.UntypedInt, types.UntypedFloat, types.I32:
case types.KindUntypedInt, types.KindUntypedFloat, types.KindI32:
return tOperandX
}

Expand All @@ -98,20 +98,20 @@ func (check *Checker) infix(node *ast.InfixOp, tOperandX, tOperandY types.Type)
ast.OperatorGt,
ast.OperatorGe:
switch primitive.Kind() {
case types.UntypedBool, types.UntypedInt, types.UntypedFloat:
return types.Primitives[types.UntypedBool]
case types.KindUntypedBool, types.KindUntypedInt, types.KindUntypedFloat:
return types.UntypedBool

case types.Bool, types.I32:
return types.Primitives[types.Bool]
case types.KindBool, types.KindI32:
return types.Bool
}

case ast.OperatorAnd, ast.OperatorOr:
switch primitive.Kind() {
case types.UntypedBool:
return types.Primitives[types.UntypedBool]
case types.KindUntypedBool:
return types.UntypedBool

case types.Bool:
return types.Primitives[types.Bool]
case types.KindBool:
return types.Bool
}

default:
Expand Down
12 changes: 6 additions & 6 deletions checker/type_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ func (check *Checker) typeOfIdent(node *ast.Ident) types.Type {
func (check *Checker) typeOfLiteral(node *ast.Literal) types.Type {
switch node.Kind {
case ast.IntLiteral:
return types.Primitives[types.UntypedInt]
return types.UntypedInt

case ast.FloatLiteral:
return types.Primitives[types.UntypedFloat]
return types.UntypedFloat

case ast.StringLiteral:
return types.Primitives[types.UntypedString]
return types.UntypedString

default:
panic(fmt.Sprintf("unhandled literal kind: '%s'", node.Kind.String()))
Expand Down Expand Up @@ -240,7 +240,7 @@ func (check *Checker) typeOfIndex(node *ast.Index) types.Type {
}

if array := types.AsArray(t); array != nil {
if !types.Primitives[types.I32].Equals(tIndex) {
if !types.I32.Equals(tIndex) {
check.errorf(node.Args.Exprs[0], "expected type (i32) for index, got (%s) instead", tIndex)
return nil
}
Expand Down Expand Up @@ -493,7 +493,7 @@ func (check *Checker) typeOfIf(node *ast.If) types.Type {
return tBody
}

if !types.Primitives[types.Bool].Equals(tCondition) {
if !types.Bool.Equals(tCondition) {
check.errorf(
node.Cond,
"expected type (bool) for condition, got (%s) instead",
Expand Down Expand Up @@ -553,7 +553,7 @@ func (check *Checker) typeOfWhile(node *ast.While) types.Type {
return nil
}

if !types.Primitives[types.Bool].Equals(tCond) {
if !types.Bool.Equals(tCond) {
check.errorf(node.Cond, "expected type 'bool' for condition, got (%s) instead", tCond)
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion checker/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (check *Checker) valueOfInternal(expr ast.Node) *TypedValue {
value := constantFromNode(node)
type_ := types.FromConstant(value)

if type_ == types.Primitives[types.UntypedString] {
if type_ == types.UntypedString {
check.Data[node] = &TypedValue{type_, value}
}

Expand Down
22 changes: 11 additions & 11 deletions types/func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,45 @@ func TestCheckArgs(t *testing.T) {
idx, err := funcType.CheckArgs(params)
checkArgs(t, idx, -1, err, "")

params = NewTuple(Primitives[I32])
params = NewTuple(I32)
funcType = NewFunc(nil, params)
idx, err = funcType.CheckArgs(params)
checkArgs(t, idx, -1, err, "")
}

func TestCheckArgsFail(t *testing.T) {
params := Unit
args := NewTuple(Primitives[I32])
args := NewTuple(I32)
funcType := NewFunc(nil, params)
idx, err := funcType.CheckArgs(args)
checkArgs(t, idx, 0, err, "too many arguments (expected 0, got 1)")

params = NewTuple(Primitives[I32])
params = NewTuple(I32)
args = Unit
funcType = NewFunc(nil, params)
idx, err = funcType.CheckArgs(args)
checkArgs(t, idx, 0, err, "not enough arguments (expected 1, got 0)")

params = NewTuple(Primitives[I32])
args = NewTuple(Primitives[I32], Primitives[Bool], Primitives[I32])
params = NewTuple(I32)
args = NewTuple(I32, Bool, I32)
funcType = NewFunc(nil, params)
idx, err = funcType.CheckArgs(args)
checkArgs(t, idx, 1, err, "too many arguments (expected 1, got 3)")

params = NewTuple(Primitives[I32], Primitives[Bool], Primitives[I32])
args = NewTuple(Primitives[I32])
params = NewTuple(I32, Bool, I32)
args = NewTuple(I32)
funcType = NewFunc(nil, params)
idx, err = funcType.CheckArgs(args)
checkArgs(t, idx, 1, err, "not enough arguments (expected 3, got 1)")

params = NewTuple(Primitives[Bool])
args = NewTuple(Primitives[I32])
params = NewTuple(Bool)
args = NewTuple(I32)
funcType = NewFunc(nil, params)
idx, err = funcType.CheckArgs(args)
checkArgs(t, idx, 0, err, "expected 'bool' for 1st argument, got 'i32' instead")

params = NewTuple(Primitives[I32], Primitives[Bool])
args = NewTuple(Primitives[I32], Primitives[I32])
params = NewTuple(I32, Bool)
args = NewTuple(I32, I32)
funcType = NewFunc(nil, params)
idx, err = funcType.CheckArgs(args)
checkArgs(t, idx, 1, err, "expected 'bool' for 2nd argument, got 'i32' instead")
Expand Down
59 changes: 29 additions & 30 deletions types/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Primitive struct {

func (p *Primitive) Equals(other Type) bool {
p2, _ := other.Underlying().(*Primitive)
return p.kind == Any || (p2 != nil && (p.kind == p2.kind || p.kind == SkipUntyped(p2).(*Primitive).kind))
return p.kind == KindAny || (p2 != nil && (p.kind == p2.kind || p.kind == SkipUntyped(p2).(*Primitive).kind))
}

func (p *Primitive) Underlying() Type { return p }
Expand All @@ -32,11 +32,11 @@ func AsPrimitive(t Type) *Primitive {
func SkipUntyped(t Type) Type {
if p, _ := t.(*Primitive); p != nil {
switch p.kind {
case UntypedBool:
return Primitives[Bool]
case KindUntypedBool:
return Bool

case UntypedInt:
return Primitives[I32]
case KindUntypedInt:
return I32

// case UntypedFloat, UntypedString:
// panic("not implemented")
Expand All @@ -49,7 +49,7 @@ func SkipUntyped(t Type) Type {
func IsUntyped(t Type) bool {
if p, _ := t.(*Primitive); p != nil {
switch p.kind {
case UntypedBool, UntypedInt, UntypedFloat, UntypedString:
case KindUntypedBool, KindUntypedInt, KindUntypedFloat, KindUntypedString:
return true
}
}
Expand All @@ -60,16 +60,16 @@ func IsUntyped(t Type) bool {
func FromConstant(value constant.Value) Type {
switch value.Kind() {
case constant.Bool:
return Primitives[UntypedBool]
return UntypedBool

case constant.Int:
return Primitives[UntypedInt]
return UntypedInt

case constant.Float:
return Primitives[UntypedFloat]
return UntypedFloat

case constant.String:
return Primitives[UntypedString]
return UntypedString

default:
panic("unreachable")
Expand All @@ -80,32 +80,31 @@ func FromConstant(value constant.Value) Type {
type PrimitiveKind byte

const (
UnknownPrimitive PrimitiveKind = iota
UnknownPrimitiveKind PrimitiveKind = iota

UntypedBool // untyped bool
UntypedInt // untyped int
UntypedFloat // untyped float
UntypedString // untyped string
KindUntypedBool // untyped bool
KindUntypedInt // untyped int
KindUntypedFloat // untyped float
KindUntypedString // untyped string

Bool // bool
I32 // i32
KindBool // bool
KindI32 // i32

// Meta types.

Any // any
AnyTypeDesc // typedesc
KindAny // any
KindAnyTypeDesc // typedesc
)

var Primitives = [...]*Primitive{
UnknownPrimitive: {UnknownPrimitive},
UntypedBool: {UntypedBool},
UntypedInt: {UntypedInt},
UntypedFloat: {UntypedFloat},
UntypedString: {UntypedString},
var (
UntypedBool = &Primitive{KindUntypedBool}
UntypedInt = &Primitive{KindUntypedInt}
UntypedFloat = &Primitive{KindUntypedFloat}
UntypedString = &Primitive{KindUntypedString}

Bool: {Bool},
I32: {I32},
Bool = &Primitive{KindBool}
I32 = &Primitive{KindI32}

Any: {Any},
AnyTypeDesc: {AnyTypeDesc},
}
Any = &Primitive{KindAny}
AnyTypeDesc = &Primitive{KindAnyTypeDesc}
)
22 changes: 11 additions & 11 deletions types/primitive_kind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6dd72d3

Please sign in to comment.