Author Topic: FreeBasic gets inheritance  (Read 9894 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 1765
    • ScriptBasic Open Source Project
FreeBasic gets inheritance
« on: March 28, 2011, 01:17:13 PM »
Quote from: v1ctor
I added some support for inheritance. The changes are in the v0_22-inheritance branch.

It's WIP and i can't promise anything.

For now the implementation is really simple, but things like this already work:

Code: [Select]
type TObject
        public:
        declare sub thing()
      
        private:
        dim as byte unused
end type

        sub TObject.thing()
                print "TObject.thing()"
        end sub

type TBase extends TObject
        public:
        declare constructor(value as integer)
      
        public:
        declare sub thing()
      
        protected:
        dim as integer value
end type

        constructor TBase (value as integer)
                this.value = value
        end constructor

        sub TBase.thing()
                print "  TBase.thing()"
        end sub
      
type TDerived extends TBase
        public:
        declare constructor (value as integer)
        declare sub thing(value as integer)
end type

        constructor TDerived(v as integer)
                ''base(value) --- can't call base ctor yet
                base.value = v
                value = v  '' --- also works, base symbols were imported
        end constructor

        sub TDerived.thing(value as integer)
                ''thing() --- shouldn't work, same as in C++
                base.thing()
                base.base.thing()
                print "    TDerived.thing(integer)"
        end sub

sub main
        var d = TDerived(1234)
      
        d.thing 1234  '' ok, accessing the overload
      
        ''d.thing()  --- not allowed, can't access thing() because it was redefined in derived, same as in C++
      
        ''d.base.thing() --- not allowed, "base" can't be accessed outside the derived class
      
        cast( TBase, d ).thing()
end sub


        main

Update

Quote from: 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.

Update

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

Update

Quote from: v1ctor
Static data members are in the to do list but won't be added atm - at least i can't work on that, sorry.

I added the IS operator, now you can do (using sir_mud's example):

Code: [Select]
const DOG_ACTION = "rub belly"
const CAT_ACTION = "sleep"

type Animal extends Object        '' must extend Object so RTTI will be generated for this class
        dim as string name
end type

type Dog extends Animal
        declare constructor (name_ as string)
        declare function getAction as string
end type

constructor Dog (name_ as string)
        base.name = name_
end constructor

function Dog.getAction as string
        return DOG_ACTION
end function

type Cat extends Animal
        declare constructor (name_ as string)
        declare function getAction as string
end type

constructor Cat (name_ as string)
        base.name = name_
end constructor

function Cat.getAction as string
        return CAT_ACTION
end function

function getAction( a as Animal ) as string

        if( a is Dog ) then
                return cast( Dog ptr, @a )->getAction
        elseif( a is Cat ) then
                return cast( Cat ptr, @a )->getAction
        end if
       
end function

sub main
        var pluto = Dog( "Pluto" )
        var tom = Cat( "Tom" )

        assert( getAction( pluto ) = DOG_ACTION )
        assert( getAction( tom ) = CAT_ACTION )
       
        print "all tests ok"
end sub

        main


No virtual methods yet. They may be added much faster now that support for RTTI was implemented.
« Last Edit: April 03, 2011, 06:49:03 PM by ABB »

Offline John

  • Forum Support / SB Dev
  • Posts: 1765
    • ScriptBasic Open Source Project
FreeBasic 2nd Chance
« Reply #1 on: April 02, 2011, 03:15:16 PM »
I hope the FreeBASIC developers and users take this opportunity of Victor returning and extending the compiler by offering to document the compiler which Victor indicated he has no time to do.

All it takes is one person to get the ball rolling and show some leadership and others will chip in once progress is being made and it's obvious it's not just talk with no substance behind it.

The FreeBASIC project has a second chance and I hope they take advantage of it. It wouldn't hurt to have a peek at what Charles has done with OxygenBasic (in FreeBASIC) to get a few fresh ideas.



Charles

  • Guest
Re: FreeBasic gets inheritance
« Reply #2 on: April 03, 2011, 08:26:31 PM »
I found that the first and most essential step before making changes to complex software is to walk through and enter comments indicating the "intent" of the code.

When you first write a piece of software, you know what it is meant to do. But after a few weeks this vital information is forgotten and all you see before you is a jumble of expressions. This is the best time to review and annotate the code.

Charles

Offline John

  • Forum Support / SB Dev
  • Posts: 1765
    • ScriptBasic Open Source Project
Re: FreeBasic gets inheritance
« Reply #3 on: April 04, 2011, 02:27:57 AM »
I haven't looked but from what I understand, FreeBASIC is written for the most part in itself. That should make it easier to document at the code level with comments. It all depends if the developer/user base feels FreeBASIC is worth extending or not. Is the core Basic still sound under today's standards? Is there code that would have to be rewritten to make serious progress moving forward? Was the C emitter a graceful way to migrate somewhere else?

Charles

  • Guest
Re: FreeBasic gets inheritance
« Reply #4 on: April 04, 2011, 11:47:29 AM »

What I have sampled in the passed looks very orderly but there are a large number of small files - a lot of material to walk through without having prior knowledge of the compiler architecture. To do any major work on the FB compiler would take considerable stamina and determination. I think this is why it got stuck half way through the OOP retrofit. Also, once you have a large userbase, avoiding breakages of older code becomes a major concern. I'm using FreeBasic in a very simple procedural way with large amounts of inline Assembler and I am very satisfied with its stability and performance. When I'm coding the fact that I am using FreeBasic is usually below the threshold of consciousness. There must be many users like me who do not participate in the FB forum or the advancement of FB for these reasons.

Charles

Offline John

  • Forum Support / SB Dev
  • Posts: 1765
    • ScriptBasic Open Source Project
Re: FreeBasic gets inheritance
« Reply #5 on: April 11, 2011, 09:57:02 PM »
Quote
What I have sampled in the passed looks very orderly but there are a large number of small files - a lot of material to walk through without having prior knowledge of the compiler architecture.

Maybe a good place to start is take that list of files and give them a paragraph each what each does in general.

At that point you would have an idea what areas you may have the skill to contribute towards.

Based on the tone/moral of the forum, I really don't see much happening unless Victor does it. I think if someone showed true leadership and pulled everyone together, FreeBASIC may evolve into a more modern Basic compiler.

I think OxygenBasic has the best chance at taking a Basic compiler to the next level and giving the language new life.
« Last Edit: April 11, 2011, 10:50:28 PM by ABB »