Skip to content

Commit

Permalink
🚀 supported do-while statement
Browse files Browse the repository at this point in the history
  • Loading branch information
shivasurya committed Nov 4, 2024
1 parent b1d5736 commit b702236
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
24 changes: 23 additions & 1 deletion sourcecode-parser/graph/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Node struct {
ClassInstanceExpr *model.ClassInstanceExpr
IfStmt *model.IfStmt
WhileStmt *model.WhileStmt
DoStmt *model.DoStmt
}

type Edge struct {
Expand Down Expand Up @@ -213,7 +214,7 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
if conditionNode != nil {
whileNode.Condition = &model.Expr{Node: *conditionNode, NodeString: conditionNode.Content(sourceCode)}
}
methodID := fmt.Sprintf("ifstmt_%d_%d_%s", node.StartPoint().Row+1, node.StartPoint().Column+1, file)
methodID := fmt.Sprintf("while_stmt_%d_%d_%s", node.StartPoint().Row+1, node.StartPoint().Column+1, file)
// add node to graph
whileStmtNode := &Node{
ID: GenerateSha256(methodID),
Expand All @@ -227,6 +228,27 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
WhileStmt: &whileNode,
}
graph.AddNode(whileStmtNode)
case "do_statement":
doWhileNode := model.DoStmt{}
// get the condition of the while statement
conditionNode := node.Child(2)
if conditionNode != nil {
doWhileNode.Condition = &model.Expr{Node: *conditionNode, NodeString: conditionNode.Content(sourceCode)}
}
methodID := fmt.Sprintf("dowhile_stmt_%d_%d_%s", node.StartPoint().Row+1, node.StartPoint().Column+1, file)
// add node to graph
doWhileStmtNode := &Node{
ID: GenerateSha256(methodID),
Type: "DoStmt",
Name: "DoStmt",
IsExternal: true,
CodeSnippet: node.Content(sourceCode),
LineNumber: node.StartPoint().Row + 1,
File: file,
isJavaSourceFile: isJavaSourceFile,
DoStmt: &doWhileNode,
}
graph.AddNode(doWhileStmtNode)

Check warning on line 251 in sourcecode-parser/graph/construct.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/graph/construct.go#L196-L251

Added lines #L196 - L251 were not covered by tests
case "binary_expression":
leftNode := node.ChildByFieldName("left")
rightNode := node.ChildByFieldName("right")
Expand Down
11 changes: 11 additions & 0 deletions sourcecode-parser/graph/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func (env *Env) GetWhileStmt() *model.WhileStmt {
return env.Node.WhileStmt

Check warning on line 127 in sourcecode-parser/graph/query.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/graph/query.go#L126-L127

Added lines #L126 - L127 were not covered by tests
}

func (env *Env) GetDoStmt() *model.DoStmt {
return env.Node.DoStmt

Check warning on line 131 in sourcecode-parser/graph/query.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/graph/query.go#L130-L131

Added lines #L130 - L131 were not covered by tests
}

func QueryEntities(graph *CodeGraph, query parser.Query) (nodes [][]*Node, output [][]interface{}) {
result := make([][]*Node, 0)

Expand Down Expand Up @@ -300,6 +304,7 @@ func generateProxyEnv(node *Node, query parser.Query) map[string]interface{} {
classInstanceExpression := "ClassInstanceExpr"
ifStmt := "IfStmt"
whileStmt := "WhileStmt"
doStmt := "DoStmt"

// print query select list
for _, entity := range query.SelectList {
Expand Down Expand Up @@ -352,6 +357,8 @@ func generateProxyEnv(node *Node, query parser.Query) map[string]interface{} {
ifStmt = entity.Alias
case "WhileStmt":
whileStmt = entity.Alias
case "DoStmt":
doStmt = entity.Alias

Check warning on line 361 in sourcecode-parser/graph/query.go

View check run for this annotation

Codecov / codecov/patch

sourcecode-parser/graph/query.go#L356-L361

Added lines #L356 - L361 were not covered by tests
}
}
env := map[string]interface{}{
Expand Down Expand Up @@ -490,6 +497,10 @@ func generateProxyEnv(node *Node, query parser.Query) map[string]interface{} {
"getWhileStmt": proxyenv.GetWhileStmt,
"toString": proxyenv.ToString,
},
doStmt: map[string]interface{}{
"getDoStmt": proxyenv.GetDoStmt,
"toString": proxyenv.ToString,
},
}
return env
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
int i = 0;
i++;
}

do {
i++;
} while (i < 10);
Cipher.getInstance("RC4")
MessageDigest.getInstance("SHA1", "BC");
return super.onOptionsItemSelected(item);
Expand Down

0 comments on commit b702236

Please sign in to comment.