New fbc branch ('inheritance') on sourceforge (fbc SVN)

For other topics related to the FreeBASIC project or its community.
AGS
Posts: 1284
Joined: Sep 25, 2007 0:26
Location: the Netherlands

New fbc branch ('inheritance') on sourceforge (fbc SVN)

Postby AGS » Mar 29, 2011 0:12

There is a new fb branch at sourceforge. It's a branch aiming to implement inheritance. It's code written by v1ctor. He's committing sizeable chucks of source code to SVN.

This is more like a 'news' item but I think it's news that must not go unnoticed. I for one am very interested in the way inheritance is going to get implemented.
qbworker
Posts: 73
Joined: Jan 14, 2011 2:34

Postby qbworker » Mar 29, 2011 0:21

Finally I have lived to see the day when this would actually get worked on. v1ctor, please don't stop!
AGS
Posts: 1284
Joined: Sep 25, 2007 0:26
Location: the Netherlands

Postby AGS » Mar 29, 2011 23:58

And v1ctor is moving fast. There is an example in SVN (in the trunk) of how to use inheritance:

Code: Select all

type Foo
   declare function DoSomething() as integer
   declare function DoIt() as integer
   declare function DoItFromBase() as integer
   
   private:
   dim unused as byte
end type

   function Foo.DoSomething() as integer
      return 1
   end function

   function Foo.DoIt() as integer
      return DoSomething()
   end function

   function Foo.DoItFromBase() as integer
      return DoSomething()
   end function
   
type SuperFoo extends Foo
   declare function DoSomething() as integer
   declare function DoIt() as integer
end type

   function SuperFoo.DoSomething() as integer
      return 2
   end function

   function SuperFoo.DoIt() as integer
      return DoSomething()
   end function

sub main   
   dim as SuperFoo inst
   
   assert( inst.DoIt() = 2 )
   assert( cast( Foo, inst ).DoIt() = 1 )
   assert( inst.DoItFromBase() = 1 )
   
   print "all tests ok"
end sub

   main


As it says in the fbc svn: it's WIP. Perhaps inheritance will be done by the time the next fbc version gets released.
v1ctor
Site Admin
Posts: 3799
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Postby v1ctor » Mar 30, 2011 18:14

I think inheritance is almost done.

I've yet to write tests to add to the compiler's test suite, plus try to rebuild the compiler using the new version.

There are some warnings with -gen gcc that i've to fix.

Support for abstract methods should come next. Then we could make a new release.

Polymorphism will probably be left to be added in a future release.
angros47
Posts: 1515
Joined: Jun 21, 2005 19:04

Postby angros47 » Mar 30, 2011 19:17

Thank you, V1ctor!
Aave
Posts: 128
Joined: Jun 13, 2008 19:55
Location: Helsinki, Finland
Contact:

Postby Aave » Mar 30, 2011 19:42

Sounds great!
Pritchard
Posts: 5492
Joined: Sep 12, 2005 20:06
Location: Ohio, USA

Postby Pritchard » Mar 30, 2011 23:51

I didn't know they had steroids for brain power...

EDIT 1: Wait never mind. SuperFoo should be the base, not the derived class, for Foo. Lame.

EDIT 2: And don't try and say SuperFoo is just a l33t h4x0r extension to Foo, either. Hence, super. I'm not falling for that.
PaulSquires
Posts: 771
Joined: Jul 14, 2005 23:41
Contact:

Postby PaulSquires » Mar 31, 2011 0:41

Excellent - very nice to see more core compiler progress happening.
sir_mud
Posts: 1401
Joined: Jul 29, 2006 3:00
Location: US
Contact:

Postby sir_mud » Mar 31, 2011 6:03

In my limited testing inheritance is very nearly there, I made a simple example for testing:

Code: Select all

type Animal
        declare sub preferredAction()
        declare constructor( byval numlegs as integer = 0 )
        as integer legs
        as integer onlyAnimal
       
end type

type Dog extends Animal
        declare sub preferredAction()
        declare constructor()
       
end type

type Cat extends Animal
        declare sub preferredAction()
        declare constructor()
end type

constructor Animal( byval numlegs as integer = 0 )
        legs = numlegs
        onlyAnimal = 1
end constructor

sub Animal.preferredAction()
        print "Florble."
end sub

constructor Dog ()
        legs = 4
        onlyAnimal = 0
end constructor

sub Dog.preferredAction()
        print "Rub Belly!"
end sub

constructor Cat ()
        legs = 4
end constructor

sub Cat.preferredAction()
        print "Feed me and leave me alone."
end sub

sub animalAction( byref x as Animal )
        x.preferredAction()
end sub


scope 'main

        var d = Dog()
        var c = Cat()
       
        animalaction(d) : print d.onlyAnimal; : print d.legs;
       
        animalaction(c) : print c.onlyAnimal; : print c.legs;
       
        cast(Animal, d).preferredAction()
       
end scope
       


running that with last nights build results in the Animal class proc getting called, but it does compile and run, but polymorphism is definitely kind of borked atm. Great start though, really good to see work being done. I'll take a look at what the changes entail to see if I can help as well.

That code produces this warning from the linker as well:
Warning: .drectve `-aligncomm:"___CTOR_LIST__",2 ' unrecognized
Warning: .drectve `-aligncomm:"___DTOR_LIST__",2' unrecognized


May be an issue with latest mingw. Binutils is ver 2.21, GCC is 4.5.2
rdc
Posts: 1713
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Postby rdc » Mar 31, 2011 13:21

Thanks for the work v1ctor. This will really make many things easier to manage (like a native GUI system).
v1ctor
Site Admin
Posts: 3799
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Postby v1ctor » Mar 31, 2011 13:56

There's no polymorphism, you would have to declare the methods as virtual to that work as expected.
Pritchard
Posts: 5492
Joined: Sep 12, 2005 20:06
Location: Ohio, USA

Postby Pritchard » Mar 31, 2011 14:30

Btw, I like your guys' use of scope and a proper main procedure to enforce scope behaviors :)
relsoft
Posts: 1767
Joined: May 27, 2005 10:34
Location: Philippines
Contact:

Postby relsoft » Apr 01, 2011 5:52

Wooot!!!! Awesome news!!!
rdc
Posts: 1713
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Postby rdc » Apr 01, 2011 12:58

v1ctor wrote:There's no polymorphism, you would have to declare the methods as virtual to that work as expected.


In my GUI system I attempted I used function pointers to simulate polymorphism and it worked fine. It is not as seamless as virtual functions, but workable. I also used function pointers for the event system which worked quite well.

The real problem was that with the containment model we have now, you have to manually expose each level of containment, which gets unmanageable very quickly.
VonGodric
Posts: 997
Joined: May 27, 2005 9:06
Location: London
Contact:

Postby VonGodric » Apr 01, 2011 14:42

v1ct0r how about adding delegates like in d or c# ? would make it much easier to simulate virtual methods but also excellent for event handlers.

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 3 guests