diff --git a/src/main/CTests/test.c b/src/main/CTests/test.c index 5df7da4..ccadb5a 100644 --- a/src/main/CTests/test.c +++ b/src/main/CTests/test.c @@ -1,20 +1,47 @@ -int main(){ - int a = 0,b=1; - int k = add(a,b); - int j = munis(a,k); - print(k) -} +#include -int add(int a, int b){ - int c = a+b; +int add(int a, int b) { + int c = a + b; return c; } -short munis(int a,int b){ - int l = a; - int f = b; - int k = l - f; - int h = add(l,f); - int p = k / h; - return p; -} \ No newline at end of file +int subtract(int a, int b) { + int d = a - b; + return d; +} + +int multiply(int a, int b) { + int e = a * b; + return e; +} + +int divide(int a, int b) { + int f = a / b; + return f; +} + +int calculate(int a, int b) { + int sum = add(a, b); // 计算和 + int diff = subtract(b, a); // 计算差 + int prod = multiply(sum, diff); // 计算和与差的乘积 + return prod; // 返回乘积 +} + +short complexCalculation(int a, int b) { + int result = calculate(a, b); // 调用 calculate 函数 + int divResult = divide(result, b); // 使用 b 进行除法 + return divResult; // 返回结果 +} + +int main() { + int a = 6; + int b = 3; + + int result1 = calculate(a, b); // 计算 a 和 b 的结果 + short finalResult = complexCalculation(a, b); // 进行复杂计算 + + printf("Result of calculate: %d\n", result1); + printf("Final result of complexCalculation: %d\n", finalResult); + + return 0; +} diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 9300f77..f35e573 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,12 +1,16 @@ import androidx.compose.desktop.ui.tooling.preview.Preview import androidx.compose.foundation.background import androidx.compose.foundation.layout.* +import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.verticalScroll import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color +import androidx.compose.ui.text.ExperimentalTextApi +import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp @@ -18,6 +22,7 @@ import core.* import ui.TextfieldWithLineNumber import utils.* +@OptIn(ExperimentalTextApi::class) @Composable @Preview fun App() { @@ -51,7 +56,8 @@ fun App() { color = Color.Black, fontWeight = FontWeight.Bold, fontSize = 24.sp, - textAlign = TextAlign.Center + textAlign = TextAlign.Center, + modifier = Modifier.padding(top = 10.dp) ) }, text = { @@ -65,9 +71,12 @@ fun App() { }, modifier = Modifier .fillMaxSize() + .verticalScroll(rememberScrollState()) .wrapContentHeight(), textAlign = TextAlign.Start, - color = Color.Black + color = Color.Black, + fontSize = 18.sp, + fontFamily = FontFamily("monospace") ) }, onDismissRequest = { @@ -107,7 +116,7 @@ fun App() { }, modifier = Modifier .padding(5.dp) - .wrapContentSize(), + .wrapContentSize(align = Alignment.Center), shape = RoundedCornerShape(20.dp), backgroundColor = Color(0xffFAF0E6) ) @@ -164,7 +173,7 @@ fun App() { traceTree = traceTreeStr.toString() funcTraceTree = - getInvokeTraceTreeString("main",sf,relations) + getInvokeTraceTreeString(sf,relations) ttGraph = generateGraph(relations) ivGraph = sf.functions.getInvokeGraph() diff --git a/src/main/kotlin/core/CParser.kt b/src/main/kotlin/core/CParser.kt index e5fcec0..1f20d26 100644 --- a/src/main/kotlin/core/CParser.kt +++ b/src/main/kotlin/core/CParser.kt @@ -92,8 +92,8 @@ class CParser(content: String, paramNameList: List) { val funName = match.groupValues[1].trim() val params = match.groupValues[2].trim().split(',') for (param in params) { - if (param in defineList) { // 定义过的才操作 - result.add(Sentence(funName, param, getType())) + if (param.trim() in defineList) { // 定义过的才操作 + result.add(Sentence(funName, param.trim(), getType())) } } } diff --git a/src/main/kotlin/core/SourceFile.kt b/src/main/kotlin/core/SourceFile.kt index 7702eb5..ccc382d 100644 --- a/src/main/kotlin/core/SourceFile.kt +++ b/src/main/kotlin/core/SourceFile.kt @@ -150,7 +150,7 @@ class SourceFile { } fun main() { - val sourceFile = SourceFile(File("/home/kagura/repo/VariableRelation/src/main/CTests/test.c")) + val sourceFile = SourceFile(File("src/main/CTests/test.c")) val funcs = sourceFile.parseFunction() val relations = mutableMapOf>() funcs.forEach { diff --git a/src/main/kotlin/core/TraceTree.kt b/src/main/kotlin/core/TraceTree.kt index d8ccf4f..9761827 100644 --- a/src/main/kotlin/core/TraceTree.kt +++ b/src/main/kotlin/core/TraceTree.kt @@ -21,7 +21,11 @@ fun List.findNode(name: String): TraceTree? { fun List.getStringRepr(): String { - fun getChanges(node: TraceTree, currentPath: String = node.name): String { + fun getChanges(node: TraceTree, currentPath: String = node.name, recTimes: Int = 0): String { + if (recTimes >= 10){ // 防止无限循环 + return "" + } + val paths = mutableListOf() // 没修改过就介绍 @@ -30,7 +34,7 @@ fun List.getStringRepr(): String { } else { // 挨个获取 change for (child in node.changes) { - paths.add(getChanges(child, "$currentPath->${child.name}")) + paths.add(getChanges(child, "$currentPath->${child.name}",recTimes + 1)) } } diff --git a/src/main/kotlin/utils/OpenBrowser.kt b/src/main/kotlin/utils/OpenBrowser.kt index ecd17ea..f4c93f3 100644 --- a/src/main/kotlin/utils/OpenBrowser.kt +++ b/src/main/kotlin/utils/OpenBrowser.kt @@ -20,7 +20,7 @@ fun openBrowser(uri: URI) { }catch (_: IOException){ // xdg-open 不存在 if (File("/run/current-system/sw/bin/xdg-open").exists()){ // nixos - Runtime.getRuntime().exec("/run/current-system/sw/bin/xdg-open $uri") + Runtime.getRuntime().exec("/run/current-system/sw/bin/kde-open $uri") }else{ val clipboard = Toolkit.getDefaultToolkit().systemClipboard val content = StringSelection(uri.toString()) diff --git a/src/main/kotlin/utils/stringMagic.kt b/src/main/kotlin/utils/stringMagic.kt index bdda941..2978094 100644 --- a/src/main/kotlin/utils/stringMagic.kt +++ b/src/main/kotlin/utils/stringMagic.kt @@ -9,6 +9,8 @@ fun getDefineList(defList: Map>): String = .replace("{", "") .replace("}", "") // 解决左右括号 .replace("=", ":def") + .replace("], ","]\n") + fun getUseList(useList: Map>): String = // Example: @@ -17,7 +19,7 @@ fun getUseList(useList: Map>): String = .replace("{}", "") // 如果是空就直接删了 .replace("{", "") // 右括号留着 .replace("=", ":{") // 换掉 - .replace("], ", "]}\n") // 保证不是最后一个 + .replace("], ", "]}\n\n") // 保证不是最后一个 .replace(", ", "],[") // 逗号后面有空格 .replace(";", "、") @@ -29,9 +31,19 @@ fun getTraceTreeString(traceTreeStr: String, funcName: String): String { return sb.toString() } -fun getInvokeTraceTreeString(funcName: String, sourceFile: SourceFile, relations: Map>): String { - val func = sourceFile.functions.find(funcName) ?: return "未找到 $funcName 函数" - val originPart = getTraceTreeString(func.getTraceTree().getStringRepr(), func.name) - val invokeTree = sourceFile.functions.getInvokeTree(funcName).getFuncRepr(funcName) - return originPart + "\n" + invokeTree +fun getInvokeTraceTreeString(sourceFile: SourceFile, relations: Map>): String { + val tree = StringBuilder() + for (function in sourceFile.functions) { + val chain = sourceFile.functions.getInvokeTree(function.name).getFuncRepr(function.name) + if (chain.isNotBlank()){ + tree.append("在 ${function.name} 函数中调用过下面的函数:\n") + tree.append(chain) + tree.append("\n\n") + } + } + return tree.toString() + .replace("[","") + .replace("]","") + .replace(", ","\n") + } \ No newline at end of file