Giter Site home page Giter Site logo

Comments (2)

tch0 avatar tch0 commented on June 17, 2024
  • 这个东西就是一个玩具,怎么简单怎么来,是从C语言中取的最容易实现的能单趟解析的子集来实现的。
  • 要部分实现初始化那肯定是可以的,问题是能实现到什么程度。
  • 如果不能相对比较完备的实现,那还不如不实现。

现在的框架下要实现初始化要解决几个问题:

  • 标准C要求全局变量的初始化器是常量,比如1+1这种常量表达式,编译后常量表达式的值已经被计算出来了。但现在因为没有实现编译期计算,编译器就不知道1+1是常量,就需要生成指令在运行时去计算,这些指令需要在main之前执行(现在是解析完直接执行main,那就需要将这些初始化解析到一个初始化函数比如叫init里面去,然后调用main之前去调init)。或者去实现编译期计算(并不简单)。
  • 局部变量的初始化倒是总是可以通过变换代码实现:
int a = 1;
int b = a + 1;
// convert to
int a;
int b;
a = 1;
b = a + 1;
  • 可以在源码层面做也可以指令层面做,在解析到int b = a + 1;前你需要将a = 1这个信息存起来,要么源码形式要么指令形式,然后在声明解析完后再来解析存起来的源码或者将存起来的指令加进去,现在并没有提供这两种机制,需要自己加。
  • 结构体联合体初始化器,这个要完善支持是很复杂的。

现在的代码仅仅是取的一个能用的功能完备的子集,没有实现的东西太多了,初始化只是其中一个,但基本的功能你都可以通过其他途径实做到。这里只是为了简单写一个只要有C语言基础谁都能读懂的编译器,越简单越好,搞复杂了反而不利于阅读。比如结构体赋值都不支持,但可以用memcpy来拷贝。

from justatoyccompiler.

c-learnc avatar c-learnc commented on June 17, 2024

感谢楼主,我也想了一些“不太完善的实现方法”,思路和楼主说的差不多。
另外这种项目,既易读,又能引发思考,对于我这种初学者的确受益很多。

from justatoyccompiler.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.