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>
#define PLUS(first,later) first+later
// 你看我还能用 define
// 当然也能写注释
int add(int a, int b) {
int c = PLUS(a,b);
return c;
struct Point {int x, y;};
void A(struct Point *p, int a, int b) {
int m, n;
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) {
int d = a - b;
return d;
void B(struct Point *po,int x, int y, int z) {
int p = x + y; // p-def
int q = y * z; // q-def
for (int i = 0; i < 10; i++) { // i-def
p = p - i; // p-def, i-use
q = q + i; // q-def, i-use
}
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; // 返回结果
A(po, p, q); // A: x-use, p-use, q-use
printf("B: p = %d, q = %d\n", p, q);
}
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);
int x = 5, y = 3, z = 2; // x-def, y-def, z-def
struct Point p1 = {1, 2}; // p1-def
B(&p1,x, y, z); // B: x-use, y-use, z-use, p1-use
printf("main: p1.x = %d, p1.y = %d\n", p1.x, p1.y);
return 0;
}

View file

@ -2,6 +2,7 @@ package core
import core.CLanguage.Companion.OPERATION.*
import core.CLanguage.Companion.typenameToTypeEnum
import kotlinx.coroutines.delay
import utils.Helper.Companion.antiC
class CFunction(
@ -93,6 +94,22 @@ class CFunction(
// 然后根据句子来获得 TraceTree
var paramIndex = 0 // 标记是第几个参数
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) {
// 例子:
// x,A(z),CHANGE_VALUE
@ -106,12 +123,14 @@ class CFunction(
left = cParser.sentenceList[index - i].left
i++
}
if (left != sentence.right) {
val lNode = nodes.findNode(left)
val rNode = nodes.findNode(sentence.right)
if (lNode != null && rNode != null) {
rNode.changes.add(lNode)
}
}
}
} else if (sentence.type == PARAM && invokeInfo != null) {
if (invokeInfo.param.lastIndex >= paramIndex) { // 在 InvokeTree 里面
val rNode = nodes.findNode(sentence.right)
@ -157,15 +176,7 @@ fun List<CFunction>.getInvokeTree(name: String): Map<String, List<TraceTree>> {
var funcName = ""
val resultMap = mutableMapOf<String, List<TraceTree>>()
func.cParser.sentenceList.forEach { sentence ->
// 🌰:
// A,z,CHANGE_VALUE_PARAM
if (sentence.type == CHANGE_VALUE_PARAM || sentence.type == DEFINE_VALUE_PARAM) {
if (sentence.left != "-") { // 左边就是函数名
args.add(sentence.right)
funcName = sentence.left
}
} else { // 更新
fun update() {
val function = this.find(funcName)
if (function != null) {
val info = InvokeInfo(name, args)
@ -181,6 +192,23 @@ fun List<CFunction>.getInvokeTree(name: String): Map<String, List<TraceTree>> {
funcName = ""
}
}
func.cParser.sentenceList.forEach { sentence ->
// 🌰:
// A,z,CHANGE_VALUE_PARAM
if (sentence.type == CHANGE_VALUE_PARAM || sentence.type == DEFINE_VALUE_PARAM || sentence.type == FUNCTION_INVOCATION) {
if (sentence.left != "-") { // 左边就是函数名
if (sentence.left != funcName) {
update()
}
args.add(sentence.right)
funcName = sentence.left
} else {
update()
}
} else { // 更新
update()
}
}
return resultMap
}

View file

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

View file

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