2
0
Эх сурвалжийг харах

Fixed function pointer array indexing. Fixes #41.
Set scope for function pointer variable before semanting.
Fixed preprocessor failing on unknown definitions.

woollybah 11 жил өмнө
parent
commit
8ceaadedff

+ 11 - 4
bcc.bmx

@@ -46,13 +46,20 @@ If opt_buildtype = BUILDTYPE_MODULE Then
 	End If
 	End If
 End If
 End If
 
 
-Local app:TAppDecl = ParseApp(opt_filepath)
+Local app:TAppDecl 
+Local trans:TCTranslator 
+Try
+	app = ParseApp(opt_filepath)
 
 
-app.Semant()
+	app.Semant()
 
 
-Local trans:TCTranslator = New TCTranslator
+	trans = New TCTranslator
 
 
-trans.TransApp(app)
+	trans.TransApp(app)
+Catch error:String
+	WriteStderr error
+	End
+End Try
 
 
 Local makeApp:Int = False
 Local makeApp:Int = False
 If opt_apptype Then
 If opt_apptype Then

+ 4 - 4
config.bmx

@@ -56,11 +56,11 @@ Function PopErr()
 End Function
 End Function
 
 
 Function Err( err$ )
 Function Err( err$ )
-	WriteStderr "Compile Error: "+err + "~n"
-	WriteStderr _errInfo + "~n"
+	'WriteStderr "Compile Error: "+err + "~n"
+	'WriteStderr _errInfo + "~n"
 '	Print _errInfo+" "+err
 '	Print _errInfo+" "+err
-DebugStop
-	End
+DebugStop ' useful for debugging!
+	Throw "Compile Error: "+err + "~n" + _errInfo + "~n"
 End Function
 End Function
 
 
 Function FormatError:String(path:String, line:Int, char:Int)
 Function FormatError:String(path:String, line:Int, char:Int)

+ 1 - 1
ctranslator.bmx

@@ -1550,7 +1550,7 @@ EndRem
 
 
 		If TArrayType( expr.expr.exprType ) Then
 		If TArrayType( expr.expr.exprType ) Then
 			If TFunctionPtrType(TArrayType( expr.expr.exprType ).elemType) Then
 			If TFunctionPtrType(TArrayType( expr.expr.exprType ).elemType) Then
-				Return Bra(Bra(TransType(TArrayType( expr.expr.exprType).elemType, "")) + Bra(Bra("(void**)BBARRAYDATA(" + t_expr + "," + t_expr + "->dims)") + "[" + t_index + "]"))
+				Return Bra(Bra(TransType(TArrayType( expr.expr.exprType).elemType, "*")) + Bra("BBARRAYDATA(" + t_expr + "," + t_expr + "->dims)")) + "[" + t_index + "]"
 			Else
 			Else
 				Return Bra("(" + TransType(expr.exprType, "") + "*)BBARRAYDATA(" + t_expr + "," + t_expr + "->dims)") + "[" + t_index + "]"
 				Return Bra("(" + TransType(expr.exprType, "") + "*)BBARRAYDATA(" + t_expr + "," + t_expr + "->dims)") + "[" + t_index + "]"
 			End If
 			End If

+ 57 - 9
decl.bmx

@@ -246,16 +246,24 @@ Type TValDecl Extends TDecl
 	End Method
 	End Method
 	
 	
 	Method OnSemant()
 	Method OnSemant()
+
 		If declTy
 		If declTy
-			ty=declTy.Semant()
-			
+			' ensure to set the scope for a function pointer array before semanting
+			If TArrayType(declTy) And TFunctionPtrType(TArrayType(declTy).elemType) Then
+				If Not TFunctionPtrType(TArrayType(declTy).elemType).func.scope Then
+					TFunctionPtrType(TArrayType(declTy).elemType).func.scope = scope
+				End If
+			End If
+
 			' pass the scope into the function ptr
 			' pass the scope into the function ptr
-			If TFunctionPtrType(ty) Then
-				If Not TFunctionPtrType(ty).func.scope Then
-					TFunctionPtrType(ty).func.scope = scope
+			If TFunctionPtrType(declTy) Then
+				If Not TFunctionPtrType(declTy).func.scope Then
+					TFunctionPtrType(declTy).func.scope = scope
 				End If
 				End If
 			End If
 			End If
 			
 			
+			ty=declTy.Semant()
+			
 			If Not deferInit Then
 			If Not deferInit Then
 				SemantInit()
 				SemantInit()
 			End If
 			End If
@@ -1608,6 +1616,10 @@ End Rem
 			decl.Semant()
 			decl.Semant()
 		Next
 		Next
 
 
+		For Local decl:TFieldDecl = EachIn Decls()
+			decl.Semant()
+		Next
+
 	End Method
 	End Method
 	
 	
 	'Ok, this dodgy looking beast 'resurrects' methods that may not currently be alive, but override methods that ARE.
 	'Ok, this dodgy looking beast 'resurrects' methods that may not currently be alive, but override methods that ARE.
@@ -1969,7 +1981,8 @@ Type TAppDecl Extends TScopeDecl
 	Method OnSemant()
 	Method OnSemant()
 'DebugStop		
 'DebugStop		
 		_env=Null
 		_env=Null
-pushenv Self
+		pushenv Self
+		
 		mainModule.Semant
 		mainModule.Semant
 
 
 		mainFunc=mainModule.FindFuncDecl( "LocalMain" )
 		mainFunc=mainModule.FindFuncDecl( "LocalMain" )
@@ -1978,9 +1991,7 @@ pushenv Self
 		' FIXME
 		' FIXME
 		If Not mainFunc Err "Function 'Main' not found."
 		If Not mainFunc Err "Function 'Main' not found."
 		
 		
-		'If Not TIntType( mainFunc.retType ) Or mainFunc.argDecls.Length
-		'	Err "Main function must be of type Main:Int()"
-		'EndIf
+		SemantDecls()
 
 
 		Repeat
 		Repeat
 			Local more:Int
 			Local more:Int
@@ -1995,6 +2006,43 @@ pushenv Self
 		Next
 		Next
 	End Method
 	End Method
 	
 	
+	Method SemantDecls()
+		For Local decl:TDecl=EachIn mainModule._decls
+
+			decl.Semant
+			
+			' consts
+			Local cdecl:TConstDecl=TConstDecl( decl )
+			If cdecl
+				cdecl.Semant()
+				Continue
+			End If
+
+			' classes
+			Local tdecl:TClassDecl=TClassDecl( decl )
+			If tdecl
+				tdecl.Semant()
+				tdecl.SemantParts()
+				Continue
+			EndIf
+
+			' functions
+			Local fdecl:TFuncDecl=TFuncDecl( decl )
+			If fdecl And fdecl <> _appInstance.mainFunc Then
+				fdecl.Semant()
+				Continue
+			End If
+
+			' globals
+			Local gdecl:TGlobalDecl=TGlobalDecl( decl )
+			If gdecl
+				gdecl.Semant()
+				Continue
+			End If
+		Next
+
+	End Method
+	
 	Method hasStringConst:Int(value:String)
 	Method hasStringConst:Int(value:String)
 		Return stringConsts.ValueForKey(value) <> Null
 		Return stringConsts.ValueForKey(value) <> Null
 	End Method
 	End Method

+ 6 - 3
parser.bmx

@@ -36,7 +36,7 @@ Type TForEachinStmt Extends TStmt
 	Field varlocal:Int
 	Field varlocal:Int
 	Field expr:TExpr
 	Field expr:TExpr
 	Field block:TBlockDecl
 	Field block:TBlockDecl
-
+	
 	Field stmts:TList=New TList
 	Field stmts:TList=New TList
 
 
 	Method Create:TForEachinStmt( varid$,varty:TType,varlocal:Int,expr:TExpr,block:TBlockDecl )
 	Method Create:TForEachinStmt( varid$,varty:TType,varlocal:Int,expr:TExpr,block:TBlockDecl )
@@ -3029,7 +3029,11 @@ End Rem
 			If Not toker.Toke() Exit
 			If Not toker.Toke() Exit
 
 
 			con = 0
 			con = 0
-			If Eval( toker,New TIntType ) = "1" con = 1
+			Try
+				If Eval( toker,New TIntType ) = "1" con = 1
+			Catch error:String
+				con = 0
+			End Try
 
 
 Rem
 Rem
 		Case "macos", "macosx86", "x86", "littleendian", "bigendian"
 		Case "macos", "macosx86", "x86", "littleendian", "bigendian"
@@ -3126,7 +3130,6 @@ Function ParseApp:TAppDecl( path$ )
 	Local toker:TToker=New TToker.Create( path,source )
 	Local toker:TToker=New TToker.Create( path,source )
 
 
 	Local parser:TParser=New TParser.Create( toker,app )
 	Local parser:TParser=New TParser.Create( toker,app )
-
 	parser.ParseMain
 	parser.ParseMain
 
 
 	Return app
 	Return app

+ 15 - 4
tests/framework/language/fp_arrays_01.bmx

@@ -6,23 +6,34 @@ SuperStrict
 Framework brl.standardio
 Framework brl.standardio
 
 
 Local t:TType = New TType
 Local t:TType = New TType
-t.funcArray = [func1, func2]
+t.funcArray = [func1, func2, Null]
 
 
 Print t.funcArray[0]("1", 1, 1.0)
 Print t.funcArray[0]("1", 1, 1.0)
 Print t.funcArray[1]("2", 2, 2.0)
 Print t.funcArray[1]("2", 2, 2.0)
 
 
+t.update(2, func3)
+
+Print t.funcArray[2]("3", 3, 3.0)
 
 
 Type TType
 Type TType
 
 
 	Field funcArray:String(param1:String, param2:Int, param3:Double)[]
 	Field funcArray:String(param1:String, param2:Int, param3:Double)[]
-
+	
+	Method update(pos:Int, func_:String(param1:String, param2:Int, param3:Double))
+		funcArray[pos] = func_
+	End Method
+	
 End Type
 End Type
 
 
 
 
 Function func1:String(param1:String, param2:Int, param3:Double)
 Function func1:String(param1:String, param2:Int, param3:Double)
-	Return "func1"
+	Return "func1 " + param1 + " " + param2 + " " + param3
 End Function
 End Function
 
 
 Function func2:String(param1:String, param2:Int, param3:Double)
 Function func2:String(param1:String, param2:Int, param3:Double)
-	Return "func2"
+	Return "func2 " + param1 + " " + param2 + " " + param3
+End Function
+
+Function func3:String(param1:String, param2:Int, param3:Double)
+	Return "func3 " + param1 + " " + param2 + " " + param3
 End Function
 End Function

+ 5 - 0
type.bmx

@@ -895,6 +895,11 @@ Type TFunctionPtrType Extends TType
 		Return ty
 		Return ty
 	End Method
 	End Method
 
 
+	Method Semant:TType()
+		func.Semant()
+		Return Self
+	End Method
+
 End Type
 End Type
 
 
 ' a holder during parsing which becomes the "real" var ptr type during semanting
 ' a holder during parsing which becomes the "real" var ptr type during semanting