1
0
mirror of https://github.com/golang/go synced 2024-10-04 02:11:22 -06:00
go/usr/gri/pretty/universe.go

124 lines
3.0 KiB
Go
Raw Normal View History

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package Universe
import (
"array";
AST "ast";
)
2009-01-20 15:40:40 -07:00
var (
2009-01-15 18:16:41 -07:00
Scope *AST.Scope;
Types array.Array;
2009-01-20 15:40:40 -07:00
// internal types
2009-01-15 18:16:41 -07:00
Void_typ,
Bad_typ,
Nil_typ,
2009-01-20 15:40:40 -07:00
// basic types
2009-01-15 18:16:41 -07:00
Bool_typ,
Uint8_typ,
Uint16_typ,
Uint32_typ,
Uint64_typ,
Int8_typ,
Int16_typ,
Int32_typ,
Int64_typ,
Float32_typ,
Float64_typ,
Float80_typ,
String_typ,
Integer_typ,
2009-01-20 15:40:40 -07:00
// convenience types
2009-01-15 18:16:41 -07:00
Byte_typ,
Uint_typ,
Int_typ,
Float_typ,
Uintptr_typ *AST.Type;
2009-01-20 15:40:40 -07:00
2009-01-15 18:16:41 -07:00
True_obj,
False_obj,
Iota_obj,
Nil_obj *AST.Object;
)
2009-01-15 18:16:41 -07:00
func declObj(kind int, ident string, typ *AST.Type) *AST.Object {
obj := AST.NewObject(-1 /* no source pos */, kind, ident);
2009-01-15 18:16:41 -07:00
obj.Typ = typ;
if kind == AST.TYPE && typ.Obj == nil {
typ.Obj = obj; // set primary type object
}
2009-01-15 18:16:41 -07:00
Scope.Insert(obj);
return obj
}
2009-01-15 18:16:41 -07:00
func declType(form int, ident string, size int) *AST.Type {
typ := AST.NewType(-1 /* no source pos */, form);
2009-01-15 18:16:41 -07:00
typ.Size = size;
return declObj(AST.TYPE, ident, typ).Typ;
}
2009-01-15 18:16:41 -07:00
func register(typ *AST.Type) *AST.Type {
typ.Ref = Types.Len();
Types.Push(typ);
return typ;
}
func init() {
2009-01-15 18:16:41 -07:00
Scope = AST.NewScope(nil); // universe has no parent
Types.Init(32);
2009-01-20 15:40:40 -07:00
// Interal types
2009-01-15 18:16:41 -07:00
Void_typ = AST.NewType(-1 /* no source pos */, AST.VOID);
AST.Universe_void_typ = Void_typ;
Bad_typ = AST.NewType(-1 /* no source pos */, AST.BADTYPE);
Nil_typ = AST.NewType(-1 /* no source pos */, AST.NIL);
2009-01-20 15:40:40 -07:00
// Basic types
2009-01-15 18:16:41 -07:00
Bool_typ = register(declType(AST.BOOL, "bool", 1));
Uint8_typ = register(declType(AST.UINT, "uint8", 1));
Uint16_typ = register(declType(AST.UINT, "uint16", 2));
Uint32_typ = register(declType(AST.UINT, "uint32", 4));
Uint64_typ = register(declType(AST.UINT, "uint64", 8));
Int8_typ = register(declType(AST.INT, "int8", 1));
Int16_typ = register(declType(AST.INT, "int16", 2));
Int32_typ = register(declType(AST.INT, "int32", 4));
Int64_typ = register(declType(AST.INT, "int64", 8));
Float32_typ = register(declType(AST.FLOAT, "float32", 4));
Float64_typ = register(declType(AST.FLOAT, "float64", 8));
Float80_typ = register(declType(AST.FLOAT, "float80", 10));
String_typ = register(declType(AST.STRING, "string", 8));
Integer_typ = register(declType(AST.INTEGER, "integer", 8));
// All but 'byte' should be platform-dependent, eventually.
2009-01-15 18:16:41 -07:00
Byte_typ = register(declType(AST.UINT, "byte", 1));
Uint_typ = register(declType(AST.UINT, "uint", 4));
Int_typ = register(declType(AST.INT, "int", 4));
Float_typ = register(declType(AST.FLOAT, "float", 4));
Uintptr_typ = register(declType(AST.UINT, "uintptr", 8));
// Predeclared constants
2009-01-15 18:16:41 -07:00
True_obj = declObj(AST.CONST, "true", Bool_typ);
False_obj = declObj(AST.CONST, "false", Bool_typ);
Iota_obj = declObj(AST.CONST, "iota", Int_typ);
Nil_obj = declObj(AST.CONST, "nil", Nil_typ);
// Builtin functions
2009-01-15 18:16:41 -07:00
declObj(AST.BUILTIN, "len", Void_typ);
declObj(AST.BUILTIN, "new", Void_typ);
declObj(AST.BUILTIN, "panic", Void_typ);
declObj(AST.BUILTIN, "print", Void_typ);
2009-01-20 15:40:40 -07:00
// scope.Print();
}