diff options
90 files changed, 2400 insertions, 3100 deletions
diff --git a/.github/workflows/linearize.sh b/.github/workflows/linearize.sh index 7ce8e52c..f793ac6d 100755 --- a/.github/workflows/linearize.sh +++ b/.github/workflows/linearize.sh @@ -17,6 +17,12 @@ git init -q -b main repo=.. +# The donated Japanese index accompanies the English one when present. +readmes=(README.md) +if [[ -f "$repo/README.ja.md" ]]; then + readmes+=(README.ja.md) +fi + last_committer_date='0 +0000' git -C "$repo" ls-tree HEAD --name-only | grep '^chapter' | @@ -47,7 +53,7 @@ while read -r chapter; do done <<< "$all_authors" # Get the first and last dates this chapter was modified. - root_files=(README.md LICENSE pom.xml .gitignore .dir-locals.el transpile-tests graph print isa build-support) + root_files=("${readmes[@]}" LICENSE pom.xml .gitignore .dir-locals.el transpile-tests graph print isa build-support) author_root_files=() if [[ $chapter = chapter01 ]]; then # Only include the commit date of the root files for chapter01. @@ -65,29 +71,47 @@ while read -r chapter; do git rm -qr --ignore-unmatch . cp -R "$repo/$chapter/." . mkdir -p docs - mv README.md docs/ + # Grammar documents now sit beside their chapter README. + for doc in docs/*.md; do + [[ -f "$doc" ]] || continue + sed -Ei 's,\(\.\./README(\.ja)?\.md([#)]),(README\1.md\2,g' "$doc" + sed -Ei 's,\(\.\./\.\./(chapter[0-9]+[a-z]?)/docs/,(../\1/,g' "$doc" + done + # Keep the READMEs and supporting notes together, including Japanese copies. + mv *.md docs/ mv docs chapter_docs + if [[ -d appendix ]]; then + mv appendix chapter_docs/ + sed -Ei 's,\(\.\./docs/,(../,g' chapter_docs/appendix/*.md + fi rm pom.xml || : git add . - # Add the shared files in the root, except for README.md and pom.xml. + # Add the shared files in the root, except for the indexes and pom.xml. root_files=(LICENSE .gitignore .dir-locals.el transpile-tests graph print isa build-support) cp -R "${root_files[@]/#/"$repo/"}" . git add "${root_files[@]}" - # Fix README.md, docs/chapter??/README.md, and pom.xml. + # Fix the English/Japanese indexes, chapter READMEs, and pom.xml. if git rev-parse HEAD >/dev/null 2>/dev/null; then # Restore the documentation and pom.xml from the previous chapter. - git diff --staged --diff-filter=D --name-only -- docs README.md pom.xml | xargs git checkout -q HEAD + git diff --staged --diff-filter=D --name-only -- docs "${readmes[@]}" pom.xml | xargs git checkout -q HEAD else # This is the first commit; setup shared files. - cp "$repo"/{README.md,pom.xml} . + cp "${readmes[@]/#/"$repo/"}" "$repo/pom.xml" . mkdir docs # Shared type-domain illustrations are referenced from chapter READMEs. cp "$repo"/docs/type-*.svg docs/ git add docs/type-*.svg + if [[ -f "$repo/docs/japanese-translation.md" ]]; then + cp "$repo/docs/japanese-translation.md" docs/ + git add docs/japanese-translation.md + fi # Remove links to chapters. sed -Ei 's,\[Chapter ([0-9]+[a-z]?)\]\(chapter0?\1/README\.md\),Chapter \1,' README.md + if [[ -f README.ja.md ]]; then + sed -Ei 's,\[([^]]+)\]\(chapter[0-9]+[a-z]?/README(\.ja)?\.md\),\1,g' README.ja.md + fi # Change to a JAR and delete the modules, for a single-project structure. sed -i -e 's,<packaging>pom</packaging>,<packaging>jar</packaging>,' \ -e '/<modules>/,/^$/d' pom.xml @@ -107,13 +131,22 @@ while read -r chapter; do git mv chapter_docs "docs/$chapter" # Restore the link to this chapter. sed -Ei "s,^\* Chapter $chapter_number: ,* [Chapter $chapter_number](docs/$chapter/README.md): ," README.md + if [[ -f README.ja.md ]]; then + translated=README.md + if [[ -f "docs/$chapter/README.ja.md" ]]; then + translated=README.ja.md + fi + sed -Ei "s,^\* 第${chapter_number}章:,* [第${chapter_number}章](docs/$chapter/$translated):," README.ja.md + fi # Repair links for this chapter. - sed -Ei 's,\bdocs/,,' "docs/$chapter/README.md" - sed -Ei 's,\(\.\./isa/,(../../isa/,g' "docs/$chapter/README.md" - # Source links are chapter-relative in the main tree; sources live at the - # repository root in a standalone chapter checkout. - sed -Ei 's,\((\./)?src/,(../../src/,g' "docs/$chapter/README.md" - git add README.md "docs/$chapter/README.md" pom.xml + for doc in "docs/$chapter"/README*.md; do + sed -Ei 's,\bdocs/,,g' "$doc" + sed -Ei 's,\(\.\./(isa|graph|print)/,(../../\1/,g' "$doc" + sed -Ei 's,\(\.\./README(\.ja)?\.md([#)]),(../../README\1.md\2,g' "$doc" + # Sources live at the root in a standalone chapter checkout. + sed -Ei 's,\((\./)?src/,(../../src/,g' "$doc" + done + git add "${readmes[@]}" "docs/$chapter" pom.xml # Create a commit for the chapter, using the metadata. if [[ ! "$first_author" =~ ^(.*)\ \<(.*)\>$ ]]; then diff --git a/README.ja.md b/README.ja.md index a6b543b4..b9d8ba51 100644 --- a/README.ja.md +++ b/README.ja.md @@ -1,6 +1,6 @@ # Simple -[English](README.md) | 日本語 +[English](README.md) | 日本語 | [翻訳について](docs/japanese-translation.md) この文書は [Simple の README](README.md) の日本語訳です。 diff --git a/chapter02/docs/02-grammar.ja.md b/chapter02/docs/02-grammar.ja.md index 4a36b68e..c08359e3 100644 --- a/chapter02/docs/02-grammar.ja.md +++ b/chapter02/docs/02-grammar.ja.md @@ -3,8 +3,7 @@ [English](02-grammar.md) | 日本語 この文書は [第2章の文法定義](02-grammar.md) の日本語版です。 -文法定義そのものは原文と同一です。原文の見出しは「Grammar for Chapter 1」ですが、 -このファイルは第2章の算術演算の文法を定義しているため、日本語版の見出しでは第2章としています。 +文法定義そのものは原文と同一です。 [第2章:算術演算に戻る](../README.ja.md) diff --git a/chapter02/docs/02-grammar.md b/chapter02/docs/02-grammar.md index 762bed5d..994a398a 100644 --- a/chapter02/docs/02-grammar.md +++ b/chapter02/docs/02-grammar.md @@ -1,4 +1,4 @@ -# Grammar for Chapter 1 +# Grammar for Chapter 2 English | [日本語](02-grammar.ja.md) diff --git a/chapter03/docs/03-grammar.ja.md b/chapter03/docs/03-grammar.ja.md index 842e7cb4..9acd1c47 100644 --- a/chapter03/docs/03-grammar.ja.md +++ b/chapter03/docs/03-grammar.ja.md @@ -4,8 +4,6 @@ この文書は [原文](03-grammar.md) の日本語訳です。 -> 訳注: 原文の見出しは「Grammar for Chapter 1」ですが、このファイルは第3章に属するため、訳の見出しは第3章としています。文法は原文のまま保持しています。 - [第3章に戻る](../README.ja.md) diff --git a/chapter03/docs/03-grammar.md b/chapter03/docs/03-grammar.md index f19b8dfc..3ae8463e 100644 --- a/chapter03/docs/03-grammar.md +++ b/chapter03/docs/03-grammar.md @@ -1,4 +1,4 @@ -# Grammar for Chapter 1 +# Grammar for Chapter 3 English | [日本語](03-grammar.ja.md) diff --git a/chapter04/appendix/dce.ja.md b/chapter04/appendix/dce.ja.md index 4c461277..10a7ef57 100644 --- a/chapter04/appendix/dce.ja.md +++ b/chapter04/appendix/dce.ja.md @@ -152,7 +152,7 @@ return (2+1)+3; public final Node peephole( ) { ... if (!(this instanceof ConstantNode) && type.isConstant()) - return new deadCodeElim(ConstantNode(type).peephole()); + return deadCodeElim(new ConstantNode(type).peephole()); ... @@ -165,8 +165,6 @@ public final Node peephole( ) { } ``` -> 訳注: 上の原文コードの `return new deadCodeElim(ConstantNode(type).peephole());` は、そのままでは Java の構文として正しくありません。コードは原文どおり保持しています。章本文の対応するコードでは `return deadCodeElim(new ConstantNode(type).peephole());` となっています。 - ```java // m is the new Node, self is the old. // Return 'm', which may have zero uses but is alive nonetheless. diff --git a/chapter04/appendix/dce.md b/chapter04/appendix/dce.md index 10ff6bf1..d8fb9adc 100644 --- a/chapter04/appendix/dce.md +++ b/chapter04/appendix/dce.md @@ -135,7 +135,7 @@ To fix this, we introduce an extra `deadCodeElim` call that is responsible for d public final Node peephole( ) { ... if (!(this instanceof ConstantNode) && type.isConstant()) - return new deadCodeElim(ConstantNode(type).peephole()); + return deadCodeElim(new ConstantNode(type).peephole()); ... diff --git a/chapter04/docs/04-grammar.ja.md b/chapter04/docs/04-grammar.ja.md index b8b6bff5..a17ec8cd 100644 --- a/chapter04/docs/04-grammar.ja.md +++ b/chapter04/docs/04-grammar.ja.md @@ -4,8 +4,6 @@ この文書は [原文](04-grammar.md) の日本語訳です。 -> 訳注: 原文の見出しは「Grammar for Chapter 1」ですが、このファイルは第4章に属するため、訳の見出しは第4章としています。文法は原文のまま保持しています。 - [第4章に戻る](../README.ja.md) diff --git a/chapter04/docs/04-grammar.md b/chapter04/docs/04-grammar.md index 3ea27850..bd01c305 100644 --- a/chapter04/docs/04-grammar.md +++ b/chapter04/docs/04-grammar.md @@ -1,4 +1,4 @@ -# Grammar for Chapter 1 +# Grammar for Chapter 4 English | [日本語](04-grammar.ja.md) diff --git a/chapter05/README.ja.md b/chapter05/README.ja.md index 0fc029f4..b5edcee6 100644 --- a/chapter05/README.ja.md +++ b/chapter05/README.ja.md @@ -28,8 +28,6 @@ 17. [Phi を通して加算を押し上げる](#phi-を通して加算を押し上げる) 18. [さらにいくつかの例](#さらにいくつかの例) -> 訳注: 原文の目次には「Recap」がありますが、対応する節がないため、この目次では省いています。 - [linear](https://github.com/SeaOfNodes/Simple/tree/linear) ブランチの直線的な Git のリビジョン履歴で [この章](https://github.com/SeaOfNodes/Simple/tree/linear-chapter05) を読み、前の章と [比較](https://github.com/SeaOfNodes/Simple/compare/linear-chapter04...linear-chapter05) することもできます。 この章では、言語の文法を拡張して次の機能を追加します。 diff --git a/chapter05/README.md b/chapter05/README.md index b706c45d..40886af2 100644 --- a/chapter05/README.md +++ b/chapter05/README.md @@ -7,25 +7,24 @@ English | [日本語](README.ja.md) # Table of Contents -1. [Recap](#recap) -2. [New Nodes](#new-nodes) -3. [`IfNode`](#ifnode) -4. [`PhiNode`](#phinode) -5. [`RegionNode`](#regionnode) -6. [Pinned data nodes](#we-do-not-associate-a-control-edge-on-every-data-node-in-the-graph) -7. [`Stop` Nodes](#stop-nodes) -8. [Parsing an `if` Statement](#parsing-an-if-statement) -9. [Operations on ScopeNodes](#operations-on-scopenodes) -10. [Duplicating a ScopeNode](#duplicating-a-scopenode) -11. [Merging two ScopeNodes](#merging-two-scopenodes) -12. [Example 1](#example-1) -13. [Before Merging](#before-merging) -14. [After Merging](#after-merging) -15. [Finally](#finally) -16. [Example 2](#example-2) -17. [Example 3](#example-3) -18. [Pushing addition up through a Phi](#pushing-addition-up-through-a-phi) -19. [More Examples](#more-examples) +1. [New Nodes](#new-nodes) +2. [`IfNode`](#ifnode) +3. [`PhiNode`](#phinode) +4. [`RegionNode`](#regionnode) +5. [Pinned data nodes](#we-do-not-associate-a-control-edge-on-every-data-node-in-the-graph) +6. [`Stop` Nodes](#stop-nodes) +7. [Parsing an `if` Statement](#parsing-an-if-statement) +8. [Operations on ScopeNodes](#operations-on-scopenodes) +9. [Duplicating a ScopeNode](#duplicating-a-scopenode) +10. [Merging two ScopeNodes](#merging-two-scopenodes) +11. [Example 1](#example-1) +12. [Before Merging](#before-merging) +13. [After Merging](#after-merging) +14. [Finally](#finally) +15. [Example 2](#example-2) +16. [Example 3](#example-3) +17. [Pushing addition up through a Phi](#pushing-addition-up-through-a-phi) +18. [More Examples](#more-examples) You can also read [this chapter](https://github.com/SeaOfNodes/Simple/tree/linear-chapter05) in a linear Git revision history on the [linear](https://github.com/SeaOfNodes/Simple/tree/linear) branch and [compare](https://github.com/SeaOfNodes/Simple/compare/linear-chapter04...linear-chapter05) it to the previous chapter. diff --git a/chapter05/docs/05-grammar.ja.md b/chapter05/docs/05-grammar.ja.md index 84c60522..dab10330 100644 --- a/chapter05/docs/05-grammar.ja.md +++ b/chapter05/docs/05-grammar.ja.md @@ -4,8 +4,6 @@ この文書は [原文](05-grammar.md) の日本語訳です。 -> 訳注: 原文の見出しは「Grammar for Chapter 1」ですが、このファイルは第5章に属するため、訳の見出しは第5章としています。文法は原文のまま保持しています。 - [第5章に戻る](../README.ja.md) diff --git a/chapter05/docs/05-grammar.md b/chapter05/docs/05-grammar.md index 86c5b007..2be5a8a5 100644 --- a/chapter05/docs/05-grammar.md +++ b/chapter05/docs/05-grammar.md @@ -1,4 +1,4 @@ -# Grammar for Chapter 1 +# Grammar for Chapter 5 English | [日本語](05-grammar.ja.md) diff --git a/chapter06/README.ja.md b/chapter06/README.ja.md index 73e660c9..bc05ae75 100644 --- a/chapter06/README.ja.md +++ b/chapter06/README.ja.md @@ -7,6 +7,8 @@ [前の章: 第5章](../chapter05/README.ja.md) | [次の章: 第7章](../chapter07/README.ja.md) +文法は[第5章](../chapter05/docs/05-grammar.ja.md)から変わりません。 + # 目次 1. [型システムの改訂](#型システムの改訂) diff --git a/chapter06/README.md b/chapter06/README.md index 3a7c7e09..ec44e7bf 100644 --- a/chapter06/README.md +++ b/chapter06/README.md @@ -5,6 +5,8 @@ English | [日本語](README.ja.md) [Previous: Chapter 5](../chapter05/README.md) | [Next: Chapter 7](../chapter07/README.md) +The grammar is unchanged from [Chapter 5](../chapter05/docs/05-grammar.md). + # Table of Contents 1. [Type System Revision](#type-system-revision) diff --git a/chapter06/docs/06-grammar.ja.md b/chapter06/docs/06-grammar.ja.md deleted file mode 100644 index fba9379a..00000000 --- a/chapter06/docs/06-grammar.ja.md +++ /dev/null @@ -1,86 +0,0 @@ -# 第6章の文法 - -[English](06-grammar.md) | 日本語 - -この文書は [原文](06-grammar.md) の日本語訳です。 - - -[第6章に戻る](../README.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program - : statement+ EOF - ; - -statement - : returnStatement - | declStatement - | blockStatment - | expressionStatement - | ifStatement - ; - -ifStatement - : 'if' '(' expression ')' statement ('else' statement)? - ; - - -expressionStatement - : IDENTIFIER '=' expression ';' - ; - -blockStatement - : '{' statement+ '}' - ; - -declStatement - : 'int' IDENTIFIER '=' expression ';' - ; - -returnStatement - : 'return' expression ';' - ; - -expression - : comparisonExpression - ; - -comparisonExpression - : additiveExpression (('==' | '!='| '>'| '<'| '>='| '<=') additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | primaryExpression - ; - -primaryExpression - : IDENTIFIER - | INTEGER_LITERAL - | 'true' - | 'false' - | '(' expression ')' - ; - -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/chapter06/docs/06-grammar.md b/chapter06/docs/06-grammar.md deleted file mode 100644 index 2dbde035..00000000 --- a/chapter06/docs/06-grammar.md +++ /dev/null @@ -1,81 +0,0 @@ -# Grammar for Chapter 6 - -English | [日本語](06-grammar.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program - : statement+ EOF - ; - -statement - : returnStatement - | declStatement - | blockStatment - | expressionStatement - | ifStatement - ; - -ifStatement - : 'if' '(' expression ')' statement ('else' statement)? - ; - - -expressionStatement - : IDENTIFIER '=' expression ';' - ; - -blockStatement - : '{' statement+ '}' - ; - -declStatement - : 'int' IDENTIFIER '=' expression ';' - ; - -returnStatement - : 'return' expression ';' - ; - -expression - : comparisonExpression - ; - -comparisonExpression - : additiveExpression (('==' | '!='| '>'| '<'| '>='| '<=') additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | primaryExpression - ; - -primaryExpression - : IDENTIFIER - | INTEGER_LITERAL - | 'true' - | 'false' - | '(' expression ')' - ; - -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/chapter07/docs/07-grammar.ja.md b/chapter07/docs/07-grammar.ja.md index ccc08250..5efcdda2 100644 --- a/chapter07/docs/07-grammar.ja.md +++ b/chapter07/docs/07-grammar.ja.md @@ -4,8 +4,6 @@ この文書は [原文](07-grammar.md) の日本語訳です。 -> 訳注: 原文の見出しは「Grammar for Chapter 6」ですが、このファイルは第7章に属するため、訳の見出しは第7章としています。文法は原文のまま保持しています。 - [第7章に戻る](../README.ja.md) diff --git a/chapter07/docs/07-grammar.md b/chapter07/docs/07-grammar.md index 51ef8ebe..c09f499c 100644 --- a/chapter07/docs/07-grammar.md +++ b/chapter07/docs/07-grammar.md @@ -1,4 +1,4 @@ -# Grammar for Chapter 6 +# Grammar for Chapter 7 English | [日本語](07-grammar.ja.md) diff --git a/chapter09/README.ja.md b/chapter09/README.ja.md index a0fe23c3..5019f5f9 100644 --- a/chapter09/README.ja.md +++ b/chapter09/README.ja.md @@ -7,6 +7,8 @@ [前の章: 第8章](../chapter08/README.ja.md) | [次の章: 第10章](../chapter10/README.ja.md) +文法は[第8章](../chapter08/docs/08-grammar.ja.md)から変わりません。 + # 目次 1. [ピープホール最適化の基盤づくり](#ピープホール最適化の基盤づくり) @@ -19,8 +21,6 @@ - [例2](#例2) 7. [解析後の反復最適化](#解析後の反復最適化) -> 訳注: 原文の目次の「Other Concerns」は、本文では「Future Work」という見出しです。日本語の目次は本文の見出しに合わせています。 - [linear](https://github.com/SeaOfNodes/Simple/tree/linear) ブランチの直線的な Git のリビジョン履歴で [この章](https://github.com/SeaOfNodes/Simple/tree/linear-chapter09) を読み、前の章と [比較](https://github.com/SeaOfNodes/Simple/compare/linear-chapter08...linear-chapter09) することもできます。 この章では次のことを行います。 diff --git a/chapter09/README.md b/chapter09/README.md index 5cb70860..f900062c 100644 --- a/chapter09/README.md +++ b/chapter09/README.md @@ -5,13 +5,15 @@ English | [日本語](README.ja.md) [Previous: Chapter 8](../chapter08/README.md) | [Next: Chapter 10](../chapter10/README.md) +The grammar is unchanged from [Chapter 8](../chapter08/docs/08-grammar.md). + # Table of Contents 1. [Engineering Peepholes](#engineering-peepholes) 2. [Global Value Numbering](#global-value-numbering) 3. [Post-Parse Iterative Peepholes](#post-parse-iterative-peepholes) 4. [Distant Neighbors](#distant-neighbors) -5. [Other Concerns](#other-concerns) +5. [Future Work](#future-work) 6. [Common SubExpressions via GVN](#common-subexpressions-via-gvn) - [Example 1](#example-1) - [Example 2](#example-2) diff --git a/chapter09/docs/09-grammar.ja.md b/chapter09/docs/09-grammar.ja.md deleted file mode 100644 index f89b449b..00000000 --- a/chapter09/docs/09-grammar.ja.md +++ /dev/null @@ -1,103 +0,0 @@ -# 第9章の文法 - -[English](09-grammar.md) | 日本語 - -この文書は [原文](09-grammar.md) の日本語訳です。 - -> 訳注: 原文の見出しは「Grammar for Chapter 8」ですが、このファイルは第9章に属するため、訳の見出しは第9章としています。文法は原文のまま保持しています。 - - -[第9章に戻る](../README.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program - : statement+ EOF - ; - -statement - : returnStatement - | declStatement - | blockStatment - | expressionStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatment - ; - -whileStatement - : 'while' '(' expression ')' statement - ; - -breakStatement - : 'break' ';' - ; - -continueStatement - : 'continue' ';' - ; - -ifStatement - : 'if' '(' expression ')' statement ('else' statement)? - ; - - -expressionStatement - : IDENTIFIER '=' expression ';' - ; - -blockStatement - : '{' statement+ '}' - ; - -declStatement - : 'int' IDENTIFIER '=' expression ';' - ; - -returnStatement - : 'return' expression ';' - ; - -expression - : comparisonExpression - ; - -comparisonExpression - : additiveExpression (('==' | '!='| '>'| '<'| '>='| '<=') additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | primaryExpression - ; - -primaryExpression - : IDENTIFIER - | INTEGER_LITERAL - | 'true' - | 'false' - | '(' expression ')' - ; - -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/chapter09/docs/09-grammar.md b/chapter09/docs/09-grammar.md deleted file mode 100644 index 5f6d5209..00000000 --- a/chapter09/docs/09-grammar.md +++ /dev/null @@ -1,96 +0,0 @@ -# Grammar for Chapter 8 - -English | [日本語](09-grammar.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program - : statement+ EOF - ; - -statement - : returnStatement - | declStatement - | blockStatment - | expressionStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatment - ; - -whileStatement - : 'while' '(' expression ')' statement - ; - -breakStatement - : 'break' ';' - ; - -continueStatement - : 'continue' ';' - ; - -ifStatement - : 'if' '(' expression ')' statement ('else' statement)? - ; - - -expressionStatement - : IDENTIFIER '=' expression ';' - ; - -blockStatement - : '{' statement+ '}' - ; - -declStatement - : 'int' IDENTIFIER '=' expression ';' - ; - -returnStatement - : 'return' expression ';' - ; - -expression - : comparisonExpression - ; - -comparisonExpression - : additiveExpression (('==' | '!='| '>'| '<'| '>='| '<=') additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | primaryExpression - ; - -primaryExpression - : IDENTIFIER - | INTEGER_LITERAL - | 'true' - | 'false' - | '(' expression ')' - ; - -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/chapter11/README.ja.md b/chapter11/README.ja.md index 137ace01..7de65f28 100644 --- a/chapter11/README.ja.md +++ b/chapter11/README.ja.md @@ -7,7 +7,7 @@ この文書は [原文](README.md) の日本語訳です。 -第10章では、1つの SSA メモリ値でメモリへの作用を明示しました。この章ではパーサーのモデルをそのまま維持し、グラフの書き換えによって独立したメモリ鎖を復元します。新しい言語構文はありません。[文法](docs/10-grammar.ja.md)、ポインタ型、null チェックは変わりません。 +第10章では、1つの SSA メモリ値でメモリへの作用を明示しました。この章ではパーサーのモデルをそのまま維持し、グラフの書き換えによって独立したメモリ鎖を復元します。新しい言語構文はありません。[文法](../chapter10/docs/10-grammar.ja.md)、ポインタ型、null チェックは変わりません。 ## なぜメモリを分割するのか diff --git a/chapter11/README.md b/chapter11/README.md index 9bf2e1e7..115cb0d2 100644 --- a/chapter11/README.md +++ b/chapter11/README.md @@ -8,7 +8,7 @@ English | [日本語](README.ja.md) Chapter 10 made memory effects explicit with one SSA memory value. This chapter keeps exactly that parser model and recovers independent memory chains through graph rewrites. There is no new language syntax; the -[grammar](docs/10-grammar.md), pointer types, and null checks are unchanged. +[grammar](../chapter10/docs/10-grammar.md), pointer types, and null checks are unchanged. ## Why split memory? diff --git a/chapter11/docs/10-grammar.ja.md b/chapter11/docs/10-grammar.ja.md deleted file mode 100644 index 1dbfba6a..00000000 --- a/chapter11/docs/10-grammar.ja.md +++ /dev/null @@ -1,131 +0,0 @@ -# 第11章の文法 - -[English](10-grammar.md) | 日本語 - -この文書は [原文](10-grammar.md) の日本語訳です。 - -[第11章に戻る](../README.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program - : statement+ EOF - ; - -statement - : returnStatement - | structDeclaration - | declStatement - | blockStatment - | expressionStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatment - ; - -field - : 'int' IDENTIFIER ';' - ; - -fields - : field+ - ; - -structDeclaration - : 'struct' IDENTIFIER '{' fields '}' - ; - -whileStatement - : 'while' '(' expression ')' statement - ; - -breakStatement - : 'break' ';' - ; - -continueStatement - : 'continue' ';' - ; - -ifStatement - : 'if' '(' expression ')' statement ('else' statement)? - ; - - -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/chapter11/docs/10-grammar.md b/chapter11/docs/10-grammar.md deleted file mode 100644 index 6d9aec1c..00000000 --- a/chapter11/docs/10-grammar.md +++ /dev/null @@ -1,127 +0,0 @@ -# Grammar for Chapter 11 - -English | [日本語](10-grammar.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program - : statement+ EOF - ; - -statement - : returnStatement - | structDeclaration - | declStatement - | blockStatment - | expressionStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatment - ; - -field - : 'int' IDENTIFIER ';' - ; - -fields - : field+ - ; - -structDeclaration - : 'struct' IDENTIFIER '{' fields '}' - ; - -whileStatement - : 'while' '(' expression ')' statement - ; - -breakStatement - : 'break' ';' - ; - -continueStatement - : 'continue' ';' - ; - -ifStatement - : 'if' '(' expression ')' statement ('else' statement)? - ; - - -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/README.ja.md b/chapter13/README.ja.md index d16b96ed..de98df7a 100644 --- a/chapter13/README.ja.md +++ b/chapter13/README.ja.md @@ -11,6 +11,8 @@ また、この章ではかなり詳しいグラフアルゴリズムをいくつか紹介します。グラフ理論を復習しておくとよいでしょう。 +文法は[第12章](../chapter12/docs/12-grammar.ja.md)から変わりません。 + # 目次 1. [概要](#概要) diff --git a/chapter13/README.md b/chapter13/README.md index f86abe57..7df00642 100644 --- a/chapter13/README.md +++ b/chapter13/README.md @@ -13,6 +13,8 @@ Loads and Stores. Also in this chapter, we'll be presenting several fairly detailed graph algorithms. You might want to brush up on your graph theory! +The grammar is unchanged from [Chapter 12](../chapter12/docs/12-grammar.md). + # Table of Contents 1. [High Level Overview](#high-level-overview) diff --git a/chapter13/docs/13-grammar.ja.md b/chapter13/docs/13-grammar.ja.md deleted file mode 100644 index 9f18bc74..00000000 --- a/chapter13/docs/13-grammar.ja.md +++ /dev/null @@ -1,136 +0,0 @@ -# 第13章の文法 - -[English](13-grammar.md) | 日本語 - -この文書は [原文](13-grammar.md) の日本語訳です。 - -[第13章に戻る](../README.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program - : statement+ EOF - ; - -statement - : returnStatement - | structDeclaration - | declStatement - | blockStatement - | expressionStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - ; - -PRIMTYPE - : 'int' - ; - -field - : PRIMTYPE IDENTIFIER ';' - | structName ('?')? IDENTIFIER ';' - ; - -fields - : field+ - ; - -structDeclaration - : 'struct' IDENTIFIER '{' fields '}' - ; - -whileStatement - : 'while' '(' expression ')' statement - ; - -breakStatement - : 'break' ';' - ; - -continueStatement - : 'continue' ';' - ; - -ifStatement - : 'if' '(' expression ')' statement ('else' statement)? - ; - - -expressionStatement - : IDENTIFIER '=' expression ';' - | fieldExpression '=' expression ';' - ; - -blockStatement - : '{' statement+ '}' - ; - -structName - : IDENTIFIER - ; - -declStatement - : PRIMTYPE 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 - : primaryExpression '.' IDENTIFIER - ; - -primaryExpression - : IDENTIFIER - | INTEGER_LITERAL - | 'true' - | 'false' - | 'null' - | newExpression - | '(' expression ')' - | fieldExpression - ; - -INTEGER_LITERAL - : [1-9][0-9]* - | [0] - ; - -IDENTIFIER - : NON_DIGIT (NON_DIGIT | DEC_DIGIT)* - ; - -NON_DIGIT: [a-zA-Z_]; -DEC_DIGIT: [0-9]; -``` diff --git a/chapter13/docs/13-grammar.md b/chapter13/docs/13-grammar.md deleted file mode 100644 index 40cfc432..00000000 --- a/chapter13/docs/13-grammar.md +++ /dev/null @@ -1,132 +0,0 @@ -# Grammar for Chapter 13 - -English | [日本語](13-grammar.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program - : statement+ EOF - ; - -statement - : returnStatement - | structDeclaration - | declStatement - | blockStatement - | expressionStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - ; - -PRIMTYPE - : 'int' - ; - -field - : PRIMTYPE IDENTIFIER ';' - | structName ('?')? IDENTIFIER ';' - ; - -fields - : field+ - ; - -structDeclaration - : 'struct' IDENTIFIER '{' fields '}' - ; - -whileStatement - : 'while' '(' expression ')' statement - ; - -breakStatement - : 'break' ';' - ; - -continueStatement - : 'continue' ';' - ; - -ifStatement - : 'if' '(' expression ')' statement ('else' statement)? - ; - - -expressionStatement - : IDENTIFIER '=' expression ';' - | fieldExpression '=' expression ';' - ; - -blockStatement - : '{' statement+ '}' - ; - -structName - : IDENTIFIER - ; - -declStatement - : PRIMTYPE 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 - : primaryExpression '.' IDENTIFIER - ; - -primaryExpression - : IDENTIFIER - | INTEGER_LITERAL - | 'true' - | 'false' - | 'null' - | newExpression - | '(' expression ')' - | fieldExpression - ; - -INTEGER_LITERAL - : [1-9][0-9]* - | [0] - ; - -IDENTIFIER - : NON_DIGIT (NON_DIGIT | DEC_DIGIT)* - ; - -NON_DIGIT: [a-zA-Z_]; -DEC_DIGIT: [0-9]; -``` diff --git a/chapter14/README.ja.md b/chapter14/README.ja.md index 76d8bf4f..6fa0cd22 100644 --- a/chapter14/README.ja.md +++ b/chapter14/README.ja.md @@ -17,8 +17,6 @@ 6. [優先順位](#優先順位) 7. [型の束](#型の実装) -> 訳注: 原文の目次にある「Nodes」に対応する節は、原文本文にはありません。 - 構造体、参照フィールド、スケジューリングがそろったので、この章では、それらが運ぶ数値を拡張します。浮動小数点演算、狭い整数型と浮動小数点型、整数の範囲を追加します。 [linear](https://github.com/SeaOfNodes/Simple/tree/linear) ブランチの線形の Git リビジョン履歴で[この章](https://github.com/SeaOfNodes/Simple/tree/linear-chapter14)を読むことや、前章との[差分](https://github.com/SeaOfNodes/Simple/compare/linear-chapter13...linear-chapter14)を見ることもできます。 diff --git a/chapter14/README.md b/chapter14/README.md index dbfaa549..81862508 100644 --- a/chapter14/README.md +++ b/chapter14/README.md @@ -14,7 +14,6 @@ English | [日本語](README.ja.md) 5. [Bitwise operations](#bitwise-operations) 6. [Precedence](#precedence) 7. [Type lattice](#type-implementation) -8. [Nodes](#nodes) With structs, reference fields, and scheduling in place, this chapter extends diff --git a/chapter17a/README.ja.md b/chapter17a/README.ja.md index 2bbb632c..057c253b 100644 --- a/chapter17a/README.ja.md +++ b/chapter17a/README.ja.md @@ -9,6 +9,8 @@ 第16章はオブジェクトにフィールドの初期値を与えます。今度は、どの束縛を再代入でき、どの参照が構築後の書き込みを許すかを区別します。フィールドや配列要素も含め、これらは独立した問いです。第17b章では、同じ規則を使って型推論と代入の短縮形を追加します。 +この章の[完全な言語文法](docs/17-grammar.ja.md)はこちらです。 + ## 2つの権限 構造体型の前の修飾子はアクセス権限を表します。`!Point` は書き込みを許し、`~Point` は深い読み取り専用ビューを与えます。変数名の直前の修飾子は、その束縛を制御します。`!p` は再代入を許し、`~p` は固定します。空白によって意味は変わりません。 diff --git a/chapter17a/README.md b/chapter17a/README.md index 97904def..9eb70e83 100644 --- a/chapter17a/README.md +++ b/chapter17a/README.md @@ -10,6 +10,8 @@ bindings may be reassigned and which references permit writes after construction These are independent questions, including for fields and array elements. Chapter 17b will add inference and assignment shortcuts using these same rules. +Here is the [complete language grammar](docs/17-grammar.md) for this chapter. + ## Two permissions A modifier before a struct type names the access permission: `!Point` permits diff --git a/chapter17a/docs/17-grammar.ja.md b/chapter17a/docs/17-grammar.ja.md index 9a71f6d6..7675f837 100644 --- a/chapter17a/docs/17-grammar.ja.md +++ b/chapter17a/docs/17-grammar.ja.md @@ -1,16 +1,111 @@ -# 第17a章の文法の追加 +# 第17a章の文法 [English](17-grammar.md) | 日本語 -この文書は [原文](17-grammar.md) の日本語訳です。 +```antlrv4 +grammar SimpleLanguage; -式と文の形式は第16章と同じです。宣言に、独立したアクセスと束縛の修飾子を追加します。 +// Name lookup, assignable operands, type compatibility, and access permissions +// are checked by the compiler in addition to these syntax rules. +program : statement* EOF ; -```text -declaration = type binding ("," binding)* ";" -binding = ("!" | "~")? identifier ("=" expression)? -type = ("!" | "~")? name suffix* -suffix = "?" | "[]" | "[~]" -``` +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 ; + +bitwiseExpression + : comparisonExpression (('&' | '|' | '^') comparisonExpression)* + ; -前置の修飾子には構造体参照が必要です。それは名前付きの基本型を修飾し、囲む配列は修飾しません。各角括弧の組は、それぞれ自身の配列の層を修飾します。束縛の修飾子は、その直後の名前だけに適用します。デフォルトとコンストラクタの初期化規則については [README](../README.ja.md) を参照してください。`var`、`val`、インクリメント、複合代入、条件式、`for` ループは第17b章で導入します。 +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 ; +``` 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 ; +``` diff --git a/chapter17b/README.ja.md b/chapter17b/README.ja.md index 68f5c389..94585031 100644 --- a/chapter17b/README.ja.md +++ b/chapter17b/README.ja.md @@ -7,6 +7,8 @@ この文書は [原文](README.md) の日本語訳です。 +この章の[完全な言語文法](docs/17-grammar.ja.md)はこちらです。 + ## 目次 1. [前置・後置インクリメント](#前置後置インクリメント) @@ -15,8 +17,6 @@ 4. [三項演算](#三項演算) 5. [for ループ](#for-ループ) -> 訳注: 原文の目次にある「Memory effects」に対応する節は、原文本文にはありません。 - 第17a章では、代入で確認する権限を確立しました。この章ではその権限を変えずに、代入と制御フローを短く書く方法を追加します。その後、第18章で関数と呼び出しを導入します。 ## 前置・後置インクリメント diff --git a/chapter17b/README.md b/chapter17b/README.md index 1b3a41af..d2c73584 100644 --- a/chapter17b/README.md +++ b/chapter17b/README.md @@ -6,6 +6,8 @@ English | [日本語](README.ja.md) [Next: Chapter 18](../chapter18/README.md) +Here is the [complete language grammar](docs/17-grammar.md) for this chapter. + ## Table of Contents 1. [Pre/Post-increment](#prepost-increment) @@ -13,7 +15,6 @@ English | [日本語](README.ja.md) 3. [var/val](#var--val) 4. [Trinary](#trinary) 5. [For Loops](#for-loops) -6. [Memory effects](#memory-effects) Chapter 17a established the permissions checked by assignments. This chapter adds shorter ways to write assignments and control flow without changing those diff --git a/chapter17b/docs/17-grammar.ja.md b/chapter17b/docs/17-grammar.ja.md index c90a9d5b..284a3de3 100644 --- a/chapter17b/docs/17-grammar.ja.md +++ b/chapter17b/docs/17-grammar.ja.md @@ -1,21 +1,123 @@ -# 第17b章の文法の追加 +# 第17b章の文法 [English](17-grammar.md) | 日本語 -この文書は [原文](17-grammar.md) の日本語訳です。 +```antlrv4 +grammar SimpleLanguage; -[第17a章の宣言構文](../../chapter17a/docs/17-grammar.ja.md)から始めます。この章は、型推論、代入の短縮形、条件式、`for` ループを追加します。アクセスと束縛の権限の意味は変わりません。 +// Name lookup, assignable operands, type compatibility, and access permissions +// are checked by the compiler in addition to these syntax rules. +program : statement* EOF ; -```text -inferredDeclaration = ("var" | "val") ("!" | "~")? identifier "=" assignment -assignment = expression (assignmentOperator assignment)? -assignmentOperator = "=" | "+=" | "-=" | "*=" | "/=" | "&=" | "|=" | "^=" - | "<<=" | ">>=" | ">>>=" -expression = bitwise ("?" assignment (":" assignment)?)? -prefixUpdate = ("++" | "--") identifier -postfixUpdate = lvalue ("++" | "--") -forStatement = "for" "(" declarationOrExpression? ";" assignment? ";" - assignment? ")" statement -``` +block : '{' statement* '}' ; + +statement + : block + | returnStatement + | ifStatement + | whileStatement + | forStatement + | breakStatement + | continueStatement + | structDeclaration + | declaration ';' + | assignment ';' + | ';' + ; + +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; +continueStatement : 'continue' ';' ; + +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement + ; + +structDeclaration : 'struct' IDENTIFIER block ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +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 ('?' assignment (':' assignment)?)? ; -宣言には、コンマで区切った複数の名前を含められます。`var` は再代入を許し、`val` は束縛を固定します。両方とも初期化子の参照の権限を維持します。後置の更新はフィールドと配列要素もサポートします。前置の更新は、今のところ局所名をサポートします。評価順序については [README](../README.ja.md) を参照してください。 +bitwiseExpression + : comparisonExpression (('&' | '|' | '^') comparisonExpression)* + ; + +comparisonExpression + : shiftExpression (('==' | '!=' | '<' | '<=' | '>' | '>=') shiftExpression)* + ; + +shiftExpression + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression + ; + +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +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 ; +``` diff --git a/chapter17b/docs/17-grammar.md b/chapter17b/docs/17-grammar.md index d42fcba1..88f6fc93 100644 --- a/chapter17b/docs/17-grammar.md +++ b/chapter17b/docs/17-grammar.md @@ -1,24 +1,123 @@ -# Chapter 17b grammar additions +# Grammar for Chapter 17b English | [日本語](17-grammar.ja.md) -Start with [Chapter 17a's declaration syntax](../../chapter17a/docs/17-grammar.md). -This chapter adds inference, assignment shortcuts, conditional expressions, and -`for` loops. Access and binding permissions keep the same meaning. - -```text -inferredDeclaration = ("var" | "val") ("!" | "~")? identifier "=" assignment -assignment = expression (assignmentOperator assignment)? -assignmentOperator = "=" | "+=" | "-=" | "*=" | "/=" | "&=" | "|=" | "^=" - | "<<=" | ">>=" | ">>>=" -expression = bitwise ("?" assignment (":" assignment)?)? -prefixUpdate = ("++" | "--") identifier -postfixUpdate = lvalue ("++" | "--") -forStatement = "for" "(" declarationOrExpression? ";" assignment? ";" - assignment? ")" statement -``` +```antlrv4 +grammar SimpleLanguage; + +// 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 + | forStatement + | breakStatement + | continueStatement + | structDeclaration + | declaration ';' + | assignment ';' + | ';' + ; + +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; +continueStatement : 'continue' ';' ; + +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement + ; + +structDeclaration : 'struct' IDENTIFIER block ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +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 + : '=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' + | '<<=' | '>>=' | '>>>=' + ; -Declarations can contain several comma-separated names. `var` permits -reassignment and `val` fixes the binding; both retain the initializer's reference -permissions. Postfix updates also support fields and array elements. Prefix -updates currently support local names. See the README for evaluation order. +expression : bitwiseExpression ('?' assignment (':' assignment)?)? ; + +bitwiseExpression + : comparisonExpression (('&' | '|' | '^') comparisonExpression)* + ; + +comparisonExpression + : shiftExpression (('==' | '!=' | '<' | '<=' | '>' | '>=') shiftExpression)* + ; + +shiftExpression + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression + ; + +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +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 ; +``` diff --git a/chapter18/README.ja.md b/chapter18/README.ja.md index 2a586c1d..6c0396b4 100644 --- a/chapter18/README.ja.md +++ b/chapter18/README.ja.md @@ -11,6 +11,8 @@ グラフ表現には、なじみのある部品を使います。関数の入口は Region、パラメータは Phi で、出口は1つの Return に合流します。呼び出しは関数境界を越えてこれらを結びます。関数の呼び出し元が1つだけなら、境界を取り除くことで通常のピープホール最適化が結合されたグラフを簡略化できます。これが最初の形のインライン化です。 +この章の[完全な言語文法](docs/18-grammar.ja.md)はこちらです。 + ## 目次 1. [関数を書く](#関数を書く) diff --git a/chapter18/README.md b/chapter18/README.md index 8039b4cb..9b13e277 100644 --- a/chapter18/README.md +++ b/chapter18/README.md @@ -16,6 +16,8 @@ connect these pieces across function boundaries. When a function has just one caller, removing that boundary lets ordinary peepholes simplify the combined graph: this is our first form of inlining. +Here is the [complete language grammar](docs/18-grammar.md) for this chapter. + ## Table of Contents 1. [Writing functions](#writing-functions) diff --git a/chapter18/docs/18-grammar.ja.md b/chapter18/docs/18-grammar.ja.md index dc9bf8c7..4c90e442 100644 --- a/chapter18/docs/18-grammar.ja.md +++ b/chapter18/docs/18-grammar.ja.md @@ -2,92 +2,84 @@ [English](18-grammar.md) | 日本語 -この文書は [原文](18-grammar.md) の日本語訳です。 - -> 訳注: 原文の見出しは「Chapter 16」となっています。本ファイルは第18章の文法文書です。 - -[第18章に戻る](../README.ja.md) - ```antlrv4 grammar SimpleLanguage; -program : block EOF ; - -block : statement+ ; +// 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 '}' + : block | returnStatement | ifStatement | whileStatement + | forStatement | breakStatement | continueStatement | structDeclaration - | expressionStatement + | declaration ';' + | assignment ';' + | ';' ; - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; continueStatement : 'continue' ';' ; - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement ; -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; +structDeclaration : 'struct' IDENTIFIER block ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +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 - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ + : functionType + | primitiveType typeSuffix* + | accessModifier? typeName typeSuffix* ; - -nestedArray : '?'? '[]'* ; - +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 ; +expression : bitwiseExpression ('?' assignment (':' assignment)?)? ; -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* +bitwiseExpression + : comparisonExpression (('&' | '|' | '^') comparisonExpression)* ; - + comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* + : shiftExpression (('==' | '!=' | '<' | '<=' | '>' | '>=') shiftExpression)* ; shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* ; - + additiveExpression : multiplicativeExpression (('+' | '-') multiplicativeExpression)* ; @@ -97,42 +89,53 @@ multiplicativeExpression ; unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression ; +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' | callSuffix ; + primaryExpression : INTEGER_LITERAL | FLOAT_LITERAL - | '(' expression ')' | 'true' | 'false' | 'null' - | newExpression | IDENTIFIER + | '(' assignment ')' + | newExpression + | functionExpression ; -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' +// A function is an expression bound with an ordinary declaration. +// Its last statement supplies the implicit result; return can exit earlier. +functionExpression : '{' parameterList? '->' statement* '}' ; +parameterList : (parameter ','?)+ ; +parameter : type bindingModifier? IDENTIFIER ; + +// Function types have unnamed, space-separated argument types. +// {int} is the zero-argument type; its function expression still uses ->. +functionType + : '{' type+ '->' type '}' '?'? + | '{' type '}' '?'? ; -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; +callSuffix : '(' argumentList? ')' ; +argumentList : assignment (',' assignment)* ','? ; +// Struct initialization uses a block; array allocation supplies a length. +newExpression : 'new' type ('[' assignment ']' | block)? ; -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; +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 ; ``` diff --git a/chapter18/docs/18-grammar.md b/chapter18/docs/18-grammar.md index 51539d0b..656bd63a 100644 --- a/chapter18/docs/18-grammar.md +++ b/chapter18/docs/18-grammar.md @@ -1,87 +1,85 @@ -# Grammar for Chapter 16 +# Grammar for Chapter 18 English | [日本語](18-grammar.ja.md) ```antlrv4 grammar SimpleLanguage; -program : block EOF ; - -block : statement+ ; +// 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 '}' + : block | returnStatement | ifStatement | whileStatement + | forStatement | breakStatement | continueStatement | structDeclaration - | expressionStatement + | declaration ';' + | assignment ';' + | ';' ; - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; continueStatement : 'continue' ';' ; - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement ; -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; +structDeclaration : 'struct' IDENTIFIER block ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +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 - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ + : functionType + | primitiveType typeSuffix* + | accessModifier? typeName typeSuffix* ; - -nestedArray : '?'? '[]'* ; - +accessModifier : '!' | '~' ; +typeSuffix : '?' | '[]' | '[~]' ; typeName : IDENTIFIER ; +primitiveType + : 'int' | 'i8' | 'i16' | 'i32' | 'i64' + | 'u1' | 'u8' | 'u16' | 'u32' | 'byte' | 'bool' + | 'flt' | 'f32' | 'f64' + ; -expression : bitWiseExpression ; +assignment : expression (assignmentOperator assignment)? ; +assignmentOperator + : '=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' + | '<<=' | '>>=' | '>>>=' + ; + +expression : bitwiseExpression ('?' assignment (':' assignment)?)? ; -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* +bitwiseExpression + : comparisonExpression (('&' | '|' | '^') comparisonExpression)* ; - + comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* + : shiftExpression (('==' | '!=' | '<' | '<=' | '>' | '>=') shiftExpression)* ; shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* ; - + additiveExpression : multiplicativeExpression (('+' | '-') multiplicativeExpression)* ; @@ -91,42 +89,53 @@ multiplicativeExpression ; unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression ; +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' | callSuffix ; + primaryExpression : INTEGER_LITERAL | FLOAT_LITERAL - | '(' expression ')' | 'true' | 'false' | 'null' - | newExpression | IDENTIFIER + | '(' assignment ')' + | newExpression + | functionExpression ; -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix +// A function is an expression bound with an ordinary declaration. +// Its last statement supplies the implicit result; return can exit earlier. +functionExpression : '{' parameterList? '->' statement* '}' ; +parameterList : (parameter ','?)+ ; +parameter : type bindingModifier? IDENTIFIER ; + +// Function types have unnamed, space-separated argument types. +// {int} is the zero-argument type; its function expression still uses ->. +functionType + : '{' type+ '->' type '}' '?'? + | '{' type '}' '?'? ; +callSuffix : '(' argumentList? ')' ; +argumentList : assignment (',' assignment)* ','? ; -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; +// Struct initialization uses a block; array allocation supplies a length. +newExpression : 'new' type ('[' assignment ']' | block)? ; -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; +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 ; ``` diff --git a/chapter19/README.ja.md b/chapter19/README.ja.md index f1de1079..5d33a73a 100644 --- a/chapter19/README.ja.md +++ b/chapter19/README.ja.md @@ -9,6 +9,8 @@ [linear](https://github.com/SeaOfNodes/Simple/tree/linear) ブランチ上の直線的な Git リビジョン履歴で [この章](https://github.com/SeaOfNodes/Simple/tree/linear-chapter19) を読み、前の章と [比較](https://github.com/SeaOfNodes/Simple/compare/linear-chapter18...linear-chapter19) することもできます。 +文法は[第18章](../chapter18/docs/18-grammar.ja.md)から変わりません。 + ## 命令選択、レジスタ割り当て、命令エンコーディングにまつわる全体的な問題と開発上の問題 命令選択の出力、すなわち理想化された Simple ノードからマシン固有のノードへの対応付けは、実行方法がないと大量にテストするのが困難です。命令選択、レジスタ割り当て、命令エンコーディングの3つすべてが完成するまでは、実行方法が得られません。そのため、エンコーディングの完成前後には、命令選択とレジスタ割り当てで多くのバグが見つかると予想しています。もちろん出力を手作業で調べて明らかな問題は修正しますが、実際にコードを動かせるようになるまで、多くのバグが残ります。 diff --git a/chapter19/README.md b/chapter19/README.md index e3d13b57..d8f7c9e1 100644 --- a/chapter19/README.md +++ b/chapter19/README.md @@ -8,6 +8,8 @@ English | [日本語](README.ja.md) You can also read [this chapter](https://github.com/SeaOfNodes/Simple/tree/linear-chapter19) in a linear Git revision history on the [linear](https://github.com/SeaOfNodes/Simple/tree/linear) branch and [compare](https://github.com/SeaOfNodes/Simple/compare/linear-chapter18...linear-chapter19) it to the previous chapter. +The grammar is unchanged from [Chapter 18](../chapter18/docs/18-grammar.md). + ## Meta-Issues / Code-Dev Issues of Instruction Selection, Register Allocation and Encodings The output of Instruction Selection - the mapping from idealized Simple Nodes diff --git a/chapter19/docs/18-grammar.ja.md b/chapter19/docs/18-grammar.ja.md deleted file mode 100644 index 0e944f06..00000000 --- a/chapter19/docs/18-grammar.ja.md +++ /dev/null @@ -1,138 +0,0 @@ -# 第19章の文法 - -[English](18-grammar.md) | 日本語 - -この文書は [原文](18-grammar.md) の日本語訳です。 - -> 訳注: 原文の見出しは「Grammar for Chapter 16」です。この日本語版では、配置されている第19章に合わせた見出しにしています。 - -[第19章に戻る](../README.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program : block EOF ; - -block : statement+ ; - - -statement - : '{' block '}' - | returnStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - | structDeclaration - | expressionStatement - ; - - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - -continueStatement : 'continue' ';' ; - - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression - ; - -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; - -type - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ - ; - -nestedArray : '?'? '[]'* ; - -typeName : IDENTIFIER ; - - -expression : bitWiseExpression ; - -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* - ; - -comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* - ; - -shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign - ; - -primaryExpression - : INTEGER_LITERAL - | FLOAT_LITERAL - | '(' expression ')' - | 'true' - | 'false' - | 'null' - | newExpression - | IDENTIFIER - ; - -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; - - -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; -``` diff --git a/chapter19/docs/18-grammar.md b/chapter19/docs/18-grammar.md deleted file mode 100644 index 51539d0b..00000000 --- a/chapter19/docs/18-grammar.md +++ /dev/null @@ -1,132 +0,0 @@ -# Grammar for Chapter 16 - -English | [日本語](18-grammar.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program : block EOF ; - -block : statement+ ; - - -statement - : '{' block '}' - | returnStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - | structDeclaration - | expressionStatement - ; - - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - -continueStatement : 'continue' ';' ; - - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression - ; - -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; - -type - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ - ; - -nestedArray : '?'? '[]'* ; - -typeName : IDENTIFIER ; - - -expression : bitWiseExpression ; - -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* - ; - -comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* - ; - -shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign - ; - -primaryExpression - : INTEGER_LITERAL - | FLOAT_LITERAL - | '(' expression ')' - | 'true' - | 'false' - | 'null' - | newExpression - | IDENTIFIER - ; - -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; - - -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; -``` diff --git a/chapter20/README.ja.md b/chapter20/README.ja.md index c33630e2..550844ca 100644 --- a/chapter20/README.ja.md +++ b/chapter20/README.ja.md @@ -7,6 +7,8 @@ [前の章: 第19章](../chapter19/README.ja.md) | [次の章: 第21章](../chapter21/README.ja.md) +文法は[第18章](../chapter18/docs/18-grammar.ja.md)から変わりません。 + ## 参考資料 これは Briggs-Chaitin-Click 型の割り当て器であり、過去25年間にわたり大きな成功を収めてきた HotSpot の C2 割り当て器に非常によく似ています。 diff --git a/chapter20/README.md b/chapter20/README.md index b4d4209f..e11dcff2 100644 --- a/chapter20/README.md +++ b/chapter20/README.md @@ -5,6 +5,8 @@ English | [日本語](README.ja.md) [Previous: Chapter 19](../chapter19/README.md) | [Next: Chapter 21](../chapter21/README.md) +The grammar is unchanged from [Chapter 18](../chapter18/docs/18-grammar.md). + ## Some Reading Material This is a Briggs-Chaitin-Click allocator, and is very similar to the one used diff --git a/chapter20/docs/18-grammar.ja.md b/chapter20/docs/18-grammar.ja.md deleted file mode 100644 index f0b12acb..00000000 --- a/chapter20/docs/18-grammar.ja.md +++ /dev/null @@ -1,138 +0,0 @@ -# 第20章の文法 - -[English](18-grammar.md) | 日本語 - -この文書は [原文](18-grammar.md) の日本語訳です。 - -> 訳注: 原文の見出しは「Grammar for Chapter 16」です。この日本語版では、配置されている第20章に合わせた見出しにしています。 - -[第20章に戻る](../README.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program : block EOF ; - -block : statement+ ; - - -statement - : '{' block '}' - | returnStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - | structDeclaration - | expressionStatement - ; - - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - -continueStatement : 'continue' ';' ; - - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression - ; - -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; - -type - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ - ; - -nestedArray : '?'? '[]'* ; - -typeName : IDENTIFIER ; - - -expression : bitWiseExpression ; - -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* - ; - -comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* - ; - -shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign - ; - -primaryExpression - : INTEGER_LITERAL - | FLOAT_LITERAL - | '(' expression ')' - | 'true' - | 'false' - | 'null' - | newExpression - | IDENTIFIER - ; - -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; - - -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; -``` diff --git a/chapter20/docs/18-grammar.md b/chapter20/docs/18-grammar.md deleted file mode 100644 index 51539d0b..00000000 --- a/chapter20/docs/18-grammar.md +++ /dev/null @@ -1,132 +0,0 @@ -# Grammar for Chapter 16 - -English | [日本語](18-grammar.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program : block EOF ; - -block : statement+ ; - - -statement - : '{' block '}' - | returnStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - | structDeclaration - | expressionStatement - ; - - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - -continueStatement : 'continue' ';' ; - - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression - ; - -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; - -type - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ - ; - -nestedArray : '?'? '[]'* ; - -typeName : IDENTIFIER ; - - -expression : bitWiseExpression ; - -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* - ; - -comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* - ; - -shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign - ; - -primaryExpression - : INTEGER_LITERAL - | FLOAT_LITERAL - | '(' expression ')' - | 'true' - | 'false' - | 'null' - | newExpression - | IDENTIFIER - ; - -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; - - -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; -``` diff --git a/chapter21/README.ja.md b/chapter21/README.ja.md index dbf11374..95ffbcf8 100644 --- a/chapter21/README.ja.md +++ b/chapter21/README.ja.md @@ -11,6 +11,8 @@ 直線的なリビジョン履歴で [この章](https://github.com/SeaOfNodes/Simple/tree/linear-chapter21) を読み、[第20章と比較](https://github.com/SeaOfNodes/Simple/compare/linear-chapter20...linear-chapter21) することもできます。 +この章の[完全な言語文法](docs/21-grammar.ja.md)はこちらです。 + ## マシンノードからバイトへ 命令選択は演算とレジスタ制約を選びます。レジスタ割り当ては物理レジスタとスタックスロットを割り当てます。その後、エンコーディングがオペコード、割り当て済みレジスタ、即値オペランドを命令バイトへ組み合わせます。この時点で命令は別のレジスタを選べません。周囲の命令が、すでに割り当て器の選択に依存しているためです。 diff --git a/chapter21/README.md b/chapter21/README.md index 57705fa7..b8d065ff 100644 --- a/chapter21/README.md +++ b/chapter21/README.md @@ -15,6 +15,8 @@ You can also read [this chapter](https://github.com/SeaOfNodes/Simple/tree/linea in the linear revision history and [compare it to Chapter 20](https://github.com/SeaOfNodes/Simple/compare/linear-chapter20...linear-chapter21). +Here is the [complete language grammar](docs/21-grammar.md) for this chapter. + ## From machine nodes to bytes Instruction selection chooses the operation and register constraints. Register diff --git a/chapter21/docs/18-grammar.ja.md b/chapter21/docs/18-grammar.ja.md deleted file mode 100644 index dbd7f0fd..00000000 --- a/chapter21/docs/18-grammar.ja.md +++ /dev/null @@ -1,138 +0,0 @@ -# 第21章の文法 - -[English](18-grammar.md) | 日本語 - -この文書は [原文](18-grammar.md) の日本語訳です。 - -> 訳注: 原文の見出しは「Grammar for Chapter 16」です。この日本語版では、配置されている第21章に合わせた見出しにしています。 - -[第21章に戻る](../README.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program : block EOF ; - -block : statement+ ; - - -statement - : '{' block '}' - | returnStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - | structDeclaration - | expressionStatement - ; - - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - -continueStatement : 'continue' ';' ; - - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression - ; - -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; - -type - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ - ; - -nestedArray : '?'? '[]'* ; - -typeName : IDENTIFIER ; - - -expression : bitWiseExpression ; - -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* - ; - -comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* - ; - -shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign - ; - -primaryExpression - : INTEGER_LITERAL - | FLOAT_LITERAL - | '(' expression ')' - | 'true' - | 'false' - | 'null' - | newExpression - | IDENTIFIER - ; - -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; - - -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; -``` diff --git a/chapter21/docs/18-grammar.md b/chapter21/docs/18-grammar.md deleted file mode 100644 index 51539d0b..00000000 --- a/chapter21/docs/18-grammar.md +++ /dev/null @@ -1,132 +0,0 @@ -# Grammar for Chapter 16 - -English | [日本語](18-grammar.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program : block EOF ; - -block : statement+ ; - - -statement - : '{' block '}' - | returnStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - | structDeclaration - | expressionStatement - ; - - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - -continueStatement : 'continue' ';' ; - - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression - ; - -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; - -type - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ - ; - -nestedArray : '?'? '[]'* ; - -typeName : IDENTIFIER ; - - -expression : bitWiseExpression ; - -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* - ; - -comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* - ; - -shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign - ; - -primaryExpression - : INTEGER_LITERAL - | FLOAT_LITERAL - | '(' expression ')' - | 'true' - | 'false' - | 'null' - | newExpression - | IDENTIFIER - ; - -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; - - -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; -``` diff --git a/chapter21/docs/21-grammar.ja.md b/chapter21/docs/21-grammar.ja.md new file mode 100644 index 00000000..8cb8f27b --- /dev/null +++ b/chapter21/docs/21-grammar.ja.md @@ -0,0 +1,141 @@ +# 第21章の文法 + +[English](21-grammar.md) | 日本語 + +```antlrv4 +grammar SimpleLanguage; + +// 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 + | forStatement + | breakStatement + | continueStatement + | structDeclaration + | declaration ';' + | assignment ';' + | ';' + ; + +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; +continueStatement : 'continue' ';' ; + +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement + ; + +structDeclaration : 'struct' IDENTIFIER block ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +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 + : functionType + | 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 ('?' assignment (':' assignment)?)? ; + +bitwiseExpression + : comparisonExpression (('&' | '|' | '^') comparisonExpression)* + ; + +comparisonExpression + : shiftExpression (('==' | '!=' | '<' | '<=' | '>' | '>=') shiftExpression)* + ; + +shiftExpression + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression + ; + +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' | callSuffix ; + +primaryExpression + : INTEGER_LITERAL + | FLOAT_LITERAL + | 'true' + | 'false' + | 'null' + | IDENTIFIER + | '(' assignment ')' + | newExpression + | functionExpression + ; + +// A function is an expression bound with an ordinary declaration. +// Its last statement supplies the implicit result; return can exit earlier. +functionExpression : '{' parameterList? '->' statement* '}' ; +parameterList : (parameter ','?)+ ; +parameter : type bindingModifier? IDENTIFIER ; + +// Function types have unnamed, space-separated argument types. +// {int} is the zero-argument type; its function expression still uses ->. +functionType + : '{' type+ '->' type '}' '?'? + | '{' type '}' '?'? + ; + +callSuffix : '(' argumentList? ')' ; +argumentList : assignment (',' assignment)* ','? ; + +// 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 ; +``` diff --git a/chapter21/docs/21-grammar.md b/chapter21/docs/21-grammar.md new file mode 100644 index 00000000..111957a1 --- /dev/null +++ b/chapter21/docs/21-grammar.md @@ -0,0 +1,141 @@ +# Grammar for Chapter 21 + +English | [日本語](21-grammar.ja.md) + +```antlrv4 +grammar SimpleLanguage; + +// 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 + | forStatement + | breakStatement + | continueStatement + | structDeclaration + | declaration ';' + | assignment ';' + | ';' + ; + +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; +continueStatement : 'continue' ';' ; + +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement + ; + +structDeclaration : 'struct' IDENTIFIER block ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +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 + : functionType + | 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 ('?' assignment (':' assignment)?)? ; + +bitwiseExpression + : comparisonExpression (('&' | '|' | '^') comparisonExpression)* + ; + +comparisonExpression + : shiftExpression (('==' | '!=' | '<' | '<=' | '>' | '>=') shiftExpression)* + ; + +shiftExpression + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression + ; + +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' | callSuffix ; + +primaryExpression + : INTEGER_LITERAL + | FLOAT_LITERAL + | 'true' + | 'false' + | 'null' + | IDENTIFIER + | '(' assignment ')' + | newExpression + | functionExpression + ; + +// A function is an expression bound with an ordinary declaration. +// Its last statement supplies the implicit result; return can exit earlier. +functionExpression : '{' parameterList? '->' statement* '}' ; +parameterList : (parameter ','?)+ ; +parameter : type bindingModifier? IDENTIFIER ; + +// Function types have unnamed, space-separated argument types. +// {int} is the zero-argument type; its function expression still uses ->. +functionType + : '{' type+ '->' type '}' '?'? + | '{' type '}' '?'? + ; + +callSuffix : '(' argumentList? ')' ; +argumentList : assignment (',' assignment)* ','? ; + +// 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 ; +``` diff --git a/chapter21/docs/encoding-reference.ja.md b/chapter21/docs/encoding-reference.ja.md index 8ccdc728..d04fc469 100644 --- a/chapter21/docs/encoding-reference.ja.md +++ b/chapter21/docs/encoding-reference.ja.md @@ -40,9 +40,6 @@ - [条件フラグ](#条件フラグ-1) 7. [再配置](#再配置) -> 訳注:原文の目次には本文に対応する節がない項目や、別の節を指すリンクがあります。 -> 日本語版の目次は本文の見出しに合わせています。本文の技術的な説明とコード例は原文に沿っています。 - この章では命令エンコーディングを追加します。命令エンコーディングとは、 プロセッサの命令セットアーキテクチャ(ISA)が定義する特定のバイナリ形式で機械命令を表すことです。 @@ -429,7 +426,7 @@ Arm は次の間接参照形式をサポートします。 [ストア](https://docsmirror.github.io/A64/2023-06/strb_imm.html):`str dst, [base + #offset]` -オフセットが定数でなければ……(訳注:原文のこの文は途中で途切れています)。 +オフセットがレジスタの場合は、即値オフセット形式ではなくレジスタオフセット形式を使います。 #### 分岐 @@ -465,7 +462,7 @@ Arm は次の間接参照形式をサポートします。 それ以外の場合は無視されます。 Simple では、REX プレフィックスをビットストリームの先頭に追加するだけです -(SEE の浮動小数点命令を除く。訳注:「SEE」は原文の表記です)。配置は次のとおりです。 +(SSE の浮動小数点命令を除く)。配置は次のとおりです。 | **フィールド** | **長さ** | **説明** | |:---------:|:----------:|---------------------------------------------------------------------------------| @@ -528,12 +525,14 @@ public enum MOD { ##### reg -reg フィールドは3ビットのフィールドで、15種類のレジスタをエンコードできます。 -(訳注:ビット数とレジスタ数について、ここでは原文の記述を保持しています。) +reg フィールドは3ビットで、8個のレジスタから1個を選びます。レジスタを指定する場合、 +REX.R によって4ビットに拡張され、16個のレジスタから1個を選べます。 ##### r/m -rm フィールドは直接または間接のレジスタオペランドを指定し、15種類のレジスタをエンコードできます。 +r/m フィールドは3ビットです。レジスタ直接指定モードでは、REX.B によって拡張され、 +16個のレジスタから1個を選べます。メモリ指定モードでは、アドレッシング形式または +ベースレジスタを選びます。一部の値は SIB バイトや RIP 相対アドレッシングを指定します。 ```java public static int modrm(MOD mod, int reg, int m_r) { @@ -625,7 +624,7 @@ X86-64 は複数の間接アドレッシングモードをサポートしますã #### 浮動小数点数 浮動小数点演算には、XMM レジスタまたはメモリにあるスカラーの単精度浮動小数点値を操作する -`SEE SIMD` 命令を使います(訳注:命令名の表記は原文どおりです)。 +`SSE SIMD` 命令を使います。 *注*:ALU 演算で XMM レジスタを使う場合、エンコーディングは非常に似ていますが、 レジスタ番号から `XMM_OFFSET` を引く必要がある点が異なります。 レジスタを連続した番号で数えているため、XMM レジスタに対応する GPR のペアを得るには、 diff --git a/chapter21/docs/encoding-reference.md b/chapter21/docs/encoding-reference.md index 5853b380..187a965e 100644 --- a/chapter21/docs/encoding-reference.md +++ b/chapter21/docs/encoding-reference.md @@ -13,37 +13,39 @@ See the [chapter overview](../README.md) for the compiler pipeline and experimen 4. [RISCV](#riscv) - [Instruction formats](#instruction-formats) - [Float](#float) - - [Indirect](#indirectmemop-2) + - [Indirect](#indirectmemop) - [Branch](#branch) - - [Function Constant](#function-constant) - - [LUI](#lui) - - [Large constants](#large-constants) + - [Immediates](#immediates) + - [Float Constants](#float-constants) + - [Int Constants](#int-constants) + - [Function Constants](#function-constants) 5. [ARM](#arm) - [Reg-form](#reg-form) - [Shift immediates](#shift-immediates) - - [asri](#asrimmediate) - - [LSL(immediate)](#lslimmediate) - - [LSR(immediate)](#lsrimmediate) - - [Logical Immediates](#logical-immediates) - - [Float](#float) + - [Float](#float-1) + - [Large constants](#large-constants) - [Function constant](#function-constant) - - [Immediate](#immediate) - [Conditional flags](#conditional-flags) - [Indirect](#indirectmemop-1) - - [Branching](#branching-) - - [Rip relative](#rip-relative) - - [Compare](#compare) -6. [AMD64](#amd64) + - [Branching](#branching) +6. [AMD64 X86-64](#amd64-x86-64) - [REX PREFIX](#rex-prefix) - - [Large constants](#large-constants) + - [Opcode](#opcode) - [MODR/M](#modrm) - [SIB](#sib) - [Displacement](#displacement) - [Immediate](#immediate) - - [Float](#float) - - [Indirect](#indirectmemop) - - [Conditional flags](#conditional-flags) + - [Indirect](#indirectmemop-2) + - [Float](#float-2) + - [Large constants](#large-constants-1) + - [Function constant](#function-constant-1) + - [Conditional flags](#conditional-flags-1) +7. [Relocation](#relocation) + - [Local Relocation](#local-relocation) + - [Global Relocation](#global-relocation) + - [Getting Relocations to All Agree](#getting-relocations-to-all-agree) + - [Large Constants](#large-constants-2) This chapter will add instruction encodings. Instruction encoding refers to @@ -433,7 +435,8 @@ Arm supports the following indirect forms: [Store](https://docsmirror.github.io/A64/2023-06/strb_imm.html): `str dst, [base + #offset]` -If the offset is not a con +For a register offset, use the register-offset form rather than the immediate-offset form. + #### Branching `B.cond` - branch [conditionally](https://developer.arm.com/documentation/dui0802/b/A32-and-T32-Instructions/Condition-codes?lang=en) to a label at a PC-relative offset. @@ -468,7 +471,7 @@ A *REX* prefix must not be encoded when: the `SSE` prefix. In all other cases, it is ignored. In Simple, the REX prefix is just appended to the beginning of the bit stream -(except SEE float instructions). The layout is the following: +(except SSE float instructions). The layout is the following: | **Field** | **Length** | **Description** | |:---------:|:----------:|---------------------------------------------------------------------------------| @@ -532,11 +535,14 @@ public enum MOD { ``` ##### reg -The reg field is a 3 bit field capable of encoding 15 different registers. +The reg field has 3 bits, selecting one of 8 registers. When it names a +register, REX.R extends it to 4 bits, selecting one of 16 registers. ##### r/m -The rm field specifies a direct or indirect register operand and is capable of encoding 15 different registers. +The r/m field has 3 bits. In register-direct mode, REX.B extends it to +select one of 16 registers. In memory modes, r/m selects the addressing form +or base register; some values select a SIB byte or RIP-relative addressing. ```java @@ -624,7 +630,7 @@ X86-64 support multiple indirect addressing modes: #### Float -For float operations we use `SEE SIMD` instructions that operate on scalar single precision floating0-point values located in the +For float operations we use `SSE SIMD` instructions that operate on scalar single precision floating-point values located in the XMM registers or memory. *Note*: When using XMM registers for ALU operations, the encoding is really similar with the exception that we need to subtract the `XMM_OFFSET` from the register number. diff --git a/chapter22/README.ja.md b/chapter22/README.ja.md index ad2e6315..91c74dcf 100644 --- a/chapter22/README.ja.md +++ b/chapter22/README.ja.md @@ -26,6 +26,8 @@ sys.io.p("Hello, World!"); [linear](https://github.com/SeaOfNodes/Simple/tree/linear) ブランチ上の直線的な Git リビジョン履歴で [この章](https://github.com/SeaOfNodes/Simple/tree/linear-chapter22) を読み、前の章と [比較](https://github.com/SeaOfNodes/Simple/compare/linear-chapter21...linear-chapter22) することもできます。 +この章の[完全な言語文法](docs/22-grammar.ja.md)はこちらです。 + ## 入れ子の型と静的フィールド Simple は入れ子の型をサポートします。これは名前空間だけの変更で、新しい意味規則はなく、型定義を入れ子にできるだけです。 diff --git a/chapter22/README.md b/chapter22/README.md index 0f19f293..34fb4d20 100644 --- a/chapter22/README.md +++ b/chapter22/README.md @@ -43,6 +43,8 @@ BrainFuck's invariant program length folds out of its main loop. You can also read [this chapter](https://github.com/SeaOfNodes/Simple/tree/linear-chapter22) in a linear Git revision history on the [linear](https://github.com/SeaOfNodes/Simple/tree/linear) branch and [compare](https://github.com/SeaOfNodes/Simple/compare/linear-chapter21...linear-chapter22) it to the previous chapter. +Here is the [complete language grammar](docs/22-grammar.md) for this chapter. + ## Nested Types and Static Fields Simple now supports nested types - this is a name-space only change, so no new diff --git a/chapter22/docs/18-grammar.ja.md b/chapter22/docs/18-grammar.ja.md deleted file mode 100644 index 29263fc2..00000000 --- a/chapter22/docs/18-grammar.ja.md +++ /dev/null @@ -1,138 +0,0 @@ -# 第22章の文法 - -[English](18-grammar.md) | 日本語 - -この文書は [原文](18-grammar.md) の日本語訳です。 - -> 訳注: 原文の見出しは「Grammar for Chapter 16」です。この日本語版では、配置されている第22章に合わせた見出しにしています。 - -[第22章に戻る](../README.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program : block EOF ; - -block : statement+ ; - - -statement - : '{' block '}' - | returnStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - | structDeclaration - | expressionStatement - ; - - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - -continueStatement : 'continue' ';' ; - - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression - ; - -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; - -type - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ - ; - -nestedArray : '?'? '[]'* ; - -typeName : IDENTIFIER ; - - -expression : bitWiseExpression ; - -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* - ; - -comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* - ; - -shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign - ; - -primaryExpression - : INTEGER_LITERAL - | FLOAT_LITERAL - | '(' expression ')' - | 'true' - | 'false' - | 'null' - | newExpression - | IDENTIFIER - ; - -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; - - -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; -``` diff --git a/chapter22/docs/18-grammar.md b/chapter22/docs/18-grammar.md deleted file mode 100644 index 51539d0b..00000000 --- a/chapter22/docs/18-grammar.md +++ /dev/null @@ -1,132 +0,0 @@ -# Grammar for Chapter 16 - -English | [日本語](18-grammar.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program : block EOF ; - -block : statement+ ; - - -statement - : '{' block '}' - | returnStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - | structDeclaration - | expressionStatement - ; - - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - -continueStatement : 'continue' ';' ; - - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression - ; - -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; - -type - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ - ; - -nestedArray : '?'? '[]'* ; - -typeName : IDENTIFIER ; - - -expression : bitWiseExpression ; - -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* - ; - -comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* - ; - -shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign - ; - -primaryExpression - : INTEGER_LITERAL - | FLOAT_LITERAL - | '(' expression ')' - | 'true' - | 'false' - | 'null' - | newExpression - | IDENTIFIER - ; - -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; - - -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; -``` diff --git a/chapter22/docs/22-grammar.ja.md b/chapter22/docs/22-grammar.ja.md new file mode 100644 index 00000000..1813539f --- /dev/null +++ b/chapter22/docs/22-grammar.ja.md @@ -0,0 +1,149 @@ +# 第22章の文法 + +[English](22-grammar.md) | 日本語 + +```antlrv4 +grammar SimpleLanguage; + +// 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 + | forStatement + | breakStatement + | continueStatement + | structDeclaration + | declaration ';' + | assignment ';' + | ';' + ; + +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; +continueStatement : 'continue' ';' ; + +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement + ; + +structDeclaration : 'struct' IDENTIFIER block ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +binding : bindingModifier? IDENTIFIER ('=' initializer)? ; +bindingModifier : '!' | '~' ; +// A declaration initializer consisting of the string "C" declares an extern +// symbol. Ordinary strings elsewhere are array-valued expressions. +initializer : assignment ; + +// The prefix qualifies the named struct reference, not an enclosing array. +// Each [] / [~] suffix qualifies its own array layer. ? requires a reference. +type + : functionType + | primitiveType typeSuffix* + | accessModifier? typeName typeSuffix* + ; +accessModifier : '!' | '~' ; +typeSuffix : '?' | '[]' | '[~]' ; +typeName : IDENTIFIER ('.' IDENTIFIER)* ; + +primitiveType + : 'int' | 'i8' | 'i16' | 'i32' | 'i64' + | 'u1' | 'u8' | 'u16' | 'u32' | 'byte' | 'bool' + | 'flt' | 'f32' | 'f64' + ; + +assignment : expression (assignmentOperator assignment)? ; +assignmentOperator + : '=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' + | '<<=' | '>>=' | '>>>=' + ; + +expression : bitwiseExpression ('?' assignment (':' assignment)?)? ; + +bitwiseExpression + : comparisonExpression (('&' | '|' | '^') comparisonExpression)* + ; + +comparisonExpression + : shiftExpression (('==' | '!=' | '<' | '<=' | '>' | '>=') shiftExpression)* + ; + +shiftExpression + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression + ; + +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' | callSuffix ; + +primaryExpression + : INTEGER_LITERAL + | FLOAT_LITERAL + | STRING_LITERAL + | CHARACTER_LITERAL + | 'true' + | 'false' + | 'null' + | IDENTIFIER + | '(' assignment ')' + | newExpression + | functionExpression + ; + +// A function is an expression bound with an ordinary declaration. +// Its last statement supplies the implicit result; return can exit earlier. +functionExpression : '{' parameterList? '->' statement* '}' ; +parameterList : (parameter ','?)+ ; +parameter : type bindingModifier? IDENTIFIER ; + +// Function types have unnamed, space-separated argument types. +// {int} is the zero-argument type; its function expression still uses ->. +functionType + : '{' type+ '->' type '}' '?'? + | '{' type '}' '?'? + ; + +callSuffix : '(' argumentList? ')' ; +argumentList : assignment (',' assignment)* ','? ; + +// 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+) ; +STRING_LITERAL : '"' (ESCAPE | ~["\\])* '"' ; +CHARACTER_LITERAL : '\'' (ESCAPE | ~['\\]) '\'' ; +fragment ESCAPE : '\\' [0ntr\\'"] ; +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 ; +BLOCK_COMMENT : '/*' .*? '*/' -> skip ; +``` diff --git a/chapter22/docs/22-grammar.md b/chapter22/docs/22-grammar.md new file mode 100644 index 00000000..dfee11bd --- /dev/null +++ b/chapter22/docs/22-grammar.md @@ -0,0 +1,149 @@ +# Grammar for Chapter 22 + +English | [日本語](22-grammar.ja.md) + +```antlrv4 +grammar SimpleLanguage; + +// 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 + | forStatement + | breakStatement + | continueStatement + | structDeclaration + | declaration ';' + | assignment ';' + | ';' + ; + +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; +continueStatement : 'continue' ';' ; + +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement + ; + +structDeclaration : 'struct' IDENTIFIER block ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +binding : bindingModifier? IDENTIFIER ('=' initializer)? ; +bindingModifier : '!' | '~' ; +// A declaration initializer consisting of the string "C" declares an extern +// symbol. Ordinary strings elsewhere are array-valued expressions. +initializer : assignment ; + +// The prefix qualifies the named struct reference, not an enclosing array. +// Each [] / [~] suffix qualifies its own array layer. ? requires a reference. +type + : functionType + | primitiveType typeSuffix* + | accessModifier? typeName typeSuffix* + ; +accessModifier : '!' | '~' ; +typeSuffix : '?' | '[]' | '[~]' ; +typeName : IDENTIFIER ('.' IDENTIFIER)* ; + +primitiveType + : 'int' | 'i8' | 'i16' | 'i32' | 'i64' + | 'u1' | 'u8' | 'u16' | 'u32' | 'byte' | 'bool' + | 'flt' | 'f32' | 'f64' + ; + +assignment : expression (assignmentOperator assignment)? ; +assignmentOperator + : '=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' + | '<<=' | '>>=' | '>>>=' + ; + +expression : bitwiseExpression ('?' assignment (':' assignment)?)? ; + +bitwiseExpression + : comparisonExpression (('&' | '|' | '^') comparisonExpression)* + ; + +comparisonExpression + : shiftExpression (('==' | '!=' | '<' | '<=' | '>' | '>=') shiftExpression)* + ; + +shiftExpression + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression + ; + +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' | callSuffix ; + +primaryExpression + : INTEGER_LITERAL + | FLOAT_LITERAL + | STRING_LITERAL + | CHARACTER_LITERAL + | 'true' + | 'false' + | 'null' + | IDENTIFIER + | '(' assignment ')' + | newExpression + | functionExpression + ; + +// A function is an expression bound with an ordinary declaration. +// Its last statement supplies the implicit result; return can exit earlier. +functionExpression : '{' parameterList? '->' statement* '}' ; +parameterList : (parameter ','?)+ ; +parameter : type bindingModifier? IDENTIFIER ; + +// Function types have unnamed, space-separated argument types. +// {int} is the zero-argument type; its function expression still uses ->. +functionType + : '{' type+ '->' type '}' '?'? + | '{' type '}' '?'? + ; + +callSuffix : '(' argumentList? ')' ; +argumentList : assignment (',' assignment)* ','? ; + +// 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+) ; +STRING_LITERAL : '"' (ESCAPE | ~["\\])* '"' ; +CHARACTER_LITERAL : '\'' (ESCAPE | ~['\\]) '\'' ; +fragment ESCAPE : '\\' [0ntr\\'"] ; +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 ; +BLOCK_COMMENT : '/*' .*? '*/' -> skip ; +``` diff --git a/chapter23/README.ja.md b/chapter23/README.ja.md index b83b0914..40fa09db 100644 --- a/chapter23/README.ja.md +++ b/chapter23/README.ja.md @@ -38,6 +38,8 @@ struct String { [linear](https://github.com/SeaOfNodes/Simple/tree/linear) ブランチ上の直線的な Git リビジョン履歴で [この章](https://github.com/SeaOfNodes/Simple/tree/linear-chapter23) を読み、前の章と [比較](https://github.com/SeaOfNodes/Simple/compare/linear-chapter22...linear-chapter23) することもできます。 +この章の[完全な言語文法](docs/23-grammar.ja.md)はこちらです。 + ## なぜ循環する型なのか 循環する型は代替手法より精密な解析を可能にし、より多くのプログラムや、よりよい最適化、あるいは両方を許します。Simple では型を型検査にも使うので、より精密な型により、より多くの正しいプログラムを許容します。 diff --git a/chapter23/README.md b/chapter23/README.md index 1f073c6b..fd330cdd 100644 --- a/chapter23/README.md +++ b/chapter23/README.md @@ -57,6 +57,8 @@ and deep-final information while querying the contents of a precise alias. You can also read [this chapter](https://github.com/SeaOfNodes/Simple/tree/linear-chapter23) in a linear Git revision history on the [linear](https://github.com/SeaOfNodes/Simple/tree/linear) branch and [compare](https://github.com/SeaOfNodes/Simple/compare/linear-chapter22...linear-chapter23) it to the previous chapter. +Here is the [complete language grammar](docs/23-grammar.md) for this chapter. + ## Why Cyclic Types? Cyclic types give us a sharper analysis than the alternative, and thus admit diff --git a/chapter23/docs/18-grammar.ja.md b/chapter23/docs/18-grammar.ja.md deleted file mode 100644 index c2b37f67..00000000 --- a/chapter23/docs/18-grammar.ja.md +++ /dev/null @@ -1,138 +0,0 @@ -# 第23章の文法 - -[English](18-grammar.md) | 日本語 - -この文書は [原文](18-grammar.md) の日本語訳です。 - -> 訳注: 原文の見出しは「Grammar for Chapter 16」です。この日本語版では、配置されている第23章に合わせた見出しにしています。 - -[第23章に戻る](../README.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program : block EOF ; - -block : statement+ ; - - -statement - : '{' block '}' - | returnStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - | structDeclaration - | expressionStatement - ; - - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - -continueStatement : 'continue' ';' ; - - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression - ; - -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; - -type - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ - ; - -nestedArray : '?'? '[]'* ; - -typeName : IDENTIFIER ; - - -expression : bitWiseExpression ; - -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* - ; - -comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* - ; - -shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign - ; - -primaryExpression - : INTEGER_LITERAL - | FLOAT_LITERAL - | '(' expression ')' - | 'true' - | 'false' - | 'null' - | newExpression - | IDENTIFIER - ; - -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; - - -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; -``` diff --git a/chapter23/docs/18-grammar.md b/chapter23/docs/18-grammar.md deleted file mode 100644 index 51539d0b..00000000 --- a/chapter23/docs/18-grammar.md +++ /dev/null @@ -1,132 +0,0 @@ -# Grammar for Chapter 16 - -English | [日本語](18-grammar.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program : block EOF ; - -block : statement+ ; - - -statement - : '{' block '}' - | returnStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - | structDeclaration - | expressionStatement - ; - - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - -continueStatement : 'continue' ';' ; - - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression - ; - -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; - -type - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ - ; - -nestedArray : '?'? '[]'* ; - -typeName : IDENTIFIER ; - - -expression : bitWiseExpression ; - -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* - ; - -comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* - ; - -shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign - ; - -primaryExpression - : INTEGER_LITERAL - | FLOAT_LITERAL - | '(' expression ')' - | 'true' - | 'false' - | 'null' - | newExpression - | IDENTIFIER - ; - -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; - - -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; -``` diff --git a/chapter23/docs/23-grammar.ja.md b/chapter23/docs/23-grammar.ja.md new file mode 100644 index 00000000..ff7b5d56 --- /dev/null +++ b/chapter23/docs/23-grammar.ja.md @@ -0,0 +1,163 @@ +# 第23章の文法 + +[English](23-grammar.md) | 日本語 + +```antlrv4 +grammar SimpleLanguage; + +// 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 + | forStatement + | breakStatement + | continueStatement + | structDeclaration + | declaration ';' + | assignment ';' + | ';' + ; + +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; +continueStatement : 'continue' ';' ; + +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement + ; + +structDeclaration : 'struct' IDENTIFIER '{' structMember* '}' ';' ; +structMember : methodDeclaration | statement ; + +// A method is an ordinary fixed function binding in a struct definition. +// The compiler supplies self; it is not written in parameterList. +// The general declaration rule also admits explicitly typed function fields. +methodDeclaration : 'val' bindingModifier? IDENTIFIER '=' functionExpression ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +binding : bindingModifier? IDENTIFIER ('=' initializer)? ; +bindingModifier : '!' | '~' ; +// A declaration initializer consisting of the string "C" declares an extern +// symbol. Ordinary strings elsewhere are array-valued expressions. +initializer : assignment ; + +// The prefix qualifies the named struct reference, not an enclosing array. +// Each [] / [~] suffix qualifies its own array layer. ? requires a reference. +type + : functionType + | primitiveType typeSuffix* + | accessModifier? typeName typeSuffix* + ; +accessModifier : '!' | '~' ; +typeSuffix : '?' | '[]' | '[~]' ; +typeName : IDENTIFIER ('.' IDENTIFIER)* ; + +primitiveType + : 'int' | 'i8' | 'i16' | 'i32' | 'i64' + | 'u1' | 'u8' | 'u16' | 'u32' | 'byte' | 'bool' + | 'flt' | 'f32' | 'f64' + ; + +assignment : expression (assignmentOperator assignment)? ; +assignmentOperator + : '=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' + | '<<=' | '>>=' | '>>>=' + ; + +expression : logicalExpression ('?' assignment (':' assignment)?)? ; + +// Both logical operators short-circuit and associate to the right here. +logicalExpression : bitwiseExpression (('&&' | '||') logicalExpression)? ; + +bitwiseExpression + : comparisonExpression (('&' | '|' | '^') comparisonExpression)* + ; + +comparisonExpression + : shiftExpression (('==' | '!=' | '<' | '<=' | '>' | '>=') shiftExpression)* + ; + +shiftExpression + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression + ; + +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' | callSuffix | methodCallSuffix ; + +// For a fixed function-valued member, object.name(args) passes object as self. +// This overlaps ordinary field selection followed by a call syntactically; +// name/type resolution determines which call form applies. +methodCallSuffix : '.' IDENTIFIER callSuffix ; + +primaryExpression + : INTEGER_LITERAL + | FLOAT_LITERAL + | STRING_LITERAL + | CHARACTER_LITERAL + | 'true' + | 'false' + | 'null' + | IDENTIFIER + | '(' assignment ')' + | newExpression + | functionExpression + ; + +// A function is an expression bound with an ordinary declaration. +// Its last statement supplies the implicit result; return can exit earlier. +functionExpression : '{' parameterList? '->' statement* '}' ; +parameterList : (parameter ','?)+ ; +parameter : type bindingModifier? IDENTIFIER ; + +// Function types have unnamed, space-separated argument types. +// {int} is the zero-argument type; its function expression still uses ->. +functionType + : '{' type+ '->' type '}' '?'? + | '{' type '}' '?'? + ; + +callSuffix : '(' argumentList? ')' ; +argumentList : assignment (',' assignment)* ','? ; + +// 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+) ; +STRING_LITERAL : '"' (ESCAPE | ~["\\])* '"' ; +CHARACTER_LITERAL : '\'' (ESCAPE | ~['\\]) '\'' ; +fragment ESCAPE : '\\' [0ntr\\'"] ; +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 ; +BLOCK_COMMENT : '/*' .*? '*/' -> skip ; +``` diff --git a/chapter23/docs/23-grammar.md b/chapter23/docs/23-grammar.md new file mode 100644 index 00000000..1d52d5a6 --- /dev/null +++ b/chapter23/docs/23-grammar.md @@ -0,0 +1,163 @@ +# Grammar for Chapter 23 + +English | [日本語](23-grammar.ja.md) + +```antlrv4 +grammar SimpleLanguage; + +// 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 + | forStatement + | breakStatement + | continueStatement + | structDeclaration + | declaration ';' + | assignment ';' + | ';' + ; + +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; +continueStatement : 'continue' ';' ; + +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement + ; + +structDeclaration : 'struct' IDENTIFIER '{' structMember* '}' ';' ; +structMember : methodDeclaration | statement ; + +// A method is an ordinary fixed function binding in a struct definition. +// The compiler supplies self; it is not written in parameterList. +// The general declaration rule also admits explicitly typed function fields. +methodDeclaration : 'val' bindingModifier? IDENTIFIER '=' functionExpression ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +binding : bindingModifier? IDENTIFIER ('=' initializer)? ; +bindingModifier : '!' | '~' ; +// A declaration initializer consisting of the string "C" declares an extern +// symbol. Ordinary strings elsewhere are array-valued expressions. +initializer : assignment ; + +// The prefix qualifies the named struct reference, not an enclosing array. +// Each [] / [~] suffix qualifies its own array layer. ? requires a reference. +type + : functionType + | primitiveType typeSuffix* + | accessModifier? typeName typeSuffix* + ; +accessModifier : '!' | '~' ; +typeSuffix : '?' | '[]' | '[~]' ; +typeName : IDENTIFIER ('.' IDENTIFIER)* ; + +primitiveType + : 'int' | 'i8' | 'i16' | 'i32' | 'i64' + | 'u1' | 'u8' | 'u16' | 'u32' | 'byte' | 'bool' + | 'flt' | 'f32' | 'f64' + ; + +assignment : expression (assignmentOperator assignment)? ; +assignmentOperator + : '=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' + | '<<=' | '>>=' | '>>>=' + ; + +expression : logicalExpression ('?' assignment (':' assignment)?)? ; + +// Both logical operators short-circuit and associate to the right here. +logicalExpression : bitwiseExpression (('&&' | '||') logicalExpression)? ; + +bitwiseExpression + : comparisonExpression (('&' | '|' | '^') comparisonExpression)* + ; + +comparisonExpression + : shiftExpression (('==' | '!=' | '<' | '<=' | '>' | '>=') shiftExpression)* + ; + +shiftExpression + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression + ; + +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' | callSuffix | methodCallSuffix ; + +// For a fixed function-valued member, object.name(args) passes object as self. +// This overlaps ordinary field selection followed by a call syntactically; +// name/type resolution determines which call form applies. +methodCallSuffix : '.' IDENTIFIER callSuffix ; + +primaryExpression + : INTEGER_LITERAL + | FLOAT_LITERAL + | STRING_LITERAL + | CHARACTER_LITERAL + | 'true' + | 'false' + | 'null' + | IDENTIFIER + | '(' assignment ')' + | newExpression + | functionExpression + ; + +// A function is an expression bound with an ordinary declaration. +// Its last statement supplies the implicit result; return can exit earlier. +functionExpression : '{' parameterList? '->' statement* '}' ; +parameterList : (parameter ','?)+ ; +parameter : type bindingModifier? IDENTIFIER ; + +// Function types have unnamed, space-separated argument types. +// {int} is the zero-argument type; its function expression still uses ->. +functionType + : '{' type+ '->' type '}' '?'? + | '{' type '}' '?'? + ; + +callSuffix : '(' argumentList? ')' ; +argumentList : assignment (',' assignment)* ','? ; + +// 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+) ; +STRING_LITERAL : '"' (ESCAPE | ~["\\])* '"' ; +CHARACTER_LITERAL : '\'' (ESCAPE | ~['\\]) '\'' ; +fragment ESCAPE : '\\' [0ntr\\'"] ; +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 ; +BLOCK_COMMENT : '/*' .*? '*/' -> skip ; +``` diff --git a/chapter24/README.ja.md b/chapter24/README.ja.md index dd650ba3..1f16365c 100644 --- a/chapter24/README.ja.md +++ b/chapter24/README.ja.md @@ -13,6 +13,8 @@ [linear](https://github.com/SeaOfNodes/Simple/tree/linear) ブランチ上の直線的な Git リビジョン履歴で [この章](https://github.com/SeaOfNodes/Simple/tree/linear-chapter24) を読み、前の章と [比較](https://github.com/SeaOfNodes/Simple/compare/linear-chapter23...linear-chapter24) することもできます。 +この章の[完全な言語文法](docs/24-grammar.ja.md)はこちらです。 + ## 関係比較の連鎖 関係比較の連鎖により、同じ変数を繰り返さず、連続する比較をすっきりと読みやすく書けます。 diff --git a/chapter24/README.md b/chapter24/README.md index ce8afae0..00d4c58c 100644 --- a/chapter24/README.md +++ b/chapter24/README.md @@ -21,6 +21,8 @@ memory, and separate compilation; see the [backport review queue](../docs/chapte You can also read [this chapter](https://github.com/SeaOfNodes/Simple/tree/linear-chapter24) in a linear Git revision history on the [linear](https://github.com/SeaOfNodes/Simple/tree/linear) branch and [compare](https://github.com/SeaOfNodes/Simple/compare/linear-chapter23...linear-chapter24) it to the previous chapter. +Here is the [complete language grammar](docs/24-grammar.md) for this chapter. + ## Chaining Relational Tests Chaining relational tests offer a cleaner, more readable way to write chained diff --git a/chapter24/docs/18-grammar.ja.md b/chapter24/docs/18-grammar.ja.md deleted file mode 100644 index a36d9504..00000000 --- a/chapter24/docs/18-grammar.ja.md +++ /dev/null @@ -1,138 +0,0 @@ -# 第24章の文法 - -[English](18-grammar.md) | 日本語 - -この文書は [原文](18-grammar.md) の日本語訳です。 - -> 訳注: 原文の見出しは「Grammar for Chapter 16」です。この日本語版では、配置されている第24章に合わせた見出しにしています。 - -[第24章に戻る](../README.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program : block EOF ; - -block : statement+ ; - - -statement - : '{' block '}' - | returnStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - | structDeclaration - | expressionStatement - ; - - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - -continueStatement : 'continue' ';' ; - - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression - ; - -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; - -type - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ - ; - -nestedArray : '?'? '[]'* ; - -typeName : IDENTIFIER ; - - -expression : bitWiseExpression ; - -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* - ; - -comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* - ; - -shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign - ; - -primaryExpression - : INTEGER_LITERAL - | FLOAT_LITERAL - | '(' expression ')' - | 'true' - | 'false' - | 'null' - | newExpression - | IDENTIFIER - ; - -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; - - -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; -``` diff --git a/chapter24/docs/18-grammar.md b/chapter24/docs/18-grammar.md deleted file mode 100644 index 51539d0b..00000000 --- a/chapter24/docs/18-grammar.md +++ /dev/null @@ -1,132 +0,0 @@ -# Grammar for Chapter 16 - -English | [日本語](18-grammar.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program : block EOF ; - -block : statement+ ; - - -statement - : '{' block '}' - | returnStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - | structDeclaration - | expressionStatement - ; - - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - -continueStatement : 'continue' ';' ; - - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression - ; - -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; - -type - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ - ; - -nestedArray : '?'? '[]'* ; - -typeName : IDENTIFIER ; - - -expression : bitWiseExpression ; - -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* - ; - -comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* - ; - -shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign - ; - -primaryExpression - : INTEGER_LITERAL - | FLOAT_LITERAL - | '(' expression ')' - | 'true' - | 'false' - | 'null' - | newExpression - | IDENTIFIER - ; - -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; - - -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; -``` diff --git a/chapter24/docs/24-grammar.ja.md b/chapter24/docs/24-grammar.ja.md new file mode 100644 index 00000000..fd3fa232 --- /dev/null +++ b/chapter24/docs/24-grammar.ja.md @@ -0,0 +1,168 @@ +# 第24章の文法 + +[English](24-grammar.md) | 日本語 + +```antlrv4 +grammar SimpleLanguage; + +// 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 + | forStatement + | breakStatement + | continueStatement + | structDeclaration + | declaration ';' + | assignment ';' + | ';' + ; + +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; +continueStatement : 'continue' ';' ; + +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement + ; + +structDeclaration : 'struct' IDENTIFIER '{' structMember* '}' ';' ; +structMember : methodDeclaration | statement ; + +// A method is an ordinary fixed function binding in a struct definition. +// The compiler supplies self; it is not written in parameterList. +// The general declaration rule also admits explicitly typed function fields. +methodDeclaration : 'val' bindingModifier? IDENTIFIER '=' functionExpression ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +binding : bindingModifier? IDENTIFIER ('=' initializer)? ; +bindingModifier : '!' | '~' ; +// A declaration initializer consisting of the string "C" declares an extern +// symbol. Ordinary strings elsewhere are array-valued expressions. +initializer : assignment ; + +// The prefix qualifies the named struct reference, not an enclosing array. +// Each [] / [~] suffix qualifies its own array layer. ? requires a reference. +type + : functionType + | primitiveType typeSuffix* + | accessModifier? typeName typeSuffix* + ; +accessModifier : '!' | '~' ; +typeSuffix : '?' | '[]' | '[~]' ; +typeName : IDENTIFIER ('.' IDENTIFIER)* ; + +primitiveType + : 'int' | 'i8' | 'i16' | 'i32' | 'i64' + | 'u1' | 'u8' | 'u16' | 'u32' | 'byte' | 'bool' + | 'flt' | 'f32' | 'f64' + ; + +assignment : expression (assignmentOperator assignment)? ; +assignmentOperator + : '=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' + | '<<=' | '>>=' | '>>>=' + ; + +expression : logicalExpression ('?' assignment (':' assignment)?)? ; + +// Both logical operators short-circuit and associate to the right here. +logicalExpression : bitwiseExpression (('&&' | '||') logicalExpression)? ; + +bitwiseExpression + : equalityExpression (('&' | '|' | '^') equalityExpression)* + ; + +equalityExpression : comparisonExpression (('==' | '!=') comparisonExpression)* ; + +// Chained comparisons test adjacent operands and short-circuit. A chain must +// keep its direction; equality has lower precedence and does not join a chain. +comparisonExpression + : shiftExpression + ( (('<' | '<=') shiftExpression)+ | (('>' | '>=') shiftExpression)+ )? + ; + +shiftExpression + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression + ; + +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' | callSuffix | methodCallSuffix ; + +// For a fixed function-valued member, object.name(args) passes object as self. +// This overlaps ordinary field selection followed by a call syntactically; +// name/type resolution determines which call form applies. +methodCallSuffix : '.' IDENTIFIER callSuffix ; + +primaryExpression + : INTEGER_LITERAL + | FLOAT_LITERAL + | STRING_LITERAL + | CHARACTER_LITERAL + | 'true' + | 'false' + | 'null' + | IDENTIFIER + | '(' assignment ')' + | newExpression + | functionExpression + ; + +// A function is an expression bound with an ordinary declaration. +// Its last statement supplies the implicit result; return can exit earlier. +functionExpression : '{' parameterList? '->' statement* '}' ; +parameterList : (parameter ','?)+ ; +parameter : type bindingModifier? IDENTIFIER ; + +// Function types have unnamed, space-separated argument types. +// {int} is the zero-argument type; its function expression still uses ->. +functionType + : '{' type+ '->' type '}' '?'? + | '{' type '}' '?'? + ; + +callSuffix : '(' argumentList? ')' ; +argumentList : assignment (',' assignment)* ','? ; + +// 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+) ; +STRING_LITERAL : '"' (ESCAPE | ~["\\])* '"' ; +CHARACTER_LITERAL : '\'' (ESCAPE | ~['\\]) '\'' ; +fragment ESCAPE : '\\' [0ntr\\'"] ; +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 ; +BLOCK_COMMENT : '/*' .*? '*/' -> skip ; +``` diff --git a/chapter24/docs/24-grammar.md b/chapter24/docs/24-grammar.md new file mode 100644 index 00000000..22867355 --- /dev/null +++ b/chapter24/docs/24-grammar.md @@ -0,0 +1,168 @@ +# Grammar for Chapter 24 + +English | [日本語](24-grammar.ja.md) + +```antlrv4 +grammar SimpleLanguage; + +// 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 + | forStatement + | breakStatement + | continueStatement + | structDeclaration + | declaration ';' + | assignment ';' + | ';' + ; + +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; +continueStatement : 'continue' ';' ; + +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement + ; + +structDeclaration : 'struct' IDENTIFIER '{' structMember* '}' ';' ; +structMember : methodDeclaration | statement ; + +// A method is an ordinary fixed function binding in a struct definition. +// The compiler supplies self; it is not written in parameterList. +// The general declaration rule also admits explicitly typed function fields. +methodDeclaration : 'val' bindingModifier? IDENTIFIER '=' functionExpression ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +binding : bindingModifier? IDENTIFIER ('=' initializer)? ; +bindingModifier : '!' | '~' ; +// A declaration initializer consisting of the string "C" declares an extern +// symbol. Ordinary strings elsewhere are array-valued expressions. +initializer : assignment ; + +// The prefix qualifies the named struct reference, not an enclosing array. +// Each [] / [~] suffix qualifies its own array layer. ? requires a reference. +type + : functionType + | primitiveType typeSuffix* + | accessModifier? typeName typeSuffix* + ; +accessModifier : '!' | '~' ; +typeSuffix : '?' | '[]' | '[~]' ; +typeName : IDENTIFIER ('.' IDENTIFIER)* ; + +primitiveType + : 'int' | 'i8' | 'i16' | 'i32' | 'i64' + | 'u1' | 'u8' | 'u16' | 'u32' | 'byte' | 'bool' + | 'flt' | 'f32' | 'f64' + ; + +assignment : expression (assignmentOperator assignment)? ; +assignmentOperator + : '=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' + | '<<=' | '>>=' | '>>>=' + ; + +expression : logicalExpression ('?' assignment (':' assignment)?)? ; + +// Both logical operators short-circuit and associate to the right here. +logicalExpression : bitwiseExpression (('&&' | '||') logicalExpression)? ; + +bitwiseExpression + : equalityExpression (('&' | '|' | '^') equalityExpression)* + ; + +equalityExpression : comparisonExpression (('==' | '!=') comparisonExpression)* ; + +// Chained comparisons test adjacent operands and short-circuit. A chain must +// keep its direction; equality has lower precedence and does not join a chain. +comparisonExpression + : shiftExpression + ( (('<' | '<=') shiftExpression)+ | (('>' | '>=') shiftExpression)+ )? + ; + +shiftExpression + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression + ; + +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' | callSuffix | methodCallSuffix ; + +// For a fixed function-valued member, object.name(args) passes object as self. +// This overlaps ordinary field selection followed by a call syntactically; +// name/type resolution determines which call form applies. +methodCallSuffix : '.' IDENTIFIER callSuffix ; + +primaryExpression + : INTEGER_LITERAL + | FLOAT_LITERAL + | STRING_LITERAL + | CHARACTER_LITERAL + | 'true' + | 'false' + | 'null' + | IDENTIFIER + | '(' assignment ')' + | newExpression + | functionExpression + ; + +// A function is an expression bound with an ordinary declaration. +// Its last statement supplies the implicit result; return can exit earlier. +functionExpression : '{' parameterList? '->' statement* '}' ; +parameterList : (parameter ','?)+ ; +parameter : type bindingModifier? IDENTIFIER ; + +// Function types have unnamed, space-separated argument types. +// {int} is the zero-argument type; its function expression still uses ->. +functionType + : '{' type+ '->' type '}' '?'? + | '{' type '}' '?'? + ; + +callSuffix : '(' argumentList? ')' ; +argumentList : assignment (',' assignment)* ','? ; + +// 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+) ; +STRING_LITERAL : '"' (ESCAPE | ~["\\])* '"' ; +CHARACTER_LITERAL : '\'' (ESCAPE | ~['\\]) '\'' ; +fragment ESCAPE : '\\' [0ntr\\'"] ; +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 ; +BLOCK_COMMENT : '/*' .*? '*/' -> skip ; +``` diff --git a/chapter25/README.ja.md b/chapter25/README.ja.md index fc4d96e4..cfb953c6 100644 --- a/chapter25/README.ja.md +++ b/chapter25/README.ja.md @@ -15,6 +15,8 @@ [直線的な履歴](https://github.com/SeaOfNodes/Simple/tree/linear) で [この章](https://github.com/SeaOfNodes/Simple/tree/linear-chapter25) を読み、[第24章と比較](https://github.com/SeaOfNodes/Simple/compare/linear-chapter24...linear-chapter25) することもできます。 +この章の[完全な言語文法](docs/25-grammar.ja.md)はこちらです。 + ## コンパイル単位と名前 `CompUnit` はソースまたはオブジェクトファイルを表します。`ParseAll` は依存関係を発見し、必要なソースを解析し、以前コンパイルした単位を読み込みます。ソースツリーとビルドツリーは対応する相対パスを使い、クラス名はドット区切りのパスを使います。たとえば `a/b/foo.smp` は名前空間 `a.b.foo` を供給します。 @@ -43,8 +45,6 @@ ELF ライタは、ネイティブコードと、正規化された型、理想ã 外部フィールドのフラグと記号的オフセットは、シリアライズされた配置を変更します。マジックは `C0DE` のままで、独立した形式バージョンはまだありません。このコンパイラへインポートする前に、すべての Simple オブジェクトファイルを再ビルドしてください。 -> 訳注:原文のこの段落は `C0DE` としていますが、後の「ビルドと確認」では `C0D2` ヘッダを使うと記しています。この訳では、原文の両方の記述を保持しています。 - ## 型が完成する前の SSA 構築 Simple は引き続き AST を介さず直接 SSA に解析します。前方参照により、解析時点では式の最終的な型が利用できない場合があります。そのため構文と解決済みの字句的束縛でトポロジーを選び、後から型でグラフを精密化、最適化、検証します。 @@ -86,7 +86,7 @@ return new Box(new i64[1]); このディレクトリで `make lib` は Java テスト依存関係を用意し、`make tests` は章の生のスイート、独立スイート、システムテスト、ファザー回帰テストを実行します。`make release` はコンパイラ jar とネイティブライブラリ成果物を構築し、`make tags` はエディタ用タグを構築します。ネイティブツールと選択した CPU/ABI は環境と一致する必要があります。Makefile は現在、既定で x86-64/win64 を使います。 -シリアライズ IR は現在 `C0D2` ヘッダを使います。ポインタ型は独立した読み取り専用アクセスビットを含みます。古いオブジェクトファイルは再ビルドが必要です。リーダは旧ヘッダを拒否し、新しい配置で旧型を解釈しません。 +シリアライズ IR は `C0DE` ヘッダを使います。ポインタ型は独立した読み取り専用アクセスビットを含みます。形式にはまだバージョン番号がないため、形式を変更したらオブジェクトファイルを再ビルドしてください。現在、後方互換性は保証していません。 コンパイラ変更は `sys.o` のシリアライズ IR とネイティブコードの両方を無効にする場合があります。リンクしたプログラムのテスト結果を解釈する前に再ビルドしてください。ソースだけの部分集合は、この章の完全な `make tests` と同等ではありません。 diff --git a/chapter25/README.md b/chapter25/README.md index 8beee7b1..bfe48809 100644 --- a/chapter25/README.md +++ b/chapter25/README.md @@ -28,6 +28,8 @@ You can also read [this chapter](https://github.com/SeaOfNodes/Simple/tree/linea in the [linear history](https://github.com/SeaOfNodes/Simple/tree/linear) and [compare it with Chapter 24](https://github.com/SeaOfNodes/Simple/compare/linear-chapter24...linear-chapter25). +Here is the [complete language grammar](docs/25-grammar.md) for this chapter. + ## Compilation units and names `CompUnit` represents a source or object file. `ParseAll` discovers dependencies, @@ -158,9 +160,9 @@ regressions. `make release` builds the compiler jar and native library artifacts `make tags` builds editor tags. Native tools and the selected CPU/ABI must match the environment; the Makefile currently defaults to x86-64/win64. -Serialized IR now uses the `C0D2` header: pointer types include an independent -read-only access bit. Older object files must be rebuilt; the reader rejects -their old header rather than interpreting their types with the new layout. +Serialized IR uses the `C0DE` header; pointer types include an independent +read-only access bit. The format is not versioned yet, so rebuild object files +after format changes. Backward compatibility is not currently supported. Compiler changes can invalidate both serialized IR and native code in `sys.o`. Rebuild it before interpreting linked-program test results. A source-only subset diff --git a/chapter25/docs/18-grammar.ja.md b/chapter25/docs/18-grammar.ja.md deleted file mode 100644 index 758632be..00000000 --- a/chapter25/docs/18-grammar.ja.md +++ /dev/null @@ -1,137 +0,0 @@ -# 第25章の文法 - -[English](18-grammar.md) | 日本語 - -この文書は [原文](18-grammar.md) の日本語訳です。 - -> 訳注: 原文の見出しは「Grammar for Chapter 16」です。この日本語版では、配置されている第25章に合わせた見出しにしています。 - -[第25章に戻る](../README.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program : block EOF ; - -block : statement+ ; - - -statement - : '{' block '}' - | returnStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - | structDeclaration - | expressionStatement - ; - - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - -continueStatement : 'continue' ';' ; - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression - ; - -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; - -type - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ - ; - -nestedArray : '?'? '[]'* ; - -typeName : IDENTIFIER ; - - -expression : bitWiseExpression ; - -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* - ; - -comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* - ; - -shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign - ; - -primaryExpression - : INTEGER_LITERAL - | FLOAT_LITERAL - | '(' expression ')' - | 'true' - | 'false' - | 'null' - | newExpression - | IDENTIFIER - ; - -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; - - -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; -``` diff --git a/chapter25/docs/18-grammar.md b/chapter25/docs/18-grammar.md deleted file mode 100644 index 9176340a..00000000 --- a/chapter25/docs/18-grammar.md +++ /dev/null @@ -1,131 +0,0 @@ -# Grammar for Chapter 16 - -English | [日本語](18-grammar.ja.md) - -```antlrv4 -grammar SimpleLanguage; - -program : block EOF ; - -block : statement+ ; - - -statement - : '{' block '}' - | returnStatement - | ifStatement - | whileStatement - | breakStatement - | continueStatement - | structDeclaration - | expressionStatement - ; - - -returnStatement : 'return' expression ';' ; - -ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; - -whileStatement : 'while' '(' expression ')' statement ; - -breakStatement : 'break' ';' ; - -continueStatement : 'continue' ';' ; - -structDeclaration : 'struct' IDENTIFIER '{' block '}' ; - -expressionStatement - : type IDENTIFIER ';' - | type IDENTIFIER '=' expression ';' - | IDENTIFIER '=' expression ';' - | expression - ; - -PRIMTYPE - : 'int' - | 'i8' - | 'i16' - | 'i32' - | 'i64' - | 'u8' - | 'u16' - | 'u32' - | 'flt' - | 'f32' - | 'f64' - | 'bool' - ; - -type - : PRIMTYPE - | typeName '?'? - | typeName nestedArray+ - ; - -nestedArray : '?'? '[]'* ; - -typeName : IDENTIFIER ; - - -expression : bitWiseExpression ; - -bitWiseExpression - : comparisonExpression ( '&' | '|' | '^' ) comparisonExpression)* - ; - -comparisonExpression - : shiftExpression (('==' | '!='| '>'| '<'| '>='| '<=') shiftExpression)* - ; - -shiftExpression - : additiveExpression ( '<<' | '>>>' | '>>' ) additiveExpression)* - ; - -additiveExpression - : multiplicativeExpression (('+' | '-') multiplicativeExpression)* - ; - -multiplicativeExpression - : unaryExpression (('*' | '/') unaryExpression)* - ; - -unaryExpression - : ('-') unaryExpression - | '!' unaryExpression - | primaryExpression postAssign - ; - -primaryExpression - : INTEGER_LITERAL - | FLOAT_LITERAL - | '(' expression ')' - | 'true' - | 'false' - | 'null' - | newExpression - | IDENTIFIER - ; - -newExpression - : 'new' IDENTIFIER [ '{' block '}' ] - | 'new' IDENTIFIER '[' expression ']' - ; - -postAssign : postFix [ '=' expression ] | [ '#' ]; - -postFix - : '.' IDENTIFIER postFix - | '[' expression ']' postFix - ; - - -IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; - -INTEGER_LITERAL - : [1-9]DIGIT* - | [0] - ; - -NON_DIGIT: [a-zA-Z_]; -DIGIT: [0-9]; -``` diff --git a/chapter25/docs/25-grammar.ja.md b/chapter25/docs/25-grammar.ja.md new file mode 100644 index 00000000..9e9fb820 --- /dev/null +++ b/chapter25/docs/25-grammar.ja.md @@ -0,0 +1,175 @@ +# 第25章の文法 + +[English](25-grammar.md) | 日本語 + +```antlrv4 +grammar SimpleLanguage; + +// Name lookup, assignable operands, type compatibility, and access permissions +// are checked by the compiler in addition to these syntax rules. +// A file is a compilation unit; its path supplies its namespace. +// Qualified names resolve nested types and other units without an import keyword. +program : statement* EOF ; + +block : '{' statement* '}' ; + +statement + : block + | returnStatement + | ifStatement + | whileStatement + | forStatement + | breakStatement + | continueStatement + | structDeclaration + | declaration ';' + | assignment ';' + | ';' + ; + +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; +continueStatement : 'continue' ';' ; + +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement + ; + +structDeclaration : 'struct' IDENTIFIER '{' structMember* '}' ';' ; +structMember : constructorDeclaration | methodDeclaration | statement ; + +// The constructor name must match the enclosing struct's name. +constructorDeclaration : 'new' IDENTIFIER '=' functionExpression ';' ; + +// A method is an ordinary fixed function binding in a struct definition. +// The compiler supplies self; it is not written in parameterList. +// Function types and expressions both receive their implicit receiver here. +methodDeclaration : 'val' bindingModifier? IDENTIFIER '=' functionExpression ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +binding : bindingModifier? IDENTIFIER ('=' initializer)? ; +bindingModifier : '!' | '~' ; +// A declaration initializer consisting of the string "C" declares an extern +// symbol: a function, or explicit numeric native storage. Ordinary strings +// elsewhere are array-valued expressions. +initializer : assignment ; + +// The prefix qualifies the named struct reference, not an enclosing array. +// Each [] / [~] suffix qualifies its own array layer. ? requires a reference. +type + : functionType + | primitiveType typeSuffix* + | accessModifier? typeName typeSuffix* + ; +accessModifier : '!' | '~' ; +typeSuffix : '?' | '[]' | '[~]' ; +typeName : IDENTIFIER ('.' IDENTIFIER)* ; + +primitiveType + : 'int' | 'i8' | 'i16' | 'i32' | 'i64' + | 'u1' | 'u8' | 'u16' | 'u32' | 'byte' | 'bool' + | 'flt' | 'f32' | 'f64' + ; + +assignment : expression (assignmentOperator assignment)? ; +assignmentOperator + : '=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' + | '<<=' | '>>=' | '>>>=' + ; + +expression : logicalExpression ('?' assignment (':' assignment)?)? ; + +// Both logical operators short-circuit and associate to the right here. +logicalExpression : bitwiseExpression (('&&' | '||') logicalExpression)? ; + +bitwiseExpression + : equalityExpression (('&' | '|' | '^') equalityExpression)* + ; + +equalityExpression : comparisonExpression (('==' | '!=') comparisonExpression)* ; + +// Chained comparisons test adjacent operands and short-circuit. A chain must +// keep its direction; equality has lower precedence and does not join a chain. +comparisonExpression + : shiftExpression + ( (('<' | '<=') shiftExpression)+ | (('>' | '>=') shiftExpression)+ )? + ; + +shiftExpression + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression + ; + +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' | callSuffix | methodCallSuffix ; + +// For a fixed function-valued member, object.name(args) passes object as self. +// This overlaps ordinary field selection followed by a call syntactically; +// name/type resolution determines which call form applies. +methodCallSuffix : '.' IDENTIFIER callSuffix ; + +primaryExpression + : INTEGER_LITERAL + | FLOAT_LITERAL + | STRING_LITERAL + | CHARACTER_LITERAL + | 'true' + | 'false' + | 'null' + | IDENTIFIER + | '(' assignment ')' + | newExpression + | functionExpression + ; + +// A function is an expression bound with an ordinary declaration. +// Its last statement supplies the implicit result; return can exit earlier. +functionExpression : '{' parameterList? '->' statement* '}' ; +parameterList : (parameter ','?)+ ; +parameter : type bindingModifier? IDENTIFIER ; + +// Function types have unnamed, space-separated argument types. +// {int} is the zero-argument type; its function expression still uses ->. +functionType + : '{' type+ '->' type '}' '?'? + | '{' type '}' '?'? + ; + +callSuffix : '(' argumentList? ')' ; +argumentList : assignment (',' assignment)* ','? ; + +// Allocation invokes the declared constructor; inline initializer blocks from +// earlier chapters are replaced by constructor arguments. +newExpression : 'new' type ('[' assignment ']' | callSuffix)? ; + +INTEGER_LITERAL : '0' | [1-9] DIGIT* ; +FLOAT_LITERAL : DIGIT+ ('.' DIGIT* ('e' DIGIT+)? | 'e' '-'? DIGIT+) ; +STRING_LITERAL : '"' (ESCAPE | ~["\\])* '"' ; +CHARACTER_LITERAL : '\'' (ESCAPE | ~['\\]) '\'' ; +fragment ESCAPE : '\\' [0ntr\\'"] ; +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 ; +BLOCK_COMMENT : '/*' .*? '*/' -> skip ; +``` diff --git a/chapter25/docs/25-grammar.md b/chapter25/docs/25-grammar.md new file mode 100644 index 00000000..065cf051 --- /dev/null +++ b/chapter25/docs/25-grammar.md @@ -0,0 +1,175 @@ +# Grammar for Chapter 25 + +English | [日本語](25-grammar.ja.md) + +```antlrv4 +grammar SimpleLanguage; + +// Name lookup, assignable operands, type compatibility, and access permissions +// are checked by the compiler in addition to these syntax rules. +// A file is a compilation unit; its path supplies its namespace. +// Qualified names resolve nested types and other units without an import keyword. +program : statement* EOF ; + +block : '{' statement* '}' ; + +statement + : block + | returnStatement + | ifStatement + | whileStatement + | forStatement + | breakStatement + | continueStatement + | structDeclaration + | declaration ';' + | assignment ';' + | ';' + ; + +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; +continueStatement : 'continue' ';' ; + +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement + ; + +structDeclaration : 'struct' IDENTIFIER '{' structMember* '}' ';' ; +structMember : constructorDeclaration | methodDeclaration | statement ; + +// The constructor name must match the enclosing struct's name. +constructorDeclaration : 'new' IDENTIFIER '=' functionExpression ';' ; + +// A method is an ordinary fixed function binding in a struct definition. +// The compiler supplies self; it is not written in parameterList. +// Function types and expressions both receive their implicit receiver here. +methodDeclaration : 'val' bindingModifier? IDENTIFIER '=' functionExpression ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +binding : bindingModifier? IDENTIFIER ('=' initializer)? ; +bindingModifier : '!' | '~' ; +// A declaration initializer consisting of the string "C" declares an extern +// symbol: a function, or explicit numeric native storage. Ordinary strings +// elsewhere are array-valued expressions. +initializer : assignment ; + +// The prefix qualifies the named struct reference, not an enclosing array. +// Each [] / [~] suffix qualifies its own array layer. ? requires a reference. +type + : functionType + | primitiveType typeSuffix* + | accessModifier? typeName typeSuffix* + ; +accessModifier : '!' | '~' ; +typeSuffix : '?' | '[]' | '[~]' ; +typeName : IDENTIFIER ('.' IDENTIFIER)* ; + +primitiveType + : 'int' | 'i8' | 'i16' | 'i32' | 'i64' + | 'u1' | 'u8' | 'u16' | 'u32' | 'byte' | 'bool' + | 'flt' | 'f32' | 'f64' + ; + +assignment : expression (assignmentOperator assignment)? ; +assignmentOperator + : '=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' + | '<<=' | '>>=' | '>>>=' + ; + +expression : logicalExpression ('?' assignment (':' assignment)?)? ; + +// Both logical operators short-circuit and associate to the right here. +logicalExpression : bitwiseExpression (('&&' | '||') logicalExpression)? ; + +bitwiseExpression + : equalityExpression (('&' | '|' | '^') equalityExpression)* + ; + +equalityExpression : comparisonExpression (('==' | '!=') comparisonExpression)* ; + +// Chained comparisons test adjacent operands and short-circuit. A chain must +// keep its direction; equality has lower precedence and does not join a chain. +comparisonExpression + : shiftExpression + ( (('<' | '<=') shiftExpression)+ | (('>' | '>=') shiftExpression)+ )? + ; + +shiftExpression + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression + ; + +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' | callSuffix | methodCallSuffix ; + +// For a fixed function-valued member, object.name(args) passes object as self. +// This overlaps ordinary field selection followed by a call syntactically; +// name/type resolution determines which call form applies. +methodCallSuffix : '.' IDENTIFIER callSuffix ; + +primaryExpression + : INTEGER_LITERAL + | FLOAT_LITERAL + | STRING_LITERAL + | CHARACTER_LITERAL + | 'true' + | 'false' + | 'null' + | IDENTIFIER + | '(' assignment ')' + | newExpression + | functionExpression + ; + +// A function is an expression bound with an ordinary declaration. +// Its last statement supplies the implicit result; return can exit earlier. +functionExpression : '{' parameterList? '->' statement* '}' ; +parameterList : (parameter ','?)+ ; +parameter : type bindingModifier? IDENTIFIER ; + +// Function types have unnamed, space-separated argument types. +// {int} is the zero-argument type; its function expression still uses ->. +functionType + : '{' type+ '->' type '}' '?'? + | '{' type '}' '?'? + ; + +callSuffix : '(' argumentList? ')' ; +argumentList : assignment (',' assignment)* ','? ; + +// Allocation invokes the declared constructor; inline initializer blocks from +// earlier chapters are replaced by constructor arguments. +newExpression : 'new' type ('[' assignment ']' | callSuffix)? ; + +INTEGER_LITERAL : '0' | [1-9] DIGIT* ; +FLOAT_LITERAL : DIGIT+ ('.' DIGIT* ('e' DIGIT+)? | 'e' '-'? DIGIT+) ; +STRING_LITERAL : '"' (ESCAPE | ~["\\])* '"' ; +CHARACTER_LITERAL : '\'' (ESCAPE | ~['\\]) '\'' ; +fragment ESCAPE : '\\' [0ntr\\'"] ; +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 ; +BLOCK_COMMENT : '/*' .*? '*/' -> skip ; +``` diff --git a/chapter25/src/main/java/com/seaofnodes/simple/codegen/Serialize.java b/chapter25/src/main/java/com/seaofnodes/simple/codegen/Serialize.java index a8cd315f..27253c5e 100644 --- a/chapter25/src/main/java/com/seaofnodes/simple/codegen/Serialize.java +++ b/chapter25/src/main/java/com/seaofnodes/simple/codegen/Serialize.java @@ -63,9 +63,8 @@ abstract public class Serialize { Type.TAGOFFS(); BAOS baos = new BAOS(); - // C0D2 adds independent pointer access permissions to serialized types. - // A - Print a header - baos.write('C').write('0').write('D').write('2'); + // A - Print the C0DE header. The format is not versioned yet. + baos.write('C').write('0').write('D').write('E'); // Count unique Types var types = new HashMap<Type,Integer>(); @@ -218,7 +217,7 @@ abstract public class Serialize { Type.TAGOFFS(); // A - Read a header - if( bais.read()!='C' || bais.read()!='0' || bais.read()!='D' || bais.read()!='2' ) + if( bais.read()!='C' || bais.read()!='0' || bais.read()!='D' || bais.read()!='E' ) throw new IllegalArgumentException("Unsupported serialized IR format; rebuild the object file"); // B - Packed read of #strings, then strings @@ -247,7 +246,7 @@ abstract public class Serialize { String[] strs = elf._strs; if( strs == null ) { // A - Read a header - if( bais.read() != 'C' || bais.read() != '0' || bais.read() != 'D' || bais.read() != '2' ) + if( bais.read() != 'C' || bais.read() != '0' || bais.read() != 'D' || bais.read() != 'E' ) throw new IllegalArgumentException( "Unsupported serialized IR format; rebuild the object file" ); // B - Packed read of #strings, then strings diff --git a/docs/chapter-backports.md b/docs/chapter-backports.md index 53fd6a9c..220bc073 100644 --- a/docs/chapter-backports.md +++ b/docs/chapter-backports.md @@ -84,8 +84,8 @@ nullable field's implicit null is still an instance field. Constructor permissions cover only allocation fields, including assignments in nested blocks. They do not permit rebinding surrounding or constructor-local fixed variables. Chapter 25's constructor-exit check also handles required -primitive fields. The object format changes to `C0D2`; rebuilding `sys.o` is -required and exercised by its native tests. +primitive fields. The object layout changes under the unversioned `C0DE` header; +rebuilding `sys.o` is required and exercised by its native tests. The changed graphs exposed two small existing assumptions: Chapter 24 must recognize a Phi whose own backedge is still under construction, and a memory diff --git a/docs/japanese-translation.md b/docs/japanese-translation.md new file mode 100644 index 00000000..39383345 --- /dev/null +++ b/docs/japanese-translation.md @@ -0,0 +1,22 @@ +# Japanese translation / 翻訳の保守方針 + +English is authoritative. The contributed Japanese translation is maintained on +a best-effort basis; English edits and chapter reorganizations never wait for +translation updates. Move Japanese files and update navigation when renumbering +chapters; prose updates can follow separately. Contributions are welcome, without +an ongoing maintenance obligation for the original translator. + +[英語版](../README.md)が正式な文書です。この日本語訳は寄贈されたもので、可能な範囲で保守されますが、 +英語版より更新が遅れることがあります。英語版の編集や章の再構成を、翻訳の更新待ちにすることはありません。 +章を移動・改番する際は、日本語ファイルも一緒に移動して章の見出しやリンクを更新し、本文の翻訳は後から更新できます。 +翻訳の改善は歓迎しますが、元の寄贈者に継続的な保守を義務付けるものではありません。 + +初回の翻訳は英語版のコミット +[`81b654d7`](https://github.com/SeaOfNodes/Simple/tree/81b654d76444d747fdf1d053760e507b50f26e18) +を基にしています。2026年10月の目次・文法見出し・コード例・命令エンコーディング・ +`C0DE` ヘッダに関する文書修正も反映しています。 +各文書の English リンクは現在の英語版を指します。更新されていない箇所の原文を確認するには、 +上記コミットの対応するファイルを参照してください。新しい章が未翻訳の場合は、その旨を示して英語版にリンクできます。 + + +[日本語の紹介に戻る](../README.ja.md) diff --git a/skills/chapter25-codex-notes.md b/skills/chapter25-codex-notes.md index fca0d9ed..2f2f299c 100644 --- a/skills/chapter25-codex-notes.md +++ b/skills/chapter25-codex-notes.md @@ -78,7 +78,8 @@ For shared viewer work, read [the graph viewer notes](graph-viewer-codex-notes.m not merely because the canonical declaration has different pointer permissions. - Construct cyclic pointers with their access bit already set before interning; creating a provisional pointer and then changing access can orphan temporary - cyclic types. Serialized objects use the `C0D2` header and require a rebuild. + cyclic types. Serialized objects use the unversioned `C0DE` header; rebuild + objects after format changes. Backward compatibility is not required yet. ## Tutorial backports |
