Hi experienced people!

I am working on an interpreter of sorts. I would like its scripts to be invokable from the command line - so It would honor the "#!" as a first line, basically by making any line starting with a "#" a comment.

The issue is that I want to be able to read the source code more than once. The first pass will deduce the number of lines, the number of variables, the number of line labels, The beginning of the second pass will allocate arrays (malloc) to hold the program and its data, then re-read the source to store it internally and fill symbol tables and mark variables. once the source is read the 2nd time the program will begin to execute.

If an interpreted program is mentioned on the command line it would only get one pass at the source, right? That source would come in on standard input, and once read is no longer available.

Is there a way for my interpreter to get the file name instead of the body of the file?

While writing the question I came up with an idea, but I hope there is a better one. I could as a first pass store each line of the program in a known temporary file, then for the second pass I could read that file. I don't like this but if there is no better way...

  • mrkite@programming.dev
    ·
    1 year ago

    How big are you expecting these files to be? Instead of a temporary file just read the whole thing into RAM. If someone has a 2 gig script they should expect it to take a while.

    • WasPentalive@lemmy.one
      hexagon
      ·
      1 year ago

      I want to make as much space available as I can. I once wrote a small language called tiny. It had a fixed array of statements, that was a hard limit on the size of any program it could run. Because I can read the source and find out how big it is, I can attempt to malloc the needed space to store and run it. That way the program can be as big as memory allows.