310

# 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

## Transcript

1. 1.

2. 2.

### Naren Independent Solution Architect  Privacy Enthusiast Amateur Systems Programmer  @DudeWhoCode

www.dudewho.codes
3. 3.

4. 4.
5. 5.

6. 6.

7. 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
8. 8.

### let a = 5 + 2 * 3 Keyword Identiﬁer

Operator Operator Operator Literal Literal Literal
9. 9.

10. 10.

11. 11.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

### Lexer Source Code Tokens let a = 5 + 2

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

23. 23.

### Abstract Syntax Tree Abstract the syntax and store it in

a tree data structure
24. 24.

2, *, 3]
25. 25.

26. 26.

Program
27. 27.

Statement
28. 28.

Statement
29. 29.

30. 30.

3
31. 31.

32. 32.

33. 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
34. 34.

35. 35.

### 1. prefixFn() a, integers 2. infixFn(left)    +, * let

a = 5 + 2 * 3
36. 36.

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

name: “a” value: }
37. 37.

38. 38.

39. 39.

40. 40.

41. 41.

{5} +
42. 42.

2
43. 43.

44. 44.

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

ast.IntegerLiteral {2}
45. 45.

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

ast.IntegerLiteral {2} 2 3
46. 46.

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

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

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

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

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

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

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

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

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

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

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

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

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

ast.Expression {operator: *}  ast.IntegerLiteral {5} ast.IntegerLiteral {2}
53. 53.

54. 54.

55. 55.

JIT
56. 56.

57. 57.

### Object System A way to represent data and keep track

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

59. 59.

60. 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: }
61. 61.

62. 62.

### ast.IntegerLiteral {3} ast.InfixExpression {operator: +}  ast.InfixExpression {operator: *}  ast.IntegerLiteral {5}

ast.IntegerLiteral {2} ast.LetStatement { name: “a” value: }
63. 63.

### ast.InfixExpression {operator: +}  ast.IntegerLiteral {3} ast.InfixExpression {operator: *}  ast.IntegerLiteral {2}

ast.IntegerLiteral {5}
64. 64.

### ast.InfixExpression {operator: +}  ast.IntegerLiteral {3} ast.InfixExpression {operator: *}  ast.IntegerLiteral {2}

ast.IntegerLiteral {5}
65. 65.

### ast.InfixExpression {operator: +}  object.Integer {5} ast.IntegerLiteral {3} ast.InfixExpression {operator: *}

ast.IntegerLiteral {2}
66. 66.

### ast.InfixExpression {operator: +}  object.Integer {5} ast.IntegerLiteral {3} ast.InfixExpression {operator: *}

ast.IntegerLiteral {2}
67. 67.

### ast.InfixExpression {operator: +}  object.Integer {5} ast.IntegerLiteral {3} ast.InfixExpression {operator: *}

ast.IntegerLiteral {2}
68. 68.

### ast.InfixExpression {operator: +}  object.Integer {5} ast.IntegerLiteral {3} ast.InfixExpression {operator: *}

ast.IntegerLiteral {2}
69. 69.

### ast.InfixExpression {operator: +}  object.Integer {5} ast.IntegerLiteral {3} ast.InfixExpression {operator: *}

object.Integer {2}
70. 70.

### ast.InfixExpression {operator: +}  object.Integer {5} ast.IntegerLiteral {3} ast.InfixExpression {operator: *}

object.Integer {2}
71. 71.

### ast.InfixExpression {operator: +}  object.Integer {5} object.Integer {3} ast.InfixExpression {operator: *}

object.Integer {2}
72. 72.

### ast.InfixExpression {operator: +}  object.Integer {5} object.Integer {3} ast.InfixExpression {operator: *}

object.Integer {2}
73. 73.

74. 74.

75. 75.

76. 76.

77. 77.

78. 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
79. 79.

80. 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
81. 81.

engines
82. 82.

83. 83.

84. 84.

85. 85.

### References Structure and Interpretation of Computer Programs https:/ /dev.to/jrop/pratt-parsing https:/

/tdop.github.io
86. 86.