Browse Source

Added more geninfo block types to mx2cc.

Mark Sibly 7 years ago
parent
commit
f975d943a8

+ 27 - 14
src/mx2cc/.mx2/test.monkey2

@@ -1,25 +1,38 @@
 
-#Import "<std>"
-
-#Import "test2"
-
-Using std..
-
-Function Testing:Int()
-	
-	Return 0
-End
-
 Function Main:Void()
 	
-	Local pixmap:=New Pixmap( 256,256 )
+	Local x:=0,y:=50,z:=100
 	
-	Local x:Int
-	Local y:Int
 	If x<10
 		Local t:=100
 	Else
 		Local t:=20
 	Endif
+	
+	While True
+		Local a:=10
+	Wend
+	
+	Repeat
+		Local q:=70
+	Until True
+	
+	For Local b:=0 Until 10
+		Local c:=20
+	Next
+	
+	Select x
+	Case 10
+		Local d:=30
+	Default
+		Local e:=40
+	End
+	
+	Try
+		Local f:=50
+	Catch ex:Throwable
+		Local g:=60
+	End
+
 End
 

+ 73 - 0
src/mx2cc/geninfo/geninfo.monkey2

@@ -1,6 +1,18 @@
 
 Namespace mx2.geninfo
 
+#rem
+
+Possible optimizations:
+
+* Use semType for functions.
+
+* Erase blocks with no vardecls.
+
+* Remove 'monkey.types.' prefix from primitive sem types.
+
+#end
+
 Class GeninfoGenerator
 
 	Method GenParseInfo:JsonValue( fdecl:FileDecl )
@@ -202,6 +214,21 @@ Class GeninfoGenerator
 		Local ifStmt:=Cast<IfStmtExpr>( stmt )
 		If ifStmt Return GenNode( ifStmt )
 		
+		Local whileStmt:=Cast<WhileStmtExpr>( stmt )
+		If whileStmt Return GenNode( whileStmt )
+		
+		Local repeatStmt:=Cast<RepeatStmtExpr>( stmt )
+		If repeatStmt Return GenNode( repeatStmt )
+		
+		Local selectStmt:=Cast<SelectStmtExpr>( stmt )
+		If selectStmt Return GenNode( selectStmt )
+		
+		Local forStmt:=Cast<ForStmtExpr>( stmt )
+		If forStmt Return GenNode( forStmt )
+		
+		Local tryStmt:=Cast<TryStmtExpr>( stmt )
+		If tryStmt Return GenNode( tryStmt )
+		
 		Return Null
 	End
 	
@@ -217,6 +244,52 @@ Class GeninfoGenerator
 		Return jarr
 	End
 	
+	Method GenNode:JsonValue( whileStmt:WhileStmtExpr )
+		
+		Local jobj:=MakeNode( whileStmt,"block" )
+		jobj.SetValue( "stmts",GenNode( whileStmt.stmts ) )
+		Return jobj
+	End
+	
+	Method GenNode:JsonValue( repeatStmt:RepeatStmtExpr )
+
+		Local jobj:=MakeNode( repeatStmt,"block" )
+		jobj.SetValue( "stmts",GenNode( repeatStmt.stmts ) )
+		Return jobj
+	End
+	
+	Method GenNode:JsonValue( selectStmt:SelectStmtExpr )
+		
+		Local jarr:=New JsonArray
+		For Local caseStmt:=Eachin selectStmt.cases
+			Local jobj:=MakeNode( caseStmt,"block" )
+			jobj.SetValue( "stmts",GenNode( caseStmt.stmts ) )
+			jarr.Add( jobj )
+		Next
+		Return jarr
+	End
+	
+	Method GenNode:JsonValue( forStmt:ForStmtExpr )
+		
+		Local jobj:=MakeNode( forStmt,"block" )
+		jobj.SetValue( "stmts",GenNode( forStmt.stmts ) )
+		Return jobj
+	End
+	
+	Method GenNode:JsonValue( tryStmt:TryStmtExpr )
+
+		Local jarr:=New JsonArray
+		Local jobj:=MakeNode( tryStmt,"block" )
+		jobj.SetValue( "stmts",GenNode( tryStmt.stmts ) )
+		jarr.Add( jobj )
+		For Local catchStmt:=Eachin tryStmt.catches
+			Local jobj:=MakeNode( catchStmt,"block" )
+			jobj.SetValue( "stmts",GenNode( catchStmt.stmts ) )
+			jarr.Add( jobj )
+		Next
+		Return jarr
+	End
+	
 	'Expressions...
 	'
 	Method MakeNode:JsonObject( expr:Expr,kind:String )

+ 16 - 8
src/mx2cc/parser.monkey2

@@ -1063,9 +1063,13 @@ Class Parser
 			SkipToNextLine()
 		End
 		
-		Local cases:=New Stack<CaseExpr>
+		Local cases:=New Stack<CaseStmtExpr>
 		
-		While CParse( "case" )
+		While Toke="case"
+			
+			Local srcpos:=SrcPos
+			
+			Bump()
 
 			Local exprs:=New Stack<Expr>
 
@@ -1084,18 +1088,22 @@ Class Parser
 			
 			Local stmts:=ParseStmts( True )
 			
-			cases.Push( New CaseExpr( exprs.ToArray(),stmts ) )
+			cases.Push( New CaseStmtExpr( exprs.ToArray(),stmts,srcpos,SrcPos ) )
 		Wend
 		
 		If cases.Empty ErrorNx( "Select statement must have at least one case" )
-		
-		If CParse( "default" )
+			
+		If Toke="default"
+			
+			Local srcpos:=SrcPos
+			
+			Bump()
 		
 			CParseEol()
 		
 			Local stmts:=ParseStmts( True )
 			
-			cases.Push( New CaseExpr( Null,stmts ) )
+			cases.Push( New CaseStmtExpr( Null,stmts,srcpos,SrcPos ) )
 		Endif
 		
 		Try
@@ -1126,7 +1134,7 @@ Class Parser
 		
 		Local stmts:=ParseStmts( True )
 		
-		Local catches:=New Stack<CatchExpr>
+		Local catches:=New Stack<CatchStmtExpr>
 		
 		While Toke="catch"
 		
@@ -1151,7 +1159,7 @@ Class Parser
 			
 			Local stmts:=ParseStmts( True )
 			
-			catches.Push( New CatchExpr( varIdent,varType,stmts ) )
+			catches.Push( New CatchStmtExpr( varIdent,varType,stmts,srcpos,SrcPos ) )
 		Wend
 		
 		Try

+ 12 - 9
src/mx2cc/stmtexpr.monkey2

@@ -499,12 +499,14 @@ Class RepeatStmtExpr Extends StmtExpr
 
 End
 
-Class CaseExpr
-
+Class CaseStmtExpr Extends StmtExpr
+	
 	Field exprs:Expr[]
 	Field stmts:StmtExpr[]
 	
-	Method New( exprs:Expr[],stmts:StmtExpr[] )
+	Method New( exprs:Expr[],stmts:StmtExpr[],srcpos:Int,endpos:Int )
+		Super.New( srcpos,endpos )
+		
 		Self.exprs=exprs
 		Self.stmts=stmts
 	End
@@ -514,9 +516,9 @@ End
 Class SelectStmtExpr Extends StmtExpr
 
 	Field expr:Expr
-	Field cases:CaseExpr[]
+	Field cases:CaseStmtExpr[]
 	
-	Method New( expr:Expr,cases:CaseExpr[],srcpos:Int,endpos:Int )
+	Method New( expr:Expr,cases:CaseStmtExpr[],srcpos:Int,endpos:Int )
 		Super.New( srcpos,endpos )
 		
 		Self.expr=expr
@@ -777,13 +779,14 @@ Class ForStmtExpr Extends StmtExpr
 	
 End
 
-Class CatchExpr
+Class CatchStmtExpr Extends StmtExpr
 
 	Field varIdent:String
 	Field varType:Expr
 	Field stmts:StmtExpr[]
 
-	Method New( varIdent:String,varType:Expr,stmts:StmtExpr[] )
+	Method New( varIdent:String,varType:Expr,stmts:StmtExpr[],srcpos:Int,endpos:Int )
+		Super.New( srcpos,endpos )
 		Self.varIdent=varIdent
 		Self.varType=varType
 		Self.stmts=stmts
@@ -794,9 +797,9 @@ End
 Class TryStmtExpr Extends StmtExpr
 
 	Field stmts:StmtExpr[]
-	Field catches:CatchExpr[]
+	Field catches:CatchStmtExpr[]
 	
-	Method New( stmts:StmtExpr[],catches:CatchExpr[],srcpos:Int,endpos:Int )
+	Method New( stmts:StmtExpr[],catches:CatchStmtExpr[],srcpos:Int,endpos:Int )
 		Super.New( srcpos,endpos )
 		Self.stmts=stmts
 		Self.catches=catches

+ 3 - 7
src/mx2cc/test.monkey2

@@ -1,12 +1,8 @@
 
-Function Test:Int()
-	
-	Return 0
-End
-
 Function Main:Void()
-	Local x:Int
-	Local y:Int
+	
+	Local x:=0
+	
 	If x<10
 		Local t:=100
 	Else