Skip to content

Commit

Permalink
Merge branch 'Open-Turing-Project-detect-turing-language'
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Nov 21, 2011
2 parents 65867be + e4fe1d1 commit df1b8e3
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/linguist/blob_helper.rb
Expand Up @@ -468,6 +468,28 @@ def guess_r_language
end
end

# Internal: Guess language of .t files.
#
# Returns a Language.
def guess_t_language
score = 0
score += 1 if lines.grep(/^% /).any?
score += data.gsub(/ := /).count
score += data.gsub(/proc |procedure |fcn |function /).count
score += data.gsub(/var \w+: \w+/).count

# Tell-tale signs its gotta be Perl
if lines.grep(/^(my )?(sub |\$|@|%)\w+/).any?
score = 0
end

if score >= 3
Language['Turing']
else
Language['Perl']
end
end

# Internal: Guess language of .gsp files.
#
# Returns a Language.
Expand Down
9 changes: 9 additions & 0 deletions lib/linguist/languages.yml
Expand Up @@ -723,6 +723,7 @@ Perl:
type: programming
overrides:
- .pl
- .t
primary_extension: .pl
extensions:
- .PL
Expand Down Expand Up @@ -972,6 +973,14 @@ Textile:
extensions:
- .textile

Turing:
type: programming
lexer: Text only
primary_extension: .t
extensions:
- .t
- .tu

Twig:
type: markup
group: PHP
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/perl-test.t
@@ -0,0 +1,10 @@
use strict;
use warnings;

use Foo::Bar

$n = 42;
$name = "world";
@array = ("1","2","3");
%hash = ("foo":"bar");
my $name = "josh";
19 changes: 19 additions & 0 deletions test/fixtures/turing.t
@@ -0,0 +1,19 @@
% Accepts a number and calculates its factorial

function factorial (n: int) : real
if n = 0 then
result 1
else
result n * factorial (n - 1)
end if
end factorial

var n: int
loop
put "Please input an integer: " ..
get n
exit when n >= 0
put "Input must be a non-negative integer."
end loop

put "The factorial of ", n, " is ", factorial (n)
4 changes: 4 additions & 0 deletions test/test_blob.rb
Expand Up @@ -281,6 +281,10 @@ def test_language
assert_equal Language['R'], blob("hello-r.R").language
assert_equal Language['Rebol'], blob("hello-rebol.r").language

# .t disambiguation
assert_equal Language['Perl'], blob("perl-test.t").language
assert_equal Language['Turing'], blob("turing.t").language

# ML
assert_equal Language['OCaml'], blob("Foo.ml").language
assert_equal Language['Standard ML'], blob("Foo.sig").language
Expand Down
3 changes: 3 additions & 0 deletions test/test_language.rb
Expand Up @@ -20,6 +20,9 @@ def test_ambiguous_extensions

assert Language.ambiguous?('.r')
assert_equal Language['R'], Language.find_by_extension('r')

assert Language.ambiguous?('.t')
assert_equal Language['Perl'], Language.find_by_extension('t')
end

def test_lexer
Expand Down

0 comments on commit df1b8e3

Please sign in to comment.