Skip to content

Commit

Permalink
fix: in C - inline switch-case + add default case
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Mar 21, 2021
1 parent b173058 commit ff1fe86
Showing 1 changed file with 79 additions and 56 deletions.
135 changes: 79 additions & 56 deletions lib/grammars/c.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,40 @@ const windows = OperatingSystem.isWindows()

const options = "-Wall -include stdio.h"

var args = function ({ filepath }) {
args = (() => {
switch (os) {
case "darwin":
return `xcrun clang ${options} -fcolor-diagnostics '${filepath}' -o /tmp/c.out && /tmp/c.out`
case "linux":
return `cc ${options} '${filepath}' -o /tmp/c.out && /tmp/c.out`
// TODO add windows support
function args({ filepath }) {
let cmdArgs = ""
switch (os) {
case "darwin":
cmdArgs = `xcrun clang ${options} -fcolor-diagnostics '${filepath}' -o /tmp/c.out && /tmp/c.out`
break
case "linux":
cmdArgs = `cc ${options} '${filepath}' -o /tmp/c.out && /tmp/c.out`
break
default: {
atom.notifications.addError(`Not support on ${os}`)
}
})()
return ["-c", args]
}
return ["-c", cmdArgs]
}

export const C = {
"File Based": {
command: "bash",
args({ filepath }) {
args = (() => {
switch (os) {
case "darwin":
return `xcrun clang ${options} -fcolor-diagnostics '${filepath}' -o /tmp/c.out && /tmp/c.out`
case "linux":
return `cc ${options} '${filepath}' -o /tmp/c.out && /tmp/c.out`
let cmdArgs = ""
switch (os) {
case "darwin":
cmdArgs = `xcrun clang ${options} -fcolor-diagnostics '${filepath}' -o /tmp/c.out && /tmp/c.out`
break
case "linux":
cmdArgs = `cc ${options} '${filepath}' -o /tmp/c.out && /tmp/c.out`
break
default: {
atom.notifications.addError(`Not support on ${os}`)
}
})()
return ["-c", args]
}
return ["-c", cmdArgs]
},
},

Expand All @@ -48,15 +57,20 @@ export const C = {
args(context) {
const code = context.getCode()
const tmpFile = GrammarUtils.createTempFileWithCode(code, ".c")
args = (() => {
switch (os) {
case "darwin":
return `xcrun clang ${options} -fcolor-diagnostics ${tmpFile} -o /tmp/c.out && /tmp/c.out`
case "linux":
return `cc ${options} ${tmpFile} -o /tmp/c.out && /tmp/c.out`
let cmdArgs = ""
switch (os) {
case "darwin":
cmdArgs = `xcrun clang ${options} -fcolor-diagnostics ${tmpFile} -o /tmp/c.out && /tmp/c.out`
break
case "linux":
cmdArgs = `cc ${options} ${tmpFile} -o /tmp/c.out && /tmp/c.out`
break
default: {
atom.notifications.addError(`Not support on ${os}`)
}
})()
return ["-c", args]
}

return ["-c", cmdArgs]
},
},
}
Expand Down Expand Up @@ -110,45 +124,54 @@ exports["C++"] = {
args(context) {
const code = context.getCode()
const tmpFile = GrammarUtils.createTempFileWithCode(code, ".cpp")
args = (() => {
switch (os) {
case "darwin":
return `xcrun clang++ -std=c++14 ${options} -fcolor-diagnostics -include iostream ${tmpFile} -o /tmp/cpp.out && /tmp/cpp.out`
case "linux":
return `g++ ${options} -std=c++14 -include iostream ${tmpFile} -o /tmp/cpp.out && /tmp/cpp.out`
let cmdArgs = ""

switch (os) {
case "darwin":
cmdArgs = `xcrun clang++ -std=c++14 ${options} -fcolor-diagnostics -include iostream ${tmpFile} -o /tmp/cpp.out && /tmp/cpp.out`
break
case "linux":
cmdArgs = `g++ ${options} -std=c++14 -include iostream ${tmpFile} -o /tmp/cpp.out && /tmp/cpp.out`
break
default: {
atom.notifications.addError(`Not support on ${os}`)
}
})()
return ["-c", args]
}
return ["-c", cmdArgs]
},
},

"File Based": {
command,
args({ filepath }) {
args = (() => {
switch (os) {
case "darwin":
return `xcrun clang++ -std=c++14 ${options} -fcolor-diagnostics -include iostream '${filepath}' -o /tmp/cpp.out && /tmp/cpp.out`
case "linux":
return `g++ -std=c++14 ${options} -include iostream '${filepath}' -o /tmp/cpp.out && /tmp/cpp.out`
case "win32":
if (
GrammarUtils.OperatingSystem.release()
.split(".")
.slice(-1 >= "14399")
) {
filepath = path.posix.join
.apply(
path.posix,
[].concat([filepath.split(path.win32.sep)[0].toLowerCase()], filepath.split(path.win32.sep).slice(1))
)
.replace(":", "")
return `g++ -std=c++14 ${options} -include iostream /mnt/${filepath} -o /tmp/cpp.out && /tmp/cpp.out`
}
break
let cmdArgs = ""
switch (os) {
case "darwin":
cmdArgs = `xcrun clang++ -std=c++14 ${options} -fcolor-diagnostics -include iostream '${filepath}' -o /tmp/cpp.out && /tmp/cpp.out`
break
case "linux":
cmdArgs = `g++ -std=c++14 ${options} -include iostream '${filepath}' -o /tmp/cpp.out && /tmp/cpp.out`
break
case "win32":
if (
GrammarUtils.OperatingSystem.release()
.split(".")
.slice(-1 >= "14399")
) {
filepath = path.posix.join
.apply(
path.posix,
[].concat([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 /tmp/cpp.out && /tmp/cpp.out`
}
break
default: {
atom.notifications.addError(`Not support on ${os}`)
}
})()
return GrammarUtils.formatArgs(args)
}
return GrammarUtils.formatArgs(cmdArgs)
},
},
}
Expand Down

0 comments on commit ff1fe86

Please sign in to comment.