this post was submitted on 14 Sep 2023
6 points (87.5% liked)

C Programming Language

993 readers
1 users here now

Welcome to the C community!

C is quirky, flawed, and an enormous success.
... When I read commentary about suggestions for where C should go, I often think back and give thanks that it wasn't developed under the advice of a worldwide crowd.
... The only way to learn a new programming language is by writing programs in it.

ยฉ Dennis Ritchie

๐ŸŒ https://en.cppreference.com/w/c

founded 1 year ago
MODERATORS
 

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...

you are viewing a single comment's thread
view the rest of the comments
[โ€“] mrkite@programming.dev 1 points 1 year ago* (last edited 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.