fix: 过静态分析
This commit is contained in:
parent
2fd68e991f
commit
3460f3133d
7 changed files with 33 additions and 27 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
@file:Suppress("FunctionName")
|
||||||
|
|
||||||
import androidx.compose.desktop.ui.tooling.preview.Preview
|
import androidx.compose.desktop.ui.tooling.preview.Preview
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
|
|
|
@ -6,10 +6,11 @@ import core.CLanguage.Companion.typenameToTypeEnum
|
||||||
class CFunction(
|
class CFunction(
|
||||||
val name: String, source: String // 函数名
|
val name: String, source: String // 函数名
|
||||||
) {
|
) {
|
||||||
val returnType: CLanguage.Companion.CTYPES // 返回类型
|
val returnType: CLanguage.Companion.TYPES // 返回类型
|
||||||
val params: List<Map<String, CLanguage.Companion.CTYPES>>? // 参数 Map<name,T> 与实际相反
|
val params: List<Map<String, CLanguage.Companion.TYPES>>? // 参数 Map<name,T> 与实际相反
|
||||||
var paramNameList: List<String> = emptyList()
|
var paramNameList: List<String> = emptyList()
|
||||||
private set
|
@Suppress("EmptyMethod")
|
||||||
|
private set // 单写一个 private set 是保持可变但可以限制改变条件,而且可以保证能直接使用变量名称
|
||||||
val content: String
|
val content: String
|
||||||
val cParser: CParser
|
val cParser: CParser
|
||||||
|
|
||||||
|
@ -17,7 +18,7 @@ class CFunction(
|
||||||
this.params = parseParameters(source)
|
this.params = parseParameters(source)
|
||||||
this.content = parseContent(source)
|
this.content = parseContent(source)
|
||||||
cParser = CParser(content, paramNameList)
|
cParser = CParser(content, paramNameList)
|
||||||
returnType = typenameToTypeEnum(parseReturnType(source)) ?: CLanguage.Companion.CTYPES.void
|
returnType = typenameToTypeEnum(parseReturnType(source)) ?: CLanguage.Companion.TYPES.void
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseReturnType(source: String): String {
|
private fun parseReturnType(source: String): String {
|
||||||
|
@ -26,7 +27,7 @@ class CFunction(
|
||||||
return match?.groups?.get(1)?.value ?: ""
|
return match?.groups?.get(1)?.value ?: ""
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseParameters(source: String): List<Map<String, CLanguage.Companion.CTYPES>>? {
|
private fun parseParameters(source: String): List<Map<String, CLanguage.Companion.TYPES>>? {
|
||||||
val paramPattern = Regex("$name[ |\n]?\\((.*?)\\)[ |\n]?\\{") // main[这里随便空格换行](params...){
|
val paramPattern = Regex("$name[ |\n]?\\((.*?)\\)[ |\n]?\\{") // main[这里随便空格换行](params...){
|
||||||
val match = paramPattern.find(source)
|
val match = paramPattern.find(source)
|
||||||
val paramsList = match?.groups?.get(1)?.value?.split(",")?.map { it.trim() } // 函数最开始的那个括号里面
|
val paramsList = match?.groups?.get(1)?.value?.split(",")?.map { it.trim() } // 函数最开始的那个括号里面
|
||||||
|
@ -130,7 +131,7 @@ fun List<CFunction>.find(name: String): CFunction? {
|
||||||
* List<CFunction>.getInvokeTree
|
* List<CFunction>.getInvokeTree
|
||||||
* @param List<CFunction>: 要求包含所有函数以查找
|
* @param List<CFunction>: 要求包含所有函数以查找
|
||||||
* @param name: 从哪个函数开始查找
|
* @param name: 从哪个函数开始查找
|
||||||
* @return 特制的 TraceTree funcName -> tracetree[], 包含的是调用者的参数
|
* @return 特制的 TraceTree funcName -> traceTree[], 包含的是调用者的参数
|
||||||
*/
|
*/
|
||||||
fun List<CFunction>.getInvokeTree(name: String): Map<String, List<TraceTree>> {
|
fun List<CFunction>.getInvokeTree(name: String): Map<String, List<TraceTree>> {
|
||||||
// 先找到开始的函数
|
// 先找到开始的函数
|
||||||
|
|
|
@ -7,14 +7,15 @@ class CLanguage {
|
||||||
enum class OPERATION {
|
enum class OPERATION {
|
||||||
DEFINE_VALUE, // `type` identifier = RHand 题目里 def-use
|
DEFINE_VALUE, // `type` identifier = RHand 题目里 def-use
|
||||||
FUNCTION_INVOCATION, // func(value-param) 题目里
|
FUNCTION_INVOCATION, // func(value-param) 题目里
|
||||||
CHANGE_VALUE, // identifier = RHAND
|
CHANGE_VALUE, // identifier = RightHandValue
|
||||||
DEFINE_VALUE_PARAM, // int a=b, 一直接在上一个后面
|
DEFINE_VALUE_PARAM, // int a=b, 一直接在上一个后面
|
||||||
CHANGE_VALUE_PARAM, // a = b
|
CHANGE_VALUE_PARAM, // a = b
|
||||||
PARAM, // 函数的参数初始化
|
PARAM, // 函数的参数初始化
|
||||||
RETURN // 字面意思
|
RETURN // 字面意思
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class CTYPES {
|
@Suppress("EnumEntryName")
|
||||||
|
enum class TYPES {
|
||||||
void,
|
void,
|
||||||
char,
|
char,
|
||||||
int,
|
int,
|
||||||
|
@ -28,16 +29,16 @@ class CLanguage {
|
||||||
val typeRegex = types.joinToString("|") { "\\b$it\\b" }
|
val typeRegex = types.joinToString("|") { "\\b$it\\b" }
|
||||||
val declarationRegex = Regex("($typeRegex)\\s+([^;]+);")
|
val declarationRegex = Regex("($typeRegex)\\s+([^;]+);")
|
||||||
|
|
||||||
fun typenameToTypeEnum(typename: String): CTYPES? {
|
fun typenameToTypeEnum(typename: String): TYPES? {
|
||||||
return when (typename) {
|
return when (typename) {
|
||||||
"void" -> CTYPES.void
|
"void" -> TYPES.void
|
||||||
"char" -> CTYPES.char
|
"char" -> TYPES.char
|
||||||
"int" -> CTYPES.int
|
"int" -> TYPES.int
|
||||||
"short" -> CTYPES.short
|
"short" -> TYPES.short
|
||||||
"long" -> CTYPES.long
|
"long" -> TYPES.long
|
||||||
"float" -> CTYPES.float
|
"float" -> TYPES.float
|
||||||
"double" -> CTYPES.double
|
"double" -> TYPES.double
|
||||||
"struct" -> CTYPES.struct
|
"struct" -> TYPES.struct
|
||||||
else -> null // 无法解析的内容
|
else -> null // 无法解析的内容
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,10 @@ import core.CLanguage.Companion.OPERATION.*
|
||||||
class Sentence(val left: String, val right: String, val type: CLanguage.Companion.OPERATION)
|
class Sentence(val left: String, val right: String, val type: CLanguage.Companion.OPERATION)
|
||||||
|
|
||||||
class CParser(content: String, paramNameList: List<String>) {
|
class CParser(content: String, paramNameList: List<String>) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val lrexp_regex = "(.*)[ ]?=[ ]?(.*)[ ]?;?$".toRegex() // 左1右2
|
val left_eq_right_regex = "(.*) ?= ?(.*) ?;?$".toRegex() // 左1右2
|
||||||
val func_invoke_regex = "(.*)[ ]?\\((.*)\\)[ ]?$".toRegex() // 匹配到的是1:函数名2:参数列表
|
val func_invoke_regex = "(.*) ?\\((.*)\\) ?$".toRegex() // 匹配到的是1:函数名2:参数列表
|
||||||
val c_op_list = listOf("+", "-", "*", "/", "%", "<<", ">>")
|
val c_op_list = listOf("+", "-", "*", "/", "%", "<<", ">>")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val sentenceList = mutableListOf<Sentence>()
|
val sentenceList = mutableListOf<Sentence>()
|
||||||
|
@ -31,7 +29,7 @@ class CParser(content: String, paramNameList: List<String>) {
|
||||||
if (line.contains('=')) { // 出现了left hand 和 right hand
|
if (line.contains('=')) { // 出现了left hand 和 right hand
|
||||||
if (line.split(' ').first() in CLanguage.types) { // 最开始是一个 type
|
if (line.split(' ').first() in CLanguage.types) { // 最开始是一个 type
|
||||||
// 说明这是一个 define 语句
|
// 说明这是一个 define 语句
|
||||||
val match = lrexp_regex.find(" " + line) // 如果是后面的就要这个
|
val match = left_eq_right_regex.find(" $line") // 如果是后面的就要这个
|
||||||
if (match != null) {
|
if (match != null) {
|
||||||
val leftHand = match.groupValues[1].trim().split(' ').last() // Left-hand side (variable)
|
val leftHand = match.groupValues[1].trim().split(' ').last() // Left-hand side (variable)
|
||||||
val rightHand = match.groupValues[2].trim() // Right-hand side (value)
|
val rightHand = match.groupValues[2].trim() // Right-hand side (value)
|
||||||
|
@ -41,7 +39,7 @@ class CParser(content: String, paramNameList: List<String>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
} else { // 重新定义
|
} else { // 重新定义
|
||||||
val match = lrexp_regex.find(line.trim())
|
val match = left_eq_right_regex.find(line.trim())
|
||||||
if (match != null) {
|
if (match != null) {
|
||||||
val leftHand = match.groupValues[1].trim() // Left-hand side (variable)
|
val leftHand = match.groupValues[1].trim() // Left-hand side (variable)
|
||||||
val rightHand = match.groupValues[2].trim() // Right-hand side (value)
|
val rightHand = match.groupValues[2].trim() // Right-hand side (value)
|
||||||
|
@ -55,11 +53,11 @@ class CParser(content: String, paramNameList: List<String>) {
|
||||||
} else { // 调用函数,解析参数就行了
|
} else { // 调用函数,解析参数就行了
|
||||||
val match = func_invoke_regex.find(line) // 如果是后面的就要这个
|
val match = func_invoke_regex.find(line) // 如果是后面的就要这个
|
||||||
if (match != null) {
|
if (match != null) {
|
||||||
val func_name = match.groupValues[1].trim().split(' ').last() // Left-hand side (variable)
|
val funcName = match.groupValues[1].trim().split(' ').last() // Left-hand side (variable)
|
||||||
val args = match.groupValues[2].trim() // Right-hand side (value)
|
val args = match.groupValues[2].trim() // Right-hand side (value)
|
||||||
args.split(',').forEach { arg ->
|
args.split(',').forEach { arg ->
|
||||||
if (arg in defineList) { // 定义过的变量
|
if (arg in defineList) { // 定义过的变量
|
||||||
sentenceList.add(Sentence(func_name, arg, FUNCTION_INVOCATION))
|
sentenceList.add(Sentence(funcName, arg, FUNCTION_INVOCATION))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import core.CLanguage.Companion.declarationRegex
|
||||||
import core.CLanguage.Companion.types
|
import core.CLanguage.Companion.types
|
||||||
|
|
||||||
class SourceFile {
|
class SourceFile {
|
||||||
|
@Suppress("unused") // IDE 错误
|
||||||
companion object {
|
companion object {
|
||||||
val function_define_regex = """\b([a-zA-Z_][a-zA-Z0-9_]*)\s*\([^)]*\)\s*\{""".toRegex() // main(){ 匹配函数名
|
val function_define_regex = """\b([a-zA-Z_][a-zA-Z0-9_]*)\s*\([^)]*\)\s*\{""".toRegex() // main(){ 匹配函数名
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
@file:Suppress("FunctionName")
|
||||||
|
|
||||||
package ui
|
package ui
|
||||||
|
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
|
@ -24,6 +26,7 @@ import androidx.compose.ui.window.Dialog
|
||||||
import core.SourceFile
|
import core.SourceFile
|
||||||
import utils.DefaultCode
|
import utils.DefaultCode
|
||||||
|
|
||||||
|
@Suppress("FunctionName")
|
||||||
@OptIn(ExperimentalTextApi::class)
|
@OptIn(ExperimentalTextApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun TextfieldWithLineNumber(
|
fun TextfieldWithLineNumber(
|
||||||
|
|
|
@ -7,7 +7,7 @@ fun getDefineList(defList: Map<String, List<String>>): String =
|
||||||
// {main=[x, y, z], A=[a, m]}
|
// {main=[x, y, z], A=[a, m]}
|
||||||
defList.toString()
|
defList.toString()
|
||||||
.replace("{", "")
|
.replace("{", "")
|
||||||
.replace("}", "") // 解决左右括号
|
.replace("}", "") // 解决左边和右边的括号
|
||||||
.replace("=", ":def")
|
.replace("=", ":def")
|
||||||
.replace("], ","]\n")
|
.replace("], ","]\n")
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ fun getUseList(useList: Map<String, List<String>>): String =
|
||||||
// Example:
|
// Example:
|
||||||
// {main=[void, x-def, y-def, z-def, z-use, x-def;z-use, y-def;x-use, y-use], A=[a-def, m-def;a-use, m-use]}
|
// {main=[void, x-def, y-def, z-def, z-use, x-def;z-use, y-def;x-use, y-use], A=[a-def, m-def;a-use, m-use]}
|
||||||
useList.toString()
|
useList.toString()
|
||||||
.replace("{}", "") // 如果是空就直接删了
|
.replace("{}", "") // 如果这里是空的就直接删了
|
||||||
.replace("{", "") // 右括号留着
|
.replace("{", "") // 右括号留着
|
||||||
.replace("=", ":{") // 换掉
|
.replace("=", ":{") // 换掉
|
||||||
.replace("], ", "]}\n\n") // 保证不是最后一个
|
.replace("], ", "]}\n\n") // 保证不是最后一个
|
||||||
|
|
Loading…
Reference in a new issue