summaryrefslogtreecommitdiff
path: root/sample/trick2018/01-kinaba/remarks.markdown
blob: a1a05bfd73e3df2e1f97bdd72953df134d2d6ecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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.