在运行时有效表示类属性

时间:2015-01-18 17:04:44

标签: c compiler-construction interpreter vm-implementation

我正在开发一个基于类的语言和虚拟机(使用C)。语言是动态类型的,我正在设计如何在运行时表示类。

基本上是一种语法:

class Point {
      var x;
      var y;
}

在运行时由结构表示:

typedef struct {
    int        numfields;
    const char *name;
    class_t    *super;
    void       *methods[];
} class_t;

关于类属性所需的唯一信息是属性数(var)及其位置。因此编译器可以知道类Point的实例将在位置0处具有属性x而在位置1处具有属性y。

实例将简单地用结构表示:

typedef struct {
    class_t class;
    value   fields[];
} instance_t;

在我尝试通过已编译的应用程序实现外部代码的加载之前,一切正常。如果该外部代码包含一个ColorPoint(Point的子类)定义为:

class ColorPoint extends Point{
    var color;
}

ColorPoint子类如何在运行时知道x和y的位置? 这是一个静态信息所以我想我需要提供一个类似文件的头文件,其中编译ColorPoint类的编译器可以查找Point类并静态地知道各种属性的相对位置。

我想应该有更好的方法。 我还应该在class_t struct中保存属性名吗?

0 个答案:

没有答案
相关问题