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
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      1 year ago

      Oh wow, you sure are right… I never tried that before.

      Running ./test.sh which calls a.out which just outputs argv

      0: '/Users/mrkite/a.out'
      1: './test.sh'
      

      edit: and I double checked, it doesn’t modify stdin at all either.