Compiler/Preprocessor: Difference between revisions
→{{header|Wren}}: Modified to allow up to 3 command line arguments as per revised task description.
(→Task) |
(→{{header|Wren}}: Modified to allow up to 3 command line arguments as per revised task description.) |
||
Line 260:
A fairly naive solution compared to the complexities of a modern C pre-processor.
I've made the simplifying assumption that macro parameters in a macro definition will always be separated from other tokens by at least one space.
Note that
import "./ioutil" for FileUtil, File, Input▼
▲Note that if macros are used incorrectly, there are no errors or warnings. The program simply assumes that this is what was intended. However, the program errors out if there are any syntax or other errors when defining the macros.
▲<lang ecmascript>import "os" for Process
▲import "./ioutil" for FileUtil, File
import "./str" for Char
import "./pattern" for Pattern
Line 282 ⟶ 280:
var clargs = Process.arguments
if (clargs.count
System.print("
-d // debug mode, comments will be included in output
input // filename: if absent or == console, gets input from console
output // filename: if absent or == console, sends output to console")
return
}
var debug = clargs.contains("-d") || clargs.contains("--debug")
if (debug) {
clargs.remove("-d")
clargs.remove("--debug")
}
var inputFileName = "console"
if (clargs.count > 0) inputFileName = clargs[0]
var lines
if (inputFileName != "console") {
var n = Input.integer("How many lines are to be entered? : ", 1)
System.print("\nOK, enter the lines and press enter after each one.\n")
lines = List.filled(n, null)
for (i in 0...n) lines[i] = Input.text("")
}
var macros = []
var comments = []
var used = []
var includes = Stack.new()
▲var lines = FileUtil.readLines(fileName)
var i = 0
while (i < lines.count) {
Line 307 ⟶ 324:
} else {
includes.push([fname, i + lines2.count - 1])
if (debug) comments.add("/* Include Header %(fname) */")
}
lines = lines[0...i] + lines2 + lines[i+1..-1]
Line 345 ⟶ 362:
var defn = line[j..-1].trimStart()
macros.add([name, params, defn])
if (
▲ } else {
} else {
▲ comments.add("/* Define %(name)(%(params.toString[1...-1])) as %(defn) */")
comments.add("/* Define %(name)(%(params.toString[1...-1])) as %(defn) */")
}
}
lines.removeAt(i)
Line 354 ⟶ 373:
Fiber.abort("Unknown directive.")
}
if (debug) {
comments.add("/* End %(includes.pop()[0]) */")
}
}
}
Line 394 ⟶ 415:
}
}
if (debug) {
comments.add("/* End %(includes.pop()[0]) */")
}
}
used = Lst.distinct(used)
if (used.count > 0) {
var temp = (used.count == 1) ? used[0] : used[0..-2].join(", ") + " and " + used[-1]
if (debug) comments.add("/* Used %(temp) */")
}
if (debug) comments = comments.join("\n")
var outputFileName = "console"
if (clargs.count > 1) outputFileName = clargs[1]
▲System.print(src)
if (outputFileName == "console") {
File.create("output_from_" + fileName) { |file|▼
} else {
file.writeBytes(src)▼
file.writeBytes("\n")▼
if (debug) {
file.writeBytes(comments)
file.writeBytes("\n")
}
▲ file.writeBytes(src)
▲ file.writeBytes("\n")
}
}</lang>
Line 419 ⟶ 448:
Using the example files;
<pre>
$ wren-cli compiler_preprocessor.wren -d
How many lines are to be entered? : 4
OK, enter the lines and press enter after each one.
#include "Header.h"
#define width 5
#define height 6
area = #area(height, width)#;
Output:
/* Include Header "Header.h" */
/* Define area(h, w]) as h * w */
Line 447 ⟶ 488:
{{out}}
<pre>
$ wren-cli compiler_preprocessor.wren -d Source.t
Output:
/* Include Header "Header.h" */
/* Define area(h, w) as h * w */
|