summaryrefslogtreecommitdiff
path: root/chapter01
diff options
context:
space:
mode:
Diffstat (limited to 'chapter01')
-rw-r--r--chapter01/README.md10
-rw-r--r--chapter01/docs/01-grammar.md39
2 files changed, 49 insertions, 0 deletions
diff --git a/chapter01/README.md b/chapter01/README.md
index e69de29b..d21a55e8 100644
--- a/chapter01/README.md
+++ b/chapter01/README.md
@@ -0,0 +1,10 @@
+# Chapter 1
+Simple control flow statements
+
+The chapter will implement:
+* Control flow statements, `if` and `while`.
+* Variables of `int` type.
+* Simple arithmetic such as addition, subtraction, multiplication, division.
+* Non-zero values will be truthy.
+
+* [Grammar](docs/01-grammar.md) \ No newline at end of file
diff --git a/chapter01/docs/01-grammar.md b/chapter01/docs/01-grammar.md
index e69de29b..92ed7357 100644
--- a/chapter01/docs/01-grammar.md
+++ b/chapter01/docs/01-grammar.md
@@ -0,0 +1,39 @@
+# Grammar for Chapter 1
+
+```antlrv4
+grammar SimpleLanguage;
+
+program
+ : declaration* statement+ EOF
+ ;
+
+declaration
+ : type IDENTIFIER ';'
+ ;
+
+type
+ : 'int'
+ ;
+
+statement
+ : 'if' '(' expression ')' statement
+ | 'if' '(' expression ')' statement 'else' statement
+ | 'while' '(' expression ')' statement
+ | IDENTIFIER '=' expression ';'
+ | '{' declaration* statement+ '}'
+ | 'break' ';'
+ ;
+
+expression
+ : primaryExpression # PrimaryExpression_
+ | expression ('*' | '/' ) expression # ArithmeticOrLogicalExpression
+ | expression ('+' | '-') expression # ArithmeticOrLogicalExpression
+ ;
+
+primaryExpression
+ :
+ | INTEGER_LITERAL
+ | IDENTIFIER
+ | '(' expression ')'
+ ;
+``` \ No newline at end of file