Translators: Processing includes / imports

I built an interpreter in C ++ and everything works fine so far, but now I'm getting stuck with the import / include / design however you want to call it a function.

I thought about the following:

  • Processing includes in the tokenization process: When present in the code include

    , the tokenization function is recursively called with the specified file name. Then the injected file token include

    d is appended to the previous include position. Disadvantages: no conditional inclusions (!)

  • Processing involves an interpretation process: I don't know how. All I know is that PHP must

    does it this way, since conditional inclusions are possible.

Now my questions are:

  • What should I do with inclusion?
  • How do modern interpreters (Python / Ruby) cope? Do they allow conditionals?
+2


source to share


1 answer


This problem is easy to solve if you have a clean design and know what you are doing. Otherwise it can be very difficult. I have written at least 6 interpreters that have this feature and it's pretty simple.

  • Your interpreter must maintain an environment that knows about all globals, functions, types, etc. that have been defined. You may be comfortable calling this the "symbol table".

  • You need to define an internal function that reads the file and updates the environment. Depending on your language design, you may or may not be doing some judgment the moment you read things. My translators are very dynamic and evaluate each definition as soon as it is read.

  • Your life will be infinitely simpler if you structure your interpreter in layers:

    • Tokenizer (breaks input into tokens)
    • Parser (reads one token at a time, converts to an abstract syntax tree)
    • Evaluator (reads abstract syntax and updates the environment)


The abstract syntax tree is really key. If you have that, when you come across an import / include construct in input, you just make a recursive call and you get more abstract syntax. You can do this in a parser or evaluator. If you want conditional imports, you must do so in the evaluator, since only the evaluator can evaluate the condition.

The source code for my interpreters is on the internet. Two of them are written in C; others are written in standard ML.

+3


source







All Articles