1
0
mirror of https://github.com/golang/go synced 2024-11-26 17:07:09 -07:00

Add support for the basic type "bool".

R=r
DELTA=51  (51 added, 0 deleted, 0 changed)
OCL=18283
CL=18290
This commit is contained in:
Ian Lance Taylor 2008-10-31 16:34:47 -07:00
parent 613a5c8bc6
commit 16fd356679
6 changed files with 51 additions and 0 deletions

View File

@ -161,3 +161,13 @@ TEXT reflect·PtrStringToAddr(SB),7,$-8
MOVQ AX, 16(SP)
RET
TEXT reflect·AddrToPtrBool(SB),7,$-8
MOVQ 8(SP), AX
MOVQ AX, 16(SP)
RET
TEXT reflect·PtrBoolToAddr(SB),7,$-8
MOVQ 8(SP), AX
MOVQ AX, 16(SP)
RET

View File

@ -36,4 +36,5 @@ Float32
Float64
Float80
String
Bool
!

View File

@ -77,6 +77,8 @@ func valuedump(s, t string) {
v.(reflect.Float64Value).Put(64.0);
case reflect.StringKind:
v.(reflect.StringValue).Put("stringy cheese");
case reflect.BoolKind:
v.(reflect.BoolValue).Put(true);
}
assert(reflect.ValueToString(v), t);
}
@ -132,6 +134,7 @@ func main() {
valuedump("float32", "+3.200000e+01");
valuedump("float64", "+6.400000e+01");
valuedump("string", "stringy cheese");
valuedump("bool", "true");
valuedump("*int8", "*int8(0)");
valuedump("**int8", "**int8(0)");
valuedump("[5]int32", "[5]int32{0, 0, 0, 0, 0}");

View File

@ -162,6 +162,12 @@ func ValueToString(val Value) string {
return "float80";
case StringKind:
return val.(StringValue).Get();
case BoolKind:
if val.(BoolValue).Get() {
return "true"
} else {
return "false"
}
case PtrKind:
v := val.(PtrValue);
return TypeToString(typ, false) + "(" + integer(int64(v.Get())) + ")";

View File

@ -16,6 +16,7 @@ export func typestrings() string // implemented in C; declared here
export const (
MissingKind = iota;
ArrayKind;
BoolKind;
ChanKind;
FloatKind;
Float32Kind;
@ -82,6 +83,7 @@ func NewBasicType(name string, kind int, size uint64) Type {
// Prebuilt basic types
export var (
Missing = NewBasicType(MissingString, MissingKind, 1);
Bool = NewBasicType("bool", BoolKind, 1); // TODO: need to know how big a bool is
Int = NewBasicType("int", IntKind, 4); // TODO: need to know how big an int is
Int8 = NewBasicType("int8", Int8Kind, 1);
Int16 = NewBasicType("int16", Int16Kind, 2);
@ -409,6 +411,7 @@ func init() {
types["float64"] = &Float64;
types["float80"] = &Float80;
types["string"] = &String;
types["bool"] = &Bool;
// Basics get prebuilt stubs
MissingStub = NewStubType(MissingString, Missing);
@ -428,6 +431,7 @@ func init() {
basicstub["float64"] = NewStubType("float64", Float64);
basicstub["float80"] = NewStubType("float80", Float80);
basicstub["string"] = NewStubType("string", String);
basicstub["bool"] = NewStubType("bool", Bool);
Unlock();
}

View File

@ -58,6 +58,7 @@ func AddrToPtrFloat32(Addr) *float32
func AddrToPtrFloat64(Addr) *float64
func AddrToPtrFloat80(Addr) *float80
func AddrToPtrString(Addr) *string
func AddrToPtrBool(Addr) *bool
// -- Int
@ -438,6 +439,31 @@ func (v *StringValueStruct) Put(s string) {
*AddrToPtrString(v.addr) = s
}
// -- Bool
export type BoolValue interface {
Kind() int;
Get() bool;
Put(bool);
Type() Type;
}
type BoolValueStruct struct {
CommonV
}
func BoolCreator(typ Type, addr Addr) Value {
return &BoolValueStruct{ CommonV{BoolKind, typ, addr} }
}
func (v *BoolValueStruct) Get() bool {
return *AddrToPtrBool(v.addr)
}
func (v *BoolValueStruct) Put(b bool) {
*AddrToPtrBool(v.addr) = b
}
// -- Pointer
export type PtrValue interface {
@ -665,6 +691,7 @@ func init() {
creator[Float64Kind] = &Float64Creator;
creator[Float80Kind] = &Float80Creator;
creator[StringKind] = &StringCreator;
creator[BoolKind] = &BoolCreator;
creator[PtrKind] = &PtrCreator;
creator[ArrayKind] = &ArrayCreator;
creator[MapKind] = &MapCreator;