From 863a6ed0ad723e06c76f392d4fb3a5f6bd22863c Mon Sep 17 00:00:00 2001 From: Jun Kawasaki Date: Tue, 6 Oct 2026 13:01:11 +0900 Subject: docs: add Japanese translations for all chapters --- chapter17b/docs/17-grammar.ja.md | 21 +++++++++++++++++++++ chapter17b/docs/17-grammar.md | 2 ++ 2 files changed, 23 insertions(+) create mode 100644 chapter17b/docs/17-grammar.ja.md (limited to 'chapter17b/docs') diff --git a/chapter17b/docs/17-grammar.ja.md b/chapter17b/docs/17-grammar.ja.md new file mode 100644 index 00000000..c90a9d5b --- /dev/null +++ b/chapter17b/docs/17-grammar.ja.md @@ -0,0 +1,21 @@ +# 第17b章の文法の追加 + +[English](17-grammar.md) | 日本語 + +この文書は [原文](17-grammar.md) の日本語訳です。 + +[第17a章の宣言構文](../../chapter17a/docs/17-grammar.ja.md)から始めます。この章は、型推論、代入の短縮形、条件式、`for` ループを追加します。アクセスと束縛の権限の意味は変わりません。 + +```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 +``` + +宣言には、コンマで区切った複数の名前を含められます。`var` は再代入を許し、`val` は束縛を固定します。両方とも初期化子の参照の権限を維持します。後置の更新はフィールドと配列要素もサポートします。前置の更新は、今のところ局所名をサポートします。評価順序については [README](../README.ja.md) を参照してください。 diff --git a/chapter17b/docs/17-grammar.md b/chapter17b/docs/17-grammar.md index 9a70f323..d42fcba1 100644 --- a/chapter17b/docs/17-grammar.md +++ b/chapter17b/docs/17-grammar.md @@ -1,5 +1,7 @@ # Chapter 17b grammar additions +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. -- cgit v1.2.3 From b2862f980f3d78012066ef1cc09d4ad58f886c0f Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Wed, 7 Oct 2026 09:53:15 -0700 Subject: Pick up japanese transaltion A bunch of minor README fixes, missing TOC, grammer cleanup --- chapter17b/docs/17-grammar.ja.md | 132 ++++++++++++++++++++++++++++++++----- chapter17b/docs/17-grammar.md | 139 +++++++++++++++++++++++++++++++++------ 2 files changed, 236 insertions(+), 35 deletions(-) (limited to 'chapter17b/docs') 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 ; +``` -- cgit v1.2.3