diff options
Diffstat (limited to 'sample/trick2018/01-kinaba')
-rw-r--r-- | sample/trick2018/01-kinaba/authors.markdown | 3 | ||||
-rw-r--r-- | sample/trick2018/01-kinaba/entry.rb | 8 | ||||
-rw-r--r-- | sample/trick2018/01-kinaba/remarks.markdown | 55 |
3 files changed, 66 insertions, 0 deletions
diff --git a/sample/trick2018/01-kinaba/authors.markdown b/sample/trick2018/01-kinaba/authors.markdown new file mode 100644 index 0000000000..d0df0b379d --- /dev/null +++ b/sample/trick2018/01-kinaba/authors.markdown @@ -0,0 +1,3 @@ +* kinaba + * twitter.com/kinaba + * cctld: jp diff --git a/sample/trick2018/01-kinaba/entry.rb b/sample/trick2018/01-kinaba/entry.rb new file mode 100644 index 0000000000..eb8284d5ab --- /dev/null +++ b/sample/trick2018/01-kinaba/entry.rb @@ -0,0 +1,8 @@ +alias BEGIN for unless def class +super true or return defined? next +break while begin undef do end +rescue then retry else undef module +nil ensure case if yield __LINE__ +self and redo elsif not __FILE__ +alias END in end when __ENCODING__ +end until false end diff --git a/sample/trick2018/01-kinaba/remarks.markdown b/sample/trick2018/01-kinaba/remarks.markdown new file mode 100644 index 0000000000..a1a05bfd73 --- /dev/null +++ b/sample/trick2018/01-kinaba/remarks.markdown @@ -0,0 +1,55 @@ +### Remarks + +Just run it with no argument: + + ruby entry.rb + +(Anyway it is just a no-op program. The above command only verifies +that entry.rb is a valid Ruby program.) + +I confirmed the following implementations/platforms: + +* ruby 2.5.0p0 (2017-12-25 revision 61468) [x64-mingw32] + +### Description + +First, look at + +https://docs.ruby-lang.org/ja/latest/doc/spec=2flexical.html#reserved + +and then, look at entry.rb. + +The source code of entry.rb consists only of reserved words of Ruby, +and all the reserved words are used in the code, in a way that the code +forms a valid Ruby program. No compile error, no warning, or no runtime error. + + +### Internals + +Difficult (and interesting) points of the theme are: + +* Since many of the reserved words define program structures, we cannot + use them independently. For instance, `retry` must be inside `rescue`, + or `break`/`next`/`redo` must be inside a looping construct. + Or, jump-out statements cannot occur at a position that requires a + value; `if return then true end` is a "void value expression" syntax error. +* Inserting newlines for each 6 word (to match with the spec html) is also + an interseting challenge, since Ruby is sensitive to newlines. + +Tricks used in the code are: + +* def/alias/undef can take even reserved words as parameters. + That is, `def class ... end` defines a method named `class`. + The feature is crucial since otherwise `BEGIN` etc inevitably + introduces non-reserved tokens (like `{}`). +* `defined?` can take some reserved words too (which I didn't know + until trying to write this program.) +* "void value expression" can be avoided by using `or` or `and`. + `if begin return end then true end` is a syntax error, but + `if begin false or return end then true end` is not. + + +### Limitation + +Sad to say that it's not a "perfect pangram". +It uses 'alias' and 'undef' twice, and 'end' 4 times. |