Many of us have been in the position where the sof...
# random
n
Many of us have been in the position where the software we have developed has been used in unexpected ways:

https://youtu.be/baY3SaIhfl0

One example of this is where some Kotliners use the IntelliJ Kotlin plugin to process C code that is pasted into a Kotlin source file 😲. Some Kotliners will be wanting a new feature where C code is converted to Kotlin code. Already there is a Java code converter, which interprets some C code as Java code to be converted to Kotlin 😂 🤣 . It is only a matter of time before the feature request is created in the Kotlin Issue Tracker ⏲️. Did anyone on the Kotlin team think about how the pasting process works in regard to text sources with the IntelliJ Kotlin plugin?
😂 2
y
Fun fact, apparently the Jetbrains Toolbox team originally had the app implemented in C++, and so when they were making the move over to kotlin, they just copy-pasted C++ code and used the Java-to-Kotlin converter and apparently it worked nearly perfectly, so you're not far off from the truth in the slightest lol.
😲 1
😆 1
n
Considering what has happened in that use case the Kotlin team might as well take the Java code converter, and make the modifications to create a C code converter that can be used with Kotlin Native projects 😁.
I bet the Kotlin team were very shocked when they heard about that use case 😆 .
j
Copy code
#!/bin/bash 
set -x 
 
#built and tested against clion default formatted c ymmv 
sed --in-place --regexp-extended\ 
 -e 's,^static\s+(.*\{)$,\1,g'                                                                           \ 
 -e 's,(\w+)([ *]+)(\w+)\[\s*\],\1\2*\3,g'                                                               \ 
 -e 's,for\s*\((\w+\s+)(\w+)\s*=\s*(\w+)\;\s*\2.*<([^;]+)\;.*(\2?(\+\+)\2?).*\),for (\2/*as \1*/ in \3 until \4),' \ 
 -e 's,for\s*\((\w+)\s*=\s*(\w+)\;\s*\1.*<([^;]+)\;.*(\1?(\+\+)\1?).*\),for (\1 in \2 until \3),'        \ 
 -e 's,^\s*void\s*(\w+)\s*(\(.*\))\s*\{\s*$,fun \1\2:Unit{,'                                             \ 
 -e 's,^\s*(\w+_t)\s*(\w+)\s*(\(.*\))\s*\{\s*$,fun \2\3:\1{,'                                            \ 
 -e 's,^\s*(\w+)\s*[*]\s*(\w+)\s*(\(.*\))\s*\{\s*$,fun \2\3:CPointer<\1>{,'                              \ 
 -e 's,^\s*(\w+)(\s*[*]\s*){2}(\w+)\s*(\(.*\))\s*\{\s*$,fun \3\4:CPointerVarOf<CPointer<\1>>{,'          \ 
 -e 's,^\s*int\s*(\w+)\s*(\(.*\))\s*\{\s*$,fun \1\2:Int{,'                                               \ 
 -e 's,^\s*long\s*(\w+)\s*(\(.*\))\s*\{\s*$,fun \1\2:Long{,'                                             \ 
 -e 's,^\s*short\s*(\w+)\s*(\(.*\))\s*\{\s*$,fun \1\2:Short{,'                                           \ 
 -e 's,^\s*bool\s*(\w+)\s*(\(.*\))\s*\{\s*$,fun \1\2:Boolean{,'                                          \ 
 -e 's,^\s*(long\s+(int)?)+\s*(\w+)\s*(\(.*\))\s*\{\s*$,fun \2\3:Long{,'                                 \ 
 -e 's,^\s*const\s+char\s*[*]\s*(\w+)\s*(\(.*\))\s*\{\s*$,fun \1\2:String{,'                             \ 
 -e 's,^\s*const\s+char\s*[*][*]\s*(\w+)\s*(\(.*\))\s*\{\s*$,fun \1\2:Array<String>{,'                   \ 
 -e 's,(\s+|\W)const char\s*\*\s*(\w+)(\W),\1\2:String\3,g'                                              \ 
 -e 's,(\s+|\W)unsigned\s+char\s+(\w+)(\W),\1\2:UByte\3,g'                                               \ 
 -e 's,(\s+|\W)unsigned\s+short\s+(\w+)(\W),\1\2:UShort\3,g'                                             \ 
 -e 's,(\s+|\W)unsigned\s+(long\s+)+(int\s+)?+(\w+)(\W),\1\4:ULong \5,g'                                 \ 
 -e 's,(\s+|\W)unsigned\s+char\s+(\w+)(\W),\1\2:UByte\3,g'                                               \ 
 -e 's,(\s+|\W)unsigned\s+(int\s+)?(\w+)(\W),\1\3:UInt\4,g'                                              \ 
 -e 's,(\s+|\W)int\s+(\w+)(\W),\1\2:Int\3,g'                                                             \ 
 -e 's,(\s+|\W)bool\s+(\w+)(\W),\1\2:Boolean\3,g'                                                        \ 
 -e 's,(\s+|\W)(long\s+)+(int)?+(\w+)(\W),\1\4:Long\5,g'                                                 \ 
 -e 's,(\s+|\W)(\w+)_t\s+(\w+)(\W),\1\3:\2_t\4,g'                                                        \ 
 -e 's,(\s+|\W)(void|char)\s*[*]\s*(\w+)(\W),\1\2:CPointer<ByteVar>\3,'                                  \ 
 -e 's,(\s+|\W)(void|char)\s*[*]\s*[*]\s*(\w+)(\W),\1\3:CPointerVarOf<CPointer<ByteVar>>\4,'             \ 
 -e 's,struct\s+(\w+)\s+(\w+)(\s*\W),\2:\1\3,'                                                           \ 
 -e 's,(\W)(struct\s+)?(\w+)\s*[*]\s*([A-Za-z]\w*)(\W),\1\4:CPointer<\3>\5,'                             \ 
 -e 's,(\W)(struct\s+)?(\w+)\s*[*][*]\s*([A-Za-z]\w*)(\W),\1\4:CPointerVarOf<CPointer<\3>>\5,'           \ 
 -e 's,(\w+)\s*[-][>]\s*(\w+),\1.pointed.\2,g'                                                           \ 
 -e 's,(\s+|\W)(\w+_t)\s+(\w+)(\W),\1\3:\2\4,g'                                                          \ 
 -e 's,switch(.*)[{]$,when \1 {,'                                                                        \ 
 -e 's,case (.*):,\1 -> ,'                                                                               \ 
 -e 's,(\w+)\s*[|]\s*(\w+), \1 or \2 ,g'                                                                 \ 
 -e 's,(\w+)\s*[&]\s*(\w+), \1 and \2 ,g'                                                                \ 
 -e 's,([^&]+)\&((\w+|[.[\]])+),\1\2.ptr,g'                                                              \ 
 -e 's,(\s+)goto\s+(\w+),\1break@\2,'                                                                    \ 
 -e 's,CPointer<int>,CPointer<Int>,g'                                                                    \ 
 -e 's,fun\s+(\w+)([^{]+)\{,fun \1\2{\n\tval __FUNCTION__="\1"\n,'                                       \ 
 -e 's,^[#]include,//include,'                                                                           \ 
  $@
1