c4se記:さっちゃんですよ☆

.。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)

.。oO(此のblogは、主に音樂考察Programmingに分類されますよ。ヾ(〃l _ l)ノ゙♬♪♡

音樂はSoundCloud等バラバラの場所に公開中です。申し訳ないがlinkをたどるなどして探してください。

考察は現在は主に此のblogで公表中です。

programmingは、ひろくみせるものはGitHubで、個人的なものはBitBucketで開発中です。

c4se

RubyのS式を調べた。S式をRuby文字列に戻す方法も調べた

昨日の(草稿) Dart風の、constructor引数を自動的にinstance変数に代入するアレをRubyでにて、Ripperを使ひ、RubyのcodeをS式にした。S式の内容を調べた。序でに必要だったので、S式をRuby codeに戻す、簡単な方法も調べた。

cf. How would I recompile Ripper's AST back to Ruby code? - Stack Overflow http://stackoverflow.com/questions/4878032/how-would-i-recompile-rippers-ast-back-to-ruby-code

Sorcererを使ってみた。defの引数辺りにbugが有る様だが、まあ使へる。

確認code

Gemfile

source 'https://rubygems.org'

gem 'sorcerer'

確認code

# coding=utf-8
# license: Public Domain

require 'bundler'
Bundler.require
require 'Ripper'
require 'pp'

$sexps = []
$DEBUG = true

# Ripper + p -> rip + p -> pir
#
# @param source [String]
def pir source
  sexp = Ripper.sexp source
  $sexps << sexp[1][0]
  re_source = Sorcerer.source Ripper::SexpBuilder.new(source).parse
  if ! $DEBUG || source != re_source
    puts source
    pp sexp[1][0]
    puts(source == re_source || re_source)
    puts
  end
end

pir '9'
pir '-9'
pir '9e10'
pir '-9e10'
pir '9e-10'
pir '9.0'
pir '9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999'

pir '"string"'
pir "'string'"
pir '"\t"'
pir "'\\t'"
pir '"string #{v} after"'
pir '"%05d" % 9'

pir ':sym'
pir ':@sym'
pir ':"sym"'
pir ':"#{sym}"'

pir 'nil'

pir 'x'
pir 'x 9'
pir 'x(9)'
pir '9 + x'
pir 'x.m'
pir 'x.m()'
pir 'x.m 9, 8'
pir 'x.m(9, 8)'
pir 'a = 9'
pir 'a.m = 9'
pir 'a[9]'
pir 'a[9, 8]'
pir 'a[9] = 9'

pir 'a, b = 9, 8'
pir 'a, b = *[9, 8]'
pir 'a, b = *a'

pir '[9, 8]'
pir '[9, a]'
pir '[]'

pir '{ :a => 9, "b" => 8 }'
pir '{ a: 9 }'

pir 'def m; end'
pir 'def m a; end'
pir 'def m a=9; end'
pir 'def m a=x; end'
pir 'def m a, b=9, c;end'
pir 'def m a, *r; end'
pir 'def m a, b=9, *r, c, d; end'
pir 'def a, b, &p; end'

pir 'Proc.new { }'
pir 'Proc.new { 9 }'
pir 'Proc.new { |x| 9 }'
pir 'Proc.new { |x, y| 9 }'
pir 'Proc.new { |x, y=9| 9 }'
pir '[9].zip([9]) { |l, r| 9 }'

pir '-> { 9 }'
pir '->(x) { 9 }'
pir '->(x=9) { 9 }'
pir 'lambda { 9 }'

p $sexps.map{|sexp| sexp[0] }.uniq
# vim:set et sw=2 sts=2 ff=unix foldmethod=marker:

結果

9
[:@int, "9", [1, 0]]
true

-9
[:unary, :-@, [:@int, "9", [1, 1]]]
true

9e10
[:@float, "9e10", [1, 0]]
true

-9e10
[:unary, :-@, [:@float, "9e10", [1, 1]]]
true

9e-10
[:@float, "9e-10", [1, 0]]
true

9.0
[:@float, "9.0", [1, 0]]
true

9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
[:@int,
 "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",
 [1, 0]]
true

"string"
[:string_literal, [:string_content, [:@tstring_content, "string", [1, 1]]]]
true

'string'
[:string_literal, [:string_content, [:@tstring_content, "string", [1, 1]]]]
"string"

"\t"
[:string_literal, [:string_content, [:@tstring_content, "\\t", [1, 1]]]]
true

'\t'
[:string_literal, [:string_content, [:@tstring_content, "\\t", [1, 1]]]]
"\t"

"string #{v} after"
[:string_literal,
 [:string_content,
  [:@tstring_content, "string ", [1, 1]],
  [:string_embexpr, [[:vcall, [:@ident, "v", [1, 10]]]]],
  [:@tstring_content, " after", [1, 12]]]]
true

"%05d" % 9
[:binary,
 [:string_literal, [:string_content, [:@tstring_content, "%05d", [1, 1]]]],
 :%,
 [:@int, "9", [1, 9]]]
true

:sym
[:symbol_literal, [:symbol, [:@ident, "sym", [1, 1]]]]
true

:@sym
[:symbol_literal, [:symbol, [:@ivar, "@sym", [1, 1]]]]
true

:"sym"
[:dyna_symbol, [[:@tstring_content, "sym", [1, 2]]]]
true

:"#{sym}"
[:dyna_symbol, [[:string_embexpr, [[:vcall, [:@ident, "sym", [1, 4]]]]]]]
true

nil
[:var_ref, [:@kw, "nil", [1, 0]]]
true

x
[:vcall, [:@ident, "x", [1, 0]]]
true

x 9
[:command,
 [:@ident, "x", [1, 0]],
 [:args_add_block, [[:@int, "9", [1, 2]]], false]]
true

x(9)
[:method_add_arg,
 [:fcall, [:@ident, "x", [1, 0]]],
 [:arg_paren, [:args_add_block, [[:@int, "9", [1, 2]]], false]]]
true

9 + x
[:binary, [:@int, "9", [1, 0]], :+, [:vcall, [:@ident, "x", [1, 4]]]]
true

x.m
[:call, [:vcall, [:@ident, "x", [1, 0]]], :".", [:@ident, "m", [1, 2]]]
true

x.m()
[:method_add_arg,
 [:call, [:vcall, [:@ident, "x", [1, 0]]], :".", [:@ident, "m", [1, 2]]],
 [:arg_paren, nil]]
true

x.m 9, 8
[:command_call,
 [:vcall, [:@ident, "x", [1, 0]]],
 :".",
 [:@ident, "m", [1, 2]],
 [:args_add_block, [[:@int, "9", [1, 4]], [:@int, "8", [1, 7]]], false]]
true

x.m(9, 8)
[:method_add_arg,
 [:call, [:vcall, [:@ident, "x", [1, 0]]], :".", [:@ident, "m", [1, 2]]],
 [:arg_paren,
  [:args_add_block, [[:@int, "9", [1, 4]], [:@int, "8", [1, 7]]], false]]]
true

a = 9
[:assign, [:var_field, [:@ident, "a", [1, 0]]], [:@int, "9", [1, 4]]]
true

a.m = 9
[:assign,
 [:field, [:vcall, [:@ident, "a", [1, 0]]], :".", [:@ident, "m", [1, 2]]],
 [:@int, "9", [1, 6]]]
true

a[9]
[:aref,
 [:vcall, [:@ident, "a", [1, 0]]],
 [:args_add_block, [[:@int, "9", [1, 2]]], false]]
true

a[9, 8]
[:aref,
 [:vcall, [:@ident, "a", [1, 0]]],
 [:args_add_block, [[:@int, "9", [1, 2]], [:@int, "8", [1, 5]]], false]]
true

a[9] = 9
[:assign,
 [:aref_field,
  [:vcall, [:@ident, "a", [1, 0]]],
  [:args_add_block, [[:@int, "9", [1, 2]]], false]],
 [:@int, "9", [1, 7]]]
true

a, b = 9, 8
[:massign,
 [[:@ident, "a", [1, 0]], [:@ident, "b", [1, 3]]],
 [:mrhs_new_from_args, [[:@int, "9", [1, 7]]], [:@int, "8", [1, 10]]]]
true

a, b = *[9, 8]
[:massign,
 [[:@ident, "a", [1, 0]], [:@ident, "b", [1, 3]]],
 [:mrhs_add_star, [], [:array, [[:@int, "9", [1, 9]], [:@int, "8", [1, 12]]]]]]
true

a, b = *a
[:massign,
 [[:@ident, "a", [1, 0]], [:@ident, "b", [1, 3]]],
 [:mrhs_add_star, [], [:var_ref, [:@ident, "a", [1, 8]]]]]
true

[9, 8]
[:array, [[:@int, "9", [1, 1]], [:@int, "8", [1, 4]]]]
true

[9, a]
[:array, [[:@int, "9", [1, 1]], [:vcall, [:@ident, "a", [1, 4]]]]]
true

[]
[:array, nil]
true

{ :a => 9, "b" => 8 }
[:hash,
 [:assoclist_from_args,
  [[:assoc_new,
    [:symbol_literal, [:symbol, [:@ident, "a", [1, 3]]]],
    [:@int, "9", [1, 8]]],
   [:assoc_new,
    [:string_literal, [:string_content, [:@tstring_content, "b", [1, 12]]]],
    [:@int, "8", [1, 18]]]]]]
true

{ a: 9 }
[:hash,
 [:assoclist_from_args,
  [[:assoc_new, [:@label, "a:", [1, 2]], [:@int, "9", [1, 5]]]]]]
true

def m; end
[:def,
 [:@ident, "m", [1, 4]],
 [:params, nil, nil, nil, nil, nil, nil, nil],
 [:bodystmt, [[:void_stmt]], nil, nil, nil]]
true

def m a; end
[:def,
 [:@ident, "m", [1, 4]],
 [:params, [[:@ident, "a", [1, 6]]], nil, nil, nil, nil, nil, nil],
 [:bodystmt, [[:void_stmt]], nil, nil, nil]]
true

def m a=9; end
[:def,
 [:@ident, "m", [1, 4]],
 [:params,
  nil,
  [[[:@ident, "a", [1, 6]], [:@int, "9", [1, 8]]]],
  nil,
  nil,
  nil,
  nil,
  nil],
 [:bodystmt, [[:void_stmt]], nil, nil, nil]]
true

def m a=x; end
[:def,
 [:@ident, "m", [1, 4]],
 [:params,
  nil,
  [[[:@ident, "a", [1, 6]], [:vcall, [:@ident, "x", [1, 8]]]]],
  nil,
  nil,
  nil,
  nil,
  nil],
 [:bodystmt, [[:void_stmt]], nil, nil, nil]]
true

def m a, b=9, c;end
[:def,
 [:@ident, "m", [1, 4]],
 [:params,
  [[:@ident, "a", [1, 6]]],
  [[[:@ident, "b", [1, 9]], [:@int, "9", [1, 11]]]],
  nil,
  [[:@ident, "c", [1, 14]]],
  nil,
  nil,
  nil],
 [:bodystmt, [[:void_stmt]], nil, nil, nil]]
def m a, b=9; end

def m a, *r; end
[:def,
 [:@ident, "m", [1, 4]],
 [:params,
  [[:@ident, "a", [1, 6]]],
  nil,
  [:rest_param, [:@ident, "r", [1, 10]]],
  nil,
  nil,
  nil,
  nil],
 [:bodystmt, [[:void_stmt]], nil, nil, nil]]
true

def m a, b=9, *r, c, d; end
[:def,
 [:@ident, "m", [1, 4]],
 [:params,
  [[:@ident, "a", [1, 6]]],
  [[[:@ident, "b", [1, 9]], [:@int, "9", [1, 11]]]],
  [:rest_param, [:@ident, "r", [1, 15]]],
  [[:@ident, "c", [1, 18]], [:@ident, "d", [1, 21]]],
  nil,
  nil,
  nil],
 [:bodystmt, [[:void_stmt]], nil, nil, nil]]
def m a, b=9, *r; end

def a, b, &p; end
:vcall
p

Proc.new { }
[:method_add_block,
 [:call,
  [:var_ref, [:@const, "Proc", [1, 0]]],
  :".",
  [:@ident, "new", [1, 5]]],
 [:brace_block, nil, [[:void_stmt]]]]
true

Proc.new { 9 }
[:method_add_block,
 [:call,
  [:var_ref, [:@const, "Proc", [1, 0]]],
  :".",
  [:@ident, "new", [1, 5]]],
 [:brace_block, nil, [[:@int, "9", [1, 11]]]]]
true

Proc.new { |x| 9 }
[:method_add_block,
 [:call,
  [:var_ref, [:@const, "Proc", [1, 0]]],
  :".",
  [:@ident, "new", [1, 5]]],
 [:brace_block,
  [:block_var,
   [:params, [[:@ident, "x", [1, 12]]], nil, nil, nil, nil, nil, nil],
   false],
  [[:@int, "9", [1, 15]]]]]
true

Proc.new { |x, y| 9 }
[:method_add_block,
 [:call,
  [:var_ref, [:@const, "Proc", [1, 0]]],
  :".",
  [:@ident, "new", [1, 5]]],
 [:brace_block,
  [:block_var,
   [:params,
    [[:@ident, "x", [1, 12]], [:@ident, "y", [1, 15]]],
    nil,
    nil,
    nil,
    nil,
    nil,
    nil],
   false],
  [[:@int, "9", [1, 18]]]]]
true

Proc.new { |x, y=9| 9 }
[:method_add_block,
 [:call,
  [:var_ref, [:@const, "Proc", [1, 0]]],
  :".",
  [:@ident, "new", [1, 5]]],
 [:brace_block,
  [:block_var,
   [:params,
    [[:@ident, "x", [1, 12]]],
    [[[:@ident, "y", [1, 15]], [:@int, "9", [1, 17]]]],
    nil,
    nil,
    nil,
    nil,
    nil],
   false],
  [[:@int, "9", [1, 20]]]]]
true

[9].zip([9]) { |l, r| 9 }
[:method_add_block,
 [:method_add_arg,
  [:call, [:array, [[:@int, "9", [1, 1]]]], :".", [:@ident, "zip", [1, 4]]],
  [:arg_paren, [:args_add_block, [[:array, [[:@int, "9", [1, 9]]]]], false]]],
 [:brace_block,
  [:block_var,
   [:params,
    [[:@ident, "l", [1, 16]], [:@ident, "r", [1, 19]]],
    nil,
    nil,
    nil,
    nil,
    nil,
    nil],
   false],
  [[:@int, "9", [1, 22]]]]]
true

-> { 9 }
[:lambda, [:params, nil, nil, nil, nil, nil, nil, nil], [[:@int, "9", [1, 5]]]]
true

->(x) { 9 }
[:lambda,
 [:paren, [:params, [[:@ident, "x", [1, 3]]], nil, nil, nil, nil, nil, nil]],
 [[:@int, "9", [1, 8]]]]
true

->(x=9) { 9 }
[:lambda,
 [:paren,
  [:params,
   nil,
   [[[:@ident, "x", [1, 3]], [:@int, "9", [1, 5]]]],
   nil,
   nil,
   nil,
   nil,
   nil]],
 [[:@int, "9", [1, 10]]]]
true

lambda { 9 }
[:method_add_block,
 [:method_add_arg, [:fcall, [:@ident, "lambda", [1, 0]]], []],
 [:brace_block, nil, [[:@int, "9", [1, 9]]]]]
true

[:@int, :unary, :@float, :string_literal, :binary, :symbol_literal, :dyna_symbol, :var_ref, :vcall, :command, :method_add_arg, :call, :command_call, :assign, :aref, :massign, :array, :hash, :def, "v", :method_add_block, :lambda]