chore: fix style
This commit is contained in:
parent
1120374505
commit
dfc9815e83
2 changed files with 20 additions and 11 deletions
|
@ -4,7 +4,7 @@
|
|||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
<file type="web" url="file://$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
|
@ -11,6 +11,15 @@ class SourceFile {
|
|||
companion object {
|
||||
val function_define_regex = """\b([a-zA-Z_][a-zA-Z0-9_]*)\s*\([^)]*\)\s*\{""".toRegex() // main(){ 匹配函数名
|
||||
|
||||
/**
|
||||
* C文件预处理器
|
||||
* 处理内容:
|
||||
* 删除注释;
|
||||
* 处理 #define;
|
||||
* 处理连续创建变量;
|
||||
* 格式化代码
|
||||
* @return 处理好的C源代码
|
||||
*/
|
||||
fun formatSource(src: String): String {
|
||||
// 去掉注释
|
||||
val noComment = src.lines()
|
||||
|
@ -30,7 +39,7 @@ class SourceFile {
|
|||
if (line.trim().startsWith("#define")) {
|
||||
val args = line.trim().split(" ")
|
||||
if (args.size >= 3) {
|
||||
if (args[1].contains("(")){ // Define Function
|
||||
if (args[1].contains("(")) { // Define Function
|
||||
val defFuncName = args[1].split('(').first().trim()
|
||||
val defineFuncArgs = args[1]
|
||||
.split('(').last()
|
||||
|
@ -44,31 +53,31 @@ class SourceFile {
|
|||
val prev = line.split("$defFuncName(") // 应该是调用之前的内容
|
||||
val later = prev.last().split(')') // 应该是调用之后的内容
|
||||
val callArgs = later.first().split(',').map { it.trim() } // 和上面一样的
|
||||
if (callArgs.size <= defineFuncArgs.size){
|
||||
if (callArgs.size <= defineFuncArgs.size) {
|
||||
var newFunction = defineFunc
|
||||
for (i in callArgs.indices){ // 参数不够不管了
|
||||
newFunction = newFunction.replace(defineFuncArgs[i],callArgs[i])
|
||||
for (i in callArgs.indices) { // 参数不够不管了
|
||||
newFunction = newFunction.replace(defineFuncArgs[i], callArgs[i])
|
||||
}
|
||||
//改回去
|
||||
noFuncInDefine.append(prev.first() + newFunction + later.last())
|
||||
noFuncInDefine.append('\n')
|
||||
}else{ // 参数多了我有啥办法
|
||||
} else { // 参数多了我有啥办法
|
||||
noFuncInDefine.append(line)
|
||||
noFuncInDefine.append('\n')
|
||||
}
|
||||
}else if (!line.startsWith('#')) {
|
||||
} else if (!line.startsWith('#')) {
|
||||
noFuncInDefine.append(line)
|
||||
noFuncInDefine.append('\n')
|
||||
}
|
||||
}
|
||||
}else { // #define SOMETHING ANOTHER 这样的
|
||||
} else { // #define SOMETHING ANOTHER 这样的
|
||||
simpleDefineList[args[1]] = args[2]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 可能上面代码因为没define没执行,这里保证下
|
||||
if (noFuncInDefine.isEmpty()){
|
||||
if (noFuncInDefine.isEmpty()) {
|
||||
noFuncInDefine.append(noComment)
|
||||
}
|
||||
|
||||
|
@ -84,7 +93,7 @@ class SourceFile {
|
|||
noDefine.append('\n')
|
||||
}
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
noDefine.append(noFuncInDefine)
|
||||
}
|
||||
|
||||
|
@ -142,7 +151,7 @@ class SourceFile {
|
|||
}
|
||||
|
||||
constructor(content: String) {
|
||||
this.content = formatSource(content.trim())
|
||||
this.content = formatSource(content)
|
||||
}
|
||||
|
||||
fun parseFunction(): List<CFunction> { // 找所有的函数定义
|
||||
|
|
Loading…
Reference in a new issue