Compiler/Preprocessor: Difference between revisions

m
(Add Python)
m (→‎{{header|Wren}}: Minor tidy)
 
(3 intermediate revisions by 3 users not shown)
Line 11:
The include command must be followed by whitespace and a double quoted string whose contents is the actual file to read. Includes should allow the inclusion of other files to a recursive limit of five active header files plus the original source file.
 
The define command must be followed by whitespace and a new macro name. Redefinition or overloading of the macro name is illegal. The same character convention for naming variables in the language is used for macro names. No whitespace is required in the arguments but is allowed between every token. When there are no parameters, both the definition or usage must either have an empty argument list or there must not be one. The empty list is required to avoid confusion when the definition needs parenthesizes to force precedence. If there is a close parenthesis, the whitespace trailing it is optional. Otherwise, it is required. From that point to end of line is the definition, whitespace removed from both the start and end of it. Whitespace within the definition between tokens must be maintained. Any names within the definition are treated as macro names first before it is assumed they are a variable in the language during the usage.
 
To make it easier to find, the usage will be within hashtags, and replaces its usage elsewhere in the files processed. These usages will be processed everywhere they are encountered without regard to the syntax of the sample language. The calling arguments replace the define's parameters as a simple string substitution. You may not assume the usage proceeds in an order to form complex combinations. Tokens detected during definition processing can remain separated during usage processing. If the contents within the double hashtags is not a valid macro usage, its entire text is written to the output as if it was not detected. It is not required to use the ending hashtag as the start of another macro usage. Both start and end hashtags must be on the same line.
Line 61:
 
=={{header|Julia}}==
<langsyntaxhighlight rubylang="julia">""" Rosetta Code task Compiler/Preprocessor task """
 
""" If the line is a macro definition, add the macro to macros. """
Line 201:
 
runwithopts(preprocess)
</langsyntaxhighlight>{{out}} Same output as Phix entry.
 
=={{header|Phix}}==
No attempt to implement command line arguments, obviously trivial on desktop/Phix but not possible under pwa/p2js, aka within a browser.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Compiler\preprocess.exw
Line 381:
<span style="color: #000080;font-style:italic;">--close_files()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 399:
=={{header|Python}}==
 
<langsyntaxhighlight lang="python">#!/usr/bin/env python
"""Rosetta Code compiler/preprocessor. Requires Python >= 3.7."""
import re
Line 742:
args = parser.parse_args()
preprocess(args.infile.read(), args.infile.name, args.outfile, debug=args.debug)
</syntaxhighlight>
</lang>
 
{{out}}
Line 802:
 
Note that the program errors out if there are any syntax or other errors when defining the macros.
<langsyntaxhighlight ecmascriptlang="wren">import "os" for Process
import "./ioutil" for FileUtil, File, Input
import "./str" for Char
Line 980:
file.writeBytes("\n")
}
}</langsyntaxhighlight>
 
{{out}}
Using the example files;
<pre>
$ wren-cli compiler_preprocessorCompiler_Preprocessor.wren -d
How many lines are to be entered? : 4
 
Line 1,025:
{{out}}
<pre>
$ wren-cli compiler_preprocessorCompiler_Preprocessor.wren -d Source.t
Output:
 
9,479

edits