Skip to content

Commit

Permalink
refactor: refactor old c parser code #24
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 6, 2024
1 parent 28f11f5 commit a0e592f
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,24 @@ open class CFullIdentListener(fileName: String) : CAstBaseListener() {
private fun handleParamDirectDeclCtx(ctx: CParser.ParameterDirectDeclaratorContext) {
parseDirectDeclarator(ctx.directDeclarator())

val parameters = ctx.parameterTypeList().parameterList().parameterDeclaration()
for (parameter in parameters) {
var type: String? = null
var name: String? = null
for (specifier in parameter.declarationSpecifiers().declarationSpecifier()) {
if (specifier.typeSpecifier().text != null) {
type = specifier.typeSpecifier().text
currentFunction.Parameters = ctx.parameterTypeList().parameterList().parameterDeclaration().mapNotNull {
val type = it.declarationSpecifiers().declarationSpecifier().firstOrNull()?.typeSpecifier()?.text

val name = it.declarator().directDeclarator()?.let { directDeclarator ->
when (directDeclarator) {
is CParser.IdentifierDirectDeclaratorContext -> {
directDeclarator.Identifier().text
}
else -> null
}
}
val declarator = parameter.declarator().directDeclarator() as CParser.IdentifierDirectDeclaratorContext
if (declarator.Identifier().text != null) {
name = declarator.Identifier().text
}

if (type != null && name != null) {
val codeParameter = CodeProperty(
TypeValue = name,
TypeType = type
)
currentFunction.Parameters += codeParameter
CodeProperty(TypeValue = name, TypeType = type)
} else {
null
}
}
}.toMutableList()
}

private fun handleFuncPointerDirectDeclCtx(ctx: CParser.FunctionPointerDirectDeclaratorContext) {
Expand Down

0 comments on commit a0e592f

Please sign in to comment.