Class: RubyLisp::Arity

Inherits:
Object
  • Object
show all
Defined in:
lib/rubylisp/function.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast) ⇒ Arity

Returns a new instance of Arity.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubylisp/function.rb', line 7

def initialize(ast)
  unless list? ast
    raise RuntimeError,
      "Invalid signature #{arity}; expected a list."
  end

  bindings, *body = ast

  unless vector? bindings
    raise RuntimeError,
      "Bindings must be a vector; got #{body.class}."
  end

  bindings.each do |binding|
    unless binding.class == Symbol
      raise RuntimeError,
        "Each binding must be a symbol; got #{binding.class}."
    end
  end

  # bindings is now an array of strings (symbol names)
  bindings = bindings.map(&:value)

  ampersand_indices = bindings.to_list.indices {|x| x == '&'}
  if ampersand_indices.any? {|i| i != bindings.count - 2}
    raise RuntimeError,
      "An '&' can only occur right before the last binding."
  end

  @required_args = bindings.take_while {|binding| binding != '&'}
  @rest_args = bindings.drop_while {|binding| binding != '&'}.drop(1)
  @body = body
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



5
6
7
# File 'lib/rubylisp/function.rb', line 5

def body
  @body
end

#required_argsObject

Returns the value of attribute required_args.



5
6
7
# File 'lib/rubylisp/function.rb', line 5

def required_args
  @required_args
end

#rest_argsObject

Returns the value of attribute rest_args.



5
6
7
# File 'lib/rubylisp/function.rb', line 5

def rest_args
  @rest_args
end