From 913cd6c309a8cf26db5f1874c2743967077e70a8 Mon Sep 17 00:00:00 2001 From: Trey Deitch Date: Thu, 8 May 2014 13:21:57 -0700 Subject: [PATCH] Add support for Cool This change includes a brief (non-sensical) sample program I wrote to illustrate many of Cool's language constructs, as well as a simple rule to distinguish Cool files from Common Lisp or OpenCL (it has a line that starts with the word 'class'). Further, it includes a second example program adapted from an example contained in the Cool distribution (list.cl), which contains a few further language constructs and captures the style of a Cool program. --- lib/linguist/heuristics.rb | 3 +- lib/linguist/languages.yml | 6 +++ lib/linguist/samples.json | 101 +++++++++++++++++++++++++++++++++++-- samples/Cool/list.cl | 26 ++++++++++ samples/Cool/sample.cl | 71 ++++++++++++++++++++++++++ 5 files changed, 203 insertions(+), 4 deletions(-) create mode 100644 samples/Cool/list.cl create mode 100644 samples/Cool/sample.cl diff --git a/lib/linguist/heuristics.rb b/lib/linguist/heuristics.rb index c1116780..420cafc7 100644 --- a/lib/linguist/heuristics.rb +++ b/lib/linguist/heuristics.rb @@ -25,7 +25,7 @@ module Linguist if languages.all? { |l| ["TypeScript", "XML"].include?(l) } disambiguate_ts(data, languages) end - if languages.all? { |l| ["Common Lisp", "OpenCL"].include?(l) } + if languages.all? { |l| ["Common Lisp", "Cool", "OpenCL"].include?(l) } disambiguate_cl(data, languages) end if languages.all? { |l| ["Rebol", "R"].include?(l) } @@ -72,6 +72,7 @@ module Linguist def self.disambiguate_cl(data, languages) matches = [] matches << Language["Common Lisp"] if data.include?("(defun ") + matches << Language["Cool"] if /^class/x.match(data) matches << Language["OpenCL"] if /\/\* |\/\/ |^\}/.match(data) matches end diff --git a/lib/linguist/languages.yml b/lib/linguist/languages.yml index 7350bb2a..b9e4102a 100644 --- a/lib/linguist/languages.yml +++ b/lib/linguist/languages.yml @@ -429,6 +429,12 @@ Common Lisp: - clisp - ecl +Cool: + type: programming + lexer: Text only + extensions: + - .cl + Coq: type: programming extensions: diff --git a/lib/linguist/samples.json b/lib/linguist/samples.json index dfabe80e..18885205 100644 --- a/lib/linguist/samples.json +++ b/lib/linguist/samples.json @@ -91,6 +91,9 @@ ".cl", ".lisp" ], + "Cool": [ + ".cl" + ], "Coq": [ ".v" ], @@ -702,8 +705,8 @@ ".gemrc" ] }, - "tokens_total": 607168, - "languages_total": 739, + "tokens_total": 607535, + "languages_total": 741, "tokens": { "ABAP": { "*/**": 1, @@ -15134,6 +15137,96 @@ "line": 2, "After": 1 }, + "Cool": { + "class": 7, + "List": 8, + "{": 27, + "isNil": 2, + "(": 30, + ")": 30, + "Bool": 5, + "true": 2, + "}": 27, + ";": 40, + "head": 2, + "Int": 15, + "abort": 2, + "tail": 2, + "self": 3, + "cons": 1, + "i": 7, + "new": 3, + "Cons": 2, + ".init": 1, + "inherits": 4, + "car": 3, + "-": 19, + "The": 2, + "element": 1, + "in": 3, + "this": 1, + "list": 2, + "cell": 1, + "cdr": 3, + "rest": 3, + "of": 2, + "the": 1, + "false": 3, + "init": 1, + "<": 9, + "Exhibit": 1, + "various": 1, + "language": 1, + "constructs": 1, + "Sample": 5, + "testCondition": 1, + "x": 3, + "if": 3, + "then": 3, + "else": 3, + "+": 1, + "*": 3, + "fi": 2, + "testLoop": 1, + "y": 7, + "while": 1, + "loop": 1, + "not": 1, + "condition": 1, + "/": 2, + "pool": 1, + "testAssign": 1, + "z": 2, + "testCase": 1, + "var": 2, + "SELF_TYPE": 2, + "io": 1, + "IO": 3, + "<->": 1, + "case": 1, + "a": 4, + "A": 3, + "io.out_string": 4, + "b": 3, + "B": 2, + "s": 1, + "o": 1, + "Object": 1, + "esac": 1, + "testLet": 1, + "let": 2, + "c": 2, + "Used": 1, + "to": 1, + "test": 1, + "subclasses": 1, + "C": 1, + "main": 2, + ".testLet": 1, + "example": 1, + "Main": 1, + "out_string": 1 + }, "Coq": { "Inductive": 41, "day": 9, @@ -65508,6 +65601,7 @@ "COBOL": 90, "CoffeeScript": 2951, "Common Lisp": 2186, + "Cool": 367, "Coq": 18259, "Creole": 134, "Crystal": 1506, @@ -65689,6 +65783,7 @@ "COBOL": 4, "CoffeeScript": 9, "Common Lisp": 3, + "Cool": 2, "Coq": 12, "Creole": 1, "Crystal": 3, @@ -65845,5 +65940,5 @@ "YAML": 2, "Zephir": 2 }, - "md5": "dfced8ef9ddd9813d2982dc25a5468fa" + "md5": "ee1f12dec040474f3864dfc0af301d17" } \ No newline at end of file diff --git a/samples/Cool/list.cl b/samples/Cool/list.cl new file mode 100644 index 00000000..3d44813e --- /dev/null +++ b/samples/Cool/list.cl @@ -0,0 +1,26 @@ +(* This simple example of a list class is adapted from an example in the + Cool distribution. *) + +class List { + isNil() : Bool { true }; + head() : Int { { abort(); 0; } }; + tail() : List { { abort(); self; } }; + cons(i : Int) : List { + (new Cons).init(i, self) + }; +}; + +class Cons inherits List { + car : Int; -- The element in this list cell + cdr : List; -- The rest of the list + isNil() : Bool { false }; + head() : Int { car }; + tail() : List { cdr }; + init(i : Int, rest : List) : List { + { + car <- i; + cdr <- rest; + self; + } + }; +}; diff --git a/samples/Cool/sample.cl b/samples/Cool/sample.cl new file mode 100644 index 00000000..e8884990 --- /dev/null +++ b/samples/Cool/sample.cl @@ -0,0 +1,71 @@ +(* Refer to Alex Aiken, "The Cool Reference Manual": + http://theory.stanford.edu/~aiken/software/cool/cool-manual.pdf + for language specification. +*) + +-- Exhibit various language constructs +class Sample { + testCondition(x: Int): Bool { + if x = 0 + then false + else + if x < (1 + 2) * 3 + then true + else false + fi + fi + }; + + testLoop(y: Int): Bool { + while y > 0 loop + { + if not condition(y) + then y <- y / 2 + else y <- y - 1; + } + pool + }; + + testAssign(z: Int): Bool { + i : Int; + i <- ~z; + }; + + testCase(var: Sample): SELF_TYPE { + io : IO <- new IO; + case var of + a : A => io.out_string("Class type is A\n"); + b : B => io.out_string("Class type is B\n"); + s : Sample => io.out_string("Class type is Sample\n"); + o : Object => io.out_string("Class type is object\n"); + esac + }; + + testLet(i: Int): Int { + let (a: Int in + let(b: Int <- 3, c: Int <- 4 in + { + a <- 2; + a * b * 2 / c; + } + ) + ) + }; +}; + +-- Used to test subclasses +class A inherits Sample {}; +class B inherits A {}; + +class C { + main() : Int { + (new Sample).testLet(1) + }; +}; + +-- "Hello, world" example +class Main inherits IO { + main(): SELF_TYPE { + out_string("Hello, World.\n") + }; +};