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

Creating a programming language for fun

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:)

Naren

September 21, 2019
Tweet

More Decks by Naren

Other Decks in Programming

Transcript

  1. 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
  2. let a = 5 + 2 * 3 Keyword Identifier

    Operator Operator Operator Literal Literal Literal
  3. let a = 5 + 2 * 3; “let a

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

    ‘ ’ token = {LET, "let"} let a = 5 + 2 * 3;
  5. “let a = 5 + 2 * 3” 4 5

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

    ‘=’ token = {ASSIGN, "="} let a = 5 + 2 * 3;
  7. “let a = 5 + 2 * 3” 8 9

    ‘5’ token = {INT, "5"} let a = 5 + 2 * 3;
  8. “let a = 5 + 2 * 3” 10 11

    ‘+’ token = {PLUS, “+"} let a = 5 + 2 * 3;
  9. “let a = 5 + 2 * 3” 12 13

    ‘2’ token = {INT, "2"} let a = 5 + 2 * 3;
  10. “let a = 5 + 2 * 3” 14 15

    ‘*’ token = {ASTERISK, "*"} let a = 5 + 2 * 3;
  11. “let a = 5 + 2 * 3” 16 17

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

    = 5 + 2 * 3” 17 17 ‘;’ token = {SEMICOLON, ";"}
  13. Lexer Source Code Tokens let a = 5 + 2

    * 3 {LET, "let"}, {IDENT, "a"}, {ASSIGN, "="}, {INT, "5"}, {PLUS, "+"}, {INT, "2"}, {ASTERISK, "*"}, {INT, "3"},
  14. 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
  15. let a = 5 + 2 * 3 ast.LetStatement {

    name: “a” value: }
  16. 5 + 2 * 3; ast.IntegerLiteral {5} ast.Expression {operator: +}


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

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

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

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

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


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


    ast.Expression {operator: *}
 ast.IntegerLiteral {5} ast.IntegerLiteral {2}
  23. Object System A way to represent data and keep track

    of them >>> let a = 5 + 2 * 3 >>> a + a
  24. 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: }
  25. 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
  26. "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