我的编程语言::基本成形、编译器数据结构及初始化
最开始设计编程语言时,最希望解决的是传统编程语言编写项目重构难的问题,在这个基础上还加入了很多宏伟的目标,比如:语法的自由,基本概念的极度简洁等。
最初的设计是为每一个对象提供一个唯一的ID,对象之间的使用都通过ID来引用。这样在对任何对象进行重命名、重新摆放位置和更新功能时,都不需要对整个工程进行改变,也就从根本上解决了重构难的问题。然后在实际编写代码的过程中,我发现这样还不够,因为在代码中要区分对象的ID和字符,编译器代码变得越来越复杂。再后来我终于想到,其实字符本身就可以看做一个对象的ID。
经过半年的演化,思路终于最终清晰:编程的目的是为了描述我们身处或想象出的世界,这个世界由许多的事物组成,在现实中,人脑对这些事物都有一个唯一标识,而颜色、尺寸直至名字本身,都不过是该事物的属性,所以最终我的编译器的代码基本成形了,将它的基本数据结构和初始化代码贴出如下:
typedef size_t Code;
// 这个Thing就是整个编译器唯一的数据结构,也是整个语言中唯一的基本概念
struct Thing
{
typedef list<Thing> Section; // 段落,用于组成文章
typedef list<Section> Article; // 文章,用于描述事物的属性
Code code; // 每个Thing有他的唯一ID
map<Thing, Article> properties; // 剩下的一切包括名字都只是它的属性
Thing() : code(0) {}
template <class T>
Thing(const T& t) : code(t) {}
bool operator == (const Thing& t) const {return code == t.code; // 未完成}
bool operator != (const Thing& t) const {return !(*this == t);}
bool operator < (const Thing& t) const {return code < t.code;// 未完成}
Article& operator [] (const Thing& thing) {return properties[thing];}
const Article& operator [] (const Thing& thing) const
{
map<Thing, Article>::const_iterator i = properties.lower_bound(thing);
if (i == properties.end() || i->first != thing)
{
assert(false);
}
return i->second;
}
};
typedef Thing::Section Section;
typedef Thing::Article Article;
const size_t DefaultThingsCount = 65536;
const size_t ReservedThingsCount = DefaultThingsCount + 64;
enum
{
UnmatchableThingsCount = ReservedThingsCount - 5,
Name,
Exit,
Output0,
Output1,
};
extern vector<Thing> things;
inline void InitThings()
{
// 使用整个UniCode集作为基本事物集
// 从某种意义上也可以把一切皆为事物看做是一切皆为编码,而编程就是扩充UniCode集的过程
things.resize(ReservedThingsCount);
for (int i = 0; i < ReservedThingsCount; ++i)
{
Thing& thing = things[i];
thing.code = i;
thing[Name] = ToArticle(i);
}
things[Name][Name] = ToArticle(_T("名字"));
things[Exit][Name] = ToArticle(_T("退出"));
things[Output][Name] = ToArticle(_T("输出0"));
things[Output][Name] = ToArticle(_T("输出1"));
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Paul_L_Ji/archive/2011/05/07/6401200.aspx
[解决办法]
楼主辛苦了
感谢分享经验
[解决办法]
感谢分享啊~~~~