summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordibyendumajumdar <mobile@majumdar.org.uk>2024-08-05 23:20:49 +0100
committerDibyendu Majumdar <dibyendumajumdar@users.noreply.github.com>2024-08-06 00:50:58 +0100
commit5dad6618f075aac78023183354f0456e5dcc1d54 (patch)
treeebd8445dfd1efbb49d6a8b7c4c698dd50068a96e
parenta15a64509645a238357416066eac27780962aaec (diff)
downloadSimple-5dad6618f075aac78023183354f0456e5dcc1d54.tar.gz
Simple-5dad6618f075aac78023183354f0456e5dcc1d54.zip
chapter 13 from 12, splitting out ref fields in structs
-rw-r--r--chapter13/docs/13-grammar.md129
-rw-r--r--chapter13/pom.xml4
-rw-r--r--pom.xml1
3 files changed, 132 insertions, 2 deletions
diff --git a/chapter13/docs/13-grammar.md b/chapter13/docs/13-grammar.md
new file mode 100644
index 00000000..65611618
--- /dev/null
+++ b/chapter13/docs/13-grammar.md
@@ -0,0 +1,129 @@
+# Grammar for Chapter 13
+
+```antlrv4
+grammar SimpleLanguage;
+
+program
+ : statement+ EOF
+ ;
+
+statement
+ : returnStatement
+ | structDeclaration
+ | declStatement
+ | blockStatment
+ | expressionStatement
+ | ifStatement
+ | whileStatement
+ | breakStatement
+ | continueStatment
+ | metaStatement
+ ;
+
+field
+ : 'int' IDENTIFIER ';'
+ ;
+
+fields
+ : field+
+ ;
+
+structDeclaration
+ : 'struct' IDENTIFIER '{' fields '}'
+ ;
+
+whileStatement
+ : 'while' '(' expression ')' statement
+ ;
+
+breakStatement
+ : 'break' ';'
+ ;
+
+continueStatement
+ : 'continue' ';'
+ ;
+
+ifStatement
+ : 'if' '(' expression ')' statement ('else' statement)?
+ ;
+
+metaStatement
+ : '#showGraph' ';'
+ ;
+
+expressionStatement
+ : IDENTIFIER '=' expression ';'
+ | fieldExpression '=' expression ';'
+ ;
+
+blockStatement
+ : '{' statement+ '}'
+ ;
+
+structName
+ : IDENTIFIER
+ ;
+
+declStatement
+ : 'int' IDENTIFIER '=' expression ';'
+ | structName IDENTIFIER ('?')? '=' expression ';'
+ ;
+
+returnStatement
+ : 'return' expression ';'
+ ;
+
+expression
+ : comparisonExpression
+ ;
+
+comparisonExpression
+ : additiveExpression (('==' | '!='| '>'| '<'| '>='| '<=') additiveExpression)*
+ ;
+
+additiveExpression
+ : multiplicativeExpression (('+' | '-') multiplicativeExpression)*
+ ;
+
+multiplicativeExpression
+ : unaryExpression (('*' | '/') unaryExpression)*
+ ;
+
+unaryExpression
+ : ('-') unaryExpression
+ | '!' unaryExpression
+ | primaryExpression
+ ;
+
+newExpression
+ : 'new' IDENTIFIER
+ ;
+
+fieldExpression
+ : primaryExpresson '.' IDENTIFIER
+ ;
+
+primaryExpression
+ : IDENTIFIER
+ | INTEGER_LITERAL
+ | 'true'
+ | 'false'
+ | 'null'
+ | newExpression
+ | '(' expression ')'
+ | fieldExpression
+ ;
+
+INTEGER_LITERAL
+ : [1-9][0-9]*
+ | [0]
+ ;
+
+IDENTIFIER
+ : NON_DIGIT (NON_DIGIT | DIGIT)*
+ ;
+
+NON_DIGIT: [a-zA-Z_];
+DEC_DIGIT: [0-9];
+```
diff --git a/chapter13/pom.xml b/chapter13/pom.xml
index c46bf13d..93761369 100644
--- a/chapter13/pom.xml
+++ b/chapter13/pom.xml
@@ -8,7 +8,7 @@
<artifactId>simple</artifactId>
<version>1.0</version>
</parent>
- <artifactId>chapter12</artifactId>
+ <artifactId>chapter13</artifactId>
<packaging>jar</packaging>
- <name>Chapter 12</name>
+ <name>Chapter 13</name>
</project>
diff --git a/pom.xml b/pom.xml
index aad91d4c..749c2e07 100644
--- a/pom.xml
+++ b/pom.xml
@@ -31,6 +31,7 @@
<module>chapter10</module>
<module>chapter11</module>
<module>chapter12</module>
+ <module>chapter13</module>
</modules>
<properties>