Sfoglia il codice sorgente

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

woollybah 11 anni fa
parent
commit
8ceaadedff
7 ha cambiato i file con 99 aggiunte e 25 eliminazioni
  1. 11 4
      bcc.bmx
  2. 4 4
      config.bmx
  3. 1 1
      ctranslator.bmx
  4. 57 9
      decl.bmx
  5. 6 3
      parser.bmx
  6. 15 4
      tests/framework/language/fp_arrays_01.bmx
  7. 5 0
      type.bmx

+ 11 - 4
bcc.bmx

@@ -46,13 +46,20 @@ If opt_buildtype = BUILDTYPE_MODULE Then
 	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
 If opt_apptype Then

+ 4 - 4
config.bmx

@@ -56,11 +56,11 @@ Function PopErr()
 End Function
 
 Function Err( err$ )
-	WriteStderr "Compile Error: "+err + "~n"
-	WriteStderr _errInfo + "~n"
+	'WriteStderr "Compile Error: "+err + "~n"
+	'WriteStderr _errInfo + "~n"
 '	Print _errInfo+" "+err
-DebugStop
-	End
+DebugStop ' useful for debugging!
+	Throw "Compile Error: "+err + "~n" + _errInfo + "~n"
 End Function
 
 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 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
 				Return Bra("(" + TransType(expr.exprType, "") + "*)BBARRAYDATA(" + t_expr + "," + t_expr + "->dims)") + "[" + t_index + "]"
 			End If

+ 57 - 9
decl.bmx

@@ -246,16 +246,24 @@ Type TValDecl Extends TDecl
 	End Method
 	
 	Method OnSemant()
+
 		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
-			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
 			
+			ty=declTy.Semant()
+			
 			If Not deferInit Then
 				SemantInit()
 			End If
@@ -1608,6 +1616,10 @@ End Rem
 			decl.Semant()
 		Next
 
+		For Local decl:TFieldDecl = EachIn Decls()
+			decl.Semant()
+		Next
+
 	End Method
 	
 	'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()
 'DebugStop		
 		_env=Null
-pushenv Self
+		pushenv Self
+		
 		mainModule.Semant
 
 		mainFunc=mainModule.FindFuncDecl( "LocalMain" )
@@ -1978,9 +1991,7 @@ pushenv Self
 		' FIXME
 		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
 			Local more:Int
@@ -1995,6 +2006,43 @@ pushenv Self
 		Next
 	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)
 		Return stringConsts.ValueForKey(value) <> Null
 	End Method

+ 6 - 3
parser.bmx

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

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

@@ -6,23 +6,34 @@ SuperStrict
 Framework brl.standardio
 
 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[1]("2", 2, 2.0)
 
+t.update(2, func3)
+
+Print t.funcArray[2]("3", 3, 3.0)
 
 Type TType
 
 	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
 
 
 Function func1:String(param1:String, param2:Int, param3:Double)
-	Return "func1"
+	Return "func1 " + param1 + " " + param2 + " " + param3
 End Function
 
 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

+ 5 - 0
type.bmx

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