|
| 1 | +"use babel" |
| 2 | + |
| 3 | +import path from "path" |
| 4 | +import GrammarUtils from "../grammar-utils" |
| 5 | +const { OperatingSystem, command } = GrammarUtils |
| 6 | + |
| 7 | +const os = OperatingSystem.platform() |
| 8 | +const windows = OperatingSystem.isWindows() |
| 9 | + |
| 10 | +const options = "-Wall -include stdio.h" |
| 11 | + |
| 12 | +// TODO add windows support |
| 13 | +function CArgs({ filepath }) { |
| 14 | + let cmdArgs = "" |
| 15 | + switch (os) { |
| 16 | + case "darwin": |
| 17 | + cmdArgs = `xcrun clang ${options} -fcolor-diagnostics '${filepath}' -o /tmp/c.out && /tmp/c.out` |
| 18 | + break |
| 19 | + case "linux": |
| 20 | + cmdArgs = `cc ${options} '${filepath}' -o /tmp/c.out && /tmp/c.out` |
| 21 | + break |
| 22 | + default: { |
| 23 | + atom.notifications.addError(`Not support on ${os}`) |
| 24 | + } |
| 25 | + } |
| 26 | + return ["-c", cmdArgs] |
| 27 | +} |
| 28 | + |
| 29 | +const C = { |
| 30 | + "File Based": { |
| 31 | + command: "bash", |
| 32 | + args(opts) { |
| 33 | + return CArgs(opts) |
| 34 | + }, |
| 35 | + }, |
| 36 | + |
| 37 | + "Selection Based": { |
| 38 | + command: "bash", |
| 39 | + args(context) { |
| 40 | + const code = context.getCode() |
| 41 | + const tmpFile = GrammarUtils.createTempFileWithCode(code, ".c") |
| 42 | + return CArgs({ filepath: tmpFile }) |
| 43 | + }, |
| 44 | + }, |
| 45 | +} |
| 46 | + |
| 47 | +const Cs = { |
| 48 | + "Selection Based": { |
| 49 | + command, |
| 50 | + args(context) { |
| 51 | + const code = context.getCode() |
| 52 | + const tmpFile = GrammarUtils.createTempFileWithCode(code, ".cs") |
| 53 | + const exe = tmpFile.replace(/\.cs$/, ".exe") |
| 54 | + if (windows) { |
| 55 | + return [`/c csc /out:${exe} ${tmpFile} && ${exe}`] |
| 56 | + } else { |
| 57 | + return ["-c", `csc /out:${exe} ${tmpFile} && mono ${exe}`] |
| 58 | + } |
| 59 | + }, |
| 60 | + }, |
| 61 | + "File Based": { |
| 62 | + command, |
| 63 | + args({ filepath, filename }) { |
| 64 | + const exe = filename.replace(/\.cs$/, ".exe") |
| 65 | + if (windows) { |
| 66 | + return [`/c csc ${filepath} && ${exe}`] |
| 67 | + } else { |
| 68 | + return ["-c", `csc '${filepath}' && mono ${exe}`] |
| 69 | + } |
| 70 | + }, |
| 71 | + }, |
| 72 | +} |
| 73 | +const CSScriptFile = { |
| 74 | + "Selection Based": { |
| 75 | + command: "scriptcs", |
| 76 | + args(context) { |
| 77 | + const code = context.getCode() |
| 78 | + const tmpFile = GrammarUtils.createTempFileWithCode(code, ".csx") |
| 79 | + return ["-script", tmpFile] |
| 80 | + }, |
| 81 | + }, |
| 82 | + "File Based": { |
| 83 | + command: "scriptcs", |
| 84 | + args({ filepath }) { |
| 85 | + return ["-script", filepath] |
| 86 | + }, |
| 87 | + }, |
| 88 | +} |
| 89 | + |
| 90 | +const Cpp = { |
| 91 | + "Selection Based": { |
| 92 | + command: "bash", |
| 93 | + args(context) { |
| 94 | + const code = context.getCode() |
| 95 | + const tmpFile = GrammarUtils.createTempFileWithCode(code, ".cpp") |
| 96 | + let cmdArgs = "" |
| 97 | + |
| 98 | + switch (os) { |
| 99 | + case "darwin": |
| 100 | + cmdArgs = `xcrun clang++ -std=c++14 ${options} -fcolor-diagnostics -include iostream ${tmpFile} -o /tmp/cpp.out && /tmp/cpp.out` |
| 101 | + break |
| 102 | + case "linux": |
| 103 | + cmdArgs = `g++ ${options} -std=c++14 -include iostream ${tmpFile} -o /tmp/cpp.out && /tmp/cpp.out` |
| 104 | + break |
| 105 | + default: { |
| 106 | + atom.notifications.addError(`Not support on ${os}`) |
| 107 | + } |
| 108 | + } |
| 109 | + return ["-c", cmdArgs] |
| 110 | + }, |
| 111 | + }, |
| 112 | + |
| 113 | + "File Based": { |
| 114 | + command, |
| 115 | + args({ filepath }) { |
| 116 | + let cmdArgs = "" |
| 117 | + switch (os) { |
| 118 | + case "darwin": |
| 119 | + cmdArgs = `xcrun clang++ -std=c++14 ${options} -fcolor-diagnostics -include iostream '${filepath}' -o /tmp/cpp.out && /tmp/cpp.out` |
| 120 | + break |
| 121 | + case "linux": |
| 122 | + cmdArgs = `g++ -std=c++14 ${options} -include iostream '${filepath}' -o /tmp/cpp.out && /tmp/cpp.out` |
| 123 | + break |
| 124 | + case "win32": |
| 125 | + if ( |
| 126 | + GrammarUtils.OperatingSystem.release() |
| 127 | + .split(".") |
| 128 | + .slice(-1 >= "14399") |
| 129 | + ) { |
| 130 | + filepath = path.posix |
| 131 | + .join(...[filepath.split(path.win32.sep)[0].toLowerCase(), ...filepath.split(path.win32.sep).slice(1)]) |
| 132 | + .replace(":", "") |
| 133 | + cmdArgs = `g++ -std=c++14 ${options} -include iostream /mnt/${filepath} -o /tmp/cpp.out && /tmp/cpp.out` |
| 134 | + } |
| 135 | + break |
| 136 | + default: { |
| 137 | + atom.notifications.addError(`Not support on ${os}`) |
| 138 | + } |
| 139 | + } |
| 140 | + return GrammarUtils.formatArgs(cmdArgs) |
| 141 | + }, |
| 142 | + }, |
| 143 | +} |
| 144 | +const Cpp14 = Cpp |
| 145 | + |
| 146 | +const ObjectiveC = { |
| 147 | + "File Based": { |
| 148 | + command: "bash", |
| 149 | + args({ filepath }) { |
| 150 | + return [ |
| 151 | + "-c", |
| 152 | + `xcrun clang ${options} -fcolor-diagnostics -framework Cocoa '${filepath}' -o /tmp/objc-c.out && /tmp/objc-c.out`, |
| 153 | + ] |
| 154 | + }, |
| 155 | + }, |
| 156 | +} |
| 157 | + |
| 158 | +const ObjectiveCpp = { |
| 159 | + "File Based": { |
| 160 | + command: "bash", |
| 161 | + args({ filepath }) { |
| 162 | + return [ |
| 163 | + "-c", |
| 164 | + `xcrun clang++ -Wc++11-extensions ${options} -fcolor-diagnostics -include iostream -framework Cocoa '${filepath}' -o /tmp/objc-cpp.out && /tmp/objc-cpp.out`, |
| 165 | + ] |
| 166 | + }, |
| 167 | + }, |
| 168 | +} |
| 169 | + |
| 170 | +const CGrammars = { |
| 171 | + C, |
| 172 | + "C++": Cpp, |
| 173 | + "C++14": Cpp14, |
| 174 | + "C#": Cs, |
| 175 | + "C# Script File": CSScriptFile, |
| 176 | + "Objective-C": ObjectiveC, |
| 177 | + "Objective-C++": ObjectiveCpp, |
| 178 | +} |
| 179 | +export default CGrammars |
0 commit comments