Jump to content

Compiler/Preprocessor: Difference between revisions

→‎{{header|Wren}}: Modified to allow up to 3 command line arguments as per revised task description.
(→‎{{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.
I've made the following simplifying assumptions:
 
1.I've Macroalso parametersassumed inthat athe macroheader definitionfiles will always be separatedactual fromfiles, otherand tokensnever byentered at leastfrom onethe spaceconsole.
 
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.
2. Macros will not occur in string literals, nor closing parentheses within macro argument strings.
<lang ecmascript>import "os" for Process
 
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 !=> 13) {
System.print("PleaseThere passcan't exactlybe onemore than 3 command line argument, the file name to preprocess.")arguments:
-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")
var fileName = clargs[0]
if (debug) {
clargs.remove("-d")
clargs.remove("--debug")
}
var inputFileName = "console"
if (clargs.count > 0) inputFileName = clargs[0]
var lines
if (inputFileName != "console") {
var lines = FileUtil.readLines(fileNameinputFileName)
} else {
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("")
System.print(src)
}
 
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 (params == nulldebug) {
comments.add("/*if Define %(name)params as== %(defnnull) */"){
comments.add("/* Define %(name)(%(params.toString[1...-1])) as %(defn) */")
} 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) {
while (includes.count > 0 && i >= includes.peek()[1]) {
comments.addwhile ("/*includes.count End> 0 && i >= %(includes.poppeek()[01]) */"){
comments.add("/* End %(includes.pop()[0]) */")
}
}
}
Line 394 ⟶ 415:
}
}
if (debug) {
while (includes.count > 0) {
comments.add("/* Endwhile %(includes.pop()[count > 0]) */"){
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"
// write to terminal
if (clargs.count > 1) outputFileName = clargs[1]
System.print(comments)
System.print(src)
 
if (outputFileName == "console") {
// write to a file
System.print(comments"Output:\n")
File.create("output_from_" + fileName) { |file|
fileif (debug) System.writeBytesprint(comments)
fileSystem.writeBytesprint("\n"src)
} else {
file.writeBytes(src)
File.create("output_from_" + fileNameoutputFileName) { |file|
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 */
9,482

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.