From 1678a24ee6f5771e611db1b579faa7dc7694df81 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Mon, 3 Feb 2020 11:23:32 +0800 Subject: [PATCH] feat: setup c ident --- .../main/kotlin/chapi/ast/cast/CIdentApp.kt | 27 +++++++++++++++++++ .../kotlin/chapi/ast/cast/CIdentListener.kt | 14 ++++++++++ .../kotlin/chapi/ast/cast/CIdentAppTest.kt | 18 +++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 chapi-ast-c/src/main/kotlin/chapi/ast/cast/CIdentApp.kt create mode 100644 chapi-ast-c/src/main/kotlin/chapi/ast/cast/CIdentListener.kt create mode 100644 chapi-ast-c/src/test/kotlin/chapi/ast/cast/CIdentAppTest.kt diff --git a/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CIdentApp.kt b/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CIdentApp.kt new file mode 100644 index 00000000..8c941080 --- /dev/null +++ b/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CIdentApp.kt @@ -0,0 +1,27 @@ +package chapi.ast.cast + +import chapi.ast.antlr.CLexer +import chapi.ast.antlr.CParser +import org.antlr.v4.runtime.CharStreams +import org.antlr.v4.runtime.CommonTokenStream +import org.antlr.v4.runtime.tree.ParseTreeWalker + +open class CIdentApp() { + open fun Analysis(Str: String) { + val context = this.Parse(Str).compilationUnit() + val listener = CIdentListener() + + ParseTreeWalker().walk(listener, context) + + listener.getNodeInfo() + } + + open fun Parse(Str: String): CParser { + val fromString = CharStreams.fromString(Str) + val tokenSource = CLexer(fromString) + val commonTokenStream = CommonTokenStream(tokenSource) + val parser = CParser(commonTokenStream) + return parser + } + +} diff --git a/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CIdentListener.kt b/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CIdentListener.kt new file mode 100644 index 00000000..c03383f5 --- /dev/null +++ b/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CIdentListener.kt @@ -0,0 +1,14 @@ +package chapi.ast.cast + +import chapi.ast.antlr.CBaseListener +import chapi.ast.antlr.CParser + +open class CIdentListener() : CBaseListener() { + override fun enterFunctionDefinition(ctx: CParser.FunctionDefinitionContext?) { + super.enterFunctionDefinition(ctx) + } + + fun getNodeInfo() { + + } +} diff --git a/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CIdentAppTest.kt b/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CIdentAppTest.kt new file mode 100644 index 00000000..42ab1a70 --- /dev/null +++ b/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CIdentAppTest.kt @@ -0,0 +1,18 @@ +package chapi.ast.cast + +import org.junit.jupiter.api.Test + +import org.junit.jupiter.api.Assertions.* + +internal class CIdentAppTest { + @Test + fun shouldAnalysis() { +val helloWorld = """ +main() +{ + printf("Hello World"); +} +""" + CIdentApp().Analysis(helloWorld) + } +}