Re: Using gcc as a sort of scripting language.

2024-12-28 Thread Paul Markfort via Gcc

To be clear.

I am not suggesting that Compilers like GCC be modified to act on the "#!", or 
even fully support it.
Just that they be simply modified to ignore "#!" - on the first line (which should terminate with 
either a "/r" or "/n").
This allows the easy creation of scripts to handle an executable Source file.
Maybe ignore all lines beginning with "#!" - as this would make it easy to add 
a line with extra arguments for a script or program that runs the source file.  The 
compiler itself doesn't need to have any special features to do that.
I realize that C is not a line oriented language and usually completely ignores 
line termination characters (so yes this is probably not a simple thing to do).

The point is to make it easier to use C and C++ (and Fortran, etc) to write 
small little utility programs.
This will also make it much easier to teach these languages - and for students 
(or anyone) to practice ideas.

Compilation of small source files is so fast these days, that an interpreter 
probably is a sub-optimal solution.
Compile, then run helps find errors before the program does anything (One 
example of this is perl - which compiles the perl code, then runs it).  We 
don't need to get into the details of how this would be used - or what the 
program that gets started by running the source needs to be able to do.



On 2024-12-28 5:48 AM, Basile Starynkevitch wrote:

Hello all,

Paul Markfort suggested

..

--

The views and opinions expressed above are strictly
those of the author(s).  The content of this message has
not been reviewed nor approved by any entity whatsoever.

Paul FM Info: http://paulfm.com/~paulfm/



Re: Using gcc as a sort of scripting language.

2024-12-29 Thread Paul Markfort via Gcc



You can also do what I do now (the example in my first message), and don't need to 
pre-process the file before sending it to the compiler.  What Jonothan suggested 
("Still it would be a nice touch ...") would be great - but simply being able 
use a custom script to (like I do in the example) without all the convolutions I used to 
make it work - would be a good solution.

My goal would just be to have C, C++, etc, simply ignore the "#!" line, if it 
is the very first line.
If you want to have additional lines, have it ignore everything until it sees an  
"#end" (or some other token).

Basically "#!" would become a special comment (but would only work if the file 
essentially started with it - first non-blank characters).

Think of users who use scripts all the time, but rarely use Compiled languages.





On 2024-12-28 4:47 PM, Florian Weimer wrote:

* Jonathan Wakely via Gcc:


Here's a complete example:

#!/bin/sh
set -e
out=$(mktemp /tmp/a.out.XX)
sed 1,5d "$0" | gcc -x c - -o "$out"
exec "$out"

#include 
int main()
{
   puts("Hello, world");
   return 0;
}


Or this, with accurate locations for diagnostics and argument
handling:

#!/bin/sh
set -e
out=$(mktemp /tmp/a.out.XX)
(echo "#line  6 \"$0\""; sed 1,5d "$0") | gcc -x c - -o "$out"
exec "$out" "$@"

#include 
int main()
{
   puts("Hello, world");
   return 0;
}

Still it would be a nice touch if we could do

#!/usr/bin/gcc -f
#include 
int main()
{
   puts("Hello, world");
   return 0;
}

instead.



--

The views and opinions expressed above are strictly
those of the author(s).  The content of this message has
not been reviewed nor approved by any entity whatsoever.

Paul FM Info: http://paulfm.com/~paulfm/



Using gcc as a sort of scripting language.

2024-12-26 Thread Paul Markfort via Gcc



This is just a suggestion to make it easier for Linux/Unix users to use the Gnu 
compilers instead of having to use a scripting language for short little 
utilities.

I know someone has created and released a binary C interpreter for this purpose.
But why would you want to install another program, if you could essentially use 
the tools you already have.

I think gcc (and all the compilers) should support the "#!" in source files to 
make something similar easily possible.
So one could write C or C++ (or Fortran, etc) code and just run the source file 
to compile and run.

The Best way to make this possible is to set up the compiler to ignore "#!" if 
it is the first line.
Maybe a special pre-processor would work as well, but it is nice if one can 
send the unmodified source through the standard compilers (the edit and run 
process is a quick way to try out ideas before creating a full program).

This could encourage people to do this more often (increasing the utility of 
writing short compiled scripts) if one could do the below by simply starting 
the source file with:

#! /usr/bin/compile-and-run-file.sh -v


Currently, to do this (with C and C++) one needs to start their code similar to 
this (this works, I have tested it):


#if 0   /*  (first line is blank on purpose)
#   Warning, the following will usually be run by sh (not bash, nor 
csh)
#   If you make this file executable - it will be processed by the 
script below.
compile-and-run-file.sh -v "${0}" "${@}"
exit
#   End of C comment*/
#endif





I have a complete example here (including the complile-and-run-file.sh script):
http://www.paulfm.com/~paulfm/share/code/make-scratch/

Note: I am not a very good C/C++ programmer, so I don't pretend to have the 
best answer (this is just an example of one way to do this).


Thanks.


--

The views and opinions expressed above are strictly
those of the author(s).  The content of this message has
not been reviewed nor approved by any entity whatsoever.

Paul FM Info: http://paulfm.com/~paulfm/