ELENA-LANG / elena-lang

ELENA is a general-purpose language with late binding. It is multi-paradigm, combining features of functional and object-oriented programming. Rich set of tools are provided to deal with message dispatching : multi-methods, message qualifying, generic message handlers, run-time interfaces
https://elena-lang.github.io/
MIT License
236 stars 26 forks source link

Resolving an interface in run-time #515

Open arakov opened 4 years ago

arakov commented 4 years ago

In the following code

import extensions;

interface IFoo
{
    abstract bar() {}
}

A : interface<IFoo>
{
    bar()
    {
        console.printLine("A implementation of IFoo")
    }
}

public program()
{
    IFoo o := new A();

    o.bar()
}

A class implements an interface IFoo. Interface typecasting method is :

@method system@interface#1&mytest@$private@IFoo.#cast<'$private'IFoo>[1]
    open       1h
    pusha      
    new        class : '$inline0 1
    xsetfi     1 0
    close      
    quitn      1h
@end

It creates a proxy class $inline0 containing interface implementation method:

@method $inline0.bar[1]
    geti       0h
    movm       mssgconst : "bar[1]"
    jumpvi     0
@end

where jumpvi command is used which uses binary search to resolve a message.

A special class should be generated which resolve interface methods in run-time and directly jump to them.