-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
217 lines (182 loc) · 4.45 KB
/
main.py
File metadata and controls
217 lines (182 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""Example 6: Multi-language code validation.
Demonstrates: Validating JavaScript, TypeScript, C, Go, Rust, and Java code using tree-sitter.
"""
from vallm import Proposal, VallmSettings, validate
js_good = """
function quickSort(arr) {
if (arr.length <= 1) return arr;
const pivot = arr[Math.floor(arr.length / 2)];
const left = arr.filter(x => x < pivot);
const middle = arr.filter(x => x === pivot);
const right = arr.filter(x => x > pivot);
return [...quickSort(left), ...middle, ...quickSort(right)];
}
"""
js_bad = """
function quickSort(arr) {
if (arr.length <= 1) return arr
const pivot = arr[Math.floor(arr.length / 2)]
const left = arr.filter(x => x < pivot
const right = arr.filter(x => x > pivot);
return [...quickSort(left), ...quickSort(right)];
}
"""
c_good = """
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
"""
c_bad = """
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b
b = a % b;
a = temp;
}
return a;
"""
ts_good = """
interface User {
id: number;
name: string;
email: string;
}
function greet(user: User): string {
return `Hello, ${user.name}!`;
}
export { User, greet };
"""
ts_bad = """
interface User {
id: number
name: string
email: string // Missing semicolons
function greet(user: User): string {
return `Hello, ${user.name}!`
// Missing closing brace
"""
go_good = """
package main
import "fmt"
func fibonacci(n int) []int {
if n <= 0 {
return []int{}
}
if n == 1 {
return []int{0}
}
fib := make([]int, n)
fib[0], fib[1] = 0, 1
for i := 2; i < n; i++ {
fib[i] = fib[i-1] + fib[i-2]
}
return fib
}
func main() {
fmt.Println(fibonacci(10))
}
"""
go_bad = """
package main
import "fmt"
func fibonacci(n int) []int {
if n <= 0 {
return []int{}
if n == 1 { // Missing closing brace
return []int{0}
}
fib := make([]int, n)
fib[0], fib[1] = 0, 1
for i := 2; i < n; i++ {
fib[i] = fib[i-1] + fib[i-2]
return fib // Missing closing brace
"""
rust_good = """
fn factorial(n: u64) -> u64 {
match n {
0 => 1,
_ => n * factorial(n - 1),
}
}
fn main() {
println!("Factorial of 10: {}", factorial(10));
}
"""
rust_bad = """
fn factorial(n: u64) -> u64 {
match n {
0 => 1,
_ => n * factorial(n - 1),
}
// Missing closing brace
fn main() {
println!("Factorial of 10: {}", factorial(10));
// Missing closing brace
"""
java_good = """
public class Calculator {
public static int add(int a, int b) {
return a + b;
}
public static int multiply(int a, int b) {
return a * b;
}
public static void main(String[] args) {
System.out.println("2 + 3 = " + add(2, 3));
System.out.println("2 * 3 = " + multiply(2, 3));
}
}
"""
java_bad = """
public class Calculator {
public static int add(int a, int b) {
return a + b
public static int multiply(int a, int b) {
return a * b;
}
// Missing closing brace
"""
def main():
settings = VallmSettings(
enable_syntax=True,
enable_imports=True,
enable_complexity=True,
enable_security=True,
enable_semantic=False,
)
examples = [
("JavaScript (good)", js_good, "javascript"),
("JavaScript (bad)", js_bad, "javascript"),
("TypeScript (good)", ts_good, "typescript"),
("TypeScript (bad)", ts_bad, "typescript"),
("C (good)", c_good, "c"),
("C (bad)", c_bad, "c"),
("Go (good)", go_good, "go"),
("Go (bad)", go_bad, "go"),
("Rust (good)", rust_good, "rust"),
("Rust (bad)", rust_bad, "rust"),
("Java (good)", java_good, "java"),
("Java (bad)", java_bad, "java"),
]
for label, code, language in examples:
print("=" * 60)
print(f"Validating: {label}")
print("=" * 60)
proposal = Proposal(code=code, language=language)
result = validate(proposal, settings)
print(f"Verdict: {result.verdict.value}")
print(f"Score: {result.weighted_score:.2f}")
for r in result.results:
print(f" {r.validator}: score={r.score:.2f}")
for issue in r.issues:
print(f" {issue}")
print()
if __name__ == "__main__":
main()