zsaleeba / picoc

A very small C interpreter
1.45k stars 183 forks source link

External includes repeatedly included #16

Open lwalewski opened 9 years ago

lwalewski commented 9 years ago

According to the IncludeFile() definition the built-in include headers (such as stdio.h) are protected against multiple inclusion, so the following code runs with no problems:

void main(void) {
  while (1) {
#include <stdio.h>
  }
}

I agree it does not make sense in itself, but read on to see why I need it. As per PicocPlatformScanFile() such a protection is not implemented for user-defined includes, so the following code will sooner or later explode in memory when interpreted by PicoC:

void main(void) {
  while (1) {
#include "my-include.h"
  }
}

with my-include.h containing arbitrary (but valid) C code, e. g.:

int i;

At the same time the same source compiled with gcc runs at constant memory. I would be grateful for explanation, what is the logic behind? What use case could take advantage of multiple inclusions of the same file?

For my usage scenario such a design is lethal, since I am trying to parametrize the program's behaviour by defining the body of a function in a separate file, which could be modified by the user, e. g.:

void my_func(void) {
#include "my-include.c"
}

The function itself would contain assignments and logical operations on input variables that modify output variables and would be called in a loop by the main program.