Skip to content

Commit

Permalink
Merge pull request #58 from vijaykrishna536/master
Browse files Browse the repository at this point in the history
C# detection issue resolved
  • Loading branch information
Reinaldy Rafli authored Oct 29, 2021
2 parents 702aa5a + 130289e commit c49e19e
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/languages/cpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const CPP: LanguagePattern[] = [
// Avoiding C# confusion
{ pattern: /Console\.(WriteLine|Write)(\s*)?\(/, type: 'not' },
{ pattern: /(using\s)?System(\..*)?(;)?/, type: 'not' },
{ pattern: /static\s+\S+\s+Main\(.*\)/, type: 'not' },
{ pattern: /(public|private|protected|internal)\s/, type: 'not' },
// Avoiding Kotlin confusion
{ pattern: /fun main\((.*)?\) {/, type: 'not' },
Expand Down
7 changes: 7 additions & 0 deletions src/languages/cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { LanguagePattern } from '../types';
export const CS: LanguagePattern[] = [
{ pattern: /using\sSystem(\..*)?(;)?/, type: 'meta.import' },
{ pattern: /Console\.(WriteLine|Write)(\s*)?\(/, type: 'keyword.print' },
{ pattern: /Console\.ReadLine\(\)/, type: 'keyword.other' },
{ pattern: /(public\s)?((partial|static|delegate)\s)?class\s/, type: 'keyword' },
// Modifiers
{ pattern: /(extern|override|sealed|readonly|virtual|volatile)/, type: 'keyword.other' },
Expand All @@ -11,6 +12,12 @@ export const CS: LanguagePattern[] = [
{ pattern: /(#region(\s.*)?|#endregion\n)/, type: 'section.scope' },
// Functions
{ pattern: /(public|private|protected|internal)\s/, type: 'keyword.visibility' },
// class keyword
{ pattern: /\bclass\s+\w+/, type: 'keyword' },
// (else )if statement
{ pattern: /(else )?if\s*\(.+\)/, type: 'keyword.control' },
// while loop
{ pattern: /\bwhile\s+\(.+\)/, type: 'keyword.control' },
// Variable declaration
{
pattern:
Expand Down
1 change: 1 addition & 0 deletions src/languages/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const Java: LanguagePattern[] = [
// Avoiding Ruby confusion
{ pattern: /def\s+\w+\s*(\(.+\))?\s*\n/, type: 'not' },
// Avoiding C# confusion
{ pattern: /\bnamespace\s.*(\s{)?/, type: 'not' },
{ pattern: /\[Attribute\]/, type: 'not' },
{ pattern: /Console\.(WriteLine|Write)(\s*)?\(/, type: 'not' },
{ pattern: /(#region(\s.*)?|#endregion\n)/, type: 'not' },
Expand Down
4 changes: 4 additions & 0 deletions src/languages/kotlin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export const Kotlin: LanguagePattern[] = [
type: 'keyword.function',
},
{ pattern: /println\((.*)\)(\n|;)/, type: 'keyword.print' },
// (else )if statement
{ pattern: /(else )?if\s*\(.+\)/, type: 'keyword.control' },
// while loop
{ pattern: /while\s+\(.+\)/, type: 'keyword.control' },
// Variables
{ pattern: /(const)?(\s+)?val(\s+)(.*)(:(\s)(.*)(\?)?)?(\s+)=(\s+)/, type: 'keyword.variable' },
{ pattern: /^(\s+)?(inner|open|data)(\s+)class/, type: 'keyword' },
Expand Down
53 changes: 53 additions & 0 deletions tests/cs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,4 +711,57 @@ test('y combinator', () => {
assert.equal(code.language, 'C#');
});

test('quick sort example with java conflict', () => {
const code = detectLang(`namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
int[] a = { 5, 3, 6, 4, 2, 9, 1, 8, 7 };
QuickSort(a);
}
static void QuickSort(int[] a)
{
QuickSort(a, 0, a.Length - 1);
}
static void QuickSort(int[] a, int start, int end)
{
if (start >= end)
{
return;
}
int num = a[start];
int i = start, j = end;
while (i < j)
{
while (i < j && a[j] > num)
{
j--;
}
a[i] = a[j];
while (i < j && a[i] < num)
{
i++;
}
a[j] = a[i];
}
a[i] = num;
QuickSort(a, start, i - 1);
QuickSort(a, i + 1, end);
}
}
}`);
assert.equal(code.language, 'C#');
});

test.run();
154 changes: 154 additions & 0 deletions tests/kotlin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,4 +518,158 @@ test('bankers algorithm', () => {
assert.equal(code.language, 'Kotlin');
});

test('average loop length', () => {
const code = detectLang(`const val NMAX = 20
const val TESTS = 1000000
val rand = java.util.Random()
fun avg(n: Int): Double {
var sum = 0
for (t in 0 until TESTS) {
val v = BooleanArray(NMAX)
var x = 0
while (!v[x]) {
v[x] = true
sum++
x = rand.nextInt(n)
}
}
return sum.toDouble() / TESTS
}
fun ana(n: Int): Double {
val nn = n.toDouble()
var term = 1.0
var sum = 1.0
for (i in n - 1 downTo 1) {
term *= i / nn
sum += term
}
return sum
}
fun main(args: Array<String>) {
println(" N average analytical (error)")
println("=== ========= ============ =========")
for (n in 1..NMAX) {
val a = avg(n)
val b = ana(n)
println(String.format("%3d %6.4f %10.4f (%4.2f%%)", n, a, b, Math.abs(a - b) / b * 100.0))
}
}`);
assert.equal(code.language, 'Kotlin');
});

test('gamma function', () => {
const code = detectLang(`fun gammaStirling(x: Double): Double = Math.sqrt(2.0 * Math.PI / x) * Math.pow(x / Math.E, x)
fun gammaLanczos(x: Double): Double {
var xx = x
val p = doubleArrayOf(
0.99999999999980993,
676.5203681218851,
-1259.1392167224028,
771.32342877765313,
-176.61502916214059,
12.507343278686905,
-0.13857109526572012,
9.9843695780195716e-6,
1.5056327351493116e-7
)
val g = 7
if (xx < 0.5) return Math.PI / (Math.sin(Math.PI * xx) * gammaLanczos(1.0 - xx))
xx--
var a = p[0]
val t = xx + g + 0.5
for (i in 1 until p.size) a += p[i] / (xx + i)
return Math.sqrt(2.0 * Math.PI) * Math.pow(t, xx + 0.5) * Math.exp(-t) * a
}
fun main(args: Array<String>) {
println(" x\tStirling\t\tLanczos\n")
for (i in 1 .. 20) {
val d = i / 10.0
print("%4.2f\t".format(d))
print("%17.15f\t".format(gammaStirling(d)))
println("%17.15f".format(gammaLanczos(d)))
}
}`);
assert.equal(code.language, 'Kotlin');
});

test('number calculation function', () => {
const code = detectLang(`fun main(args: Array<String>) {
var sum: Int = 0
var input: String
do {
print("Enter an integer: ")
input = readLine()!!
sum += input.toInt()
} while (input != "0")
println("sum = $sum")
}`);
assert.equal(code.language, 'Kotlin');
});

test('simple while loop implementation', () => {
const code = detectLang(`fun main(args: Array) {
var i: Int = 1
while(true){
println(i)
i=i+1
if(i>4)
break;
}
}`);
assert.equal(code.language, 'Kotlin');
});

test('largest among 3 numbers programme', () => {
const code = detectLang(`fun main(args: Array<String>) {
val n1 = 3
val n2 = 5
val n3 = -2
val max = if (n1 > n2) {
if (n1 > n3)
n1
else
n3
} else {
if (n2 > n3)
n2
else
n3
}
println("max = $max")
}`);
assert.equal(code.language, 'Kotlin');
});

test('bubble sort function', () => {
const code = detectLang(`import java.util.Comparator
fun <T> bubbleSort(a: Array<T>, c: Comparator<T>) {
var changed: Boolean
do {
changed = false
for (i in 0..a.size - 2) {
if (c.compare(a[i], a[i + 1]) > 0) {
val tmp = a[i]
a[i] = a[i + 1]
a[i + 1] = tmp
changed = true
}
}
} while (changed)
}`);
assert.equal(code.language, 'Kotlin');
});

test.run();
4 changes: 2 additions & 2 deletions tests/large.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ test('large input', () => {
C: 111,
Clojure: 0,
'C++': 160,
'C#': 19,
'C#': 85,
CSS: 0,
Dockerfile: 0,
Elixir: 0,
Expand All @@ -671,7 +671,7 @@ test('large input', () => {
Javascript: -452,
Julia: 24,
JSON: 0,
Kotlin: 0,
Kotlin: 66,
Lua: -1820,
Markdown: 0,
Pascal: 0,
Expand Down

0 comments on commit c49e19e

Please sign in to comment.