Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Let’s Create An Interpreter In Go

Let’s Create An Interpreter In Go

Go Conference 2017 Autumn

Kenshi Kamata

November 05, 2017
Tweet

More Decks by Kenshi Kamata

Other Decks in Programming

Transcript

  1. Self Introduce • Kenshi Kamata • @knsh14 (twitter, GitHub) •

    KLab inc. ◦ Unity3d Editor Extension ◦ Create Facebook Instant Game
  2. What I did? • Read Writing An Interpreter In Go

    ◦ https://interpreterbook.com/ • Actually Create An Interpreter • Create An Interpreter in about 1400 lines! ◦ Included test code, about 2500 lines
  3. DEMO let x = 0; let array = [0, 10,

    20, 30, 40]; array[1]; let fib = fn(x) { if(x == 0) { 0; } else { if (x == 1) { 1;} else { fib(x-1) + fib(x-2);}}}; let loop = fn(i, max, f) { if(i == max){return 0;} f(); loop(i +1, max, f);}; let p = fn() { puts(“hello world”);};
  4. Why I did? • I want to create self-produced Language

    • Tried “Writing An Interpreter In C” book ◦ but it was so hard and gave up • I heard good book by Go is released! so I decided to try again.
  5. Specification • Monkey Language • Integer, Boolean, String, Function •

    Array, Hash • Able to use if statement ◦ No For loop • No yacc, use self-implemented Lexer and Parser
  6. How long Until Finished • About 1 month • some

    finished in one week • Most of people can finish within 1.5 month
  7. Hard Part • Parser logic was hard • Pratt Parser

    • google 演算子順位法 in japanese
  8. What I learned • Fun to make Language by myself!

    • Understand AST a little through Parser Chapter • Not Following Go Practice in Some part
  9. Understand AST • Build AST in Parser Chapter • Wrote

    A tool to check cyclomatic complexity with AST in past but it was poor code • I must be able to write better code after reading Parser chapter
  10. Not Following Go Practice • Return Error ASAP / Return

    Error type • Not good practice in Interpreter • Example Code in Monkey • Actually, go/parser has similar code • https://golang.org/src/go/parser/parser.go filter by “p.error”, you can see no handling error
  11. Not following Go practice • Don’t want to stop processing

    • For user experience, Parse to End then show all error messages • append to error string list, so not need to handle error at the time
  12. Finally • Great feeling to exec code on my own

    interpreter ◦ different feeling to playing my own game • Good for Japanese people ◦ Easy English ◦ Understand by English after Understanding by Code