Skip to content

Commit

Permalink
Merge pull request #10 from cs4215-2023/ben-lester/parser
Browse files Browse the repository at this point in the history
basic parser implementation
  • Loading branch information
blue-plum-cloud authored Mar 13, 2023
2 parents 7a3ce63 + cd2d549 commit 2e88757
Show file tree
Hide file tree
Showing 49 changed files with 3,912 additions and 1,535 deletions.
4 changes: 4 additions & 0 deletions examples/additionTest.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int a = 1;
int b = 3+10;
int c = a + b;
int d = b + 10;
3 changes: 3 additions & 0 deletions examples/bitwiseTest.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int c = 1;
int d = 1 >> c;
int e = 3 << 4;
4 changes: 4 additions & 0 deletions examples/forLoopTest.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
for (int i = 0; i < 10; i++) {
int a = 1;
a += i;
}
11 changes: 11 additions & 0 deletions examples/functionTest.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int foo(int a, int b) {
int c = a+b;

if (1) {
a++;
} else {
b++;
}
}

foo(1,2);
5 changes: 5 additions & 0 deletions examples/logicalOpTest.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1 && 2;
1 || 2;
1 ^ 2;
int a = 1;
a && 2;
2 changes: 2 additions & 0 deletions examples/pointerAssignmentTest.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
int a = 1;
int *c = a;
3 changes: 3 additions & 0 deletions examples/pointerTest.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int* a;
*a;
&b;
3 changes: 3 additions & 0 deletions examples/postFixTest.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int a = 0;
a++;
a--;
11 changes: 11 additions & 0 deletions examples/simpleTest.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int a = 1;

for (int i = 0; i < 10; i++) {
int b = 1;
}

if (1) {
return 2;
} else {
return 3;
}
44 changes: 44 additions & 0 deletions examples/test.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
void main(int a) {
int a = +1;
a = -a;
a++;
int* b;
*b;
&b;

int arr[5] = { 1, 2, 3, 4, 5 };
int *ptr = arr;
ptr++;
arr++;

1 % 2;
1 / 2;
1 + 2;
1 - 2;
1 >> 1;
1 << 2;

a += 3;
a -= 2;
char full_name[] = "Drex";
for (int i = 0; i < 10; i++) {
int a = 1;
a += i;
}

int i =0;
while(i <5){
i++;
}

do{
i--;
} while(i > 0);

char* string = "hello";
if (1) {
i++;
} else if (2) {
i--;
}
}
2 changes: 2 additions & 0 deletions examples/tester.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yarn build;
node ../dist/repl/repl.js -e "$(< $1)";
6 changes: 6 additions & 0 deletions examples/unaryOpTest.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
int a = 1;
!1;
+1;
-1;
1 == !1;
!a;
4 changes: 4 additions & 0 deletions examples/voidfunctionTest.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
void foo(){
int a = 3;
int b = 3+a;
}
4 changes: 4 additions & 0 deletions examples/whileLoopTest.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int i = 5;
while (i--) {
int a = 1;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"antlr4ts": "antlr4ts -visitor ./src/lang/Clang.g4",
"autocomplete": "node ./scripts/updateAutocompleteDocs.js",
"benchmark": "jest --runInBand --testPathPattern='.*benchmark.*' --testPathIgnorePatterns='/dist/'",
"build": "yarn docs && yarn tsc --build --force",
"build": "yarn tsc --build --force",
"build_sicp_package": "./scripts/build_sicp_package.sh",
"ci": "yarn jsdoc && yarn autocomplete && yarn format:ci && yarn eslint",
"compile": "yarn tsc",
Expand Down
173 changes: 108 additions & 65 deletions src/lang/Clang.g4
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
grammar Clang;

MUL: '*';
DIV: '/';
ADD: '+';
SUB: '-';
MOD: '%';
LESSTHAN: '<';
LESSTHANOREQUAL: '<=';
GREATERTHAN: '>';
GREATERTHANOREQUAL: '>=';
EQUAL: '=';
EQUALCHECK: '==';
NOTEQUALCHECK: '!=';
XOR: '^';
AND: '&&';
OR: '||';
NOT: '!';
BITWISEAND: '&';
BITWISEOR: '|';
MINUSEQUAL: '-=';
PLUSEQUAL: '+=';
BITSHIFTRIGHT: '>>';
BITSHIFTLEFT: '<<';

WHITESPACE: [ \t]+ -> skip;

NEWLINE: ( '\r' '\n'? | '\n') -> skip;

PRIMITIVETYPE:
'void'
| 'char'
| 'int'
| 'float'
| 'signed'
| 'unsigned';
PRIMITIVETYPE: 'void' | 'char' | 'int' | 'float';

SIGN: ('-' | '+');
SIGNEDTYPE: 'signed' | 'unsigned';

IDENTIFIER: SIGN? [a-zA-Z_] [a-zA-Z0-9_]*;
IDENTIFIER: [a-zA-Z_] [a-zA-Z0-9_]*;

FORMATSPECIFIERS:
'"' '%d' '"'
Expand All @@ -24,7 +41,9 @@ FORMATSPECIFIERS:
| '"' '%s' '"'
| '"' '%p' '"';

NUMBER: SIGN? [0-9_]+;
NUMBER: [0-9_]+;
CHAR: '\'' ~[\])] '\'';
FLOAT: ('0' ..'9')+ '.' ('0' ..'9')*;

PLUSPLUS: '++';
MINUSMINUS: '--';
Expand All @@ -35,7 +54,10 @@ stringLiteral: '"' IDENTIFIER? '"';

stringLiteralList: stringLiteral (',' stringLiteral)*;

identifierWithType: idType = PRIMITIVETYPE id = IDENTIFIER;
identifierWithType: idType = type id = IDENTIFIER;

// rename so as not to conflict with built in type
type: signed = SIGNEDTYPE? primType = PRIMITIVETYPE;

identifierWithTypeList:
identifierWithType (',' identifierWithType)*;
Expand All @@ -44,75 +66,94 @@ identifierList: IDENTIFIER (',' IDENTIFIER)*;

numberList: NUMBER (',' NUMBER)*;

pointerList: pointer (',' pointer)*;
statement:
expressionStatement
| selectionStatement
| conditionalStatement
| iterationStatement
| function;
| returnStatement
| functionDeclaration;

expression:
identifierWithType # TypedIdentifierExpression
| NUMBER # NumberExpression
| stringLiteral # StringLiteralExpression
| IDENTIFIER # IdentifierExpression
| postFix # PostFixNotationExpression
| arrayInitialisation # ArrayInitialisationExpression
| '(' inner = expression ')' # ParenthesisExpression
| pointer # PointerExpression
| pointerDerefernce # PointerDereferenceExpression
| pointerReference # PointerReferenceExpression
| functionCall # FunctionCallExpression
| printf # PrintfExpression
| left = expression operator = '*' right = expression # Multiplication
| left = expression operator = '/' right = expression # Division
| left = expression operator = '%' right = expression # ModulusDivision
| left = expression operator = '+' right = expression # Addition
| left = expression operator = '-' right = expression # Subtraction
| left = expression operator = '<<' right = expression # BitShiftLeft
| left = expression operator = '>>' right = expression # BitShiftRight
| left = expression operator = '>' right = expression # GreaterThan
| left = expression operator = '<' right = expression # LesserThan
| left = expression operator = '>=' right = expression # GreaterThanOrEqual
| left = expression operator = '<=' right = expression # LesserThanOrEqual
| left = expression operator = '==' right = expression # EqualityChecking
| left = expression operator = '!=' right = expression # NotEqual
| left = expression operator = '||' right = expression # Or
| left = expression operator = '&&' right = expression # And
| left = expression operator = '&' right = expression # BitwiseAnd
| left = expression operator = '|' right = expression # BitwiseOr
| left = expression operator = '^' right = expression # Xor
| left = expression operator = '=' right = expression # Assignment
| left = expression operator = '-=' right = expression # AssignAndMinusOne
| left = expression operator = '+=' right = expression # AssignAndAddOne;
identifierWithType # TypedIdentifierExpression
| NUMBER # NumberExpression
| CHAR # CharExpression
| FLOAT # FloatExpression
| stringLiteral # StringLiteralExpression
| IDENTIFIER # IdentifierExpression
| postFix # PostFixNotationExpression
| arrayInitialisation # ArrayInitialisationExpression
| '(' inner = expression ')' # ParenthesisExpression
| pointer # PointerExpression
| pointerDerefernce # PointerDereferenceExpression
| pointerReference # PointerReferenceExpression
| functionCall # FunctionCallExpression
| printf # PrintfExpression
| left = expression operator = MUL right = expression # Multiplication
| left = expression operator = DIV right = expression # Division
| left = expression operator = MOD right = expression # ModulusDivision
| left = expression operator = ADD right = expression # Addition
| left = expression operator = SUB right = expression # Subtraction
| left = expression operator = BITSHIFTLEFT right = expression # BitShiftLeft
| left = expression operator = BITSHIFTRIGHT right = expression # BitShiftRight
| left = expression operator = GREATERTHAN right = expression # GreaterThan
| left = expression operator = LESSTHAN right = expression # LesserThan
| left = expression operator = GREATERTHANOREQUAL right = expression # GreaterThanOrEqual
| left = expression operator = LESSTHANOREQUAL right = expression # LesserThanOrEqual
| left = expression operator = EQUALCHECK right = expression # EqualityChecking
| left = expression operator = NOTEQUALCHECK right = expression # NotEqual
| left = expression operator = OR right = expression # Or
| left = expression operator = AND right = expression # And
| left = expression operator = BITWISEAND right = expression # BitwiseAnd
| left = expression operator = BITWISEOR right = expression # BitwiseOr
| left = expression operator = XOR right = expression # Xor
| left = expression operator = EQUAL right = expression # Assignment
| left = expression operator = MINUSEQUAL right = expression # AssignAndMinusOne
| left = expression operator = PLUSEQUAL right = expression # AssignAndAddOne
| operators = SUB argument = expression # Negative
| operators = ADD argument = expression # Positive
| operators = NOT argument = expression # Not;

statementBlock: (statement)*;

parenthesesExpression: '(' inner = expression ')';

statementList: '{' ((statement)+)? '}';

postFix: (IDENTIFIER) (PLUSPLUS | MINUSMINUS);
postFix: argument = IDENTIFIER (PLUSPLUS | MINUSMINUS);

conditionalExpression:
test = expression '?' consequent = expression ':' alternate = expression;

returnStatement: 'return' expressionStatement;

expressionStatement: expression ';';

selectionStatement:
'if' '(' test = expression ')' consequentStatement = statement (
'else' alternateStatement = statement
conditionalStatement:
'if' '(' test = expression ')' '{' consequentStatement = statementBlock '}' (
'else' (
'{' alternateStatementBlock = statementBlock '}'
| elseIfStatement = conditionalStatement
)
)?;

iterationStatement:
'while' '(' condition = expression ')' body = statementList
| 'do' body = statementList 'while' '(' condition = expression ')' ';'
| 'for' '(' forCondition ')' body = statementList;
iterationStatement: whileLoop | doWhileLoop | forLoop;

whileLoop:
'while' '(' condition = expression ')' '{' body = statementBlock '}';

doWhileLoop:
'do' '{' body = statementBlock '}' 'while' '(' condition = expression ')' ';';

forLoop:
'for' '(' innerForCondition = forCondition ')' '{' body = statementBlock '}';

forCondition:
initialise = expression ';' endCondition = expression? ';' increment = expression;
initialise = expression ';' test = expression? ';' update = expression;

arrayIdentifierWithType:
idType = PRIMITIVETYPE id = IDENTIFIER '[' size = NUMBER? ']';
idType = type id = IDENTIFIER '[' size = NUMBER? ']';

arrayContent: '{' (identifierList | numberList) '}';
arrayContent:
'{' (pointerList | numberList | identifierList) '}';

arrayInitialisation:
arrayIdentifierWithType (
Expand All @@ -122,17 +163,19 @@ arrayInitialisation:

pointer: PRIMITIVETYPE '*' IDENTIFIER;

pointerDerefernce: '*' IDENTIFIER;
pointerDerefernce: operator = MUL argument = IDENTIFIER;

pointerReference: operator = BITWISEAND argument = IDENTIFIER;

pointerReference: '&' IDENTIFIER;
functionDeclaration: function;

function:
funcType = PRIMITIVETYPE (funcName = IDENTIFIER) (
params = '(' identifierWithTypeList? ')'
) body = statementList;
funcType = type (funcName = IDENTIFIER) (
'(' params = identifierWithTypeList ')'
) '{' body = statementBlock '}';

functionCall:
IDENTIFIER params = '(' functionCallParameters ')';
func = IDENTIFIER '(' args = functionCallParameters ')';

functionCallParameters: (
stringLiteralList
Expand Down
Loading

0 comments on commit 2e88757

Please sign in to comment.