Sha256: e24716ea08d5bbf1b547b0e77b66637e99e54d7ea9bbafff1097b7e63bd6ef79

Contents?: true

Size: 1.29 KB

Versions: 14

Compression:

Stored size: 1.29 KB

Contents

module Opal
  # [Opal::Sexp] is used to build up the syntax tree inside [Opal::Parser]. The
  # compiler then steps through the sexp trees to generate the javascript code.
  #
  # For example, an array of integers `[1, 2]` might be represented by:
  #
  #     s(:array, s(:int, 1), s(:int, 2))
  #
  class Sexp

    attr_reader :array

    attr_accessor :source

    def initialize(args)
      @array = args
    end

    def type
      @array[0]
    end

    def type=(type)
      @array[0] = type
    end

    def children
      @array[1..-1]
    end

    def method_missing(sym, *args, &block)
      @array.send sym, *args, &block
    end

    def <<(other)
      @array << other
      self
    end

    def push(*parts)
      @array.push(*parts)
      self
    end

    def to_ary
      @array
    end

    def dup
      Sexp.new(@array.dup)
    end

    def ==(other)
      if other.is_a? Sexp
        @array == other.array
      else
        @array == other
      end
    end

    alias eql? ==

    def line
      @source && @source[0]
    end

    def column
      @source && @source[1]
    end

    def inspect
      "(#{@array.map { |e| e.inspect }.join ', '})"
    end

    def pretty_inspect
      "(#{line ? "#{line} " : ''}#{@array.map { |e| e.inspect }.join ', '})"
    end

    alias to_s inspect
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
opal-0.9.0.beta2 lib/opal/parser/sexp.rb
opal-0.9.0.beta1 lib/opal/parser/sexp.rb
opal-0.8.1 lib/opal/parser/sexp.rb
opal-0.8.1.rc1 lib/opal/parser/sexp.rb
opal-wedge-0.9.0.dev lib/opal/parser/sexp.rb
opal-0.8.0 lib/opal/parser/sexp.rb
opal-0.8.0.rc3 lib/opal/parser/sexp.rb
opal-0.8.0.rc2 lib/opal/parser/sexp.rb
opal-0.8.0.rc1 lib/opal/parser/sexp.rb
opal-0.8.0.beta1 lib/opal/parser/sexp.rb
opal-0.7.2 lib/opal/parser/sexp.rb
opal-0.7.1 lib/opal/parser/sexp.rb
opal-0.7.0 lib/opal/parser/sexp.rb
opal-0.7.0.rc1 lib/opal/parser/sexp.rb