Kristian Kristensen’s Blog


My Talk From JAOO/GOTOcon Is Up On Channel9

Posted in Code,IronPython,IronRuby,Misc by Kristian Kristensen on the October 16th, 2010

As I said in my previous blog entry my talk at GOTOcon (previously JAOO) was recorded by Microsoft with the intent of putting it up on Channel9. After some processing and uploading it is now up online.

Link to Channel9: Kristian Kristensen – Iron* – An Introduction to Getting Dynamic on .NET

From Channel9 you can download the video in a variety of formats.

I’ve embedded the video below using the Channel9 SilverLight player:

Get Microsoft Silverlight

Let me know if you have any questions, comments or suggestions.

  • If you like my writing you should subscribe to my RSS feed.

    Iron Languages Talk at JAOO (Now Goto Conference)

    Posted in Code,IronPython,IronRuby by Kristian Kristensen on the October 5th, 2010

    Today I held my talk on Iron Languages on .NET at JAOO (now Goto Conference). I think it went well, and the quick look I got at the evaluation sheet seems to agree with me. Microsoft taped my talk today, and it’ll be posted on Channel9 later. I’ll blog a link when it gets online.

    See the slides at Slideshare.

    Image of me presenting at JAOO courtesy of Niels Beck:

    If you have any questions please don’t hesitate to contact me.

  • If you like my writing you should subscribe to my RSS feed.

    Speaking at JAOO 2010

    Posted in IronPython,IronRuby by Kristian Kristensen on the September 20th, 2010

    I will be speaking at JAOO 2010 in Aarhus on Iron languages. Here’s a link to the schedule with my talk: Iron* – An Introduction to Getting Dynamic on .NET.

    In recent years dynamically typed languages have received more and more attention on the .NET platform. Initially, an implementation of Python showed up. Later, the Dynamic Language Runtime (DLR) appeared which made it much easier to implement dynamically typed languages on .NET. This session explores how dynamically typed languages fit in the ecosystem of .NET. We’ll see why dynamic languages are interesting and contrast them to their static brethren (C#, Java). Then we will touch on how IronPython and IronRuby are implemented on .NET via the Dynamic Language Runtime (DLR). Finally, we’ll look at some code examples of how you can utilize these languages on .NET today.

    If you’re attending JAOO, and want to chat I’d be happy to meet up. I’ll be there for the duration of the conference. Also I plan on attending the .NET User Group meeting on Tuesday night.

  • If you like my writing you should subscribe to my RSS feed.

    IronLanguages and the DLR Podcast with Me

    Posted in IronPython,IronRuby,Misc,Podcast by Kristian Kristensen on the July 16th, 2010

    Some time ago I recorded a podcast with Daniel from DPE in Microsoft Denmark on IronLanguages (IronRuby and IronPython) and the DLR. It’s in Danish and is now up online. You can listen to it here:

    If you prefer to download it and listen to it offline there’s a direct link to an MP3 here.

    Link to Daniel’s original post.

  • If you like my writing you should subscribe to my RSS feed.

    Iron Languages Talk at Community Day 2010

    Posted in Code,IronPython,IronRuby by Kristian Kristensen on the May 30th, 2010

    On Thursday I held a talk on Iron Languages at Community Day 2010. It’s a free one day conference that tries to bridge different technology stacks and encourage discussion. My talk was an updated and shortened version of the Tech Talk I did at Microsofts office some time ago.

    The slides are up on Slideshare – IronSprog Community Day 2010.

    I demoed a Ruby Testing Framework originally done by John Lam, and also used by Harry Pierson in his Pumping Iron talk. The code is pasted below.
    I demoed the CTP of IronPython Tools for Visual Studio.  To showcase some of the features I used BeautifulSoup.
    I demoed BadPaint, which is a sample that comes with IronPython. Get it here including the example code I used.

    It appears my talk was video taped. I should get a copy of that, and I ‘m quite sure it’ll find its way to the Internet as well. I’ll post a link when and where it goes online.

    Ruby Test Framework and unit testing the .NET stack demo code:

    #testharness.rb
    $examples = 0
    $messages = []
    
    class PositiveExpectation
    
      def initialize(obj)
        @obj = obj
      end
    
      def ==(other)
        $examples += 1
        if @obj != other
          $messages << "Want #{@obj.inspect} got #{other.inspect}"
          print "E"
        else
          print "OK"
        end
      end
    
    end
    
    class Object
      def should
        PositiveExpectation.new(self)
      end
    end
    
    def it(description)
      print "\n it #{description}: "
      yield
    end
    
    def describe(description)
      print "#{description}"
      yield
      puts "\nend\n"
    end
    
    at_exit do
      puts "#{$messages.length} / #{$examples} failed"
      if $messages.length > 0
        puts "Failures: "
        $messages.each { |m| puts "- #{m}"}
      end
    end
    #stack.rb
    require 'testharness'
    require 'System'
    
    include System::Collections
    
    describe ".NET Stack Operations" do
      it "should create an instance with zero elements" do
        Stack.new.count.should == 0
      end
    
      it "should let us peek at the element pushed" do
        s = Stack.new
        s.push "bob"
        s.peek.should == "bob"
        s.count.should == 1
      end
    
      it "should let us pop the element we pushed" do
        s = Stack.new
        s.push "bob"
        s.pop.should == "bob"
        s.count.should == 0
      end
    end</pre>
    <div>
    $examples = 0
    $messages = []
    class PositiveExpectation
    def initialize(obj)
    @obj = obj
    end
    def ==(other)
    $examples += 1
    if @obj != other
    $messages << “Want #{@obj.inspect} got #{other.inspect}”
    print “E”
    else
    print “OK”
    end
    end
    end
    class Object
    def should
    PositiveExpectation.new(self)
    end
    end
    def it(description)
    print “\n it #{description}: “
    yield
    end
    def describe(description)
    print “#{description}”
    yield
    puts “\nend\n”
    end
    at_exit do
    puts “#{$messages.length} / #{$examples} failed”
    if $messages.length > 0
    puts “Failures: “
    $messages.each { |m| puts “- #{m}”}
    end
    ende]
  • If you like my writing you should subscribe to my RSS feed.

    Getting the AST from IronRuby

    Posted in Code,IronRuby by Kristian Kristensen on the February 28th, 2010

    I’ve been looking into IronRuby and how to retrieve the Abstract Syntax Tree (AST) that’s used. In this post I’ll show a simple example of how you can do the same.

    First let’s look at what a compiler is and how it works. A compiler basically works in three phases: Lexical Analysis, Syntactic/Semantic Analysis and Code Generation. Given a piece of code the compiler first tokenizes the input. This means splitting the code into its simplest parts and it’s the Lexical Analysis phase. Examples of these parts could be a variable name, a string, an assignment operator (such as = in C#). The output from the Tokenizer is feed into a Parser in the Syntactic/Semantic Analysis phase. The Parser takes the individual parts – the tokens – and creates a generic representation of the code. This representation is the Abstract Syntax Tree (AST). In the last phase (Code Generation) the compiler uses the AST to generate code that can run on the target platform.
    Usually each of these phases has multiple sub phases that further helps to validate, optimize, etc. the code.

    If we take a modern language such as C# the input will be C# code in a .cs file and the output will be IL code that can run on the Common Language Runtime (CLR).
    Now let’s take a look at an example of how the AST might look like for the simple assignment shown in the following code segment:

    a = 2 + 3

    After being parsed this code might have an AST representation as shown in the following:

      =
     / \
    a   +
       / \
       2  3

    The parts shown in the tree are called nodes, and there are 5 in total. A variable name, two integers (or literals as they’re called), a plus sign (or more specifically a Binary Expression) and an Assignment (the “=”).

    Getting the AST is a first step in trying to analyze the code yourself. Since creating a tokenizer and parser for a language such as Ruby is a non trivial effort, it’s great if you can piggyback on others work. And for looking at Ruby code on .NET IronRuby is obviously perfect.

    In the following code I’m using IronRuby 1.0 RC2, which is the latest release.
    First we need to add a couple of assembly references, which you can find where you installed IronRuby (usually “c:\ironruby\bin\”). We need IronRuby.dll, IronRuby.Libraries.dll, Microsoft.Dynamic.dll, Microsoft.Scripting.dll, Microsoft.Scripting.Core.dll, Microsoft.Scripting.Core.dll, Microsoft.Scripting.Helpers.dll

    Then import the following namespaces:

    using IronRuby.Builtins;
    using IronRuby.Compiler;
    using IronRuby.Compiler.Ast;
    using Microsoft.Scripting;
    using Microsoft.Scripting.Hosting.Providers;

    Next we’ll setup an IronRuby runtime and engine, this is the core infrastructure we need to work with Ruby code. Last we’ll create an instance of the ScriptSource class with a simple piece of Ruby code that calls “puts” with “hello” as a parameter.

    var runtime = IronRuby.Ruby.CreateRuntime();
    var engine = runtime.GetEngine('rb');
    var src = engine.CreateScriptSourceFromString(@"puts 'hello'");

    The ScriptSource is actually executeable, so if you called the method Execute() the simple Ruby expression would be executed. This is the basic pattern you’d follow if you wanted to add scripting capabilities to your application.

    Using a helper class in the Dynamic Language Runtime (DLR) API’s we can access the SourceUnit instance. We create a new IronRuby Parser and ask it to parse the code found in the SourceUnit instance.

    var srcUnit = HostingHelpers.GetSourceUnit(src);
    var parser = new Parser();
    var srcTreeUnit = parser.Parse(srcUnit, new RubyCompilerOptions(), ErrorSink.Default);

    To access the AST we need to implement a Walker. This class implements the Visitor Design Pattern, and using it we can look at each of the nodes in the AST. A simple walker is shown below.

    public class MyWalker : Walker
        {
            protected override void Walk(MethodCall node)
            {
                Console.WriteLine("Method call: " + node.MethodName);
                base.Walk(node);
            }
    
            protected override void Walk(StringLiteral node)
            {
                Console.WriteLine("String Literal: " + node.GetMutableString(RubyEncoding.Default).ToString());
                base.Walk(node);
            }
    }

    We kickstart the process by calling the Walk() method on our Walker implementation and gives it the SourceUnit as input.

    var walker = new MyWalker();
    walker.Walk(srcTreeUnit);

    For each node type that the AST can contain a method is called on our Walker, and we can then do what we want. In this example I print the current node to the console.

    The output of running this code is:

    Method call: puts
    String Literal: hello
    

    The next step is to implement the rest of the methods on the walker, thereby supporting the full set of AST nodes. Also you’d probably want to build up your own data structure for analysis.
    This example showed how you can access the parsed Ruby code using IronRuby. The next step is to actually do something interesting with this knowledge.

  • If you like my writing you should subscribe to my RSS feed.

    Iron Languages Talk – Slides Posted

    Posted in IronPython,IronRuby by Kristian Kristensen on the February 7th, 2010

    I held a TechTalk at the Microsoft office on Wednesday, and have promised to post the slides. So here goes.

    Slides @ slideshare.net

    The video I showed of the Nabaztag bunny being connected to a TFS server is embedded in that deck. However, you can also view it directly on YouTube.

    IronBunny @ YouTube

    If there’s interest, I’ll post my demos up here as well.

  • If you like my writing you should subscribe to my RSS feed.