Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CFGG4/ALFA3.g4
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ assignStmt: <assoc=right> ID '=' (builtInCreateShapeCall | expr) ';';

ifStmt: 'if' '(' expr ')' block ('else if' '(' expr ')' block)* ('else' block)?;

loopStmt: 'loop' '(' 'int' ID 'from' (NUM | ID) '..' (NUM | ID) ')' block;
loopStmt: 'loop' '(' 'int' ID 'from' expr '..' expr ')' block;

paralStmt: 'paral' paralBlock;

Expand Down
68 changes: 27 additions & 41 deletions CFGG4/ALFA4.g4
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//Additions: arrays, new data types, colours, built-in utilities and play statement.
//Additions: strings, colours and new shapes (lines, triangles, circles).
grammar ALFA4;

prog: stmt+ play EOF;
prog: stmt+ EOF;

stmt
: varDcl
Expand All @@ -12,44 +12,35 @@ stmt
| paralStmt
| animDcl //Only allowed at the top level (NOT allowed in blocks).
| animCall
| builtInUtilCall
;

varDcl
: <assoc=right> type ID '=' elem ';' #VarDecl
| <assoc=right> type '[]' ID ('=' '{' elem (',' elem)* '}')? ';' #ArrayDecl
;

assignStmt
: <assoc=right> ID '=' elem ';' #Assign
| <assoc=right> ID '[' NUM ']' '=' elem #ArrayAssign
;
varDcl: type assignStmt;

elem: (builtInCreateShapeCall | expr);
assignStmt: <assoc=right> ID '=' (builtInCreateShapeCall | expr) ';';

ifStmt: 'if' '(' expr ')' block ('else if' '(' expr ')' block)* ('else' block)?;

loopStmt: 'loop' '(' 'int' ID 'from' (NUM | ID) '..' (NUM | ID) ')' block;
loopStmt: 'loop' '(' 'int' ID 'from' expr '..' expr ')' block;

paralStmt: 'paral' paralBlock;

animDcl: 'animation' ID '(' formalParams ')' block;

expr
: '(' expr ')' #Parens
| '!' expr #Not
| '-' expr #UnaryMinus
| expr op=('*' | '/' | '%') expr #MulDiv
| expr op=('+' | '-') expr #AddSub
| expr op=('<' | '>' | '<=' | '>=') expr #Relational
| expr op=('==' | '!=') expr #Equality
| expr 'and' expr #And
| expr 'or' expr #Or
| ID #Id
| NUM #Num
| ID '[' NUM ']' #ArrayAccess
| bool #Boolean
| color #Colour
: '(' expr ')' #Parens
| '!' expr #Not
| '-' expr #UnaryMinus
| expr op=('*' | '/' | '%') expr #MulDiv
| expr op=('+' | '-') expr #AddSub
| expr op=('<' | '>' | '<=' | '>=') expr #Relational
| expr op=('==' | '!=') expr #Equality
| expr 'and' expr #And
| expr 'or' expr #Or
| ID #Id
| NUM #Num
| STRING #String
| bool #Boolean
| color #Colour
;

block: '{' blockStmt* '}';
Expand All @@ -61,7 +52,6 @@ blockStmt
| loopStmt
| paralStmt
| animCall
| builtInUtilCall
;

paralBlock: '{' paralBlockStmt* '}';
Expand All @@ -70,30 +60,26 @@ paralBlockStmt
| animCall
;

play: 'play' '{' blockStmt* '}';

animCall: ID '(' actualParams ')' ';';

builtInAnim: 'move' | 'moveTo' | 'wait' | 'color';
builtInAnim: 'move' | 'wait' | 'color';
builtInAnimCall: builtInAnim '(' actualParams ')' ';';

builtInCreateShape: 'createRect' | 'createCircle' | 'createTriangle' | 'createLine' | 'createCanvas';
builtInCreateShape: 'createRect' | 'createCircle' | 'createTriangle' | 'createLine' | 'createText';
builtInCreateShapeCall: builtInCreateShape '(' actualParams ')';

builtInUtil: 'add' | 'remove' | 'print' | 'show' | 'hide' | 'resetCanvas';
builtInUtilCall: builtInUtil '(' actualParams ')' ';';

builtInParalAnim: 'move' | 'moveTo' | 'color';
builtInParalAnim: 'move' | 'color';
builtInParalAnimCall: builtInParalAnim '(' actualParams ')' ';';

animCall: ID '(' actualParams ')' ';';

formalParams: (type ID (',' type ID)*)?;
actualParams: (expr (',' expr)*)?;

COMMENT: '#' ~[\r\n]* -> skip;
ID: [a-zA-Z_][a-zA-Z0-9_]*;
NUM: '0'| '-'?[1-9][0-9]*;
type: 'int' | 'bool' | 'color' | 'rect' | 'circle' | 'triangle' | 'line' | 'shape' | 'canvas';
STRING: '"' (~["\r\n\\] | '\\' [\\"])* '"';
type: 'int' | 'bool' | 'rect' | 'string' | 'circle' | 'triangle' | 'line' | 'color' | 'text';
bool: 'true' | 'false';
color: 'white' | 'black' | 'red' | 'green' | 'blue';

WS: [ \t\r\n]+ -> skip;
WS: [ \t\r\n]+ -> skip;
101 changes: 101 additions & 0 deletions CFGG4/ALFA5.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//Additions: arrays, new data types, constants, built-in utilities and play statement.
grammar ALFA5;

prog: stmt+ play EOF;

stmt
: varDcl
| assignStmt
| builtInAnimCall
| ifStmt
| loopStmt
| paralStmt
| animDcl //Only allowed at the top level (NOT allowed in blocks).
| animCall
| builtInUtilCall
;

varDcl
: <assoc=right> 'const'? type ID '=' elem ';' #VarDecl
| <assoc=right> 'const'? type '[]' ID ('=' '{' elem (',' elem)* '}')? ';' #ArrayDecl
;

assignStmt
: <assoc=right> ID '=' elem ';' #Assign
| <assoc=right> ID '[' NUM ']' '=' elem #ArrayAssign
;

elem: (builtInCreateShapeCall | expr);

ifStmt: 'if' '(' expr ')' block ('else if' '(' expr ')' block)* ('else' block)?;

loopStmt: 'loop' '(' 'int' ID 'from' expr '..' expr ')' block;

paralStmt: 'paral' paralBlock;

animDcl: 'animation' ID '(' formalParams ')' block;

expr
: '(' expr ')' #Parens
| '!' expr #Not
| '-' expr #UnaryMinus
| expr op=('*' | '/' | '%') expr #MulDiv
| expr op=('+' | '-') expr #AddSub
| expr op=('<' | '>' | '<=' | '>=') expr #Relational
| expr op=('==' | '!=') expr #Equality
| expr 'and' expr #And
| expr 'or' expr #Or
| ID #Id
| NUM #Num
| ID '[' NUM ']' #ArrayAccess
| STRING #String
| bool #Boolean
| color #Colour
;

block: '{' blockStmt* '}';
blockStmt
: varDcl
| assignStmt
| builtInAnimCall
| ifStmt
| loopStmt
| paralStmt
| animCall
| builtInUtilCall
;

paralBlock: '{' paralBlockStmt* '}';
paralBlockStmt
: builtInParalAnimCall
| animCall
;

play: 'play' '{' blockStmt* '}';

animCall: ID '(' actualParams ')' ';';

builtInAnim: 'move' | 'wait' | 'color';
builtInAnimCall: builtInAnim '(' actualParams ')' ';';

builtInCreateShape: 'createRect' | 'createCircle' | 'createTriangle' | 'createLine' | 'createCanvas' | 'createImage' | 'createText';
builtInCreateShapeCall: builtInCreateShape '(' actualParams ')';

builtInUtil: 'add' | 'remove' | 'print' | 'resetCanvas' | 'changeDimensions' | 'import';
builtInUtilCall: builtInUtil '(' actualParams ')' ';';

builtInParalAnim: 'move' | 'color';
builtInParalAnimCall: builtInParalAnim '(' actualParams ')' ';';

formalParams: (type ID (',' type ID)*)?;
actualParams: (expr (',' expr)*)?;

COMMENT: '#' ~[\r\n]* -> skip;
ID: [a-zA-Z_][a-zA-Z0-9_]*;
NUM: '0'| '-'?[1-9][0-9]*;
STRING: '"' (~["\r\n\\] | '\\' [\\"])* '"';
type: 'int' | 'bool' | 'color' | 'rect' | 'circle' | 'triangle' | 'line' | 'shape' | 'canvas' | 'img' | 'text';
bool: 'true' | 'false';
color: 'white' | 'black' | 'red' | 'green' | 'blue';

WS: [ \t\r\n]+ -> skip;
30 changes: 30 additions & 0 deletions CFGG4/v3_report_exampleProgram.alfa
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
int len = 20;
rect myRect1 = createRect(0,0,len,len);
rect myRect2 = createRect(75,75,len,len);

int offsetX = 100;
int offsetY = 100;
int duration = 1500;

animation myAnimation (rect someRect, int someOffsetX, int someOffsetY, int someDuration) {
move(someRect, someOffsetX, 0, someDuration);
move(someRect, 0, someOffsetY, someDuration);
wait(200);
move(someRect, -someOffsetX, 0, someDuration);
move(someRect, 0, -someOffsetY, someDuration);
}

loop(int i from 1 .. 4) {
if (i == 1) {
paral {
myAnimation(myRect1, offsetX, offsetY, duration);
move(myRect2, 30, 30, duration);
}
duration = duration / 2;
} else if (i < 4) {
myAnimation(myRect1, offsetX, offsetY, duration);
move(myRect2, 30, 0, duration);
} else {
move(myRect2, -(30*(i - 1)), -30, duration);
}
}