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
- #add_to_interpreter_boolean_functions ⇒ Object
- #add_to_interpreter_char_functions ⇒ Object
- #add_to_interpreter_decimal_functions ⇒ Object
- #add_to_interpreter_integer_functions ⇒ Object
- #add_to_interpreter_list_functions ⇒ Object
- #add_to_interpreter_string_functions ⇒ Object
- #def_function(id, *params) ⇒ Object
- #def_object_message(object, message) ⇒ Object
- #def_var(id, value) ⇒ Object
- #object(expr) ⇒ Object
- #plambda(*params) ⇒ Object
Instance Method Details
#add_to_interpreter_boolean_functions ⇒ Object
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 "{boolean: x}", :_and do |object| plambda "{boolean: y}" do |value| PObject.new(:boolean, [(object.params[0] and value[0].params[0])]) end end "{boolean: x}", :_or do |object| plambda "{boolean: y}" do |value| PObject.new(:boolean, [(object.params[0] or value[0].params[0])]) end end "{boolean: x}", :_not do |object| plambda do PObject.new(:boolean, [(not object.params[0])]) end end end |
#add_to_interpreter_char_functions ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/vm/core/pchar.rb', line 4 def add_to_interpreter_char_functions "{char: x}", :ord do |object| plambda do |value| PObject.new(:integer, [object.params[0].ord]) end end end |
#add_to_interpreter_decimal_functions ⇒ Object
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 "{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 "{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 "{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 "{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 "{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 "{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 "{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 "{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 "{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 "{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 "{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_functions ⇒ Object
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 "{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 "{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 "{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 "{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 "{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 "{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 "{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 "{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 "{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 "{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 "{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_functions ⇒ Object
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 "{list: x, xs}", :head do |object| plambda do |value| object.params[0] end end "{list: x, xs}", :tail do |object| plambda do |value| object.params[1] end end "{list: x, xs}", :concat do |object| plambda "x" do |value| PObject.new(:list, [value[0], object]) end end "{empty}", :concat do |object| plambda "x" do |value| PObject.new(:list, [value[0], object]) end end end |
#add_to_interpreter_string_functions ⇒ Object
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 "{string: x}", :at do |object| plambda "{integer: y}" do |value| PObject.new(:char, [object.params[0][value[0].params[0]]]) end end "{string: x}", :concat do |object| plambda "{string: y}" do |value| PObject.new(:string, [object.params[0] + value[0].params[0]]) end end "x", :to_string do |object| plambda do |value| PObject.new(:string, [object.to_s]) end end "{string: x}", :to_integer do |object| plambda do |value| PObject.new(:integer, [object.params[0].to_i]) end end "{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 (object, ) lamb = plambda(object, "{#{.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 |