Sfoglia il codice sorgente

Fixed some 2d array issues, added Array.Size( dimension ) method.

blitz-research 9 anni fa
parent
commit
5c7daf700d

+ 1 - 0
modules/monkey/native/bbarray.h

@@ -84,6 +84,7 @@ template<class T,int D> class bbArray : public bbGCNode{
 	}
 	
 	int size( int q )const{
+		bbDebugAssert( q<D );
 		return this ? (q ? _sizes[q]/_sizes[q-1] : _sizes[0]) : 0;
 	}
 	

+ 4 - 0
modules/monkey/native/bbmonkey.cpp

@@ -114,6 +114,10 @@ int main( int argc,char **argv ){
 			printf( "%s\n",ex->debugStack()->at( i ).c_str() );
 		}
 
+	}catch( bbThrowable *t ){
+	
+		printf( "\n***** Uncaught Monkey 2 Throwable *****\n\n" );
+
 	}catch(...){
 	
 		printf( "***** Uncaught Native Exception *****\n" );fflush( stdout );

+ 2 - 2
modules/monkey/types.monkey2

@@ -1,8 +1,6 @@
 
 Namespace monkey.types
 
-Alias Exception:Throwable
-
 Extern
 
 Interface INumeric
@@ -145,6 +143,8 @@ Struct @Array<T>
 	
 	Property Length:Int()="length"
 	
+	Method Size:Int( dimension:Int )="size"
+	
 	Method Slice:T[]( from:Int )="slice"
 	
 	Method Slice:T[]( from:Int,term:Int )="slice" 

+ 0 - 233
modules/std/std.json

@@ -1,233 +0,0 @@
-{
-  "module":{
-    "name":"std"
-    ,"namespaces":[
-      {
-        "ident":"time"
-        ,"classes":[
-          {
-            "ident":"Time"
-            ,"docs":"<p>DateTime class.</p>\n"
-            ,"flags":1
-            ,"superType":"monkey.Object"
-            ,"constants":[
-              {
-                "ident":"_days"
-                ,"flags":2
-                ,"type":"monkey.String[]"
-              }
-              ,{
-                "ident":"_months"
-                ,"flags":2
-                ,"type":"monkey.String[]"
-              }
-            ]
-            ,"fields":[
-              {
-                "ident":"_timer"
-                ,"flags":2
-                ,"type":"libc.time_t"
-              }
-              ,{
-                "ident":"_tm"
-                ,"flags":2
-                ,"type":"libc.tm_t"
-              }
-            ]
-            ,"constructors":[
-              {
-                "ident":"new"
-                ,"overloads":[
-                  {
-                    "params":[
-                      {
-                        "ident":"timer"
-                        ,"flags":1
-                        ,"type":"libc.time_t"
-                      }
-                      ,{
-                        "ident":"tm"
-                        ,"flags":1
-                        ,"type":"libc.tm_t Ptr"
-                      }
-                    ]
-                  }
-                ]
-              }
-            ]
-            ,"properties":[
-              {
-                "ident":"Day"
-                ,"docs":"<p>Day of the month (1-31)</p>\n"
-                ,"flags":1
-                ,"type":"monkey.Int"
-              }
-              ,{
-                "ident":"DaylightSavings"
-                ,"docs":"<p>True if daylight savings is in effect.</p>\n"
-                ,"flags":1
-                ,"type":"monkey.Bool"
-              }
-              ,{
-                "ident":"Hours"
-                ,"docs":"<p>Hours since midnight (0-23)</p>\n"
-                ,"flags":1
-                ,"type":"monkey.Int"
-              }
-              ,{
-                "ident":"Minutes"
-                ,"docs":"<p>Minutes (0-59)</p>\n"
-                ,"flags":1
-                ,"type":"monkey.Int"
-              }
-              ,{
-                "ident":"Month"
-                ,"docs":"<p>Month since January (0-11)</p>\n"
-                ,"flags":1
-                ,"type":"monkey.Int"
-              }
-              ,{
-                "ident":"Seconds"
-                ,"docs":"<p>Seconds (0-61)</p>\n\n<pre><code>May include &#39;leap&#39; seconds.\n</code></pre>\n"
-                ,"flags":1
-                ,"type":"monkey.Int"
-              }
-              ,{
-                "ident":"WeekDay"
-                ,"docs":"<p>Week day since Sunday (0-6)</p>\n"
-                ,"flags":1
-                ,"type":"monkey.Int"
-              }
-              ,{
-                "ident":"Year"
-                ,"docs":"<p>Year</p>\n"
-                ,"flags":1
-                ,"type":"monkey.Int"
-              }
-              ,{
-                "ident":"YearDay"
-                ,"docs":"<p>Days since January 1 (0-365)</p>\n"
-                ,"flags":1
-                ,"type":"monkey.Int"
-              }
-            ]
-            ,"operators":[
-              {
-                "ident":"<=>"
-                ,"docs":"<p>Overloaded compare operator.</p>\n\n<pre><code>Time x is &#39;less than&#39; time y if time x represents a time &#39;earlier&#39; than time y.\n</code></pre>\n"
-                ,"overloads":[
-                  {
-                    "return":{
-                      "type":"monkey.Int"
-                    }
-                    ,"params":[
-                      {
-                        "ident":"time"
-                        ,"flags":1
-                        ,"type":"std.time.Time"
-                      }
-                    ]
-                  }
-                ]
-              }
-            ]
-            ,"methods":[
-              {
-                "ident":"<=>"
-                ,"docs":"<p>Overloaded compare operator.</p>\n\n<pre><code>Time x is &#39;less than&#39; time y if time x represents a time &#39;earlier&#39; than time y.\n</code></pre>\n"
-                ,"overloads":[
-                  {
-                    "return":{
-                      "type":"monkey.Int"
-                    }
-                    ,"params":[
-                      {
-                        "ident":"time"
-                        ,"flags":1
-                        ,"type":"std.time.Time"
-                      }
-                    ]
-                  }
-                ]
-              }
-              ,{
-                "ident":"Day"
-                ,"overloads":[
-                  {
-                    "return":{
-                      "type":"monkey.Int"
-                    }
-                  }
-                ]
-              }
-              ,{
-                "ident":"DaylightSavings"
-                ,"overloads":[
-                  {
-                    "return":{
-                      "type":"monkey.Bool"
-                    }
-                  }
-                ]
-              }
-              ,{
-                "ident":"Hours"
-                ,"overloads":[
-                  {
-                    "return":{
-                      "type":"monkey.Int"
-                    }
-                  }
-                ]
-              }
-              ,{
-                "ident":"Minutes"
-                ,"overloads":[
-                  {
-                    "return":{
-                      "type":"monkey.Int"
-                    }
-                  }
-                ]
-              }
-              ,{
-                "ident":"Month"
-                ,"overloads":[
-                  {
-                    "return":{
-                      "type":"monkey.Int"
-                    }
-                  }
-                ]
-              }
-            ]
-          }
-        ]
-        ,"functions":[
-          {
-            "ident":"Millisecs"
-            ,"docs":"<p>Gets the number of milliseconds since the app started.</p>\n"
-            ,"overloads":[
-              {
-                "return":{
-                  "type":"monkey.Long"
-                }
-              }
-            ]
-          }
-          ,{
-            "ident":"Seconds"
-            ,"docs":"<p>Gets the number of seconds since the app started.</p>\n"
-            ,"overloads":[
-              {
-                "return":{
-                  "type":"monkey.Double"
-                }
-              }
-            ]
-          }
-        ]
-      }
-    ]
-  }
-}

+ 4 - 0
src/mx2new/builder.monkey2

@@ -683,6 +683,8 @@ Class Builder
 		Type.WStringClass=Cast<ClassType>( types.nodes["WString"] )
 		Type.Utf8StringClass=Cast<ClassType>( types.nodes["Utf8String"] )
 
+		Type.ExceptionClass=Cast<ClassType>( types.nodes["@Exception"] )
+
 		rootNamespace.Insert( "void",Type.VoidType )
 		rootNamespace.Insert( "bool",Type.BoolType )
 		rootNamespace.Insert( "byte",Type.ByteType )
@@ -703,6 +705,8 @@ Class Builder
 		rootNamespace.Insert( "WString",Type.WStringClass )
 		rootNamespace.Insert( "Utf8String",Type.Utf8StringClass )
 		
+		rootNamespace.Insert( "Exception",Type.ExceptionClass )
+		
 		Type.BoolType.Semant()
 		Type.ByteType.Semant()
 		Type.UByteType.Semant()

+ 23 - 19
src/mx2new/expr.monkey2

@@ -151,7 +151,7 @@ Class IdentExpr Extends Expr
 	Method OnSemant:Value( scope:Scope ) Override
 	
 		Local value:=scope.FindValue( ident )
-		If Not value Throw New IdentEx( ident )
+		If Not value Throw New SemantEx( "Identifier '"+ident+"' not found" )
 		
 		Return value
 	End
@@ -187,7 +187,7 @@ Class MemberExpr Extends Expr
 		Local value:=expr.SemantRValue( scope )
 		
 		Local tvalue:=value.FindValue( ident )
-		If Not tvalue Throw New SemantEx( "Value of type '"+value.type.ToString()+"' has no member named '"+ident+"'" )
+		If Not tvalue Throw New SemantEx( "Value of type '"+value.type.Name+"' has no member named '"+ident+"'" )
 '		If Not tvalue Throw New IdentEx( ident )
 		
 		Return tvalue
@@ -198,7 +198,7 @@ Class MemberExpr Extends Expr
 		Local type:=expr.SemantType( scope )
 		
 		Local type2:=type.FindType( ident )
-		If Not type2 Throw New SemantEx( "Identifier '"+ident+"' not found in type '"+type.Name+"'" )
+		If Not type2 Throw New SemantEx( "Type '"+type.Name+"' has no member type named '"+ident+"'" )
 		
 		Return type2
 	End
@@ -296,12 +296,12 @@ Class NewObjectExpr Extends Expr
 		Local type:=Self.type.Semant( scope )
 		
 		Local ctype:=Cast<ClassType>( type )
-		If Not ctype Throw New SemantEx( "Type '"+type.ToString()+"' is not a class" )
+		If Not ctype Throw New SemantEx( "Type '"+type.Name+"' is not a class" )
 		
 		'hmmm...
 		'ctype.SemantMembers()
 		
-		If ctype.IsGeneric Throw New SemantEx( "Class '"+ctype.ToString()+"' is generic" )
+		If ctype.IsGeneric Throw New SemantEx( "Class '"+ctype.Name+"' is generic" )
 		
 		If ctype.IsAbstract
 			Local t:=""
@@ -309,8 +309,8 @@ Class NewObjectExpr Extends Expr
 				If t t+=","
 				t+=func.ToString()
 			Next
-			If t Throw New SemantEx( "Class '"+ctype.ToString()+"' is abstract due to unimplemented method(s) "+t )
-			Throw New SemantEx( "Class '"+ctype.ToString()+"' is abstract" )
+			If t Throw New SemantEx( "Class '"+ctype.Name+"' is abstract due to unimplemented method(s) "+t )
+			Throw New SemantEx( "Class '"+ctype.Name+"' is abstract" )
 		Endif
 		
 		Local args:=SemantArgs( Self.args,scope )
@@ -323,7 +323,7 @@ Class NewObjectExpr Extends Expr
 
 			Local invoke:=Cast<InvokeValue>( ctor.ToValue( Null ).Invoke( args ) )
 			
-			If Not invoke Throw New SemantEx( "Can't invoke class '"+ctype.ToString()+"' constuctor with arguments '"+Join( args )+"'" )
+			If Not invoke Throw New SemantEx( "Can't invoke class '"+ctype.Name+"' constuctor with arguments '"+Join( args )+"'" )
 			
 			ctorFunc=Cast<FuncValue>( invoke.value )
 			If Not ctorFunc SemantError( "NewObjectExpr.OnSemant()" )
@@ -332,7 +332,7 @@ Class NewObjectExpr Extends Expr
 
 		Else If args
 		
-			Throw New SemantEx( "Class '"+type.ToString()+"' has no constructors" )
+			Throw New SemantEx( "Class '"+type.Name+"' has no constructors" )
 			
 		Endif
 		
@@ -364,7 +364,7 @@ Class NewArrayExpr Extends Expr
 		Local atype:=Cast<ArrayType>( type.Semant( scope ) )
 		If Not atype SemantError( "NewArrayExpr.OnSemant()" )
 		
-		If atype.elemType.IsGeneric Throw New SemantEx( "Array element type '"+atype.elemType.ToString()+"' is generic" )
+		If atype.elemType.IsGeneric Throw New SemantEx( "Array element type '"+atype.elemType.Name+"' is generic" )
 		
 		Local sizes:Value[],inits:Value[]
 		If Self.inits
@@ -426,7 +426,9 @@ Class ExtendsExpr Extends Expr
 	Method OnSemant:Value( scope:Scope ) Override
 	
 		Local ctype:=Cast<ClassType>( Self.type.Semant( scope ) )
-		If Not ctype Or (ctype.cdecl.kind<>"class" And ctype.cdecl.kind<>"interface" And ctype.cdecl.kind<>"protocol" ) Throw New SemantEx( "Type must be a class or interface '"+type.ToString()+"'" )
+		If Not ctype Or (ctype.cdecl.kind<>"class" And ctype.cdecl.kind<>"interface" And ctype.cdecl.kind<>"protocol" ) 
+			Throw New SemantEx( "Type '"+type.ToString()+"' is not a class or interface type" )
+		Endif
 		
 		Local value:=Self.expr.SemantRValue( scope )
 
@@ -450,7 +452,9 @@ Class ExtendsExpr Extends Expr
 	Method OnSemantWhere:Bool( scope:Scope ) Override
 	
 		Local ctype:=Cast<ClassType>( Self.type.Semant( scope ) )
-		If Not ctype Or (ctype.cdecl.kind<>"class" And ctype.cdecl.kind<>"interface" And ctype.cdecl.kind<>"protocol" ) Throw New SemantEx( "Type must be a class or interface '"+type.ToString()+"'" )
+		If Not ctype Or (ctype.cdecl.kind<>"class" And ctype.cdecl.kind<>"interface" And ctype.cdecl.kind<>"protocol" ) 
+			Throw New SemantEx( "Type '"+type.ToString()+"' is not a class or interface type" )
+		endif
 		
 		Local type:=Self.expr.SemantType( scope )
 
@@ -479,15 +483,15 @@ Class CastExpr Extends Expr
 		Local type:=Self.type.Semant( scope )
 		
 		Local value:=Self.expr.Semant( scope )
-		
+
+		'simple upcast?		
 		If value.type.DistanceToType( type )>=0 Return value.UpCast( type )
-		
-		value=value.ToRValue()
 
-'		Local value:=Self.expr.SemantRValue( scope )
+		'nope...		
+		value=value.ToRValue()
 		
 		If Not value.type.CanCastToType( type ) 
-			Throw New SemantEx( "Value of type '"+value.type.ToString()+"' cannot be cast to type '"+type.ToString()+"'" )
+			Throw New SemantEx( "Value of type '"+value.type.Name+"' cannot be cast to type '"+type.Name+"'" )
 		Endif
 		
 		Return New ExplicitCastValue( type,value )
@@ -537,7 +541,7 @@ Class SuperExpr Extends Expr
 				Local superType:=ctype.superType
 				If superType Return New SuperValue( superType )
 
-				Throw New SemantEx( "Class '"+ctype.ToString()+"' has no super class" )
+				Throw New SemantEx( "Class '"+ctype.Name+"' has no super class" )
 
 			Endif
 		Endif
@@ -684,7 +688,7 @@ Class BinaryopExpr Extends Expr
 		If lhs.DistanceToType( rhs )>=0 Return rhs		'And rhs.DistanceToType( lhs )<=0 Return rhs
 		If rhs.DistanceToType( lhs )>=0 Return lhs		'And lhs.DistanceToType( rhs )<=0 Return lhs
 		
-		Throw New SemantEx( "Types '"+lhs.ToString()+"' and '"+rhs.ToString()+"' are incompatible" )
+		Throw New SemantEx( "Types '"+lhs.Name+"' and '"+rhs.Name+"' are incompatible" )
 		
 		Return Null
 	End

+ 1 - 1
src/mx2new/parser.monkey2

@@ -1307,7 +1307,7 @@ Class Parser
 			If CParse( "[" )
 				Local sizes:=ParseExprs()
 				Parse( "]" )
-				Return New NewArrayExpr( New ArrayTypeExpr( type,1,srcpos,EndPos ),sizes,Null,srcpos,EndPos )
+				Return New NewArrayExpr( New ArrayTypeExpr( type,sizes.Length,srcpos,EndPos ),sizes,Null,srcpos,EndPos )
 			Endif
 			
 			If Toke="("

+ 2 - 0
src/mx2new/type.monkey2

@@ -31,6 +31,8 @@ Class Type Extends SNode
 	Global WStringClass:ClassType
 	Global Utf8StringClass:ClassType
 	
+	Global ExceptionClass:ClassType
+	
 	Field flags:Int
 	
 	Property IsGeneric:Bool()

+ 47 - 0
tests/misc/loadmap2d.monkey2

@@ -0,0 +1,47 @@
+
+#Import "<std>"
+
+#Import "map2d.json"
+
+Using std.json
+Using std.stringio
+
+Function LoadMap2d:Int[,]( path:String )
+
+	Local src:=LoadString( "asset::map2d.json" )
+	
+	Local json:=New JsonParser( src ).ParseJson()
+	
+	Local w:=json["width"].ToInt()
+	Local h:=json["height"].ToInt()
+	
+	Local map:=New Int[w,h]
+	
+	For Local y:=0 Until h
+		For Local x:=0 Until w
+			map[x,y]=json["data"][y][x].ToInt()
+		Next
+	Next
+	
+	Return map
+End
+
+Function Main()
+
+	Local map:=LoadMap2d( "asset::map2d.json" )
+	
+	Local w:=map.Size( 0 )
+	Local h:=map.Size( 1 )
+	
+	Print "w="+w
+	Print "h="+h
+
+	For Local y:=0 Until h
+		Local str:=""
+		For Local x:=0 Until w
+			str+=","+map[x,y]
+		Next
+		Print str.Slice( 1 )
+	Next
+
+End

+ 16 - 0
tests/misc/map2d.json

@@ -0,0 +1,16 @@
+{
+	"width":10,
+	"height":10,
+	"data":[
+		[1,2,3,4,5,6,7,8,9,10],
+		[2,3,4,5,6,7,8,9,10,1],
+		[3,4,5,6,7,8,9,10,1,2],
+		[4,5,6,7,8,9,10,1,2,3],
+		[5,6,7,8,9,10,1,2,3,4],
+		[6,7,8,9,10,1,2,3,4,5],
+		[7,8,9,10,1,2,3,4,5,6],
+		[8,9,10,1,2,3,4,5,6,7],
+		[9,10,1,2,3,4,5,6,7,8],
+		[10,1,2,3,4,5,6,7,8,9]
+	]
+}