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

Let’s create a programming language! [RUG::B edition]

Let’s create a programming language! [RUG::B edition]

Denis Defreyne

June 02, 2016
Tweet

More Decks by Denis Defreyne

Other Decks in Programming

Transcript

  1. 19 DIGIT = … NUMBER = … EXPRESSION = …

    EXPRESSIONS = intersperse( EXPRESSION, char("\n").ignore, ).compact
  2. 20 DIGIT = … NUMBER = …
 EXPRESSION = …

    EXPRESSIONS = … PROGRAM = seq(EXPRESSIONS, eof) .first
 

  3. 34 FUNCTION_CALL = seq( FUNCTION_NAME, char('(').ignore, intersperse( lazy { EXPRESSION

    }, char(',').ignore, ).compact, char(')').ignore, ).compact
  4. 41 def eval_function_call(expr) case expr[0] when 'add' expr[1] .map {

    |e| eval_expr(e) } .reduce(:+) when 'mul' … end end
  5. 48 DIGIT = … NUMBER = repeat1(DIGIT) .capture .map {

    |d| IntNode.new(d.to_i) } 
 
 
 

  6. 53 def eval_function_call(expr) case expr[0] when 'add' expr[1] .map {

    |e| eval_expr(e) } .reduce(:+) when 'mul' … end end
  7. 54 def eval_function_call(expr) case expr.name when 'add' expr.args .map {

    |e| eval_expr(e) } .reduce(:+) when 'mul' … end end
  8. 58 def eval_function_call(expr) case expr.name when 'add' expr.args .map {

    |e| eval_expr(e) } .reduce(:+) when 'mul' … end end
  9. 70 def eval_expr(expr) case expr when IntNode expr.value when FunCallNode

    eval_function_call(expr) when VarNode eval_var(expr) end end
  10. 79 ASSIGNMENT = seq( VARIABLE_NAME, char('=').ignore, lazy { EXPRESSION },

    ).compact.map do |data| AssignNode.new(data[0], data[1]) end
  11. 92 def eval_expr(expr, env) case expr when … … when

    AssignNode eval_assign(expr, env) end end
  12. % 102 cat stuff.cke 4173 amount = { a =

    2 b = 3 add(a, b) } print(add(2, amount))
  13. 110 def eval_expr(expr, env) case expr when … … when

    ScopeNode eval_scope(expr, env) end end
  14. 111 def eval_scope(expr, env) init = ValAndEnv.new(0, env) result =

    expr.exprs.reduce(init) do |result, e| eval_expr(e, result.env) end ValAndEnv.new(result.val, env) end
  15. 112 def eval_scope(expr, env) init = ValAndEnv.new(0, env) result =

    expr.exprs.reduce(init) do |result, e| eval_expr(e, result.env) end ValAndEnv.new(result.val, env) end
  16. % 113 cat stuff.cke 4173 amount = { a =

    2 b = 3 add(a, b) } print(add(2, amount))
  17. % 119 cat stuff.cke fun multiply(a, b) { if (b)

    { add(a, multiply(a, sub(b, 1))) } else { 0 } } print(multiply(3, 5))
  18. 123 FUNCTION_DEF = seq( string('fun').ignore, FUNCTION_NAME, char('(').ignore, intersperse( VARIABLE_NAME, char(',').ignore,

    ).compact, char(')').ignore, SCOPE, ).compact.map do |data| FunDefNode.new(data[0], data[1], data[2]) end
  19. 125

  20. 126 def eval_function_call(expr, env) fun = env[expr.name] values = expr.args.map

    { |e| eval_expr(e) } 
 
 
 
 fun.body.call(*values)
  21. 127 def eval_function_call(expr, env) fun = env[expr.name] values = expr.args.map

    { |e| eval_expr(e) } case fun.body when Proc when Scope … fun.body.call(*values)
  22. 127 def eval_function_call(expr, env) fun = env[expr.name] values = expr.args.map

    { |e| eval_expr(e) } case fun.body when Proc when Scope … fun.body.call(*values)
  23. 128

  24. % 129 cat stuff.cke fun multiply(a, b) { if (b)

    { add(a, multiply(a, sub(b, 1))) } else { 0 } } print(multiply(3, 5))
  25. % 131 cat stuff.cke fun multiply(a, b) { if (b)

    { a + multiply(a, b - 1) } else { 0 } } print(multiply(3, 5))
  26. 136

  27. 136 L1 Numbers L2 Functions C1 Abstract syntax C2 Environment

    L3 Variables C3 Print function C4 Immutable environment
  28. 136 L1 Numbers L2 Functions C1 Abstract syntax C2 Environment

    L3 Variables C3 Print function C4 Immutable environment L4 Scopes
  29. 136 L1 Numbers L2 Functions C1 Abstract syntax C2 Environment

    L3 Variables C3 Print function C4 Immutable environment L4 Scopes L5 Conditionals
  30. 136 L1 Numbers L2 Functions C1 Abstract syntax C2 Environment

    L3 Variables C3 Print function C4 Immutable environment L4 Scopes L5 Conditionals L6 Function definitions
  31. 136 L1 Numbers L2 Functions C1 Abstract syntax C2 Environment

    L3 Variables C3 Print function C4 Immutable environment L4 Scopes L5 Conditionals L6 Function definitions L7 Operators