Module: PLang::VM::PFunctions

Included in:
Interpreter
Defined in:
lib/vm/core/pchar.rb,
lib/vm/core/plist.rb,
lib/vm/pfunctions.rb,
lib/vm/core/pstring.rb,
lib/vm/core/pboolean.rb,
lib/vm/core/pdecimal.rb,
lib/vm/core/pinteger.rb

Instance Method Summary collapse

Instance Method Details

#add_to_interpreter_boolean_functionsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/vm/core/pboolean.rb', line 4

def add_to_interpreter_boolean_functions
  
  def_object_message "{boolean: x}", :_and do |object|
    plambda "{boolean: y}" do |value|
      PObject.new(:boolean, [(object.params[0] and value[0].params[0])])
    end
  end
  
  def_object_message "{boolean: x}", :_or do |object|
    plambda "{boolean: y}" do |value|
      PObject.new(:boolean, [(object.params[0] or value[0].params[0])])
    end
  end
  
  def_object_message "{boolean: x}", :_not do |object|
    plambda do
      PObject.new(:boolean, [(not object.params[0])])
    end
  end
  
end

#add_to_interpreter_char_functionsObject



4
5
6
7
8
9
10
# File 'lib/vm/core/pchar.rb', line 4

def add_to_interpreter_char_functions
  def_object_message "{char: x}", :ord do |object|
    plambda do |value|
      PObject.new(:integer, [object.params[0].ord])
    end
  end
end

#add_to_interpreter_decimal_functionsObject



4
5
6
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/vm/core/pdecimal.rb', line 4

def add_to_interpreter_decimal_functions
  
  def_object_message "{decimal: x}", :add do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:decimal, [object.params[0] + value[0].params[0]])
        else
          raise "TODO: decimal"
      end
    end
  end
  
  def_object_message "{decimal: x}", :sub do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:decimal, [object.params[0] - value[0].params[0]])
        else
          raise "TODO: decimal"
      end
    end
  end
  
  def_object_message "{decimal: x}", :mul do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:decimal, [object.params[0] * value[0].params[0]])
        else
          raise "TODO: decimal"
      end
    end
  end
  
  def_object_message "{decimal: x}", :div do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:decimal, [object.params[0] / value[0].params[0]])
        else
          raise "TODO: decimal"
      end
    end
  end
  
  def_object_message "{decimal: x}", :mod do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:decimal, [object.params[0] % value[0].params[0]])
        else
          raise "TODO: decimal"
      end
    end
  end
  
  def_object_message "{decimal: x}", :major do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:boolean, [(object.params[0] > value[0].params[0]).to_s.to_sym])
        else
          raise "TODO: decimal"
      end
    end
  end
  
  def_object_message "{decimal: x}", :major_equal do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:boolean, [(object.params[0] >= value[0].params[0]).to_s.to_sym])
        else
          raise "TODO: decimal"
      end
    end
  end
  
  def_object_message "{decimal: x}", :minor do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:boolean, [(object.params[0] < value[0].params[0]).to_s.to_sym])
        else
          raise "TODO: decimal"
      end
    end
  end
  
  def_object_message "{decimal: x}", :minor_equal do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:boolean, [(object.params[0] <= value[0].params[0]).to_s.to_sym])
        else
          raise "TODO: decimal"
      end
    end
  end
  
  def_object_message "{decimal: x}", :equal do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:boolean, [(object.params[0] == value[0].params[0]).to_s.to_sym])
        else
          raise "TODO: decimal"
      end
    end
  end
  
  def_object_message "{decimal: x}", :diff do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:boolean, [(object.params[0] != value[0].params[0]).to_s.to_sym])
        else
          raise "TODO: decimal"
      end
    end
  end
  
end

#add_to_interpreter_integer_functionsObject



4
5
6
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/vm/core/pinteger.rb', line 4

def add_to_interpreter_integer_functions
  
  def_object_message "{integer: x}", :add do |object|
    plambda "y" do |value|
      if value[0].id == :integer
        PObject.new(:integer, [object.params[0] + value[0].params[0]])
      elsif value[0].id == :decimal
        PObject.new(:decimal, [object.params[0] + value[0].params[0]])
      end
    end
  end
  
  def_object_message "{integer: x}", :sub do |object|
    plambda "y" do |value|
      if value[0].id == :integer
        PObject.new(:integer, [object.params[0] - value[0].params[0]])
      elsif value[0].id == :decimal
        PObject.new(:decimal, [object.params[0] - value[0].params[0]])
      end
    end
  end
  
  def_object_message "{integer: x}", :mul do |object|
    plambda "y" do |value|
      if value[0].id == :integer
        PObject.new(:integer, [object.params[0] * value[0].params[0]])
      elsif value[0].id == :decimal
        PObject.new(:decimal, [object.params[0] * value[0].params[0]])
      end
    end
  end
  
  def_object_message "{integer: x}", :div do |object|
    plambda "y" do |value|
      if value[0].id == :integer
        PObject.new(:integer, [object.params[0] / value[0].params[0]])
      elsif value[0].id == :decimal
        PObject.new(:decimal, [object.params[0] / value[0].params[0]])
      end
    end
  end
  
  def_object_message "{integer: x}", :mod do |object|
    plambda "y" do |value|
      if value[0].id == :integer
        PObject.new(:integer, [object.params[0] % value[0].params[0]])
      elsif value[0].id == :decimal
        PObject.new(:decimal, [object.params[0] % value[0].params[0]])
      end
    end
  end
  
  def_object_message "{integer: x}", :major do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:boolean, [(object.params[0] > value[0].params[0]).to_s.to_sym])
        else
          raise "TODO: integer"
      end
    end
  end
  
  def_object_message "{integer: x}", :major_equal do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:boolean, [(object.params[0] >= value[0].params[0]).to_s.to_sym])
        else
          raise "TODO: integer"
      end
    end
  end
  
  def_object_message "{integer: x}", :minor do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:boolean, [(object.params[0] < value[0].params[0]).to_s.to_sym])
        else
          raise "TODO: integer"
      end
    end
  end
  
  def_object_message "{integer: x}", :minor_equal do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:boolean, [(object.params[0] <= value[0].params[0]).to_s.to_sym])
        else
          raise "TODO: integer"
      end
    end
  end
  
  def_object_message "{integer: x}", :equal do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:boolean, [(object.params[0] == value[0].params[0]).to_s.to_sym])
        else
          raise "TODO: integer"
      end
    end
  end
  
  def_object_message "{integer: x}", :diff do |object|
    plambda "y" do |value|
      case value[0].id
        when :integer, :decimal
          PObject.new(:boolean, [(object.params[0] != value[0].params[0]).to_s.to_sym])
        else
          raise "TODO: integer"
      end
    end
  end
  
end

#add_to_interpreter_list_functionsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vm/core/plist.rb', line 4

def add_to_interpreter_list_functions

  def_object_message "{list: x, xs}", :head do |object|
    plambda do |value|
      object.params[0]
    end
  end

  def_object_message "{list: x, xs}", :tail do |object|
    plambda do |value|
      object.params[1]
    end
  end

  def_object_message "{list: x, xs}", :concat do |object|
    plambda "x" do |value|
      PObject.new(:list, [value[0], object])
    end
  end

  def_object_message "{empty}", :concat do |object|
    plambda "x" do |value|
      PObject.new(:list, [value[0], object])
    end
  end

end

#add_to_interpreter_string_functionsObject



4
5
6
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
# File 'lib/vm/core/pstring.rb', line 4

def add_to_interpreter_string_functions
  
  def_object_message "{string: x}", :at do |object|
    plambda "{integer: y}" do |value|
      PObject.new(:char, [object.params[0][value[0].params[0]]])
    end
  end
  
  def_object_message "{string: x}", :concat do |object|
    plambda "{string: y}" do |value|
      PObject.new(:string, [object.params[0] + value[0].params[0]])
    end
  end
  
  def_object_message "x", :to_string do |object|
    plambda do |value|
      PObject.new(:string, [object.to_s])
    end
  end

  def_object_message "{string: x}", :to_integer do |object|
    plambda do |value|
      PObject.new(:integer, [object.params[0].to_i])
    end
  end

  def_object_message "{string: x}", :to_decimal do |object|
    plambda do |value|
      PObject.new(:decimal, [object.params[0].to_f])
    end
  end
  
end

#def_function(id, *params) ⇒ Object



5
6
7
8
9
# File 'lib/vm/pfunctions.rb', line 5

def def_function(id, *params)
  def_var(id, (plambda(*params) do |values|
    yield(values)
  end))
end

#def_object_message(object, message) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/vm/pfunctions.rb', line 11

def def_object_message(object, message)
  lamb = plambda(object, "{#{message.to_s}}") do |values|
    yield(values[0], values[1])
  end
  begin
    @env.set_var(:get_object_message, lamb)
  rescue
    @env.add_lambda(:get_object_message, lamb)
  end
end

#def_var(id, value) ⇒ Object



22
23
24
# File 'lib/vm/pfunctions.rb', line 22

def def_var(id, value)
  @env.set_var(id.to_sym, value)
end

#object(expr) ⇒ Object



46
47
48
# File 'lib/vm/pfunctions.rb', line 46

def object(expr)
  PLang::Parser::SyntaxAnalyser.new(expr).parse[0]
end

#plambda(*params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vm/pfunctions.rb', line 26

def plambda(*params)
  lamb = PLambda.new do |values|
    yield(values)
  end
  
  obj_params = []
  params.each do |param|
    param = object(param)
    case param.type
      when :id
        lamb.form <<  nil
      else
        lamb.form << param
    end
    obj_params << param
  end
          
  PObject.new(:lambda, [lamb, PObject.new(:empty, [])])
end