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