summaryrefslogtreecommitdiff
path: root/Compiler/CGrammar.g4
diff options
context:
space:
mode:
authorMelodi <62381002+Melodi17@users.noreply.github.com>2025-08-09 09:45:22 +1000
committerMelodi <62381002+Melodi17@users.noreply.github.com>2025-08-09 09:45:22 +1000
commit7f334796e8d0619dc744b1345a7b7b73a27220f9 (patch)
tree709c9a7df920d8eb1e779df4f878a25596303f17 /Compiler/CGrammar.g4
parentd36d51429d604f0ab3b2076c4c05e28ebf9b52c5 (diff)
added method calling and for loops
Diffstat (limited to 'Compiler/CGrammar.g4')
-rw-r--r--Compiler/CGrammar.g425
1 files changed, 19 insertions, 6 deletions
diff --git a/Compiler/CGrammar.g4 b/Compiler/CGrammar.g4
index 484cc90..b0f145f 100644
--- a/Compiler/CGrammar.g4
+++ b/Compiler/CGrammar.g4
@@ -1,5 +1,9 @@
grammar CGrammar;
+options {
+ contextSuperClass = Q1.Compiler.Context;
+}
+
ID: [a-zA-Z_][a-zA-Z0-9_]*;
INT: [0-9]+;
WS: [ \t\r\n]+ -> skip;
@@ -7,11 +11,12 @@ COMMENT: '//' .*? ([\n] | EOF) -> channel(HIDDEN);
program
- : function EOF
+ : function+ EOF
;
function
- : 'int' name=ID '(' ')' block
+ : 'int' name=ID '(' ( types+='int' params+=ID (',' types+='int' params+=ID )* )? ')' ';' #functionPrototype
+ | 'int' name=ID '(' ( types+='int' params+=ID (',' types+='int' params+=ID )* )? ')' block #functionDefinition
;
block
@@ -26,8 +31,15 @@ block_item
statement
: 'return' expression ';' #returnStatement
| 'if' '(' expression ')' then=statement ('else' else=statement)? #ifStatement
- | expression #expressionStatement
+ | 'for' '(' (initializer=expression)? ';' cond=expression? ';' post=expression? ')' body=statement #forExprStatement
+ | 'for' '(' initializer=declaration cond=expression? ';' post=expression? ')' body=statement #forDeclStatement
+ | 'while' '(' cond=expression ')' body=statement #whileStatement
+ | 'do' body=statement 'while' '(' cond=expression ')' ';' #doWhileStatement
+ | 'break' ';' #breakStatement
+ | 'continue' ';' #continueStatement
+ | expression ';' #expressionStatement
| block #blockStatement
+ | ';' #nullStatement
;
@@ -37,10 +49,13 @@ declaration
expression
: '(' expression ')' #parenthesizedExpression
+// | left=expression ',' right=expression #commaExpression
| constant #constantExpression
- | target '=' expression ';' #assignmentExpression
+ | target '=' expression #assignmentExpression
| ID #variableExpression
+ | ID '(' ( params+=expression (',' params+=expression )* )? ')' #callExpression
+
| '!' expression #notExpression
| '~' expression #invertExpression
@@ -87,8 +102,6 @@ expression
| target '<<=' expression #compoundBitwiseLeftShiftExpression
| target '>>=' expression #compoundBitwiseRightShiftExpression
- | left=expression ',' right=expression #commaExpression
-
| cond=expression '?' then=expression ':' else=expression #ternaryExpression
;