summaryrefslogtreecommitdiff
path: root/Compiler/CGrammar.g4
diff options
context:
space:
mode:
authorMelodi <62381002+Melodi17@users.noreply.github.com>2025-08-12 07:41:29 +1000
committerMelodi <62381002+Melodi17@users.noreply.github.com>2025-08-12 07:41:29 +1000
commit94ddc02e03ab942d0c881e29053e8df3d2940c30 (patch)
tree4cd91b46b70a29ffd8a8247d68afd97d0852a0ce /Compiler/CGrammar.g4
parent252ad79d05e3805f7a8c990e19dccb373a2c5231 (diff)
WIP: added type specification, indexes and hex numerics
Diffstat (limited to 'Compiler/CGrammar.g4')
-rw-r--r--Compiler/CGrammar.g416
1 files changed, 13 insertions, 3 deletions
diff --git a/Compiler/CGrammar.g4 b/Compiler/CGrammar.g4
index 1b47978..b0a03f7 100644
--- a/Compiler/CGrammar.g4
+++ b/Compiler/CGrammar.g4
@@ -4,6 +4,8 @@ options {
contextSuperClass = Q1.Compiler.Context;
}
+HEX: '0x' [0-9a-fA-F];
+
ID: [a-zA-Z_][a-zA-Z0-9_]*;
INT: [0-9]+;
WS: [ \t\r\n]+ -> skip;
@@ -15,8 +17,8 @@ program
;
function
- : '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
+ : rtype=type name=ID '(' ( types+=type params+=ID (',' types+=type params+=ID )* )? ')' ';' #functionPrototype
+ | rtype=type name=ID '(' ( types+=type params+=ID (',' types+=type params+=ID )* )? ')' block #functionDefinition
;
block
@@ -44,7 +46,7 @@ statement
declaration
- : 'int' ptr='*' name=ID ('=' value=expression)? ';' #variableDeclaration
+ : type name=ID ('=' value=expression)? ';' #variableDeclaration
;
expression
@@ -57,6 +59,7 @@ expression
| ID #variableExpression
| ID '(' ( params+=expression (',' params+=expression )* )? ')' #callExpression
+ | obj=expression '[' indexer=expression ']' #indexExpression
| '!' expression #notExpression
| '~' expression #invertExpression
@@ -109,6 +112,13 @@ expression
constant
: INT # intConstant
+ | HEX # hexConstant
+ ;
+
+type
+ : 'int' #intType
+ | 'char' #charType
+ | type '*' #pointerType
;
target