Compiler/Simple file inclusion pre processor: Difference between revisions
Content added Content deleted
(→{{header|ALGOL 68}}: Changed the "read" pragmatic comment to include the file only if it hasn't been already included.) |
(Added AWK) |
||
Line 466: | Line 466: | ||
END |
END |
||
</pre> |
</pre> |
||
=={{header|AWK}}== |
|||
AWK does not have file-inclusion as standard, however some implementations, particularly GNU Awk do provide file inclusion. |
|||
<br>This include pre-processor differs from GAWK syntax in that the include directive is ".include", not "@include" and ".include" can appear inside or outside functions. The file name can be quoted or not. Nested includes are not supported. |
|||
<br>The source can be a named file or read from stdin. If it is read from stdin, <code>-v sec=sourceName</code> can be specified on the AWK command line to name the file. The pre-processed source is writen to stdout. |
|||
<lang awk># include.awk: simple file inclusion pre-processor |
|||
# |
|||
# the command line can specify: |
|||
# -v srcName=<source file path> |
|||
BEGIN { |
|||
FALSE = 0; |
|||
TRUE = 1; |
|||
srcName = srcName ""; |
|||
} # BEGIN |
|||
{ |
|||
if( $1 == ".include" ) |
|||
{ |
|||
# must include a file |
|||
includeFile( $0 ); |
|||
} |
|||
else |
|||
{ |
|||
# normal line |
|||
printf( "%s\n", |
|||
$0 ); |
|||
} # if various lines;; |
|||
} |
|||
function includeFile( includeLine, fileName, |
|||
ioStat, |
|||
line ) |
|||
{ |
|||
# get the file name from the .include line |
|||
fileName = includeLine; |
|||
sub( /^ *.include */, "", fileName ); |
|||
sub( / *$/, "", fileName ); |
|||
sub( / *#.*$/, "", fileName ); |
|||
if( fileName ~ /^"/ ) |
|||
{ |
|||
# quoted file name |
|||
sub( /^"/, "", fileName ); |
|||
sub( /"$/, "", fileName ); |
|||
gsub( /""/, "\"", fileName ); |
|||
} # if fileName ~ /^"/ |
|||
printf( "#line 1 %s\n", |
|||
fileName ); |
|||
ioStat = ( getline line < fileName ); |
|||
while( ioStat > 0 ) |
|||
{ |
|||
# have a source line |
|||
printf( "%s\n", |
|||
line ); |
|||
ioStat = ( getline line < fileName ); |
|||
} # while ioStat > 0 |
|||
if( ioStat < 0 ) |
|||
{ |
|||
# I/O error |
|||
printf( ".include %s # not found or I/O error\n", |
|||
fileName ); |
|||
} # if ioStat < 0 |
|||
close( fileName ); |
|||
printf( "#line %d %s\n", |
|||
NR, |
|||
( srcName != "" ? srcName : FILENAME ) ); |
|||
} # includeFile</lang> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |