Skip to content

Commit 67fd8c9

Browse files
authored
[Feature:Autograding] Add Java counting (#9)
* Add java * Add tests for java
1 parent d1ef35a commit 67fd8c9

9 files changed

Lines changed: 86 additions & 9 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ add_library(tree-sitter-cpp include/tree-sitter-cpp/src/parser.c
1717

1818
add_library(tree-sitter-c include/tree-sitter-c/src/parser.c)
1919

20-
link_libraries(tree-sitter-python tree-sitter-c tree-sitter-cpp
20+
add_library(tree-sitter-java include/tree-sitter-java/src/parser.c)
21+
22+
link_libraries(tree-sitter-python tree-sitter-c tree-sitter-cpp tree-sitter-java
2123
${PROJECT_SOURCE_DIR}/include/tree-sitter/libtree-sitter.a)
2224

2325
add_executable(submitty_count_ts src/count.cpp src/parser.cpp src/utils.cpp)

install_analysistoolsts.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mkdir -p "${INCLUDE_DIR}"
3232
########################################################################
3333

3434
# Clone the tree-sitter repos
35-
repos=( tree-sitter tree-sitter-python tree-sitter-c tree-sitter-cpp)
35+
repos=( tree-sitter tree-sitter-python tree-sitter-c tree-sitter-cpp tree-sitter-java)
3636

3737
for repo in "${repos[@]}"
3838
do

src/count.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ extern "C" {
1414
void check_call_node(const char *type, bool &is_parent_call) {
1515
// types of function nodes: call, call_expression, binary_expression (eg:
1616
// std::cout << "hello")
17-
if (!strcmp("call", type) || !strcmp("call_expression", type) || !strcmp("binary_expression", type)) {
17+
if (!strcmp("call", type) || !strcmp("call_expression", type) || !strcmp("binary_expression", type) || !strcmp("method_invocation", type)) {
1818
is_parent_call = true;
1919
}
2020
}
2121

2222
// check if the cur node is a function definition node
2323
void check_func_def_node(const char *type, bool &is_parent_func_def) {
24-
if (!strcmp("function_definition", type)) {
24+
if (!strcmp("function_definition", type) || !strcmp("method_declaration", type)) {
2525
is_parent_func_def = true;
2626
}
2727
}
@@ -61,7 +61,7 @@ int count_feature(Parser *parser, const Countable &countable, const std::string
6161
const char *type = ts_node_type(cur);
6262
// check if cur node is a identifier; used when counting
6363
// identifiers, functions or calls
64-
if (!strcmp("identifier", type) || !strcmp("field_identifier", type)) {
64+
if (!strcmp("identifier", type) || !strcmp("field_identifier", type) || !strcmp("type_identifier", type)) {
6565
int start = ts_node_start_byte(cur);
6666
int end = ts_node_end_byte(cur);
6767
// retrieve identifier name

src/parser.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern "C" {
99
extern "C" TSLanguage *tree_sitter_python();
1010
extern "C" TSLanguage *tree_sitter_c();
1111
extern "C" TSLanguage *tree_sitter_cpp();
12+
extern "C" TSLanguage *tree_sitter_java();
1213

1314
Parser::Parser(const Language &lang) {
1415
parser = ts_parser_new();
@@ -18,6 +19,8 @@ Parser::Parser(const Language &lang) {
1819
ts_parser_set_language(parser, tree_sitter_c());
1920
} else if (lang == CPP) {
2021
ts_parser_set_language(parser, tree_sitter_cpp());
22+
} else if (lang == JAVA) {
23+
ts_parser_set_language(parser, tree_sitter_java());
2124
}
2225
}
2326

@@ -40,4 +43,4 @@ std::string Parser::get_identifier(int start, int end) {
4043
char const *c = cur_code.c_str();
4144
std::string str(c + start, c + end);
4245
return str;
43-
}
46+
}

src/parser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extern "C" {
66
#include <tree_sitter/api.h>
77
}
88

9-
enum Language { PYTHON, C, CPP };
9+
enum Language { PYTHON, C, CPP, JAVA };
1010

1111
class Parser {
1212
public:
@@ -20,4 +20,4 @@ class Parser {
2020
std::string cur_code;
2121
};
2222

23-
#endif
23+
#endif

src/utils.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ Language get_language(const std::string &arg) {
3030
return C;
3131
} else if (arg == "cpp" || arg == "c++") {
3232
return CPP;
33+
} else if (arg == "java") {
34+
return JAVA;
3335
}
3436
std::cerr << "Error: Invalid language" << std::endl;
35-
std::cerr << "Usage: Use either py/python, c, cpp/c++" << std::endl;
37+
std::cerr << "Usage: Use either py/python, c, cpp/c++, java" << std::endl;
3638
exit(EXIT_FAILURE);
3739
}
3840

testSuite/commands.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,23 @@
2929
{ "-l py call dog testSuite/solution.py": 1 },
3030
{ "-l py node class testSuite/solution.py": 1 },
3131
{ "-l py identifier dog testSuite/solution.py": 6 }
32+
],
33+
"oop.java": [
34+
{ "-l java node interface testSuite/oop.java": 1 },
35+
{ "-l java node abstract testSuite/oop.java": 1 },
36+
{ "-l java node extends testSuite/oop.java": 1 },
37+
{ "-l java function sound testSuite/oop.java": 2 },
38+
{ "-l java call println testSuite/oop.java": 2 },
39+
{ "-l java call sound testSuite/oop.java": 1 },
40+
{ "-l java identifier Dog testSuite/oop.java": 3 }
41+
],
42+
"fibonacci.java": [
43+
{ "-l java node if testSuite/fibonacci.java": 1 },
44+
{ "-l java node for testSuite/fibonacci.java": 1 },
45+
{ "-l java node + testSuite/fibonacci.java": 4 },
46+
{ "-l java function printFibonacci testSuite/fibonacci.java": 1 },
47+
{ "-l java call print testSuite/fibonacci.java": 2 },
48+
{ "-l java identifier fibonacci testSuite/fibonacci.java": 2 },
49+
{ "-l java identifier Fibonacci testSuite/fibonacci.java": 4 }
3250
]
3351
}

testSuite/fibonacci.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.io.*;
2+
3+
class Fibonacci {
4+
int count;
5+
6+
public Fibonacci(int count) {
7+
this.count = count;
8+
}
9+
10+
void printFibonacci() {
11+
int n1 = 0, n2 = 1, n3 = 0;
12+
if (count <= 0) {
13+
return;
14+
}
15+
System.out.print(n1 + " " + n2);
16+
for (int i = 2; i < count; ++i) {
17+
n3 = n1 + n2;
18+
System.out.print(" " + n3);
19+
n1 = n2;
20+
n2 = n3;
21+
}
22+
}
23+
}
24+
25+
class Main {
26+
public static void main(String args[]) {
27+
Fibonacci fibonacci = new Fibonacci(10);
28+
fibonacci.printFibonacci();
29+
}
30+
}

testSuite/oop.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
interface Animal {
2+
void sound();
3+
}
4+
5+
abstract class Dog implements Animal {
6+
public void sound() {
7+
System.out.println("Bark");
8+
}
9+
}
10+
11+
public class Labrador extends Dog {
12+
public void labradorMethod() {
13+
System.out.println("Labrador");
14+
}
15+
}
16+
17+
class Main {
18+
public static void main(String args[]) {
19+
Dog labrador = new Labrador();
20+
labrador.sound();
21+
}
22+
}

0 commit comments

Comments
 (0)