A toy dynamic programming language written in Ruby
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

function_call_spec.rb 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. RSpec.describe AST::FunctionCall do
  2. it 'evaluates built-in functions' do
  3. expect {
  4. AST::FunctionCall.new(
  5. AST::Identifier.new('print'),
  6. [AST::String.new('hello world')]
  7. ).execute(Environment.new)
  8. }.to output("hello world\n").to_stdout
  9. end
  10. it 'evaluates user-defined functions' do
  11. env = Environment.new
  12. AST::FunctionDefinition.new(
  13. AST::Identifier.new('add_one'),
  14. [AST::Identifier.new('n')],
  15. AST::Block.new([
  16. AST::Binary.new(
  17. AST::Operators::ADD,
  18. AST::Identifier.new('n'),
  19. AST::Number.new(1.0)
  20. )
  21. ])
  22. ).execute(env)
  23. expect(
  24. AST::FunctionCall.new(
  25. AST::Identifier.new('add_one'),
  26. [AST::Number.new(5.0)]
  27. ).execute(env)
  28. ).to eq(6.0)
  29. end
  30. # This corresponds to the program
  31. # function factorial(n) {
  32. # if n == 0 {
  33. # 1;
  34. # } else {
  35. # n * factorial(n - 1);
  36. # }
  37. # }
  38. # factorial(5);
  39. it 'evaluates recursive functions' do
  40. env = Environment.new
  41. AST::FunctionDefinition.new(
  42. AST::Identifier.new('factorial'),
  43. [AST::Identifier.new('n')],
  44. AST::Block.new([
  45. AST::Conditional.new(
  46. [
  47. AST::Branch.new(
  48. AST::Binary.new(
  49. AST::Operators::DOUBLE_EQUALS,
  50. AST::Identifier.new('n'),
  51. AST::Number.new(0.0),
  52. ),
  53. AST::Block.new([
  54. AST::Number.new(1.0),
  55. ])
  56. ),
  57. AST::Branch.new(
  58. AST::Boolean.new(true),
  59. AST::Block.new([
  60. AST::Binary.new(
  61. AST::Operators::MULTIPLY,
  62. AST::Identifier.new('n'),
  63. AST::FunctionCall.new(
  64. AST::Identifier.new('factorial'),
  65. [
  66. AST::Binary.new(
  67. AST::Operators::SUBTRACT,
  68. AST::Identifier.new('n'),
  69. AST::Number.new(1.0),
  70. )
  71. ]
  72. )
  73. )
  74. ])
  75. ),
  76. ]
  77. )
  78. ])
  79. ).execute(env)
  80. expect(
  81. AST::FunctionCall.new(
  82. AST::Identifier.new('factorial'),
  83. [AST::Number.new(5.0)]
  84. ).execute(env)
  85. ).to eq(120.0)
  86. end
  87. end