This commit is contained in:
Kagura 2024-11-06 22:32:26 +08:00
parent 0bba6d9943
commit 5d7d1126c8
4 changed files with 104 additions and 73 deletions

View file

@ -1,49 +1,37 @@
#include <stdio.h> #include <stdio.h>
#define PLUS(first,later) first+later
// 你看我还能用 define struct Point {int x, y;};
// 当然也能写注释
int add(int a, int b) { void A(struct Point *p, int a, int b) {
int c = PLUS(a,b); int m, n;
return c; m = a + b; // m-def
n = a * b; // n-def
p->x = m; // p->x-def
p->y = n; // p->y-def
for (int i = 0; i < 5; i++) { // i-def
m = m - i; // m-def, i-use
if (n > m) { // n-use, m-use
n = n + i; // n-def, i-use
}
}
printf("A: m = %d, n = %d, p->x = %d, p->y = %d\n", m, n, p->x, p->y);
} }
int subtract(int a, int b) { void B(struct Point *po,int x, int y, int z) {
int d = a - b; int p = x + y; // p-def
return d; int q = y * z; // q-def
} for (int i = 0; i < 10; i++) { // i-def
p = p - i; // p-def, i-use
int multiply(int a, int b) { q = q + i; // q-def, i-use
int e = a * b; }
return e; A(po, p, q); // A: x-use, p-use, q-use
} printf("B: p = %d, q = %d\n", p, q);
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 main() {
int a = 6; int x = 5, y = 3, z = 2; // x-def, y-def, z-def
int b = 3; struct Point p1 = {1, 2}; // p1-def
B(&p1,x, y, z); // B: x-use, y-use, z-use, p1-use
int result1 = calculate(a, b); // 计算 a 和 b 的结果 printf("main: p1.x = %d, p1.y = %d\n", p1.x, p1.y);
short finalResult = complexCalculation(a, b); // 进行复杂计算
printf("Result of calculate: %d\n", result1);
printf("Final result of complexCalculation: %d\n", finalResult);
return 0; return 0;
} }

View file

@ -2,6 +2,7 @@ package core
import core.CLanguage.Companion.OPERATION.* import core.CLanguage.Companion.OPERATION.*
import core.CLanguage.Companion.typenameToTypeEnum import core.CLanguage.Companion.typenameToTypeEnum
import kotlinx.coroutines.delay
import utils.Helper.Companion.antiC import utils.Helper.Companion.antiC
class CFunction( class CFunction(
@ -39,18 +40,18 @@ class CFunction(
val param = it.split(' ').last() val param = it.split(' ').last()
val name = if (param.contains('`')) { // struct`*po -> *po) val name = if (param.contains('`')) { // struct`*po -> *po)
param.split('`').last() param.split('`').last()
}else{ } else {
param param
} }
antiC(name) antiC(name)
} }
} }
return paramsList?.map { param -> return paramsList?.map { param ->
if (param.contains('`')){ // struct`*po -> *po if (param.contains('`')) { // struct`*po -> *po
val name = antiC(param.split(' ').last()) val name = antiC(param.split(' ').last())
val type = CLanguage.Companion.TYPES.void val type = CLanguage.Companion.TYPES.void
mapOf(name to type) mapOf(name to type)
}else { } else {
val parts = param.split(" ") val parts = param.split(" ")
val type = typenameToTypeEnum(parts[0]) ?: return@map emptyMap() val type = typenameToTypeEnum(parts[0]) ?: return@map emptyMap()
val name = antiC(parts.getOrNull(1) ?: "") val name = antiC(parts.getOrNull(1) ?: "")
@ -93,6 +94,22 @@ class CFunction(
// 然后根据句子来获得 TraceTree // 然后根据句子来获得 TraceTree
var paramIndex = 0 // 标记是第几个参数 var paramIndex = 0 // 标记是第几个参数
cParser.sentenceList.forEachIndexed { index, sentence -> cParser.sentenceList.forEachIndexed { index, sentence ->
// 修复 node list
if (sentence.left.contains('.')) {
if (sentence.left.split('.').first() in cParser.defineList) {
if (nodes.findNode(sentence.left) == null) {
nodes.add(TraceTree(sentence.left, mutableListOf<TraceTree>()))
}
}
}
if (sentence.right.contains('.')) {
if (sentence.right.split('.').first() in cParser.defineList) {
if (nodes.findNode(sentence.right) == null) {
nodes.add(TraceTree(sentence.right, mutableListOf<TraceTree>()))
}
}
}
if (sentence.type == DEFINE_VALUE_PARAM || sentence.type == CHANGE_VALUE_PARAM) { if (sentence.type == DEFINE_VALUE_PARAM || sentence.type == CHANGE_VALUE_PARAM) {
// 例子: // 例子:
// x,A(z),CHANGE_VALUE // x,A(z),CHANGE_VALUE
@ -106,10 +123,12 @@ class CFunction(
left = cParser.sentenceList[index - i].left left = cParser.sentenceList[index - i].left
i++ i++
} }
val lNode = nodes.findNode(left) if (left != sentence.right) {
val rNode = nodes.findNode(sentence.right) val lNode = nodes.findNode(left)
if (lNode != null && rNode != null) { val rNode = nodes.findNode(sentence.right)
rNode.changes.add(lNode) if (lNode != null && rNode != null) {
rNode.changes.add(lNode)
}
} }
} }
} else if (sentence.type == PARAM && invokeInfo != null) { } else if (sentence.type == PARAM && invokeInfo != null) {
@ -157,29 +176,38 @@ fun List<CFunction>.getInvokeTree(name: String): Map<String, List<TraceTree>> {
var funcName = "" var funcName = ""
val resultMap = mutableMapOf<String, List<TraceTree>>() val resultMap = mutableMapOf<String, List<TraceTree>>()
fun update() {
val function = this.find(funcName)
if (function != null) {
val info = InvokeInfo(name, args)
val tt = function.getTraceTree(info)
// 过滤 trace tree
invokeTrees.addAll(tt.filter { it.name.startsWith(name) })
}
if (funcName != "") {
val newTree = invokeTrees.toList()
resultMap[funcName] = newTree // 因为之后要清空
invokeTrees.clear()
args.clear()
funcName = ""
}
}
func.cParser.sentenceList.forEach { sentence -> func.cParser.sentenceList.forEach { sentence ->
// 🌰: // 🌰:
// A,z,CHANGE_VALUE_PARAM // A,z,CHANGE_VALUE_PARAM
if (sentence.type == CHANGE_VALUE_PARAM || sentence.type == DEFINE_VALUE_PARAM) { if (sentence.type == CHANGE_VALUE_PARAM || sentence.type == DEFINE_VALUE_PARAM || sentence.type == FUNCTION_INVOCATION) {
if (sentence.left != "-") { // 左边就是函数名 if (sentence.left != "-") { // 左边就是函数名
if (sentence.left != funcName) {
update()
}
args.add(sentence.right) args.add(sentence.right)
funcName = sentence.left funcName = sentence.left
} else {
update()
} }
} else { // 更新 } else { // 更新
val function = this.find(funcName) update()
if (function != null) {
val info = InvokeInfo(name, args)
val tt = function.getTraceTree(info)
// 过滤 trace tree
invokeTrees.addAll(tt.filter { it.name.startsWith(name) })
}
if (funcName != "") {
val newTree = invokeTrees.toList()
resultMap[funcName] = newTree // 因为之后要清空
invokeTrees.clear()
args.clear()
funcName = ""
}
} }
} }
return resultMap return resultMap

View file

@ -284,9 +284,7 @@ class SourceFile {
if (it.paramNameList.isEmpty()) { if (it.paramNameList.isEmpty()) {
funcResult.add("void") funcResult.add("void")
} else { } else {
it.paramNameList.forEach { param -> funcResult.add(it.paramNameList.joinToString(separator = "-def;", postfix = "-def"))
funcResult.add("$param-def")
}
} }
// 后面的 // 后面的
@ -295,7 +293,11 @@ class SourceFile {
val ifCache = StringBuilder() val ifCache = StringBuilder()
fun invalidCache(){ fun invalidCache(){
useFuncName = ""
if (useCache.isNotEmpty()) { if (useCache.isNotEmpty()) {
if (useCache.toString().last() == ';'){
useCache.setLength(useCache.length - 1)
}
funcResult.add(useCache.toString()) funcResult.add(useCache.toString())
useCache.clear() // 清空 useCache.clear() // 清空
} }
@ -308,18 +310,24 @@ class SourceFile {
it.cParser.sentenceList.forEach { sentence -> it.cParser.sentenceList.forEach { sentence ->
when (sentence.type) { when (sentence.type) {
FUNCTION_INVOCATION -> { FUNCTION_INVOCATION -> {
invalidCache() if (ifCache.isNotEmpty()) {
if (useFuncName != sentence.left && useFuncName.isNotEmpty()){ funcResult.add(ifCache.toString().dropLast(1))
ifCache.clear()
}
if (useFuncName != sentence.left){
useFuncName = sentence.left useFuncName = sentence.left
if (useCache.toString().last() == ';'){
useCache.dropLast(1)
}
funcResult.add(useCache.toString()) funcResult.add(useCache.toString())
useCache.clear() // 清空 useCache.clear() // 清空
} }
if (sentence.right in it.cParser.defineList) { if (sentence.right in it.cParser.defineList) {
useCache.append("${sentence.right}-use;") useCache.append("${sentence.right}-use;")
}else if (sentence.right.contains('.')) { }else if (sentence.right.contains('.')) {
val param = sentence.right.split('.').first() val param = sentence.right.split('.').first().trim()
if (param in it.cParser.defineList){ if (param in it.cParser.defineList){
useCache.append("${param}-use;") useCache.append("${sentence.right}-use;")
} }
} }
} }
@ -331,7 +339,7 @@ class SourceFile {
}else if (sentence.right.contains('.')) { }else if (sentence.right.contains('.')) {
val param = sentence.right.split('.').first() val param = sentence.right.split('.').first()
if (param in it.cParser.defineList){ if (param in it.cParser.defineList){
funcResult.add("${param}-use") funcResult.add("${sentence.right}-use")
} }
} }
} }
@ -347,6 +355,9 @@ class SourceFile {
IF_PARAM -> { IF_PARAM -> {
if (useCache.isNotEmpty()) { if (useCache.isNotEmpty()) {
if (useCache.toString().last() == ';'){
useCache.setLength(useCache.length - 1)
}
funcResult.add(useCache.toString()) funcResult.add(useCache.toString())
useCache.clear() useCache.clear()
} }
@ -357,7 +368,10 @@ class SourceFile {
} }
} }
if (useCache.isNotEmpty()) { if (useCache.isNotEmpty()) {
funcResult.add(useCache.toString())s if (useCache.toString().last() == ';'){
useCache.setLength(useCache.length - 1)
}
funcResult.add(useCache.toString())
} }
globalResult[it.name] = funcResult globalResult[it.name] = funcResult
} }

View file

@ -22,6 +22,7 @@ fun getUseList(useList: Map<String, List<String>>): String =
.replace("], ", "]}\n\n") // 保证不是最后一个 .replace("], ", "]}\n\n") // 保证不是最后一个
.replace(", ", "],[") // 逗号后面有空格 .replace(", ", "],[") // 逗号后面有空格
.replace(";", "") .replace(";", "")
.replace("、]","]")
fun getTraceTreeString(traceTreeStr: String, funcName: String): String { fun getTraceTreeString(traceTreeStr: String, funcName: String): String {
val sb = StringBuilder("$funcName:\n") val sb = StringBuilder("$funcName:\n")