Parcourir la source

Completed builtin function expressions.
Refactored Var handling code (again). Fixes #77.

woollybah il y a 10 ans
Parent
commit
6b0360d562
5 fichiers modifiés avec 155 ajouts et 156 suppressions
  1. 33 87
      ctranslator.bmx
  2. 74 64
      expr.bmx
  3. 1 1
      options.bmx
  4. 33 0
      parser.bmx
  5. 14 4
      translator.bmx

+ 33 - 87
ctranslator.bmx

@@ -139,7 +139,6 @@ Type TCTranslator Extends TTranslator
 		Local p:String = TransSPointer(ty, True)
 		
 		If TVoidType( ty ) Or Not ty Then
-'DebugStop
 			Return "void"
 		End If
 		If TBoolType( ty ) Return "BBINT" + p
@@ -157,7 +156,6 @@ Type TCTranslator Extends TTranslator
 			End If
 			Return "BBSTRING" + p
 		End If
-		'If TStringVarPtrType( ty ) Return "BBSTRING *"
 		If TArrayType( ty ) Return "BBARRAY" + p
 		If TObjectType( ty ) Then
 			Return TransObject(TObjectType(ty).classdecl) + p
@@ -289,7 +287,6 @@ Type TCTranslator Extends TTranslator
 				End If
 			End If
 			If TFunctionPtrType( ty) Return "&brl_blitz_NullFunctionError" ' todo ??
-			'If TByteType( ty ) Return "0"
 		EndIf
 		InternalErr
 	End Method
@@ -312,6 +309,10 @@ Type TCTranslator Extends TTranslator
 					If TCastExpr(args[i]) And TStringType(TCastExpr(args[i]).expr.exprType) Then
 						t:+ "&"
 					End If
+				Else If TArrayType(ty) And (ty._flags & TType.T_VAR) Then
+					If TVarExpr(args[i]) And TArrayType(TVarExpr(args[i]).exprType) Then
+						t:+ "&"
+					End If
 				Else If TObjectType(ty) And (ty._flags & TType.T_VAR) Then
 					If TVarExpr(args[i]) And TObjectType(TVarExpr(args[i]).exprType) Then
 						t:+ "&"
@@ -348,12 +349,6 @@ Type TCTranslator Extends TTranslator
 						Continue
 					End If
 
-					'If TObjectType(args[i].exprType) 'And TObjectType(args[i].exprType).classDecl = TClassDecl.nullObjectClass Then
-					'err "NULL"
-					'	t:+ "0"
-					'	Continue
-					'End If
-
 					' Object -> Byte Ptr
 					If IsPointerType(ty, TType.T_BYTE) And TObjectType(args[i].exprType) Then
 						t:+ Bra("(BBBYTE*)" + Bra(args[i].Trans())) + "+" + Bra("sizeof(void*)")
@@ -775,12 +770,6 @@ t:+"NULLNULLNULL"
 			End If
 		End If
 		
-		' built-in functions
-		Select decl.ident.ToLower()
-			Case "min", "max", "len", "asc", "chr", "sgn"
-				Return TransBuiltin(decl, args)
-		End Select
-
 		Return TransStatic( decl )+TransArgs( args,decl )
 	End Method
 
@@ -826,62 +815,6 @@ t:+"NULLNULLNULL"
 '		End If
 	End Method
 
-	Method TransBuiltin$( decl:TFuncDecl,args:TExpr[] )
-		Select decl.ident.ToLower()
-			Case "min", "max"
-				Local isFloat:Int
-				For Local arg:TExpr = EachIn args
-					If TFloatType(arg.exprType) Or TDoubleType(arg.exprType) Then
-						isFloat = True
-					End If
-				Next
-				If isFloat Then
-					Return "bbFloat" + decl.ident + TransArgsTypes(args, [New TFloatType, New TFloatType])
-				Else
-					' TODO : Long support
-					Return "bbInt" + decl.ident + TransArgs(args, decl)
-				End If
-			Case "len"
-				Local arg:TExpr = args[0]
-
-				If TStringType(arg.exprType) Then
-					Return TVarExpr(arg).decl.munged + "->length"
-				Else If TArrayType(arg.exprType) Then
-					Return TVarExpr(arg).decl.munged + "->scales[0]"
-				Else If TCastExpr(arg) Then
-					If TArrayType(TCastExpr(arg).expr.exprType) Then
-						Return TCastExpr(arg).expr.Trans() + "->scales[0]"
-					End If
-				'other types just have a length of "1"
-				Else
-					Return "1"
-				End If
-			Case "sgn"
-				Local arg:TExpr = args[0]
-				'should be handled already
-				If TConstExpr(arg) Then InternalErr
-
-				'decide whether to use float or int (cast done BEFORE
-				'sgn() is executed)
-				'bbFloatSng/bbIntSng call "Sgn#(v#)" as defined
-				'in config.bmx -> keywords
-				If TFloatType(arg.exprType) Or TDoubleType(arg.exprType)
-					'decl.ident contains "sgn", same like "bbFloatSng"
-					Return "bbFloat" + decl.ident + TransArgsTypes(args, [New TFloatType])
-				Else
-					' TODO : Long support
-					Return "bbInt" + decl.ident + TransArgs(args, decl)
-				End If
-			Case "asc"
-				Local arg:TExpr = args[0]
-				If TConstExpr(arg) InternalErr ' we should have handled this case already
-				Return "bbStringAsc" + TransArgs(args, decl)
-			Case "chr"
-				If TConstExpr(args[0]) InternalErr ' we should have handled this case already
-				Return "bbStringFromChar" + TransArgs(args, decl)
-		End Select
-	End Method
-
 	Method TransMinExpr:String(expr:TMinExpr)
 		Local s:String
 		If TDecimalType(expr.exprType) Then
@@ -891,6 +824,7 @@ t:+"NULLNULLNULL"
 		Else
 			s = "bbIntMin"
 		End If
+
 		Return s + Bra(expr.expr.trans() + "," + expr.expr2.Trans())
 	End Method
 
@@ -907,9 +841,21 @@ t:+"NULLNULLNULL"
 	End Method
 
 	Method TransAscExpr:String(expr:TAscExpr)
+		Return "bbStringAsc" + Bra(expr.expr.Trans())
+	End Method
+
+	Method TransChrExpr:String(expr:TChrExpr)
+		Return "bbStringFromChar" + Bra(expr.expr.Trans())
 	End Method
 
 	Method TransSgnExpr:String(expr:TSgnExpr)
+		If TFloatType(expr.expr.exprType) Or TDoubleType(expr.expr.exprType)
+			'decl.ident contains "sgn", same like "bbFloatSng"
+			Return "bbFloatSgn" + Bra(expr.expr.Trans())
+		Else
+			' TODO : Long support
+			Return "bbIntSgn" + Bra(expr.expr.Trans())
+		End If
 	End Method
 
 	Method TransAbsExpr:String(expr:TAbsExpr)
@@ -934,12 +880,12 @@ t:+"NULLNULLNULL"
 		End If
 		
 		If TStringType(expr.expr.exprType) Then
-			Return expr.expr.Trans() + "->length"
+			Return Bra(expr.expr.Trans()) + "->length"
 		Else If TArrayType(expr.expr.exprType) Then
-			Return expr.expr.Trans() + "->scales[0]"
+			Return Bra(expr.expr.Trans()) + "->scales[0]"
 		Else If TCastExpr(expr.expr) Then
 			If TArrayType(TCastExpr(expr.expr).expr.exprType) Then
-				Return TCastExpr(expr.expr).expr.Trans() + "->scales[0]"
+				Return Bra(TCastExpr(expr.expr).expr.Trans()) + "->scales[0]"
 			End If
 		'other types just have a length of "1"
 		Else
@@ -1145,7 +1091,7 @@ Rem
 			Else
 				Return "sizeof(void*)"
 			End If
-endrem
+EndRem
 		'class instances function calls
 		ElseIf TInvokeExpr(expr.expr) Then
 			'Throw ("Implement TInvokeMemberExpr(expr.expr)")
@@ -1296,12 +1242,12 @@ EndRem
 		If TNumericType(src) And (src._flags & TType.T_VAR) Then
 			' var number being cast to a varptr 
 			If (dst._flags & TType.T_VARPTR) Then
-				Return t
+				Return "&" + Bra(t)
 			End If
-			t = Bra("*" + t)
 		End If
 
 		If (dst._flags & TType.T_VARPTR) Or (dst._flags & TType.T_VAR) Then
+
 			If Not TConstExpr(expr.expr) Then
 				If TInvokeExpr(expr.expr) Return t
 
@@ -1454,9 +1400,9 @@ EndRem
 				End If
 				If src._flags & TType.T_VAR Then
 					If TSliceExpr( expr.expr ) Then
-						Return t
+						Return "&" + Bra(t)
 					End If
-					Return "*" + t
+					Return t
 				End If
 				Return t
 			End If
@@ -1638,9 +1584,9 @@ EndRem
 
 		If TArrayType( expr.expr.exprType ) Then
 			If TFunctionPtrType(TArrayType( expr.expr.exprType ).elemType) Then
-				Return Bra(Bra(TransType(TArrayType( expr.expr.exprType).elemType, "*")) + Bra("BBARRAYDATA(" + t_expr + "," + t_expr + "->dims)")) + "[" + t_index + "]"
+				Return Bra(Bra(TransType(TArrayType( expr.expr.exprType).elemType, "*")) + Bra("BBARRAYDATA(" + Bra(t_expr) + "," + Bra(t_expr) + "->dims)")) + "[" + t_index + "]"
 			Else
-				Return Bra("(" + TransType(expr.exprType, "") + "*)BBARRAYDATA(" + t_expr + "," + t_expr + "->dims)") + "[" + t_index + "]"
+				Return Bra("(" + TransType(expr.exprType, "") + "*)BBARRAYDATA(" + Bra(t_expr) + "," + Bra(t_expr) + "->dims)") + "[" + t_index + "]"
 			End If
 		End If
 
@@ -1665,11 +1611,11 @@ EndRem
 			t_args:+","+expr.term.Trans()
 		Else
 			If TArrayType(expr.exprType) Then
-				t_args :+ "," + t_expr + "->scales[0]"
+				t_args :+ "," + Bra(t_expr) + "->scales[0]"
 			'Else If TStringVarPtrType(expr.exprType) Then
 			'	t_args :+ ",(*" + t_expr + ")->length"
 			Else
-				t_args :+ "," + t_expr + "->length"
+				t_args :+ "," + Bra(t_expr) + "->length"
 			End If
 		End If
 
@@ -1921,10 +1867,6 @@ EndRem
 		Local rhs$=stmt.rhs.Trans()
 		Local lhs$=stmt.lhs.TransVar()
 
-		If stmt.lhs.exprType._flags & TType.T_VAR Then
-			lhs = "*" + lhs
-		End If
-
 		Local s:String
 
 '		If ObjectType( stmt.rhs.exprType )
@@ -3163,6 +3105,10 @@ End Rem
 	Method TransFieldRef:String(decl:TFieldDecl, variable:String, exprType:TType = Null)
 		Local s:String = variable
 
+		If variable.StartsWith("*") Then
+			variable = Bra(variable)
+		End If
+
 		' array.length
 		If decl.scope And decl.scope.ident = "___Array" Then
 			If decl.ident = "length" Then

+ 74 - 64
expr.bmx

@@ -483,44 +483,6 @@ Type TInvokeExpr Extends TExpr
 
 		If exprType Return Self
 
-		' handle Sgn, Asc and Chr keywords/functions for const values
-		Select decl.ident.ToLower()
-			Case "sgn"
-				Local arg:TExpr = args[0]
-				If TConstExpr(arg) Then
-					'use different calls to only return a "float sgn"
-					'when param is a float
-					Local val:String = TConstExpr(arg).value
-					Local expr:TExpr
-					If String(Int(val)) = val
-						expr = New TConstExpr.Create(New TIntType, Sgn(Int(TConstExpr(arg).value)))
-					Else
-						expr = New TConstExpr.Create(New TIntType, Sgn(Float(TConstExpr(arg).value)))
-					End If
-					
-					_appInstance.removeStringConst(TConstExpr(arg).value)
-					expr.Semant()
-					Return expr
-				End If
-			Case "asc"
-				Local arg:TExpr = args[0]
-				If TConstExpr(arg) Then
-					Local expr:TExpr = New TConstExpr.Create(New TIntType, Asc(TConstExpr(arg).value))
-					_appInstance.removeStringConst(TConstExpr(arg).value)
-					expr.Semant()
-					Return expr
-				End If
-			Case "chr"
-				Local arg:TExpr = args[0]
-				If TConstExpr(arg) Then
-					Local expr:TConstExpr = New TConstExpr.Create(New TStringType, Chr(Int(TConstExpr(arg).value)))
-					expr.Semant()
-					_appInstance.mapStringConsts(expr.value)
-					Return expr
-				End If
-		End Select
-
-
 		If Not decl.retType
 			decl.Semant()
 		End If
@@ -544,32 +506,7 @@ Type TInvokeExpr Extends TExpr
 	End Method
 
 	Method Eval$()
-		Select decl.ident.ToLower()
-			Case "sgn"
-				If args.length = 1 Then
-					'use different calls to only return a "float sgn"
-					'when param is a float
-					Local v:String = String(args[0].Eval())
-					If String(Int(v)) = v
-						Return Sgn(Int(v))
-					Else
-						Return Sgn(Float(v))
-					End If
-				End If
-				DebugStop
-			Case "asc"
-				If args.length = 1 Then
-					Local v:String = String(args[0].Eval())
-					If v And v.length = 1 Then
-						Return Asc(v)
-					End If
-				End If
-				DebugStop
-			Case "chr"
-				DebugStop
-		Default
-			Return Super.Eval()
-		End Select
+		Return Super.Eval()
 	End Method
 
 End Type
@@ -2164,6 +2101,21 @@ Type TAscExpr Extends TBuiltinExpr
 		Return Self
 	End Method
 
+	Method Semant:TExpr()
+		If exprType Return Self
+
+		If TConstExpr(expr) Then
+			Local cexpr:TExpr = New TConstExpr.Create(New TIntType, Asc(TConstExpr(expr).value))
+			_appInstance.removeStringConst(TConstExpr(expr).value)
+			cexpr.Semant()
+			Return cexpr
+		End If
+		
+		expr = expr.SemantAndCast( New TStringType )
+		exprType = New TIntType
+		Return Self
+	End Method
+
 	Method Copy:TExpr()
 		Return New TAscExpr.Create( CopyExpr(expr) )
 	End Method
@@ -2182,6 +2134,30 @@ Type TSgnExpr Extends TBuiltinExpr
 		Return Self
 	End Method
 
+	Method Semant:TExpr()
+		If exprType Return Self
+
+		If TConstExpr(expr) Then
+			'use different calls to only return a "float sgn"
+			'when param is a float
+			Local val:String = TConstExpr(expr).value
+			Local cexpr:TExpr
+			If String(Int(val)) = val
+				cexpr = New TConstExpr.Create(New TIntType, Sgn(Int(TConstExpr(expr).value)))
+			Else
+				cexpr = New TConstExpr.Create(New TFloatType, Sgn(Float(TConstExpr(expr).value)))
+			End If
+			
+			_appInstance.removeStringConst(TConstExpr(expr).value)
+			cexpr.Semant()
+			Return cexpr
+		End If
+		
+		expr = expr.Semant()
+		exprType=expr.exprType
+		Return Self
+	End Method
+
 	Method Copy:TExpr()
 		Return New TSgnExpr.Create( CopyExpr(expr) )
 	End Method
@@ -2279,6 +2255,40 @@ Type TSizeOfExpr Extends TBuiltinExpr
 
 End Type
 
+Type TChrExpr Extends TBuiltinExpr
+
+	Method Create:TChrExpr( expr:TExpr )
+		Self.id="chr"
+		Self.expr=expr
+		Return Self
+	End Method
+	
+	Method Semant:TExpr()
+		If exprType Return Self
+
+		If TConstExpr(expr) Then
+			Local cexpr:TConstExpr = New TConstExpr.Create(New TStringType, Chr(Int(TConstExpr(expr).value)))
+			cexpr.Semant()
+			_appInstance.mapStringConsts(cexpr.value)
+			Return cexpr
+		End If
+		
+		expr = expr.SemantAndCast( New TIntType )
+		exprType = New TStringType
+		Return Self
+	End Method
+
+	Method Copy:TExpr()
+		Return New TChrExpr.Create( CopyExpr(expr) )
+	End Method
+
+	Method ToString$()
+		Return "TChrExpr("+expr.ToString()+")"
+	End Method
+
+End Type
+
+
 Type TFuncCallExpr Extends TExpr
 	Field expr:TExpr
 	Field args:TExpr[]

+ 1 - 1
options.bmx

@@ -25,7 +25,7 @@ SuperStrict
 
 Import "base.configmap.bmx"
 
-Const version:String = "0.38"
+Const version:String = "0.39"
 
 Const BUILDTYPE_APP:Int = 0
 Const BUILDTYPE_MODULE:Int = 1

+ 33 - 0
parser.bmx

@@ -1011,6 +1011,39 @@ Type TParser
 			End If
 
 			expr=New TMaxExpr.Create( expr, expr2 )
+		Case "asc"
+			NextToke
+			' optional brackets
+			If CParse( "(" )
+				expr=ParseExpr()
+				Parse ")"
+				expr=New TAscExpr.Create( expr )
+			Else
+				expr=ParseExpr()
+				expr=New TAscExpr.Create( expr )
+			EndIf
+		Case "chr"
+			NextToke
+			' optional brackets
+			If CParse( "(" )
+				expr=ParseExpr()
+				Parse ")"
+				expr=New TChrExpr.Create( expr )
+			Else
+				expr=ParseExpr()
+				expr=New TChrExpr.Create( expr )
+			EndIf
+		Case "sgn"
+			NextToke
+			' optional brackets
+			If CParse( "(" )
+				expr=ParseExpr()
+				Parse ")"
+				expr=New TSgnExpr.Create( expr )
+			Else
+				expr=ParseExpr()
+				expr=New TSgnExpr.Create( expr )
+			EndIf
 		Case "string"
 			Local id$=_toke
 			Local ty:TType=ParseType()

+ 14 - 4
translator.bmx

@@ -442,9 +442,9 @@ End Rem
 	
 	Method TransSubExpr$( expr:TExpr,pri:Int=2 )
 		Local t_expr$=expr.Trans()
-		If expr.exprType._flags & TTYPE.T_VAR Then
-			t_expr = Bra("*" + t_expr)
-		End If
+		'If expr.exprType._flags & TTYPE.T_VAR Then
+		'	t_expr = Bra("*" + t_expr)
+		'End If
 		If ExprPri( expr )>pri t_expr=Bra( t_expr )
 		Return t_expr
 	End Method
@@ -598,7 +598,13 @@ End Rem
 
 		If decl.munged.StartsWith( "$" ) Return TransIntrinsicExpr( decl,Null )
 		
-		If TLocalDecl( decl ) Return decl.munged
+		If TLocalDecl( decl ) Then
+			If decl.ty._flags & TType.T_VAR Then
+				Return "*" + decl.munged
+			Else
+				Return decl.munged
+			End If
+		End If
 		
 		If TFieldDecl( decl ) Return TransField( TFieldDecl( decl ),Null )
 		
@@ -863,6 +869,7 @@ End Rem
 		If TMaxExpr(expr) Return TransMaxExpr(TMaxExpr(expr))
 		If TAbsExpr(expr) Return TransAbsExpr(TAbsExpr(expr))
 		If TAscExpr(expr) Return TransAscExpr(TAscExpr(expr))
+		If TChrExpr(expr) Return TransChrExpr(TChrExpr(expr))
 		If TSgnExpr(expr) Return TransSgnExpr(TSgnExpr(expr))
 		If TLenExpr(expr) Return TransLenExpr(TLenExpr(expr))
 		If TSizeOfExpr(expr) Return TransSizeOfExpr(TSizeOfExpr(expr))
@@ -881,6 +888,9 @@ End Rem
 	Method TransAscExpr:String(expr:TAscExpr)
 	End Method
 
+	Method TransChrExpr:String(expr:TChrExpr)
+	End Method
+
 	Method TransSgnExpr:String(expr:TSgnExpr)
 	End Method