Permalink
Browse files

Merge pull request #704 from linonetwo/COOL

Add COOL language
  • Loading branch information...
teverett committed May 16, 2017
2 parents d9067ab + e127676 commit c864cd8c1892c388d38ea35224f61abb3570e015
@@ -0,0 +1,119 @@
/*
The MIT License (MIT)
Copyright (c) 2017 Linonetwo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
COOL grammar derived from:
http://sist.shanghaitech.edu.cn/faculty/songfu/course/spring2017/cs131/COOL/COOLAid.pdf
*/
grammar COOL;
program: programBlocks;
programBlocks
: classDefine ';' programBlocks #class
| EOF #eof
;
classDefine: CLASS TYPEID (INHERITS TYPEID)? '{' (feature ';')* '}';
feature
: OBJECTID '(' (formal (',' formal)*)* ')' ':' TYPEID '{' expression '}' #method
| OBJECTID ':' TYPEID (ASSIGNMENT expression)? /* class member variable */ #classProperty
;
formal: OBJECTID ':' TYPEID; /* method argument */
expression
: OBJECTID ASSIGNMENT expression #assignment
| expression ('@' TYPEID)? '.' OBJECTID '(' (expression (',' expression)*)* ')' /* call super class method */ #superClassMethod
| OBJECTID '(' (expression (',' expression)*)* ')' /* call function that refered by variable */ #functionCall
| IF expression THEN expression ELSE expression FI #if
| WHILE expression LOOP expression POOL #whild
| '{' (expression ';')+ '}' #multipleExpression
| LET OBJECTID ':' TYPEID (ASSIGNMENT expression)? (',' OBJECTID ':' TYPEID (ASSIGNMENT expression)?)* IN expression /* let num : Int <- num_cells() in */ #letIn
| CASE expression OF (OBJECTID ':' TYPEID CASE_ARROW expression ';')+ ESAC #case
| NEW TYPEID #newType
| ISVOID expression #isvoid
| expression ADD expression #add
| expression MINUS expression #minus
| expression MULTIPLY expression #multiply
| expression DIVISION expression #division
| INTEGER_COMPLEMENT expression #integerComplement
| expression LESS_THAN expression #lessThan
| expression LESS_EQUAL expression #lessEqual
| expression EQUAL expression #equal
| NOT expression #boolNot
| '(' expression ')' #parentheses
| OBJECTID #id
| INT #int
| STRING #string
| TRUE #true
| FALSE #false
;
// skip spaces, tabs, newlines, note that \v is not suppoted in antlr
WHITESPACE: [ \t\r\n\f]+ -> skip;
// comments
OPEN_COMMENT: '(*';
CLOSE_COMMENT: '*)';
COMMENT: OPEN_COMMENT (COMMENT|.)*? CLOSE_COMMENT -> channel(HIDDEN);
ONE_LINE_COMMENT: '--' .*? '\n' -> channel(HIDDEN);
// key words
CLASS: ('C'|'c')('L'|'l')('A'|'a')('S'|'s')('S'|'s');
ELSE: ('E'|'e')('L'|'l')('S'|'s')('E'|'e');
FALSE: 'f'('A'|'a')('L'|'l')('S'|'s')('E'|'e');
FI: ('F'|'f')('I'|'i');
IF: ('I'|'i')('F'|'f');
IN: ('I'|'i')('N'|'n');
INHERITS: ('I'|'i')('N'|'n')('H'|'h')('E'|'e')('R'|'r')('I'|'i')('T'|'t')('S'|'s');
ISVOID: ('I'|'i')('S'|'s')('V'|'v')('O'|'o')('I'|'i')('D'|'d');
LET: ('L'|'l')('E'|'e')('T'|'t');
LOOP: ('L'|'l')('O'|'o')('O'|'o')('P'|'p');
POOL: ('P'|'p')('O'|'o')('O'|'o')('L'|'l');
THEN: ('T'|'t')('H'|'h')('E'|'e')('N'|'n');
WHILE: ('W'|'w')('H'|'h')('I'|'i')('L'|'l')('E'|'e');
CASE: ('C'|'c')('A'|'a')('S'|'s')('E'|'e');
ESAC: ('E'|'e')('S'|'s')('A'|'a')('C'|'c');
NEW: ('N'|'n')('E'|'e')('W'|'w');
OF: ('O'|'o')('F'|'f');
NOT: ('N'|'n')('O'|'o')('T'|'t');
TRUE: 't'('R'|'r')('U'|'u')('E'|'e');
// premitives
STRING: '"' (ESC | ~ ["\\])* '"'; // non-greedy matching one line string
INT: [0-9]+;
TYPEID: [A-Z][_0-9A-Za-z]*;
OBJECTID: [a-z][_0-9A-Za-z]*;
ASSIGNMENT: '<-';
CASE_ARROW: '=>';
ADD: '+';
MINUS: '-';
MULTIPLY: '*';
DIVISION: '/';
LESS_THAN: '<';
LESS_EQUAL: '<=';
EQUAL: '=';
INTEGER_COMPLEMENT: '~';
fragment ESC: '\\' (["\\/bfnrt] | UNICODE);
fragment UNICODE: 'u' HEX HEX HEX HEX;
fragment HEX: [0-9a-fA-F];
@@ -0,0 +1,7 @@
# COOL
Classroom Object Oriented Language is a widely used language in CS classes.
This Grammar defination comes from [COOL manual](http://sist.shanghaitech.edu.cn/faculty/songfu/course/spring2017/cs131/COOL/COOLAid.pdf).
This is a byproduct from a COOL-to-javascript compiler.
@@ -0,0 +1,26 @@
# COOL examples
The examples directory contains a few small and not so small
example programs which illustrate various aspects of the Cool
programming language.
Original files include inputs can be found [here](http://sist.shanghaitech.edu.cn/faculty/songfu/course/spring2017/cs131/COOL/cool-examples.tar.gz)
## list
- arith.cl: Tests various aspects of arithmetic in Cool.
- atoi.cl: An implementation of the C function by thesame name. It converts a String into an Int.
- atoi_test.cl: An example program using atoi.cl. Since youwill most likely use atoi.cl in the firstassignment, you should study this example.
- list.cl: A very simple program showing how to create alist data type for integers. It illustratesinheritance and dynamic dispatch.
- book_list.cl: Illustrates inheritance and in particular themechanism of STATIC DISPATCH and the CASEstatement.
- cells.cl: Models a one-dimensional cellular automaton.
- cool.cl: An short and obscure program.
- io.cl: Shows how to use the IO class. (input/output)
- hairyscary.cl: Exercises many of the obscure features of thelanguage in obscure ways.
- hello_world.cl: The classic first program.
- primes.cl: An unusual prime number generator.
- graph.cl: A program reading descriptions of weighted-directed graphs from stdin in text format.
- palindrome.cl: Recognizes palindromes.
- complex.cl: Checks the = operator and cummulative effects ofmethod calls via a complex number object.
- life.cl: The classic Game of Life
- sort_list.cl: A more complex example sorting lists of integers.
Oops, something went wrong.

0 comments on commit c864cd8

Please sign in to comment.