Creating a programming language for fun

2357408ed1ee90461022f24f29a38cbd?s=47 Naren
September 21, 2019

Creating a programming language for fun

Creating an interpreter or a programming language can sound complicated but in reality getting started with it is not that difficult as it sounds – it is a bunch of switch statements and recursive logics. In this talk I am going to give an introduction to interpreter internals and we are going to walk through the high level blocks and the Golang code to create a simple interpreted programming language.
The key take away for the audience will be my lessons from creating a programming language, resources to get started to create one and most importantly having fun:)

2357408ed1ee90461022f24f29a38cbd?s=128

Naren

September 21, 2019
Tweet

Transcript

  1. 4.
  2. 7.

    Lexer Source Code Tokens // Declaring a var "a" let

    a = 5 + 2 * 3 {LET, "let"}, {IDENT, "a"}, {ASSIGN, "="}, {INT, "5"}, {PLUS, "+"}, {INT, "2"}, {ASTERISK, "*"}, {INT, "3"}, Lexer
  3. 8.

    let a = 5 + 2 * 3 Keyword Identifier

    Operator Operator Operator Literal Literal Literal
  4. 11.

    let a = 5 + 2 * 3; “let a

    = 5 + 2 * 3” 0 1 ‘l’ token = {LET, "let"}
  5. 12.

    “let a = 5 + 2 * 3” 3 4

    ‘ ’ token = {LET, "let"} let a = 5 + 2 * 3;
  6. 13.

    “let a = 5 + 2 * 3” 4 5

    ‘a’ token = {IDENT, "a"} let a = 5 + 2 * 3;
  7. 14.

    “let a = 5 + 2 * 3” 6 7

    ‘=’ token = {ASSIGN, "="} let a = 5 + 2 * 3;
  8. 15.

    “let a = 5 + 2 * 3” 8 9

    ‘5’ token = {INT, "5"} let a = 5 + 2 * 3;
  9. 16.

    “let a = 5 + 2 * 3” 10 11

    ‘+’ token = {PLUS, “+"} let a = 5 + 2 * 3;
  10. 17.

    “let a = 5 + 2 * 3” 12 13

    ‘2’ token = {INT, "2"} let a = 5 + 2 * 3;
  11. 18.

    “let a = 5 + 2 * 3” 14 15

    ‘*’ token = {ASTERISK, "*"} let a = 5 + 2 * 3;
  12. 19.

    “let a = 5 + 2 * 3” 16 17

    ‘3’ token = {INT, "3"} let a = 5 + 2 * 3;
  13. 20.

    let a = 5 + 2 * 3; “let a

    = 5 + 2 * 3” 17 17 ‘;’ token = {SEMICOLON, ";"}
  14. 21.

    Lexer Source Code Tokens let a = 5 + 2

    * 3 {LET, "let"}, {IDENT, "a"}, {ASSIGN, "="}, {INT, "5"}, {PLUS, "+"}, {INT, "2"}, {ASTERISK, "*"}, {INT, "3"},
  15. 33.

    Pratt parser 1. Null-Denotation(NUD) or prefix expression -5 2. Left-Denotation(LED)

    or infix expression
 5 + 3
 5 * 3 3. Precedences
 difference-1, sum-2, product-3
  16. 36.

    let a = 5 + 2 * 3 ast.LetStatement {

    name: “a” value: }
  17. 46.

    5 + 2 * 3; ast.IntegerLiteral {5} ast.Expression {operator: +}


    ast.IntegerLiteral {2} ast.Expression {operator: *}
 {2} *
  18. 47.

    5 + 2 * 3; ast.Expression {operator: +}
 ast.Expression {operator:

    *}
 ast.IntegerLiteral {5} ast.IntegerLiteral {2} 3
  19. 48.

    5 + 2 * 3; ast.Expression {operator: +}
 ast.Expression {operator:

    *}
 ast.IntegerLiteral {5} ast.IntegerLiteral {2}
  20. 49.

    5 + 2 * 3; ast.Expression {operator: +}
 ast.Expression {operator:

    *}
 ast.IntegerLiteral {5} ast.IntegerLiteral {2} ast.IntegerLiteral {3}
  21. 50.

    5 + 2 * 3; ast.Expression {operator: +}
 ast.Expression {operator:

    *}
 ast.IntegerLiteral {3} ast.IntegerLiteral {5} ast.IntegerLiteral {2}
  22. 51.

    ast.IntegerLiteral {3} 5 + 2 * 3; ast.Expression {operator: +}


    ast.Expression {operator: *}
 ast.IntegerLiteral {5} ast.IntegerLiteral {2}
  23. 52.

    ast.IntegerLiteral {3} 5 + 2 * 3; ast.Expression {operator: +}


    ast.Expression {operator: *}
 ast.IntegerLiteral {5} ast.IntegerLiteral {2}
  24. 57.

    Object System A way to represent data and keep track

    of them >>> let a = 5 + 2 * 3 >>> a + a
  25. 60.

    let a = 5 + 2 * 3 ast.IntegerLiteral {3}

    ast.InfixExpression {operator: +}
 ast.InfixExpression {operator: *}
 ast.IntegerLiteral {5} ast.IntegerLiteral {2} ast.LetStatement { name: “a” value: }
  26. 78.

    let a = 5 + 2 * 3 {LET, "let"},

    {IDENT, "a"}, {ASSIGN, "="}, {INT, "5"}, {PLUS, "+"}, {INT, "2"}, {ASTERISK, "*"}, {INT, "3"}, + 5 * 2 3 11 Lexer Parser Evaluator
  27. 80.

    "If you don’t know how compilers work, then you don’t

    know how computers work. If you’re not 100% sure whether you know how compilers work, then you don’t know how they work.” – Steve Yegge You learn more about computers