1
0

update c snippets

This commit is contained in:
Patrick Auernig 2016-05-08 17:25:02 +02:00
parent 9fe574d716
commit 1ce695d4db
2 changed files with 49 additions and 9 deletions

View File

@ -8,7 +8,7 @@ snippet fun "C function" b
${1:int} ${2:main}(${3:void}) ${1:int} ${2:main}(${3:void})
{ {
${0} ${0}
`!p snip.rv = returnval(t[1])` return `!p snip.rv = returnval(t[1], t[2])`;
} }
endsnippet endsnippet
@ -46,12 +46,12 @@ endsnippet
snippet malloc "Allocate memory on the heap" snippet malloc "Allocate memory on the heap"
${1:TYPE} *${2:NAME} = ($1 *)malloc(sizeof(*$2)); ${1:TYPE} ${2:NAME} = ($1)malloc(sizeof(*$2));
endsnippet endsnippet
snippet calloc "Allocate memory on the heap" snippet calloc "Allocate memory on the heap"
${1:TYPE} *${2:NAME} = ($1 *)calloc(${3:N}, sizeof(*$2)); ${1:TYPE} ${2:NAME} = ($1)calloc(${3:N}, sizeof(*$2));
endsnippet endsnippet
@ -60,3 +60,40 @@ snippet incl "Default includes"
#include <stdio.h> #include <stdio.h>
endsnippet endsnippet
snippet for "For loop"
for (${1:INITIALIZER}; ${2:TEST}; ${3:INCREMENTER}) \{
${4:#error "incomplete for loop"}
\}
endsnippet
snippet while "While loop"
while (${1:TEST}) \{
${2:#error "incomplete while loop"}
\}
endsnippet
snippet if "If-Then control construct"
if (${1:TEST}) \{
${2:#error "incomplete if branch"}
\}
endsnippet
snippet ite "If-Then-Else control construct"
if (${1:TEST}) \{
${2:#error "incomplete if branch"}
\} else \{
${3:#error "incomplete else branch"}
\}
endsnippet
snippet elif "Else-If control construct"
else if (${1:TEST}) \{
${2:#error "incomplete elif branch"}
\}
endsnippet

View File

@ -1,7 +1,10 @@
def returnval(t): def returnval(rtype, fname):
if t == "void": if rtype == "void":
return "" return ""
if fname == "main":
return "EXIT_SUCCESS"
ret = { ret = {
"int": "0", "int": "0",
"float": "0.0", "float": "0.0",
@ -9,10 +12,10 @@ def returnval(t):
"bool": "false" "bool": "false"
} }
if t in ret.keys(): if rtype in ret.keys():
return "return " + ret[t] + ";" return ret[rtype]
else: else:
return "return NULL;" return "NULL"
def getNoteCandidates(t): def getNoteCandidates(t):
options = ["TODO", "FIXME", "XXX"] options = ["TODO", "FIXME", "XXX"]
@ -21,6 +24,6 @@ def getNoteCandidates(t):
if len(options) == 1: if len(options) == 1:
return options[0] return options[0]
return "[" + ",".join(options) + "]" return "[" + ",".join(options) + "]"