This commit is contained in:
kagura 2024-10-30 19:28:08 +08:00
parent 99c9440a39
commit ae6271aa51
7 changed files with 84 additions and 32 deletions

View file

@ -1,20 +1,47 @@
int main(){
int a = 0,b=1;
int k = add(a,b);
int j = munis(a,k);
print(k)
}
#include <stdio.h>
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;
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;
}

View file

@ -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()

View file

@ -92,8 +92,8 @@ class CParser(content: String, paramNameList: List<String>) {
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()))
}
}
}

View file

@ -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<String, List<Relation>>()
funcs.forEach {

View file

@ -21,7 +21,11 @@ fun List<TraceTree>.findNode(name: String): TraceTree? {
fun List<TraceTree>.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<String>()
// 没修改过就介绍
@ -30,7 +34,7 @@ fun List<TraceTree>.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))
}
}

View file

@ -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())

View file

@ -9,6 +9,8 @@ fun getDefineList(defList: Map<String, List<String>>): String =
.replace("{", "")
.replace("}", "") // 解决左右括号
.replace("=", ":def")
.replace("], ","]\n")
fun getUseList(useList: Map<String, List<String>>): String =
// Example:
@ -17,7 +19,7 @@ fun getUseList(useList: Map<String, List<String>>): 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, List<Relation>>): 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, List<Relation>>): 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")
}