2. Intended Audience
โข An eccentric Go programmer who happens to want
to add feture XX to the language, knowing her
patch will never be merged.
โข A keen-minded programmer who wants to know
how the compiler works.
5. Lexer
โข A lexer scans over the source code and cut it into a
bunch of meaningful chunks (the first abstraction).
โข Example:
a := b + c()
LNAME LASOP LNAME + LNAME ( )
8. When do you want to hack the
lexer
โข Modify the keyword such as func and make.
โข Modify the operator only cosmetically (e.g. != โ
~=)
โข Modify how literals and identifiers are represented.
โข Add a new keyword or operator to the language to
later use in the parser.
9. Example: Emojis for identifiers
โข http://moriyoshi.hatenablog.com/entry/2014/06/0
3/121728
โข Go doesnโt treat emojis as part of identifiers.
./sushi.go:8: invalid identifier character U+1f363
โข But I wanted to have ๅฏฟๅธ(in the source)
10. Example: Emojis for identifiers
โข Patched the following place to let it accept emojis:
if(c >= Runeself) {
ungetc(c);
rune = getr();
// 0xb7 ยท is used for internal names
if(!isalpharune(rune) && !isdigitrune(rune) &&
(importpkg == nil || rune != 0xb7))
yyerror("invalid identifier character U+%04x",
rune);
cp += runetochar(cp, &rune);
} else if(!yy_isalnum(c) && c != '_')
break;
12. Parser
โข Parser repeatedly calls the lexer to fetch the tokens
and builds an abstract syntax tree (AST) that
represents the source code.
โข The AST is retouched (โtypecheckโand โwalkโ sub-phase)
during type inference and assersion phase
so it would be less verbose and contain information
helpful for the later stages.
โข src/cmd/gc/go.y, src/cmd/gc/dcl.c
src/cmd/gc/typecheck.c,
src/cmd/gc/walk.c,
src/cmd/gc/reflect.c
15. Example: Bracket operator
overload!
โข Let the following code (A) expand to (B)
โข https://gist.github.com/moriyoshi/c0e2b2f9be688
3e33251
(A)
(B)
a := &struct{}{}
fmt.Println(a[1])
a[1] = "test2"
fmt.Println(a.__getindex(1))
a.__setindex(1, "test2")
16. Example: Bracket operator
overload!
โข Things to do:
โข Introduce a new AST node type (e.g. OINDEXINTER)
โข Add a branch point in โtypecheckโ to handle the case
where the indexed target is neither a string, array, slice
nor map type.
โข Supply a code in โwalkโ to specially treat the assignment
and dereference that involves that kind of node. The
code synthesizes the node to invoke the special
functions, then typecheck and walk over themselves in a
recursive manner.
โข Donโt forget to take care of evaluation order corrector.
17. Helpful functions to debug your
hack
โข print(const char *, โฆ)
โข This is actually printf() of standard libc.
โข Accepts the following extra format specifiers:
โข %N (node)
โข %T (type)
โข %E, %J, %H, %L, %O, %S, %V, %Z, %B, %F
18. Roll-up
โข Goโs compiler internals should look complex at first
glance, but it would turn out pretty straightforward
and hacker-friendly ;)