IRONLanguagesJimmy Schementijschementi@lab49.comjimmy.schementi.comDynamic Languages for the .NET developergithub.com/IronLanguages
JavaScriptTclMATLABRubySmalltalk
xkcd.com
Executes many common behaviors, at runtime, that other languages might perform during compilation, if at all.
Most are dynamically-typed, but not all.Dynamic TypingThe majority of its type checking is performed at run-time as opposed to at compile-time.
why?
Simple enough for non-programmers,             capable enough for programmersprint File.read("foo.txt")name = "Jimmy"[1,2,3].each do |i|   puts iendclass Foodefmethod_missing(m)puts "called: #{m}"  endendFoo.new.dfhajsdhfl"-" * 79a.downcaserescue "No name"
Scripting LanguagesDynamic Languages
“Python, like many good technologies, soon spreads virally throughout your development team and finds its way into all sorts of applications and tools…Python scripts are used in many areas of the game.”Mustafa Thamer		Civilization IV development team
http://www.unreal.com/media/banners/kismet1.jpg
http://logo.twentygototen.org/
Interactive>>> 2 + 24
.NET?
Dynamic Languages on .NETConsumers
Keep it SimplePythonRubydef fact n    return 1 if n == 0    n * fact(n-1)endputs fact 13def fact(n):    if n == 0: return 1    return n * fact(n-1)print fact(13)C#using System;public class MathClass {    public static int Factorial(int n) {         if (n == 0) return 1;         return n * Factorial(n – 1);    }    public static void Main(string[] args) {Console.WriteLine(Factorial(13));    }}
Ecosystem exchange.NET gets Python and Ruby world, and Ruby/Python gets .NET worldRuby on Rails/Django/RSpec/etc  on .NETScripting the .NET frameworkDriving .NET code from scripts for testing, exploration, etc.DSL-like characteristicsHostingIn-Application extensibility / customizationTreating code as data (or configuration)Discussion on best practices
Scripting the .NET frameworkDriving .NET code from scriptsDomain-specific languagesCompiler Geek-OutHigh-level discussion on how compilers work and what the DLR does.HostingIn-Application extensibility / customizationTreating code as data (or configuration)Discussion on best practices
DemoScripting .NET
rb> puts 2 + 2    4# => nil
traditional compiler front-endScanToken streamdef add():  return 2 + areturn2+aParseSyntax Treereturn addNamed(a)Const(2)
IronPython: Hand-written LL(1) parserIronRuby: Gardens Point Parser Generator LALR(1)
compiler back-end on CLRGenerate ILSyntax Treeldc.i4.2		// load 2box  [mscorlib]System.Int32ldarg.0		// load “a”call  object LangHelpers::Add(object, object)retILreturn addpublic static object Add   (object x, object y)  {    ... }Const(2)Runtime LibraryNamed(a)
compiler back-end on DLRLinq ExpressionTreeToExpression TreeReturn Syntax Treereturn MethodCallLangHelpers.AddaddConvertToObjectBoundExpressionConst(2)Named(a)Variablea: ObjectConstantExpression2
puts 2 + 2 -> Expression tree  .Dynamic puts(.S,1) @1(    $#scope,$#self,.Call IronRuby.Runtime.RubyOps.CreateMutableStringL(   "hi",        .Constant<IronRuby.Builtins.RubyEncoding>(US-ASCII)))
IronRuby.Runtime.RubyScriptCodeinternalstaticDelegate/*!*/CompileLambda(LambdaExpression/*!*/ lambda, booldebugMode, boolnoAdaptiveCompilation, intcompilationThreshold) {if (debugMode) {returnCompileDebug(lambda);            } elseif (noAdaptiveCompilation) {returnlambda.Compile();} else {returnlambda.LightCompile(compilationThreshold);            }        }
static vs. dynamic dispatchMethodCallExpressionMethod    : {RuntimeMethodInfo              {Name: "Print"}}Arguments : [0] ActionExpressiondef yo (name):     "hello " + nameprint yo("jimmy")ActionExpressionyo("jimmy")Action    : CallActionArguments : [0] {BoundExpression                  {Variable: Local{yo}}}            [1] {ConstantExpression {"jimmy"}}
public static object Handle (	object[] args, DynamicSite<object, object, object> site1,	object obj1, object obj2){	if (obj1 != null && obj1.GetType() == typeof(string) &&       obj2 != null && obj2.GetType() == typeof(string)) 	{		return StringOps.Add(Converter.ConvertToString(obj1),Converter.ConvertToString(obj2));	}   return site1.UpdateBindingAndInvoke(obj1, obj2);}
print yo(1)
public static object Handle (	object[] args, DynamicSite<object, object, object> site1,	object obj1, object obj2){	if (obj1 != null && obj1.GetType() == typeof(int) &&       obj2 != null && obj2.GetType() == typeof(int)) 	{		return Int32Ops.Add(Converter.ConvertToInt(obj1),Converter.ConvertToInt(obj2));	}   if (obj1 != null && obj1.GetType() == typeof(string) &&       obj2 != null && obj2.GetType() == typeof(string)) 	{		return StringOps.Add(Converter.ConvertToString(obj1),Converter.ConvertToString(obj2));	}   return site1.UpdateBindingAndInvoke(obj1, obj2);}
System.Dynamic.DynamicObject
Dynamic Language RuntimeInfrastructure for creating  languagesFocus on dynamic compiler back-end.Dynamic-lookup protocolDynamicObject: shared protocol between languagesLightweight hosting APIOne API for all DLR languages
HostingHostingScriptRuntimeScriptScopeScriptEngineScriptSource
var engine = Ruby.CreateEngine(); engine.Execute("puts 2 + 2");
var engine = Python.CreateEngine(); dynamicscope = engine.CreateScope(); scope.page= this; engine.Execute("page.Message.Text = 'Hello from Python!'",  scope);
var runtime = ScriptRuntime.CreateFromConfiguration(); var engine = ScriptEngine.CreateEngine("IronRuby"); dynamic scope = engine.CreateScope(); scope.page = this;engine.Execute("page.Message.Text = 'Hello from IronRuby!'", scope);
require 'IronPython'require 'Microsoft.Scripting'include Microsoft::Scripting::Hostinginclude IronPython::Hostingpython = Python.create_enginescope = python.create_scopepython.execute "class Foo(object):    def bar(self):        print 'Look ma, white-space-sensitivity!'", scopepython.execute "Foo().bar()", scope
# foo.py:class Foo(object): def bar(self):     print 'Look ma, white-space-sensitivity!' # bar.rb:foo_module = IronRuby.require 'foo' foo_module.foo.bar
DemoHosting
Hosting best-practicesStore scripts where you want with PlatformAdaptationLayerMakes script file-system operations use database, source-control, whatever …Pick isolation level for scriptsIn-App-Domain: you totally controlOut-App-Domain: limit permission levelOut of process: total isolation
Project StatusIronRubyis working towards 1.9 compat – Rails 3, FFI, static type system integrationIronPythonworking towards 2.7/3.0 compat – Django, IronClad, and other libraries.ToolingIronRuby Gems/Rake supportDebugging w/REPLFully open sourceContributions welcome!
How you can participateUse it  at your company, and tell us about it!Ask the mailing lists and stackoverflow for helpLog any bugs you findContributing to the projectEven if you’re not a compiler hacker … but hackers welcome!samples, documentation, blogs, and talks are all welcome also
ironruby.netIronRuby website & downloadironpython.netIronPython website & downloaddlr.codeplex.comDLR documentation for hosters and language developersjimmy.schementi.comme
?

Iron Languages - NYC CodeCamp 2/19/2011