lib/vm/pobject.rb in p-lang-0.1.1 vs lib/vm/pobject.rb in p-lang-0.2.0

- old
+ new

@@ -1,47 +1,49 @@ module PLang - class PObject - attr_accessor :type - attr_accessor :params - - def initialize(type, params) - @type = type - @params = params - end - - def ==(obj) - if @type == obj.type - @params.each_with_index do |param, i| - unless param == obj.params[i] - return false - end + module VM + class PObject + attr_reader :id + attr_accessor :params + + def initialize(id, params) + @id = id + @params = params + end + + def to_s + case @id + when :integer, :decimal, :char, :string, :boolean + return params[0] + when :empty + return "'()" + when :list + str = "'(" + params = @params + ok = false + while params and params != [] + str += "#{params[0].to_s}, " + params = params[1].params + ok = true + end + if ok + str[-2] = ')' + else + str += ')' + end + return str.strip + else + str = "{#{@id}" + if @params.length > 0 + str += ": " + @params.each do |param| + str += "#{param.to_s}, " + end + str[-2] = "}" + else + str += "}" + end + return str.strip end - return true - else - return false end end - - def form - case @type - when :integer, :decimal, :boolean, :char, :string - [:literal, @type, @params[0]] - else - [:object, @type, @params.collect(&:form)] - end - end - - def to_s - case @type - when :integer, :decimal, :boolean - @params[0] - when :string - "\"#{@params[0]}\"" - when :char - "\'#{@params[0]}\'" - else - "{#{@type}: #{@params.collect(&:to_s).join(",")}}" - end - end - end -end \ No newline at end of file +end