summaryrefslogtreecommitdiff
path: root/chapter17a/docs/17-grammar.md
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-10-07 09:53:15 -0700
committerCliff Click <cliffc@acm.org>2026-10-07 09:53:15 -0700
commitb2862f980f3d78012066ef1cc09d4ad58f886c0f (patch)
treebbeab7de6ac8b556911d117989ab4bbc0324ecb5 /chapter17a/docs/17-grammar.md
parent863a6ed0ad723e06c76f392d4fb3a5f6bd22863c (diff)
downloadSimple-b2862f980f3d78012066ef1cc09d4ad58f886c0f.tar.gz
Simple-b2862f980f3d78012066ef1cc09d4ad58f886c0f.zip
Pick up japanese transaltionpr262-doc-followups
A bunch of minor README fixes, missing TOC, grammer cleanup
Diffstat (limited to 'chapter17a/docs/17-grammar.md')
-rw-r--r--chapter17a/docs/17-grammar.md120
1 files changed, 106 insertions, 14 deletions
diff --git a/chapter17a/docs/17-grammar.md b/chapter17a/docs/17-grammar.md
index a8cd8818..52afb324 100644
--- a/chapter17a/docs/17-grammar.md
+++ b/chapter17a/docs/17-grammar.md
@@ -1,19 +1,111 @@
-# Chapter 17a grammar additions
+# Grammar for Chapter 17a
English | [日本語](17-grammar.ja.md)
-The expression and statement forms are those of Chapter 16. Declarations add
-independent access and binding modifiers:
+```antlrv4
+grammar SimpleLanguage;
-```text
-declaration = type binding ("," binding)* ";"
-binding = ("!" | "~")? identifier ("=" expression)?
-type = ("!" | "~")? name suffix*
-suffix = "?" | "[]" | "[~]"
-```
+// Name lookup, assignable operands, type compatibility, and access permissions
+// are checked by the compiler in addition to these syntax rules.
+program : statement* EOF ;
+
+block : '{' statement* '}' ;
+
+statement
+ : block
+ | returnStatement
+ | ifStatement
+ | whileStatement
+ | breakStatement
+ | continueStatement
+ | structDeclaration
+ | declaration ';'
+ | assignment ';'
+ | ';'
+ ;
+
+returnStatement : 'return' assignment ';' ;
+ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ;
+whileStatement : 'while' '(' assignment ')' statement ;
+breakStatement : 'break' ';' ;
+continueStatement : 'continue' ';' ;
+
+structDeclaration : 'struct' IDENTIFIER block ';' ;
+
+declaration : declarationType binding (',' binding)* ;
+declarationType : type ;
+binding : bindingModifier? IDENTIFIER ('=' initializer)? ;
+bindingModifier : '!' | '~' ;
+initializer : assignment ;
+
+// The prefix qualifies the named struct reference, not an enclosing array.
+// Each [] / [~] suffix qualifies its own array layer. ? requires a reference.
+type
+ : primitiveType typeSuffix*
+ | accessModifier? typeName typeSuffix*
+ ;
+accessModifier : '!' | '~' ;
+typeSuffix : '?' | '[]' | '[~]' ;
+typeName : IDENTIFIER ;
+
+primitiveType
+ : 'int' | 'i8' | 'i16' | 'i32' | 'i64'
+ | 'u1' | 'u8' | 'u16' | 'u32' | 'byte' | 'bool'
+ | 'flt' | 'f32' | 'f64'
+ ;
+
+assignment : expression (assignmentOperator assignment)? ;
+assignmentOperator : '=' ;
+
+expression : bitwiseExpression ;
-A prefix qualifier requires a struct reference. It qualifies the named base,
-not enclosing arrays. Each bracket pair qualifies its own array layer. A binding
-modifier applies only to the following name. See the README for defaults and
-constructor initialization rules. `var`, `val`, increments, compound assignments,
-conditional expressions, and `for` loops are introduced in Chapter 17b.
+bitwiseExpression
+ : comparisonExpression (('&' | '|' | '^') comparisonExpression)*
+ ;
+
+comparisonExpression
+ : shiftExpression (('==' | '!=' | '<' | '<=' | '>' | '>=') shiftExpression)*
+ ;
+
+shiftExpression
+ : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)*
+ ;
+
+additiveExpression
+ : multiplicativeExpression (('+' | '-') multiplicativeExpression)*
+ ;
+
+multiplicativeExpression
+ : unaryExpression (('*' | '/') unaryExpression)*
+ ;
+
+unaryExpression
+ : ('-' | '!') unaryExpression
+ | postfixExpression
+ ;
+
+postfixExpression : primaryExpression postfixSuffix* ;
+postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' ;
+
+primaryExpression
+ : INTEGER_LITERAL
+ | FLOAT_LITERAL
+ | 'true'
+ | 'false'
+ | 'null'
+ | IDENTIFIER
+ | '(' assignment ')'
+ | newExpression
+ ;
+
+// Struct initialization uses a block; array allocation supplies a length.
+newExpression : 'new' type ('[' assignment ']' | block)? ;
+
+INTEGER_LITERAL : '0' | [1-9] DIGIT* ;
+FLOAT_LITERAL : DIGIT+ ('.' DIGIT* ('e' DIGIT+)? | 'e' DIGIT+) ;
+IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ;
+fragment NON_DIGIT : [a-zA-Z_] ;
+fragment DIGIT : [0-9] ;
+WHITESPACE : [ \t\r\n]+ -> skip ;
+LINE_COMMENT : '//' ~[\r\n]* -> skip ;
+```