diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 8643296..1181d5e 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -6,8 +6,10 @@ - + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 0e338d8..6ddae43 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/main/java/Decaf.g4 b/src/main/java/Decaf.g4 index 2af8297..28887f3 100644 --- a/src/main/java/Decaf.g4 +++ b/src/main/java/Decaf.g4 @@ -3,32 +3,33 @@ grammar Decaf; program: classdecl+; //class identifier{...} -classdecl: AccessModifierPublic? 'class' Identifier OpenCurlyBracket (constuctorDecl|fieldDecl|methodDecl)*(MainMethodDecl block)? ClosedCurlyBracket; -constuctorDecl: AccessModifierPublic? Identifier OpenRoundBracket parameterList? ClosedRoundBracket block; //Method without +classdecl: AccessModifierPublic? 'class' Identifier OpenCurlyBracket (constuctorDecl|localVarDecl|methodDecl)*(MainMethodDecl block)? ClosedCurlyBracket; +constuctorDecl: AccessModifierPublic? Identifier OpenRoundBracket parameterList? ClosedRoundBracket block; //Method and FieldVar methodDecl: AccessModifierPublic? (type | Void) Identifier OpenRoundBracket parameterList? ClosedRoundBracket block; -fieldDecl: AccessModifierPublic? type Identifier Semicolon; //Parameters parameterList: parameter(Comma parameter)*; parameter: type Identifier; +argumentList: expression? | expression (Comma expression)+; //property, object.a, 3+1, a = 3 expression: subExpression | binaryExpr; //subExpression to dissolve left-recusion subExpression: This | assignableExpr | stmtExpr | OpenRoundBracket subExpression ClosedRoundBracket; -assignableExpr: Identifier | instVar; -instVar: subReceiver? receivingMethod* Identifier; - //.trim().toLength().toLowerCase().count ... methodCall: receiver? receivingMethod* Identifier OpenRoundBracket argumentList ClosedRoundBracket; -argumentList: expression? | expression (Comma expression)+; + +statement: returnStmt Semicolon | localVarDecl Semicolon | block | whileStmt | ifElseStmt | stmtExpr Semicolon; + +stmtExpr: assign | newDecl | methodCall; + +assignableExpr: Identifier | instVar; subReceiver: ((This | newDecl | Identifier) Dot); -receiver: ((This | instVar | newDecl | Identifier) Dot); -receivingMethod: Identifier OpenRoundBracket argumentList ClosedRoundBracket Dot; +instVar: subReceiver? receivingMethod* Identifier; binaryExpr: calcExpr | nonCalcExpr| value | Not binaryExpr; @@ -38,15 +39,11 @@ dotSubExpr: IntValue | Identifier | instVar | methodCall | OpenRoundBracket calc nonCalcExpr: subExpression nonCalcOperator expression; nonCalcOperator: LogicalOpertor | ComparisonOperator; -//Statement but also expression -//a = expr, new Object(), method(param1) -stmtExpr: assign | newDecl | methodCall; +BooleanValue: 'true'|'false'; +NullValue: 'null'; -//Statements -//int a, {...}, while(a > 10){...}, if(...){...} else if{...} else{...} -statement: returnStmt Semicolon | localVarDecl Semicolon | block | whileStmt | ifElseStmt | stmtExpr Semicolon; returnStmt: Return (expression)?; -localVarDecl: type Identifier (Assign expression)?; +localVarDecl: AccessModifierPublic? type Identifier (Assign expression)? Semicolon; block: OpenCurlyBracket statement* ClosedCurlyBracket; whileStmt: While OpenRoundBracket expression ClosedRoundBracket statement; ifElseStmt: ifStmt elseStmt?; @@ -54,6 +51,8 @@ ifStmt: If OpenRoundBracket expression ClosedRoundBracket statement; elseStmt: Else statement; assign: assignableExpr Assign expression; newDecl: New Identifier OpenRoundBracket argumentList ClosedRoundBracket; +receiver: ((This | instVar | newDecl | Identifier) Dot); +receivingMethod: Identifier OpenRoundBracket argumentList ClosedRoundBracket Dot; type: Int | Boolean | Char | Identifier; @@ -63,7 +62,11 @@ value: IntValue | BooleanValue | CharValue | NullValue; AccessModifierPublic : 'public' ; MainMethodDecl : 'public static void main(String[] args)'; - +//Types +Void : 'void'; +Int : 'int'; +Boolean : 'bool'; +Char : 'char'; //Operators DotOperator : Multipilkation | Division | Modulo; @@ -105,24 +108,23 @@ Else : 'else'; Return : 'return'; New : 'new'; + + +//Values +IntValue : ('+'|'-')*[0-9]+; +CharValue: '\''~[\r\n]?'\''; + + //Identifier fragment Alpabetic : [a-zA-Z]; fragment Numeric: [0-9]; fragment ValidIdentSymbols : Alpabetic|Numeric|'$'|'_'; Identifier: Alpabetic ValidIdentSymbols*; -//Types -Void : 'void'; -Int : 'int'; -Boolean : 'bool'; -Char : 'char'; - -//Values -IntValue : ('+'|'-')*[0-9]+; -CharValue: '\''~[\r\n]?'\''; -BooleanValue: 'true'|'false'; -NullValue: 'null'; - //Whitespace? Right into the trash it gooeesss WS : [ \t\r\n] -> skip; + + + +