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

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
AGS
Posts: 1284
Joined: Sep 25, 2007 0:26
Location: the Netherlands

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

Post by AGS »

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

Post by qbworker »

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

Post by AGS »

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: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Post by v1ctor »

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: 2324
Joined: Jun 21, 2005 19:04

Post by angros47 »

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

Post by Aave »

Sounds great!
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

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: 1002
Joined: Jul 14, 2005 23:41

Post by PaulSquires »

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

Post by sir_mud »

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: 1741
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Post by rdc »

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

Post by v1ctor »

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

Post by anonymous1337 »

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:

Post by relsoft »

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

Post by rdc »

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:

Post by VonGodric »

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.
Post Reply