Pointers et al

Discuss the Firewing language

Pointers et al

Postby johngb » Wed Feb 10, 2016 5:09 pm

In Pascal you have the ability to reference and dereference an address in memory.

In Firewing there is the AddressOf function which is sort of equivalent to @MyVariable in Pascal.
What would be the equivalent to dereferencing that address. (In Pascal I would write MyVariable^)

i.e. I have a UShort variable which actually holds the address of the variable or constant I want to access and this address could be the first location of a string or array. How do I dereference and typecast it? Is passing it ByRef to a procedure or function the only way?

JohnB
johngb
 
Posts: 19
Joined: Thu Dec 25, 2014 12:00 pm

Re: Pointers et al

Postby Jerry Messina » Wed Feb 10, 2016 7:54 pm

This is sort of related to your question before http://www.firewing.info/forum/viewtopic.php?f=7&t=257

There's no real 'pointer datatype', so put the address into the system variable 'addr0' or 'addr1' and dereference it using the '*' operator
Code: Select all
   addr1 = addressof(MyVariable)   
   myData = *(addr1)


That works for things in RAM, but won't work for ROM addresses (ie 'const' data)
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: Pointers et al

Postby johngb » Thu Feb 11, 2016 10:07 am

This is an extract of the code I am trying to develop. It relies on 3 tables each of which are linked.
Screens table contains list of screen addresses which point to an individual screen, each individual screen contains a list of objects.
Each object is an array of property values.

Code: Select all
'*************************************************************************************
' Name:   DrawObject
' Input:  Object - a reference to a Constant array of the objects properties
'         The first property is an object type used by this routine to determine
'         which Draw routine to call.
'*************************************************************************************
Sub DrawObject(ByRefConst Object As tObjectProps)       
   Select Object.ObjType
   Case 0 DrawButton(Object, State, Caption)
   Case 1 DrawImage(Object, State, Image)
   Case 2 DrawCheckBox(Object, State, Value)
   Case 3 etc...
   end Select
end Sub

'*************************************************************************************
' Name:   GetItem
' Input:  Object - a reference to a Constant array of the object's properties
'         Index  - index into the constant array
' Output: The value held in the constant array at the index
'*************************************************************************************
private function GetItem(ByRefConst Object() As UShort, Index As byte) As UShort
  Result = Object[index]
end function

'*************************************************************************************
' Name:   DrawScreen
' Input:  Index - the index into the array of screens
' Function: Draws all the objects on the screen
'*************************************************************************************
Public Sub DrawScreen(Index as byte)         ' Index into List of Screens 
  Dim idx As Byte
  Dim ObjCnt As Byte
  Dim ScrnPtr As UShort                     
  Dim ObjectPtr As UShort  '
  ScrnPtr = Screens[Index]                   ' ScrnPtr points to the required screen
  ObjCnt = GetItem(ScrnPtr, 0)               ' Get the number of objects in the screen array
  ActiveScreenColor = GetItem(ObjCnt +1)     ' Screen color is last element in the screen array   
  for idx = 1 to ObjCnt                      ' Now cycle through the list of objects on screen and
    ObjectPtr = GetItem(ScrnPtr, idx)        ' get the pointer to each Object
    DrawObject(ObjectPtr)                    ' Now call draw object passing pointer to Object properties
  next 
end Sub

'*************************************************************************************
'All these constants will be produced by the Code Generator.
'*************************************************************************************

'  Fixed strings will be held in Code Memory
Const Msg1 As String = 'OK'
Const Msg2 As String = 'Cancel'
etc...

' This is the list of all the objects used in the current project
Const MyButton(14) As UShort = 0, 78,218,78+64,219+32,clSilver,clBlue,clGray,clBlack,Arial,1,0,0,ClickLabel
Const MySecondButton(14) As UShort = 0, etc...
Const MyImage(NumImgProps) As UShort = 1, etc
Const MyCheckBox(NumCBxProps) As UShort = 2, etc

' Each screen lists pointers to Objects which should appear on screen, the last entry is background color of screen   
Const Screen0(4) As UShort = 3, MyButton, MySecondButton, MyImage, clSkyBlue
Const Screen1(3) As UShort = 2, MyImage, My Button, clWhite

' Screens comprises a list of all the screens (Pointers to the start of each screen array)
Const Screens(2) As UShort = Screen0, Screen1


This is what I have sketched out so far. I use a routine GetItem which is supposed to return the content of an element in a constant array passed by reference. If you follow the logic from DrawScreen you might be able to see where I am trying to go but I am not convinced I am getting it right.
johngb
 
Posts: 19
Joined: Thu Dec 25, 2014 12:00 pm

Re: Pointers et al

Postby Jerry Messina » Thu Feb 11, 2016 12:01 pm

Code: Select all
Const Screen0(4) As UShort = 3, MyButton, MySecondButton, MyImage, clSkyBlue

Ignoring some minor syntax issues for the moment (that's the least of your worries), I don't know of any way to get that statement to work.
AFAIK, there's no construct for getting the address of an object (rom or ram) and using that as an initializer in a const.

The definitions of Screen0(), Screen1(), and Screens() would have to be changed to ram-based arrays and filled in at runtime using 'addressof()'.

Even then I think you'll run into issues trying to use them as parameters in a call... you'll get a type mismatch since the "address variable" won't match a 'byref' or 'byrefconst' parameter declaration and there's no way to cast it to match either.

I think you'd need to have real support for a pointer datatype for this to work.
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: Pointers et al

Postby johngb » Thu Feb 11, 2016 2:18 pm

Looks like I am going to have to have a complete rethink.
johngb
 
Posts: 19
Joined: Thu Dec 25, 2014 12:00 pm


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x