mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 09:40:21 +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:
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