Ver Fonte

Added String to Byte Ptr generation.
Improved iParser parsing of .i files, with initial values.
Fixed generation of functions with optional args.

woollybah há 11 anos atrás
pai
commit
ce46857334
4 ficheiros alterados com 69 adições e 23 exclusões
  1. 26 3
      ctranslator.bmx
  2. 7 6
      expr.bmx
  3. 28 13
      iparser.bmx
  4. 8 1
      translator.bmx

+ 26 - 3
ctranslator.bmx

@@ -126,10 +126,19 @@ Type TCTranslator Extends TTranslator
 		If objParam Then
 			t:+ objParam
 		End If
-		For Local i:Int=0 Until args.Length
+		For Local i:Int=0 Until decl.argDecls.Length
 			If t t:+","
-			t:+TransTemplateCast( TArgDecl(decl.argDecls[i].actual).ty,args[i].exprType,args[i].Trans() )
+			If i < args.length
+				t:+TransTemplateCast( TArgDecl(decl.argDecls[i].actual).ty,args[i].exprType,args[i].Trans() )
+			Else
+				decl.argDecls[i].Semant()
+				' default values
+				If decl.argDecls[i].init Then
+					t:+ decl.argDecls[i].init.Trans()
+				End If
+			End If
 		Next
+		
 		Return Bra(t)
 	End Method
 
@@ -196,6 +205,14 @@ Type TCTranslator Extends TTranslator
 	Method TransLocalDecl$( munged$,init:TExpr )
 		Return TransType( init.exprType, munged )+" "+munged+"="+init.Trans()
 	End Method
+
+	Method CreateLocal2$( ty:TType, t$ )
+		Local tmp:TLocalDecl=New TLocalDecl.Create( "", ty,Null )
+		MungDecl tmp
+		Emit TransType(ty, "") + " " + tmp.munged + " = bbStringToCString" + Bra(t)+ ";"
+		customVarStack.Push(tmp.munged)
+		Return tmp.munged
+	End Method
 	
 	Method EmitPushErr()
 		Emit "pushErr();"
@@ -301,7 +318,7 @@ Type TCTranslator Extends TTranslator
 			Case "min", "max", "len", "asc", "chr"
 				Return TransBuiltin(decl, args)
 		End Select
-'If decl.ident = "stat_" DebugStop
+'If decl.ident = "Encode" DebugStop
 		Return TransStatic( decl )+TransArgs( args,decl )
 	End Method
 	
@@ -467,6 +484,12 @@ Type TCTranslator Extends TTranslator
 				Return Bra(Bra(TransType(dst, "")) + "BBARRAYDATA(" + t + "," + t + "->dims)")
 			End If
 			If TByteType( src) Return Bra("&"+t)
+			
+			If TStringType(src) Then
+				Local tmp:String = CreateLocal2(TType.bytePointerType, t)
+				
+				Return tmp
+			End If
 		EndIf
 		
 		Return TransPtrCast( dst,src,t,"dynamic" )

+ 7 - 6
expr.bmx

@@ -737,13 +737,14 @@ Type TCastExpr Extends TExpr
 					exprType = ty
 				End If
 				Return Self
-			Else
-				If TArrayType(src) Then
-					If TNumericType(TArrayType(src).elemType) Then
-						exprType = TNumericType(TArrayType(src).elemType).ToPointer()
-						Return Self
-					End If
+			Else If TArrayType(src) Then
+				If TNumericType(TArrayType(src).elemType) Then
+					exprType = TNumericType(TArrayType(src).elemType).ToPointer()
+					Return Self
 				End If
+			Else If TStringType(src) Then
+				exprType = ty
+				Return Self
 			End If
 		End If
 		

+ 28 - 13
iparser.bmx

@@ -719,9 +719,15 @@ Method ParseFuncDecl:TFuncDecl( toke$,attrs:Int )
 	'		init=ParseExpr()
 		Else
 			ty=ParseDeclType(attrs)
+Rem
 			If CParse( "=" )
 				' TODO init=ParseExpr()
-				init = ParseUnaryExpr()
+				If CParse("$") Then
+					' string value
+					init = ParseUnaryExpr()
+				Else
+					init = ParseUnaryExpr()
+				End If
 				'DebugLog "TODO : ParseExpression"
 			Else If CParse( "[" )
 				'Local ln:TExpr=ParseExpr()
@@ -736,6 +742,7 @@ Method ParseFuncDecl:TFuncDecl( toke$,attrs:Int )
 			Else
 				Err "Constants must be initialized."
 			EndIf
+End Rem
 		EndIf
 		
 		Local decl:TValDecl
@@ -749,8 +756,8 @@ Method ParseFuncDecl:TFuncDecl( toke$,attrs:Int )
 		Else If attrs & DECL_LOCAL
 			decl=New TLocalDecl.Create( id,ty,init,attrs )
 		EndIf
-		
-		If decl.IsExtern() 
+'DebugStop
+'		If decl.IsExtern() 
 			If CParse( "=" )
 				'decl.munged=ParseStringLit()
 				
@@ -792,21 +799,29 @@ Method ParseFuncDecl:TFuncDecl( toke$,attrs:Int )
 						EndIf
 					End If
 				Else
+'					init = ParseUnaryExpr()
+					
 					If ty = TType.stringType
-						decl.munged=ParseStringLit()
+						If CParse("$") Then
+					init = ParseUnaryExpr()
+'							decl.init=New TConstExpr.Create(ty, ParseStringLit())
+						End If
 					Else
 						' a default value ?
 'DebugStop
-						Local value:String
-						'_toker.NextToke()
+					decl.init = ParseUnaryExpr()
+'					Local value:String
 						
-						If CParse("-") Then
-							value = "-"
-							_toker.NextToke()
-						End If
+						'_toker.NextToke()
 						
-						decl.init = New TConstExpr.Create(ty, value + _toker._toke)
+'						If CParse("-") Then
+'DebugStop
+'							value = "-"
+'							_toker.NextToke()
+'						End If
 						
+'						decl.init = New TConstExpr.Create(ty, value + _toker._toke)
+'						_toker.NextToke()
 					End If
 				End If
 				
@@ -815,8 +830,8 @@ Method ParseFuncDecl:TFuncDecl( toke$,attrs:Int )
 			Else
 				decl.munged=decl.ident
 			EndIf
-		EndIf
-	
+'		EndIf
+
 		Return decl
 	End Method
 

+ 8 - 1
translator.bmx

@@ -15,6 +15,7 @@ Type TTranslator
 	Field mungScope:TMap=New TMap'<TDecl>
 	Field mungStack:TStack=New TStack'< StringMap<TDecl> >
 	Field funcMungs:TMap=New TMap'<FuncDeclList>
+	Field customVarStack:TStack = New TStack
 
 	Field mungedScopes:TMap=New TMap'<StringSet>
 '	Field funcMungs:=New StringMap<FuncDeclList>
@@ -317,7 +318,7 @@ End Rem
 		Emit TransLocalDecl( tmp.munged,expr )+";"
 		Return tmp.munged
 	End Method
-	
+
 	'***** Utility *****
 
 	Method TransLocalDecl$( munged$,init:TExpr ) Abstract
@@ -578,6 +579,12 @@ End Rem
 			Local t$=stmt.Trans()
 			If t Emit t+";"
 			
+			Local v:String = String(customVarStack.Pop())
+			While v
+				Emit "bbMemFree" + Bra(v) + ";"
+				v = String(customVarStack.Pop())
+			Wend
+			
 		Next
 		Local r:Int=unreachable
 		unreachable=False