mirror of
				https://github.com/KevinMidboe/linguist.git
				synced 2025-10-29 17:50:22 +00:00 
			
		
		
		
	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.
This commit is contained in:
		| @@ -25,7 +25,7 @@ module Linguist | |||||||
|         if languages.all? { |l| ["TypeScript", "XML"].include?(l) } |         if languages.all? { |l| ["TypeScript", "XML"].include?(l) } | ||||||
|           disambiguate_ts(data, languages) |           disambiguate_ts(data, languages) | ||||||
|         end |         end | ||||||
|         if languages.all? { |l| ["Common Lisp", "OpenCL"].include?(l) } |         if languages.all? { |l| ["Common Lisp", "Cool", "OpenCL"].include?(l) } | ||||||
|           disambiguate_cl(data, languages) |           disambiguate_cl(data, languages) | ||||||
|         end |         end | ||||||
|         if languages.all? { |l| ["Rebol", "R"].include?(l) } |         if languages.all? { |l| ["Rebol", "R"].include?(l) } | ||||||
| @@ -72,6 +72,7 @@ module Linguist | |||||||
|     def self.disambiguate_cl(data, languages) |     def self.disambiguate_cl(data, languages) | ||||||
|       matches = [] |       matches = [] | ||||||
|       matches << Language["Common Lisp"] if data.include?("(defun ") |       matches << Language["Common Lisp"] if data.include?("(defun ") | ||||||
|  |       matches << Language["Cool"] if /^class/x.match(data) | ||||||
|       matches << Language["OpenCL"] if /\/\* |\/\/ |^\}/.match(data) |       matches << Language["OpenCL"] if /\/\* |\/\/ |^\}/.match(data) | ||||||
|       matches |       matches | ||||||
|     end |     end | ||||||
|   | |||||||
| @@ -429,6 +429,12 @@ Common Lisp: | |||||||
|   - clisp |   - clisp | ||||||
|   - ecl |   - ecl | ||||||
|  |  | ||||||
|  | Cool: | ||||||
|  |   type: programming | ||||||
|  |   lexer: Text only | ||||||
|  |   extensions: | ||||||
|  |   - .cl | ||||||
|  |  | ||||||
| Coq: | Coq: | ||||||
|   type: programming |   type: programming | ||||||
|   extensions: |   extensions: | ||||||
|   | |||||||
| @@ -91,6 +91,9 @@ | |||||||
|       ".cl", |       ".cl", | ||||||
|       ".lisp" |       ".lisp" | ||||||
|     ], |     ], | ||||||
|  |     "Cool": [ | ||||||
|  |       ".cl" | ||||||
|  |     ], | ||||||
|     "Coq": [ |     "Coq": [ | ||||||
|       ".v" |       ".v" | ||||||
|     ], |     ], | ||||||
| @@ -702,8 +705,8 @@ | |||||||
|       ".gemrc" |       ".gemrc" | ||||||
|     ] |     ] | ||||||
|   }, |   }, | ||||||
|   "tokens_total": 607168, |   "tokens_total": 607535, | ||||||
|   "languages_total": 739, |   "languages_total": 741, | ||||||
|   "tokens": { |   "tokens": { | ||||||
|     "ABAP": { |     "ABAP": { | ||||||
|       "*/**": 1, |       "*/**": 1, | ||||||
| @@ -15134,6 +15137,96 @@ | |||||||
|       "line": 2, |       "line": 2, | ||||||
|       "After": 1 |       "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": { |     "Coq": { | ||||||
|       "Inductive": 41, |       "Inductive": 41, | ||||||
|       "day": 9, |       "day": 9, | ||||||
| @@ -65508,6 +65601,7 @@ | |||||||
|     "COBOL": 90, |     "COBOL": 90, | ||||||
|     "CoffeeScript": 2951, |     "CoffeeScript": 2951, | ||||||
|     "Common Lisp": 2186, |     "Common Lisp": 2186, | ||||||
|  |     "Cool": 367, | ||||||
|     "Coq": 18259, |     "Coq": 18259, | ||||||
|     "Creole": 134, |     "Creole": 134, | ||||||
|     "Crystal": 1506, |     "Crystal": 1506, | ||||||
| @@ -65689,6 +65783,7 @@ | |||||||
|     "COBOL": 4, |     "COBOL": 4, | ||||||
|     "CoffeeScript": 9, |     "CoffeeScript": 9, | ||||||
|     "Common Lisp": 3, |     "Common Lisp": 3, | ||||||
|  |     "Cool": 2, | ||||||
|     "Coq": 12, |     "Coq": 12, | ||||||
|     "Creole": 1, |     "Creole": 1, | ||||||
|     "Crystal": 3, |     "Crystal": 3, | ||||||
| @@ -65845,5 +65940,5 @@ | |||||||
|     "YAML": 2, |     "YAML": 2, | ||||||
|     "Zephir": 2 |     "Zephir": 2 | ||||||
|   }, |   }, | ||||||
|   "md5": "dfced8ef9ddd9813d2982dc25a5468fa" |   "md5": "ee1f12dec040474f3864dfc0af301d17" | ||||||
| } | } | ||||||
							
								
								
									
										26
									
								
								samples/Cool/list.cl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								samples/Cool/list.cl
									
									
									
									
									
										Normal file
									
								
							| @@ -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; | ||||||
|  |       } | ||||||
|  |    }; | ||||||
|  | }; | ||||||
							
								
								
									
										71
									
								
								samples/Cool/sample.cl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								samples/Cool/sample.cl
									
									
									
									
									
										Normal file
									
								
							| @@ -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") | ||||||
|  |     }; | ||||||
|  | }; | ||||||
		Reference in New Issue
	
	Block a user