Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Tuesday, August 4, 2009

Hacking parse.y with me

Yesterday I made my first English presentation.

http://ruby.meetup.com/112/calendar/10614880/

What was the topic?

Since this year I have tried some short hacks with MRI (Matz Ruby Implementation), and showed the results on my blog. This presentation was what and how did I fix ruby. Almost half time of my presentation was demonstration, which includes applying patches and building ruby.

How was the presentation?

I must have spoken in awkward and broken English. I hope attendees could understand what I tried to explain. It was still difficult for me to speak and listen English. Even worse I had a trouble in listinig English. I hope I had answered correctly to questions.

The ruby meetup people were friendly and generous. Thank you very much!

workspace

workspace

workspace

(These photos are not my presentation. It's just for showing how the room was like.)

Friday, July 31, 2009

Abnormal Weather in Vancouver

http://www.theglobeandmail.com/news/national/record-setting-heat-wave-cooks-the-coast/article1237198/

In Vancouver, where the definition of a hot summer day is 24 degrees, the temperature topped out Wednesday afternoon at 33.8 degrees, which was the hottest day in the city's history, at least until yesterday.

I certainly said the following on the last day of RubyKaigi2009 in Japan. "It is too hot in Japan, so I will escape from here tomorrow" Unfortunately I seem to fail to escape.

In many ways, the scorching days and nights are harder on Vancouverites, where heat waves are as rare as mosquitoes. Few have air conditioning, so people have been flocking to malls and movie theatres. Apartment buildings - especially high rises - have become ovens. Health officials have urged people to check on elderly neighbours and relatives.

Yes, few have air conditioning, and I'm one of them. My room is exactly an oven.

Haskell Platform Instead of MacPorts

Someone hospitably suggested me in this comment to use Haskell Platform instead of MacPorts to install ghc or other haskell libraries.

As most Mac users know, it takes 12+ hours to build ghc. Once you typed sudo port install ghc, you would have to go to bed and sleep for half a day.

The Haskell Platform is a binary package, so now we don't have to

Installing pandoc via Haskell Platform cabal

cabal update
cabal install pandoc

then

Resolving dependencies...
[1 of 1] Compiling Main             ( /var/folders/Dz/Dz5WpFSZGUaFLA8jp8kT5E+++TM/-Tmp-/pandoc-1.2.12656/pandoc-1.2.1/Setup.hs, /var/folders/Dz/Dz5WpFSZGUaFLA8jp8kT5E+++TM/-Tmp-/pandoc-1.2.12656/pandoc-1.2.1/dist/setup/Main.o )
Linking /var/folders/Dz/Dz5WpFSZGUaFLA8jp8kT5E+++TM/-Tmp-/pandoc-1.2.12656/pandoc-1.2.1/dist/setup/setup ...
Configuring pandoc-1.2.1...
Preprocessing library pandoc-1.2.1...
Preprocessing executables for pandoc-1.2.1...
Building pandoc-1.2.1...

src/Text/Pandoc/Shared.hs:118:7:
    Could not find module `Network.URI':
      There are files missing in the `network-2.2.1.4' package,
      try running 'ghc-pkg check'.
      Use -v to see a list of the files searched for.
cabal: Error: some packages failed to install:
pandoc-1.2.1 failed during the building phase. The exception was:
exit: ExitFailure 1
~/.cabal
$ pandoc install network
pandoc: install: openBinaryFile: does not exist (No such file or directory)
$ ghc-pkg check |& grep network-
There are problems in package network-2.2.1.4:
  import-dirs: /Users/ujihisa/.cabal/lib/network-2.2.1.4/ghc-6.10.3 doesn't exist or isn't a directory
  library-dirs: /Users/ujihisa/.cabal/lib/network-2.2.1.4/ghc-6.10.3 doesn't exist or isn't a directory
  include-dirs: /Users/ujihisa/.cabal/lib/network-2.2.1.4/ghc-6.10.3/include doesn't exist or isn't a directory
  cannot find libHSnetwork-2.2.1.4.a on library path
network-2.2.1.4
network-2.2.1
network-2.2.1.1

hmm...

Wednesday, July 29, 2009

Incremental Operator in Ruby

Today I succeeded to add a new syntax "Pre Incremental Operator" like C's ++i into Ruby. The following code can run as you expect.

irb192

There are two possible ways what does ++i mean.

  1. Call succ and assign the result

    i = i.succ
    
  2. Call + with 1 and assign the result

    i = i.+(1)
    

I chose the former this time.

Patch

http://gist.github.com/158449

diff --git a/parse.y b/parse.y
index 10ac878..e02a1b1 100644
--- a/parse.y
+++ b/parse.y
@@ -685,6 +685,7 @@ static void token_info_pop(struct parser_params*, const char *token);
 %type <val> program reswords then do dot_or_colon
 %*/
 %token tUPLUS      /* unary+ */
+%token tINCR       /* ++var */
 %token tUMINUS     /* unary- */
 %token tPOW        /* ** */
 %token tCMP        /* <=> */
@@ -1783,6 +1784,7 @@ op        : '|'       { ifndef_ripper($$ = '|'); }
 '!'       { ifndef_ripper($$ = '!'); }
 '~'       { ifndef_ripper($$ = '~'); }
 tUPLUS    { ifndef_ripper($$ = tUPLUS); }
+       | tINCR     { ifndef_ripper($$ = tINCR); }
 tUMINUS   { ifndef_ripper($$ = tUMINUS); }
 tAREF     { ifndef_ripper($$ = tAREF); }
 tASET     { ifndef_ripper($$ = tASET); }
@@ -4130,6 +4132,15 @@ var_ref      : variable
      $$ = dispatch1(var_ref, $1);
        %*/
        }
+       | tINCR variable
+           {
+           /*%%%*/
+           $$ = assignable($2, 0);
+           $$->nd_value = NEW_CALL(gettable($$->nd_vid), rb_intern("succ"), 0);
+           /*%
+           $$ = dispatch2(unary, ripper_intern("++@"), $2);
+           %*/
+           }
    ;

 var_lhs        : variable
@@ -6773,6 +6784,9 @@ parser_yylex(struct parser_params *parser)

       case '+':
  c = nextc();
+   if (c == '+') {
+       return tINCR;
+   }
  if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) {
      lex_state = EXPR_ARG;
      if (c == '@') {
@@ -8277,6 +8291,7 @@ void_expr_gen(struct parser_params *parser, NODE *node)
    case '%':
    case tPOW:
    case tUPLUS:
+     case tINCR:
    case tUMINUS:
    case '|':
    case '^':
@@ -9111,6 +9126,7 @@ static const struct {
     {'-',  "-(binary)"},
     {tPOW, "**"},
     {tUPLUS,   "+@"},
+    {tINCR,    "++@"},
     {tUMINUS,  "-@"},
     {tCMP, "<=>"},
     {tGEQ, ">="},

Post Incremental Operator

I tried to add post incremental operator like C's i++, but I found that I couldn't.

There are two possible ways what does i++ mean.

  1. Store the original value, assign the result of succ, and return the original value

    tmp = i; i = i.succ; tmp
    
  2. Assign the result of + with 1, and return the result of - with 1

    (i = i.+(1)) - 1
    

The latter is easier to implement, because there's no storing a value.

Sunday, July 26, 2009

Parsers around Ruby

I'll show some parsers for ruby or parsers on ruby include parse_tree, ruby_parser, treetop and ripper.

Within this entry, there are only two definitions of ruby's syntax. The one is parser.y which is in MRI, and ruby_parser.y which is only in the ruby_parser library.

parse_tree

A parser which converts code in Ruby to S-expression. It is written in Ruby with C extension.

parse_tree uses ruby standard library racc. ([Added on July 28]: It was wrong: parse_tree doesn't use racc.)

require 'rubygems'
require 'parse_tree'

a = ParseTree.translate '1+1'
p a.class #=> Array
p a #=> [:call, [:lit, 1], :+, [:array, [:lit, 1]]]

or simply

$ echo "1+1" | parse_tree_show -f
s(:call, s(:lit, 1), :+, s(:arglist, s(:lit, 1)))

While parse_tree is a gem library for some ruby implementations, it is a standard library in Rubinius.

parse_tree uses C function rb_compile_string which is defined in MRI's parse.c.

Note that parse_tree cannot accept a string represented code instead of a method or a class. ([Added on July 28]: It was wrong: parse_tree can accept both.)

ruby_parser

Exactly same as parse_tree except that it is written in pure Ruby.

require 'rubygems'
require 'ruby_parser'

a = RubyParser.new.parse '1+1'
p a.class #=> Sexp
p a #=> s(:call, s(:lit, 1), :+, s(:arglist, s(:lit, 1)))

or

$ ruby_parse a.rb # Unfortunately ruby_parse command handles only actual files.

ruby_parser has own yacc file ruby_parser.y which has 1789 lines. It will be processed by racc.

Rubinius uses ruby_parser as its ruby parser.

Note that ruby_parser cannot accept a method or a class instead of a string represented code.

treetop

Treetop is a packrat parser written in pure Ruby. Treetop has an original syntax.

c.f.

Ripper

The ruby parser written in C. It is a ruby 1.9 standard library. It uses parse.y as ruby itself does.

const RUBY_ENGINE

p RUBY_ENGINE

ruby 1.8: (The constant RUBY_ENGINE is not defined)

ruby 1.9: "ruby"

jruby: "jruby"

rubinius: "rbx"

Followers