diff --git a/Objective-J/CommonJS/lib/objective-j/compiler.js b/Objective-J/CommonJS/lib/objective-j/compiler.js index f7b15a8a1..b0ed29d2e 100644 --- a/Objective-J/CommonJS/lib/objective-j/compiler.js +++ b/Objective-J/CommonJS/lib/objective-j/compiler.js @@ -1,13 +1,14 @@ var FILE = require("file"), OS = require("os"), - ObjectiveJ = require("objective-j"); + ObjectiveJ = require("objective-j"), + JAKE = require("jake"); require("objective-j/rhino/regexp-rhino-patch"); -ObjectiveJ.Preprocessor.Flags.Preprocess = 1 << 10; -ObjectiveJ.Preprocessor.Flags.Compress = 1 << 11; -ObjectiveJ.Preprocessor.Flags.CheckSyntax = 1 << 12; +ObjectiveJ.ObjJAcornCompiler.Flags.Preprocess = 1 << 10; +ObjectiveJ.ObjJAcornCompiler.Flags.Compress = 1 << 11; +ObjectiveJ.ObjJAcornCompiler.Flags.CheckSyntax = 1 << 12; var compressors = { ss : { id : "minify/shrinksafe" } @@ -36,16 +37,15 @@ function compressor(code) function compileWithResolvedFlags(aFilePath, objjcFlags, gccFlags, asPlainJavascript) { - var shouldObjjPreprocess = objjcFlags & ObjectiveJ.Preprocessor.Flags.Preprocess, - shouldCheckSyntax = objjcFlags & ObjectiveJ.Preprocessor.Flags.CheckSyntax, - shouldCompress = objjcFlags & ObjectiveJ.Preprocessor.Flags.Compress, + var shouldObjjPreprocess = objjcFlags & ObjectiveJ.ObjJAcornCompiler.Flags.Preprocess, + shouldCheckSyntax = objjcFlags & ObjectiveJ.ObjJAcornCompiler.Flags.CheckSyntax, + shouldCompress = objjcFlags & ObjectiveJ.ObjJAcornCompiler.Flags.Compress, fileContents = "", executable, code; - if (OS.popen("which gcc").stdout.read().length === 0) + if (!shouldObjjPreprocess && OS.popen("which gcc").stdout.read().length === 0) fileContents = FILE.read(aFilePath, { charset:"UTF-8" }); - else { // GCC preprocess the file. @@ -54,30 +54,38 @@ function compileWithResolvedFlags(aFilePath, objjcFlags, gccFlags, asPlainJavasc while (chunk = gcc.stdout.read()) fileContents += chunk; - } - if (!shouldObjjPreprocess) return fileContents; + } // Preprocess contents into fragments. // FIXME: should calculate relative path, etc. try { - executable = ObjectiveJ.preprocess(fileContents, FILE.basename(aFilePath), objjcFlags); + var sources = new JAKE.FileList("**/*.j"), + translateFilenameToPath = {}, + otherwayTranslateFilenameToPath = {}; + + // Create a filename to filename path dictionary. (For example: CPArray.j -> CPArray/CPArray.j) + sources.forEach(function(/*String*/ aFilename) + { + translateFilenameToPath[FILE.basename(aFilename)] = aFilename; + otherwayTranslateFilenameToPath[aFilename] = FILE.basename(aFilename); + }, this); + + var translatedFilename = translateFilenameToPath[aFilePath] ? translateFilenameToPath[aFilePath] : aFilePath, + otherwayTranslatedFilename = otherwayTranslateFilenameToPath[aFilePath] ? otherwayTranslateFilenameToPath[aFilePath] : aFilePath, + theTranslatedFilename = otherwayTranslatedFilename ? otherwayTranslatedFilename : translatedFilename, + absolutePath = FILE.absolute(theTranslatedFilename), + basePath = absolutePath.substring(0, absolutePath.length - theTranslatedFilename.length); + + ObjectiveJ.setCurrentCompilerFlags(objjcFlags); + ObjectiveJ.make_narwhal_factory(absolutePath, basePath, translateFilenameToPath)(require, {}, module, system, print); + + executable = new ObjectiveJ.FileExecutable(FILE.basename(aFilePath)); } catch (anException) - {print(anException); - var lines = fileContents.split("\n"), - PAD = 3, - lineNumber = anException.lineNumber || anException.line, - errorInfo = "Syntax error in " + aFilePath + - " on preprocessed line number " + lineNumber + "\n\n" + - "\t" + lines.slice(Math.max(0, lineNumber - 1 - PAD), lineNumber + PAD).join("\n\t"); - - print(errorInfo); - - throw errorInfo; - } + {} if (shouldCompress) { @@ -100,7 +108,7 @@ function resolveFlags(args) count = args.length, gccFlags = [], - objjcFlags = ObjectiveJ.Preprocessor.Flags.Preprocess | ObjectiveJ.Preprocessor.Flags.CheckSyntax; + objjcFlags = ObjectiveJ.ObjJAcornCompiler.Flags.Preprocess | ObjectiveJ.ObjJAcornCompiler.Flags.CheckSyntax; for (; index < count; ++index) { @@ -128,19 +136,19 @@ function resolveFlags(args) } else if (argument.indexOf("-E") === 0) - objjcFlags &= ~ObjectiveJ.Preprocessor.Flags.Preprocess; + objjcFlags &= ~ObjectiveJ.ObjJAcornCompiler.Flags.Preprocess; else if (argument.indexOf("-S") === 0) - objjcFlags &= ~ObjectiveJ.Preprocessor.Flags.CheckSyntax; + objjcFlags &= ~ObjectiveJ.ObjJAcornCompiler.Flags.CheckSyntax; else if (argument.indexOf("-T") === 0) - objjcFlags |= ObjectiveJ.Preprocessor.Flags.IncludeTypeSignatures; + objjcFlags |= ObjectiveJ.ObjJAcornCompiler.Flags.IncludeTypeSignatures; else if (argument.indexOf("-g") === 0) - objjcFlags |= ObjectiveJ.Preprocessor.Flags.IncludeDebugSymbols; + objjcFlags |= ObjectiveJ.ObjJAcornCompiler.Flags.IncludeDebugSymbols; else if (argument.indexOf("-O") === 0) - objjcFlags |= ObjectiveJ.Preprocessor.Flags.Compress; + objjcFlags |= ObjectiveJ.ObjJAcornCompiler.Flags.Compress; else filePaths.push(argument); @@ -191,7 +199,7 @@ exports.main = function(args) if (argv[0] === "-T" || argv[0] === "--includeTypeSignatures") { - objjcFlags |= ObjectiveJ.Preprocessor.Flags.IncludeTypeSignatures; + objjcFlags |= ObjectiveJ.ObjJAcornCompiler.Flags.IncludeTypeSignatures; argv.shift(); continue; }