0

I'm aware this might be a broad question (there's no specific code for you to look at), but I'm hoping I'd get some insights as to what to do, or how to approach the problem.

To keep things simple, suppose the compiler that I'm writing performs these three steps:

  • parse (and bind all variables)
  • typecheck
  • codegen

Also the language that I'm building the compiler for wants to support late-analysis/late-binding (ie., it has a function that takes a String, which is to be compiled and executed as a piece of source-code during runtime).

Now during parse-phase, I have a piece of context that I need to keep around till run-time for the sole benefit of the aforementioned function (because it needs to parse and typecheck its argument in that context).

So the question, how should I do this? What do other compilers do? Should I just serialise the context object to disk (codegen for it) and resurrect it during run-time or something?

Thanks

One Two Three
  • 22,327
  • 24
  • 73
  • 114
  • I'm probably not the best one to answer this question, but it sounds like if the language had reflection (that is, if there was enough metadata around at runtime), that could solve this problem and also be useful to users in general. Also, I would suggest looking at languages that have similar features and actually going and looking at the source for their compilers, where it is available. Sorry I can be of more help than that. – David Conrad Feb 07 '14 at 20:24

1 Answers1

0

Yes, you'll need to emit the type information (or other context, you weren't very specific) in your object/executable files, so that your eval can read it at runtime. You might look at Java's .class file format for inspiration; Java doesn't have eval as such, but you can dynamically spin new bytecode at runtime that must be linked in a type-safe manner. David Conrad's comment is spot-on: this information can also be used to implement reflection, if your language has such a feature.

That's as much as I can help you without more specifics.

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92