Class: Nydp::Builtin::Plus

Inherits:
Object show all
Includes:
Base, Singleton
Defined in:
lib/nydp/builtin/plus.rb

Instance Method Summary collapse

Methods included from Base

#handle_error, #inspect, #invoke, #invoke_1, #invoke_2, #invoke_3, #invoke_4, #nydp_type, #to_s

Methods included from Helper

#cons, #list, #literal?, #pair?, #sig, #sym, #sym?

Methods included from Converter

#n2r, #r2n

Instance Method Details

#builtin_invoke(vm, args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/nydp/builtin/plus.rb', line 9

def builtin_invoke vm, args
  vm.push_arg case args.car
              when Nydp::Pair
                sum(args, Nydp::NIL)
              when String
                string_concat("", args)
              else
                sum(args.cdr, args.car)
              end
end

#builtin_invoke_1(vm) ⇒ Object



4
# File 'lib/nydp/builtin/plus.rb', line 4

def builtin_invoke_1 vm             ; vm.push_arg 0             ; end

#builtin_invoke_2(vm, a) ⇒ Object



5
# File 'lib/nydp/builtin/plus.rb', line 5

def builtin_invoke_2 vm, a          ; vm.push_arg a             ; end

#builtin_invoke_3(vm, a0, a1) ⇒ Object



6
# File 'lib/nydp/builtin/plus.rb', line 6

def builtin_invoke_3 vm, a0, a1     ; vm.push_arg(a0 + a1)      ; end

#builtin_invoke_4(vm, a0, a1, a2) ⇒ Object



7
# File 'lib/nydp/builtin/plus.rb', line 7

def builtin_invoke_4 vm, a0, a1, a2 ; vm.push_arg(a0 + a1 + a2) ; end

#nameObject



36
# File 'lib/nydp/builtin/plus.rb', line 36

def name ; "+" ; end

#string_concat(init, others) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/nydp/builtin/plus.rb', line 20

def string_concat init, others
  while others && !Nydp::NIL.is?(others)
    init << others.car.to_s
    others = others.cdr
  end
  init
end

#sum(args, accum) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/nydp/builtin/plus.rb', line 28

def sum args, accum
  while args && !Nydp::NIL.is?(args)
    accum += args.car
    args = args.cdr
  end
  accum
end