Sha256: 9a3a538fd7830b48c899ee8ec5007fcc0e6e3b4d64f69b8092cc534d1a224c73

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

require File.dirname(__FILE__) + "/tickly/parser"
require File.dirname(__FILE__) + "/tickly/evaluator"
require 'forwardable'

module Tickly
  VERSION = '0.0.1'
  
  # Represents a TCL expression with it's arguments (in curly or square braces)
  class Expr
    extend Forwardable
    
    def_delegators :@e, :push, :<<, :any?, :reject!, :map!, :[], :delete_at, :include?, :each, :empty?, :join, :length
    
    def initialize(elements = [])
      @e = elements
    end
    
    def map(&blk)
      self.class.new(@e.map(&blk))
    end
    
    def to_a
      @e
    end
    
    def ==(another)
      another.to_a == to_a
    end
    
    def inspect
      @e.map{|e| e.inspect }.join(', ')
    end
    
  end
  
  # Represents an expression between curly braces (within which no text substitution will be done)
  # like  { 1 2 3 }
  class LiteralExpr < Expr
    def inspect
      "le(%s)" % super
    end
  end

  # Represents an expression between square brackets (where text substitution will be done)
  # like  [1 2 3]
  class StringExpr < Expr
    def inspect
      "se(%s)" % super
    end
  end
  
  # Provides the methods for quickly emitting the LiteralExpr and StringExpr objects
  module Emitter
    def le(*elems)
      LiteralExpr.new(elems)
    end
    
    def se(*elems)
      LiteralExpr.new(elems)
    end
  end
  
  
  def self.to_tcl(e)
    if e.is_a?(Tickly::LiteralExpr)
      '{%s}' % e.map{|e| to_tcl(e)}.join(' ')
    elsif e.is_a?(Tickly::StringExpr)
      '[%s]' % e.map{|e| to_tcl(e)}.join(' ')
    elsif e.is_a?(String) && (e.include?('"') || e.include?("'"))
      e.inspect
    else
      e.to_s
    end
  end
  
  def self.split_array(arr, separator = nil)
    return arr unless arr.include?(separator)
    
    subarrays = arr.class.new
    subarrays.push(arr.class.new)
    
    arr.each do | element |
      if element == separator
        subarrays.push(arr.class.new)
      else
        subarrays[-1].push(element)
      end
    end
    return subarrays
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tickly-0.0.1 lib/tickly.rb