style: reformat

This commit is contained in:
Kagura 2024-11-01 10:05:16 +08:00
parent 0e461e1419
commit 008af1f7e3
7 changed files with 47 additions and 46 deletions

View file

@ -32,7 +32,6 @@ import utils.*
@Composable @Composable
@Preview @Preview
fun App() { fun App() {
var source: SourceFile? = null
val showResult = remember { mutableStateOf(false) } val showResult = remember { mutableStateOf(false) }
var defineList by remember { mutableStateOf("") } var defineList by remember { mutableStateOf("") }
var useList by remember { mutableStateOf("") } var useList by remember { mutableStateOf("") }
@ -51,8 +50,10 @@ fun App() {
}) { }) {
AlertDialog( AlertDialog(
title = { title = {
Column(modifier = Modifier.fillMaxWidth(), Column(
horizontalAlignment = Alignment.CenterHorizontally) { modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text( Text(
text = when (page) { text = when (page) {
0 -> "函数定义的变量" 0 -> "函数定义的变量"
@ -69,8 +70,10 @@ fun App() {
} }
}, },
text = { text = {
Box(modifier = Modifier.fillMaxWidth() Box(
.fillMaxHeight(0.8f)) { modifier = Modifier.fillMaxWidth()
.fillMaxHeight(0.8f)
) {
Text( Text(
text = when (page) { text = when (page) {
0 -> defineList 0 -> defineList
@ -104,21 +107,21 @@ fun App() {
dismissButton = { dismissButton = {
TextButton( TextButton(
onClick = { onClick = {
if (page < 2){ if (page < 2) {
showResult.value = false showResult.value = false
}else{ } else {
if (page == 2){ if (page == 2) {
openGraph(ttGraph) openGraph(ttGraph)
}else if (page == 3){ } else if (page == 3) {
openGraph(ivGraph) openGraph(ivGraph)
} }
} }
} }
) { ) {
Text( Text(
if (page < 2){ if (page < 2) {
"关闭" "关闭"
}else{ } else {
"查看图像(在线)" "查看图像(在线)"
} }
) )
@ -151,7 +154,6 @@ fun App() {
return@TextfieldWithLineNumber return@TextfieldWithLineNumber
} }
val sf = SourceFile(it) val sf = SourceFile(it)
source = sf
try { try {
sf.parseFunction() sf.parseFunction()
} catch (e: Exception) { } catch (e: Exception) {
@ -197,8 +199,7 @@ fun App() {
traceTree = traceTreeStr.toString() traceTree = traceTreeStr.toString()
funcTraceTree = funcTraceTree = getInvokeTraceTreeString(sf)
getInvokeTraceTreeString(sf)
ttGraph = generateGraph(relations) ttGraph = generateGraph(relations)
ivGraph = sf.functions.getInvokeGraph() ivGraph = sf.functions.getInvokeGraph()

View file

@ -95,20 +95,19 @@ class CParser(content: String, paramNameList: List<String>) {
} }
} }
} }
} } else {
else {
if (right in defineList) { // 看看有没有使用值 if (right in defineList) { // 看看有没有使用值
result.add( result.add(
Sentence( Sentence(
"-", right, getType() "-", right, getType()
) )
) )
} else{ // 看是不是有运算符号 } else { // 看是不是有运算符号
for (op in c_op_list){ for (op in c_op_list) {
if (op in right){ if (op in right) {
val splits = right.split(op) val splits = right.split(op)
splits.forEach { split -> splits.forEach { split ->
result.addAll(rightHandSimpleParser(split.trim(),type)) result.addAll(rightHandSimpleParser(split.trim(), type))
} }
} }
} }

View file

@ -34,7 +34,7 @@ fun generateGraph(relations: Map<String, List<Relation>>, crossLabelPaths: List<
// from 到 to 用虚线 // from 到 to 用虚线
val str = "$fromNode${parts[1]} -> $toNode${parts[0]} [style=dashed];" val str = "$fromNode${parts[1]} -> $toNode${parts[0]} [style=dashed];"
val hash = str.hashCode() val hash = str.hashCode()
if (hash !in hashes){ // 不重复 if (hash !in hashes) { // 不重复
sb.append(str) sb.append(str)
hashes.add(hash) hashes.add(hash)
} }
@ -48,18 +48,18 @@ fun generateGraph(relations: Map<String, List<Relation>>, crossLabelPaths: List<
} }
fun parseRelation(funcName: String, parse: String): Map<String, List<Relation>>{ fun parseRelation(funcName: String, parse: String): Map<String, List<Relation>> {
val list = mutableListOf<Relation>() val list = mutableListOf<Relation>()
val added = mutableListOf<Int>() val added = mutableListOf<Int>()
parse.split('\n').forEach { line -> parse.split('\n').forEach { line ->
val split = line.split("->") val split = line.split("->")
if (split.size < 2){ if (split.size < 2) {
return@forEach return@forEach
} }
for (i in 0..<split.lastIndex){ for (i in 0..<split.lastIndex) {
val hash = "${split[i]}->${split[i+1]}".hashCode() val hash = "${split[i]}->${split[i + 1]}".hashCode()
if (hash !in added){ if (hash !in added) {
list.add(Relation(split[i],split[i+1])) list.add(Relation(split[i], split[i + 1]))
added.add(hash) added.add(hash)
} }
} }
@ -73,9 +73,9 @@ fun List<CFunction>.getInvokeGraph(): String {
val relations = mutableMapOf<String, List<Relation>>() val relations = mutableMapOf<String, List<Relation>>()
val crossLabelPaths = StringBuilder() val crossLabelPaths = StringBuilder()
this.forEach { this.forEach {
relations.putAll(parseRelation(it.name,it.getTraceTree().getStringRepr())) relations.putAll(parseRelation(it.name, it.getTraceTree().getStringRepr()))
crossLabelPaths.append(this.getInvokeTree(it.name).getFuncRepr(it.name)) crossLabelPaths.append(this.getInvokeTree(it.name).getFuncRepr(it.name))
crossLabelPaths.append('\n') crossLabelPaths.append('\n')
} }
return generateGraph(relations,crossLabelPaths.split('\n').filter { it.isNotEmpty() }) return generateGraph(relations, crossLabelPaths.split('\n').filter { it.isNotEmpty() })
} }

View file

@ -22,7 +22,7 @@ fun List<TraceTree>.findNode(name: String): TraceTree? {
fun List<TraceTree>.getStringRepr(): String { fun List<TraceTree>.getStringRepr(): String {
fun getChanges(node: TraceTree, currentPath: String = node.name, recTimes: Int = 0): String { fun getChanges(node: TraceTree, currentPath: String = node.name, recTimes: Int = 0): String {
if (recTimes >= 10){ // 防止无限循环 if (recTimes >= 10) { // 防止无限循环
return "" return ""
} }
@ -34,7 +34,7 @@ fun List<TraceTree>.getStringRepr(): String {
} else { } else {
// 挨个获取 change // 挨个获取 change
for (child in node.changes) { for (child in node.changes) {
paths.add(getChanges(child, "$currentPath->${child.name}",recTimes + 1)) paths.add(getChanges(child, "$currentPath->${child.name}", recTimes + 1))
} }
} }
@ -56,7 +56,7 @@ fun Map<String, List<TraceTree>>.getFuncRepr(func: String): String {
this.forEach { (name, list) -> this.forEach { (name, list) ->
val listRepr = list.getStringRepr() val listRepr = list.getStringRepr()
for (line in listRepr.lines()) { for (line in listRepr.lines()) {
if (line.startsWith(func)){ // main:x-> 这样的 if (line.startsWith(func)) { // main:x-> 这样的
result.append("$name:$line\n") result.append("$name:$line\n")
} }
} }

View file

@ -97,7 +97,7 @@ fun TextfieldWithLineNumber(
onValueChange = { textFieldValue -> onValueChange = { textFieldValue ->
val nbLines = textFieldValue.count { it == '\n' } + 1 val nbLines = textFieldValue.count { it == '\n' } + 1
if (nbLines != linesText) linesText = nbLines if (nbLines != linesText) linesText = nbLines
text = textFieldValue.replace("\t"," ") // \t 显示不了 text = textFieldValue.replace("\t", " ") // \t 显示不了
}, },
) )

View file

@ -17,24 +17,25 @@ fun openBrowser(uri: URI) {
"nix" in osName || "nux" in osName -> { "nix" in osName || "nux" in osName -> {
try { try {
Runtime.getRuntime().exec("xdg-open $uri") Runtime.getRuntime().exec("xdg-open $uri")
}catch (_: IOException){ } catch (_: IOException) {
// xdg-open 不存在 // xdg-open 不存在
if (File("/run/current-system/sw/bin/xdg-open").exists()){ // nixos if (File("/run/current-system/sw/bin/xdg-open").exists()) { // nixos
Runtime.getRuntime().exec("/run/current-system/sw/bin/kde-open $uri") Runtime.getRuntime().exec("/run/current-system/sw/bin/kde-open $uri")
}else{ } else {
val clipboard = Toolkit.getDefaultToolkit().systemClipboard val clipboard = Toolkit.getDefaultToolkit().systemClipboard
val content = StringSelection(uri.toString()) val content = StringSelection(uri.toString())
clipboard.setContents(content, null) clipboard.setContents(content, null)
} }
} }
} }
else -> desktop.browse(uri) else -> desktop.browse(uri)
} }
} }
fun openGraph(graph: String){ fun openGraph(graph: String) {
val site = "https://dreampuf.github.io/GraphvizOnline/#" val site = "https://dreampuf.github.io/GraphvizOnline/#"
val data = java.net.URLEncoder.encode(graph,"utf-8") val data = java.net.URLEncoder.encode(graph, "utf-8")
.replace("+","%20") .replace("+", "%20")
openBrowser(URI.create(site+data)) openBrowser(URI.create(site + data))
} }

View file

@ -9,7 +9,7 @@ fun getDefineList(defList: Map<String, List<String>>): String =
.replace("{", "") .replace("{", "")
.replace("}", "") // 解决左边和右边的括号 .replace("}", "") // 解决左边和右边的括号
.replace("=", ":def") .replace("=", ":def")
.replace("], ","]\n") .replace("], ", "]\n")
fun getUseList(useList: Map<String, List<String>>): String = fun getUseList(useList: Map<String, List<String>>): String =
@ -35,15 +35,15 @@ fun getInvokeTraceTreeString(sourceFile: SourceFile): String {
val tree = StringBuilder() val tree = StringBuilder()
for (function in sourceFile.functions) { for (function in sourceFile.functions) {
val chain = sourceFile.functions.getInvokeTree(function.name).getFuncRepr(function.name) val chain = sourceFile.functions.getInvokeTree(function.name).getFuncRepr(function.name)
if (chain.isNotBlank()){ if (chain.isNotBlank()) {
tree.append("${function.name} 函数中调用过下面的函数:\n") tree.append("${function.name} 函数中调用过下面的函数:\n")
tree.append(chain) tree.append(chain)
tree.append("\n\n") tree.append("\n\n")
} }
} }
return tree.toString() return tree.toString()
.replace("[","") .replace("[", "")
.replace("]","") .replace("]", "")
.replace(", ","\n") .replace(", ", "\n")
} }