| 
									
										
										
										
											2008-07-14 16:57:42 -07:00
										 |  |  | // 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 Globals "globals" | 
					
						
							|  |  |  | import Object "object" | 
					
						
							|  |  |  | import Type "type" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-04 15:37:47 -07:00
										 |  |  | export var ( | 
					
						
							| 
									
										
										
										
											2008-07-14 16:57:42 -07:00
										 |  |  | 	scope *Globals.Scope; | 
					
						
							| 
									
										
										
										
											2008-07-18 14:04:21 -07:00
										 |  |  | 	types *Globals.List; | 
					
						
							| 
									
										
										
										
											2008-07-14 16:57:42 -07:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	// internal types | 
					
						
							|  |  |  | 	undef_t, | 
					
						
							|  |  |  | 	bad_t, | 
					
						
							|  |  |  | 	nil_t, | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	// basic types | 
					
						
							|  |  |  | 	bool_t, | 
					
						
							|  |  |  | 	uint8_t, | 
					
						
							|  |  |  | 	uint16_t, | 
					
						
							|  |  |  | 	uint32_t, | 
					
						
							|  |  |  | 	uint64_t, | 
					
						
							|  |  |  | 	int8_t, | 
					
						
							|  |  |  | 	int16_t, | 
					
						
							|  |  |  | 	int32_t, | 
					
						
							|  |  |  | 	int64_t, | 
					
						
							|  |  |  | 	float32_t, | 
					
						
							|  |  |  | 	float64_t, | 
					
						
							|  |  |  | 	float80_t, | 
					
						
							|  |  |  | 	string_t, | 
					
						
							| 
									
										
										
										
											2008-08-05 18:52:37 -07:00
										 |  |  | 	integer_t, | 
					
						
							| 
									
										
										
										
											2008-07-14 16:57:42 -07:00
										 |  |  | 	any_t, | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	// alias types | 
					
						
							|  |  |  | 	byte_t, | 
					
						
							|  |  |  | 	ushort_t, | 
					
						
							|  |  |  | 	uint_t, | 
					
						
							|  |  |  | 	ulong_t, | 
					
						
							|  |  |  | 	short_t, | 
					
						
							|  |  |  | 	int_t, | 
					
						
							|  |  |  | 	long_t, | 
					
						
							|  |  |  | 	float_t, | 
					
						
							|  |  |  | 	double_t, | 
					
						
							|  |  |  | 	ptrint_t *Globals.Type; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	true_, | 
					
						
							|  |  |  | 	false_ *Globals.Object; | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func DeclObj(kind int, ident string, typ *Globals.Type) *Globals.Object { | 
					
						
							| 
									
										
										
										
											2008-07-15 15:37:14 -07:00
										 |  |  | 	obj := Globals.NewObject(-1 /* no source pos */, kind, ident); | 
					
						
							| 
									
										
										
										
											2008-07-14 16:57:42 -07:00
										 |  |  | 	obj.typ = typ; | 
					
						
							|  |  |  | 	if kind == Object.TYPE && typ.obj == nil { | 
					
						
							|  |  |  | 		typ.obj = obj;  // set primary type object | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	scope.Insert(obj); | 
					
						
							|  |  |  | 	return obj | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func DeclType(form int, ident string, size int) *Globals.Type { | 
					
						
							|  |  |  |   typ := Globals.NewType(form); | 
					
						
							|  |  |  |   typ.size = size; | 
					
						
							| 
									
										
										
										
											2008-07-31 10:47:10 -07:00
										 |  |  |   return DeclObj(Object.TYPE, ident, typ).typ; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func DeclAlias(ident string, typ *Globals.Type) *Globals.Type { | 
					
						
							|  |  |  | 	alias := Globals.NewType(Type.ALIAS); | 
					
						
							| 
									
										
										
										
											2008-08-05 15:20:58 -07:00
										 |  |  | 	alias.aux = typ; | 
					
						
							| 
									
										
										
										
											2008-07-31 10:47:10 -07:00
										 |  |  | 	alias.elt = typ; | 
					
						
							|  |  |  | 	return DeclObj(Object.TYPE, ident, alias).typ; | 
					
						
							| 
									
										
										
										
											2008-07-14 16:57:42 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func Register(typ *Globals.Type) *Globals.Type { | 
					
						
							| 
									
										
										
										
											2008-07-18 14:04:21 -07:00
										 |  |  | 	if types.len_ < 0 { | 
					
						
							|  |  |  | 		panic "types.len_ < 0"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	typ.ref = types.len_; | 
					
						
							|  |  |  | 	types.AddTyp(typ); | 
					
						
							| 
									
										
										
										
											2008-07-14 16:57:42 -07:00
										 |  |  | 	return typ; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-30 17:36:03 -07:00
										 |  |  | func init() { | 
					
						
							| 
									
										
										
										
											2008-07-14 16:57:42 -07:00
										 |  |  | 	scope = Globals.NewScope(nil);  // universe has no parent | 
					
						
							| 
									
										
										
										
											2008-07-18 14:04:21 -07:00
										 |  |  | 	types = Globals.NewList(); | 
					
						
							| 
									
										
										
										
											2008-07-14 16:57:42 -07:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	// Interal types | 
					
						
							|  |  |  | 	undef_t = Globals.NewType(Type.UNDEF); | 
					
						
							| 
									
										
										
										
											2008-07-31 13:35:30 -07:00
										 |  |  | 	Globals.Universe_undef_t = undef_t; | 
					
						
							| 
									
										
										
										
											2008-07-14 16:57:42 -07:00
										 |  |  | 	bad_t = Globals.NewType(Type.BAD); | 
					
						
							|  |  |  | 	nil_t = DeclType(Type.NIL, "nil", 8); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	// Basic types | 
					
						
							|  |  |  | 	bool_t = Register(DeclType(Type.BOOL, "bool", 1)); | 
					
						
							|  |  |  | 	uint8_t = Register(DeclType(Type.UINT, "uint8", 1)); | 
					
						
							|  |  |  | 	uint16_t = Register(DeclType(Type.UINT, "uint16", 2)); | 
					
						
							|  |  |  | 	uint32_t = Register(DeclType(Type.UINT, "uint32", 4)); | 
					
						
							|  |  |  | 	uint64_t = Register(DeclType(Type.UINT, "uint64", 8)); | 
					
						
							|  |  |  | 	int8_t = Register(DeclType(Type.INT, "int8", 1)); | 
					
						
							|  |  |  | 	int16_t = Register(DeclType(Type.INT, "int16", 2)); | 
					
						
							|  |  |  | 	int32_t = Register(DeclType(Type.INT, "int32", 4)); | 
					
						
							|  |  |  | 	int64_t = Register(DeclType(Type.INT, "int64", 8)); | 
					
						
							|  |  |  | 	float32_t = Register(DeclType(Type.FLOAT, "float32", 4)); | 
					
						
							|  |  |  | 	float64_t = Register(DeclType(Type.FLOAT, "float64", 8)); | 
					
						
							|  |  |  | 	float80_t = Register(DeclType(Type.FLOAT, "float80", 10)); | 
					
						
							|  |  |  | 	string_t = Register(DeclType(Type.STRING, "string", 8)); | 
					
						
							| 
									
										
										
										
											2008-08-05 18:52:37 -07:00
										 |  |  | 	integer_t = Register(DeclType(Type.INTEGER, "integer", 8)); | 
					
						
							| 
									
										
										
										
											2008-07-14 16:57:42 -07:00
										 |  |  | 	any_t = Register(DeclType(Type.ANY, "any", 8)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// All but 'byte' should be platform-dependent, eventually. | 
					
						
							| 
									
										
										
										
											2008-08-01 13:33:31 -07:00
										 |  |  | 	byte_t = Register(DeclAlias("byte", uint8_t)); | 
					
						
							|  |  |  | 	ushort_t = Register(DeclAlias("ushort", uint16_t)); | 
					
						
							|  |  |  | 	uint_t = Register(DeclAlias("uint", uint32_t)); | 
					
						
							|  |  |  | 	ulong_t = Register(DeclAlias("ulong", uint32_t)); | 
					
						
							|  |  |  | 	short_t = Register(DeclAlias("short", int16_t)); | 
					
						
							|  |  |  | 	int_t = Register(DeclAlias("int", int32_t)); | 
					
						
							|  |  |  | 	long_t = Register(DeclAlias("long", int32_t)); | 
					
						
							|  |  |  | 	float_t = Register(DeclAlias("float", float32_t)); | 
					
						
							|  |  |  | 	double_t = Register(DeclAlias("double", float64_t)); | 
					
						
							|  |  |  | 	ptrint_t = Register(DeclAlias("ptrint", uint64_t)); | 
					
						
							| 
									
										
										
										
											2008-07-14 16:57:42 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Predeclared constants | 
					
						
							|  |  |  | 	true_ = DeclObj(Object.CONST, "true", bool_t); | 
					
						
							|  |  |  | 	false_ = DeclObj(Object.CONST, "false", bool_t); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Builtin functions | 
					
						
							|  |  |  | 	DeclObj(Object.FUNC, "len", Globals.NewType(Type.FUNCTION));  // incomplete | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	// scope.Print(); | 
					
						
							|  |  |  | } |