Iron Languages - NYC CodeCamp 2/19/2011

5,081 views
5,056 views

Published on

NYC CodeCamp 2011 Talk - Iron Languages, Dynamic Language on .NET

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
5,081
On SlideShare
0
From Embeds
0
Number of Embeds
4,103
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Iron Languages - NYC CodeCamp 2/19/2011

  1. 1. IRON<br />Languages<br />Jimmy Schementi<br />jschementi@lab49.com<br />jimmy.schementi.com<br />Dynamic Languages for the .NET developer<br />github.com/IronLanguages<br />
  2. 2. JavaScript<br />Tcl<br />MATLAB<br />Ruby<br />Smalltalk<br />
  3. 3. xkcd.com<br />
  4. 4. Executes many common behaviors, at runtime, that other languages might perform during compilation, if at all.<br />
  5. 5. Most are dynamically-typed, but not all.<br />Dynamic Typing<br />The majority of its type checking is performed at run-time as opposed to at compile-time.<br />
  6. 6. why?<br />
  7. 7. Simple enough for non-programmers, capable enough for programmers<br />print File.read("foo.txt")<br />name = "Jimmy"<br />[1,2,3].each do |i| <br /> puts i<br />end<br />class Foo<br />defmethod_missing(m)<br />puts "called: #{m}"<br /> end<br />end<br />Foo.new.dfhajsdhfl<br />"-" * 79<br />a.downcaserescue "No name"<br />
  8. 8. Scripting <br />Languages<br />Dynamic <br />Languages<br />
  9. 9. “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.”<br />Mustafa Thamer<br /> Civilization IV development team<br />
  10. 10.
  11. 11. http://www.unreal.com/media/banners/kismet1.jpg<br />
  12. 12. http://logo.twentygototen.org/<br />
  13. 13. Interactive<br />>>> 2 + 2<br />4<br />
  14. 14. .NET?<br />
  15. 15. Dynamic Languages on .NET<br />Consumers<br />
  16. 16. Keep it Simple<br />Python<br />Ruby<br />def fact n<br />    return 1 if n == 0<br />    n * fact(n-1)<br />end<br />puts fact 13<br />def fact(n):<br /> if n == 0: return 1<br /> return n * fact(n-1)<br />print fact(13)<br />C#<br />using System;<br />public class MathClass {<br /> public static int Factorial(int n) {<br /> if (n == 0) return 1;<br /> return n * Factorial(n – 1);<br /> }<br /> public static void Main(string[] args) {<br />Console.WriteLine(Factorial(13));<br /> }<br />}<br />
  17. 17. Ecosystem exchange<br />.NET gets Python and Ruby world, and Ruby/Python gets .NET world<br />Ruby on Rails/Django/RSpec/etc on .NET<br />Scripting the .NET framework<br />Driving .NET code from scripts for testing, exploration, etc.<br />DSL-like characteristics<br />Hosting<br />In-Application extensibility / customization<br />Treating code as data (or configuration)<br />Discussion on best practices<br />
  18. 18. Scripting the .NET framework<br />Driving .NET code from scripts<br />Domain-specific languages<br />Compiler Geek-Out<br />High-level discussion on how compilers work and what the DLR does.<br />Hosting<br />In-Application extensibility / customization<br />Treating code as data (or configuration)<br />Discussion on best practices<br />
  19. 19. Demo<br />Scripting .NET<br />
  20. 20. rb> puts 2 + 2<br /> 4<br /># => nil<br />
  21. 21. traditional compiler front-end<br />Scan<br />Token stream<br />def add():<br /> return 2 + a<br />return<br />2<br />+<br />a<br />Parse<br />Syntax Tree<br />return <br />add<br />Named(a)<br />Const(2)<br />
  22. 22. IronPython: <br />Hand-written LL(1) parser<br />IronRuby: <br />Gardens Point Parser Generator LALR(1)<br />
  23. 23. compiler back-end on CLR<br />Generate IL<br />Syntax Tree<br />ldc.i4.2 // load 2<br />box [mscorlib]System.Int32<br />ldarg.0 // load “a”<br />call object LangHelpers::Add(object, object)<br />ret<br />IL<br />return <br />add<br />public static object Add<br /> (object x, object y) {<br /> ... <br />}<br />Const(2)<br />Runtime Library<br />Named(a)<br />
  24. 24. compiler back-end on DLR<br />Linq Expression<br />Tree<br />ToExpression Tree<br />Return <br />Syntax Tree<br />return <br />MethodCall<br />LangHelpers.Add<br />add<br />ConvertTo<br />Object<br />BoundExpression<br />Const(2)<br />Named(a)<br />Variable<br />a: Object<br />ConstantExpression<br />2<br />
  25. 25. puts 2 + 2 -> Expression tree <br />.Dynamic puts(.S,1) @1(<br /> $#scope,<br />$#self,<br />.Call IronRuby.Runtime.RubyOps.CreateMutableStringL(<br /> "hi",<br /> .Constant<IronRuby.Builtins.RubyEncoding>(US-ASCII)))<br />
  26. 26. IronRuby.Runtime.RubyScriptCode<br />internalstaticDelegate/*!*/CompileLambda(<br />LambdaExpression/*!*/ lambda, <br />booldebugMode, <br />boolnoAdaptiveCompilation, <br />intcompilationThreshold) {<br />if (debugMode) {<br />returnCompileDebug(lambda);<br /> } elseif (noAdaptiveCompilation) {<br />returnlambda.Compile();<br />} else {<br />returnlambda.LightCompile(compilationThreshold);<br /> }<br /> }<br />
  27. 27. static vs. dynamic dispatch<br />MethodCallExpression<br />Method : {RuntimeMethodInfo<br /> {Name: "Print"}}<br />Arguments : [0] ActionExpression<br />def yo (name): <br /> "hello " + name<br />print yo("jimmy")<br />ActionExpression<br />yo("jimmy")<br />Action : CallAction<br />Arguments : [0] {BoundExpression<br /> {Variable: Local{yo}}}<br /> [1] {ConstantExpression {"jimmy"}}<br />
  28. 28. public static object Handle (<br /> object[] args, DynamicSite<object, object, object> site1,<br /> object obj1, object obj2)<br />{<br /> if (obj1 != null && obj1.GetType() == typeof(string) &&<br /> obj2 != null && obj2.GetType() == typeof(string))<br /> {<br /> return StringOps.Add(Converter.ConvertToString(obj1),<br />Converter.ConvertToString(obj2));<br /> }<br /> return site1.UpdateBindingAndInvoke(obj1, obj2);<br />}<br />
  29. 29. print yo(1)<br />
  30. 30. public static object Handle (<br /> object[] args, DynamicSite<object, object, object> site1,<br /> object obj1, object obj2)<br />{<br /> if (obj1 != null && obj1.GetType() == typeof(int) &&<br /> obj2 != null && obj2.GetType() == typeof(int))<br /> {<br /> return Int32Ops.Add(Converter.ConvertToInt(obj1),<br />Converter.ConvertToInt(obj2));<br /> }<br /> if (obj1 != null && obj1.GetType() == typeof(string) &&<br /> obj2 != null && obj2.GetType() == typeof(string))<br /> {<br /> return StringOps.Add(Converter.ConvertToString(obj1),<br />Converter.ConvertToString(obj2));<br /> }<br /> return site1.UpdateBindingAndInvoke(obj1, obj2);<br />}<br />
  31. 31. System.Dynamic.DynamicObject<br />
  32. 32. Dynamic Language Runtime<br />Infrastructure for creating languages<br />Focus on dynamic compiler back-end.<br />Dynamic-lookup protocol<br />DynamicObject: shared protocol between languages<br />Lightweight hosting API<br />One API for all DLR languages<br />
  33. 33. Hosting<br />Hosting<br />ScriptRuntime<br />ScriptScope<br />ScriptEngine<br />ScriptSource<br />
  34. 34. var engine = Ruby.CreateEngine(); engine.Execute("puts 2 + 2");<br />
  35. 35. var engine = Python.CreateEngine(); dynamicscope = engine.CreateScope(); scope.page= this; engine.Execute("page.Message.Text = 'Hello from Python!'", scope);<br />
  36. 36. 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);<br />
  37. 37. 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<br />
  38. 38. # 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<br />
  39. 39. Demo<br />Hosting<br />
  40. 40. Hosting best-practices<br />Store scripts where you want with PlatformAdaptationLayer<br />Makes script file-system operations use database, source-control, whatever …<br />Pick isolation level for scripts<br />In-App-Domain: you totally control<br />Out-App-Domain: limit permission level<br />Out of process: total isolation<br />
  41. 41. Project Status<br />IronRubyis working towards 1.9 compat – Rails 3, FFI, static type system integration<br />IronPythonworking towards 2.7/3.0 compat – Django, IronClad, and other libraries.<br />Tooling<br />IronRuby Gems/Rake support<br />Debugging w/REPL<br />Fully open source<br />Contributions welcome!<br />
  42. 42. How you can participate<br />Use it at your company, and tell us about it!<br />Ask the mailing lists and stackoverflow for help<br />Log any bugs you find<br />Contributing to the project<br />Even if you’re not a compiler hacker … <br />but hackers welcome!<br />samples, documentation, blogs, and talks are all welcome also<br />
  43. 43. ironruby.netIronRuby website & download<br />ironpython.netIronPython website & downloaddlr.codeplex.comDLR documentation for hosters and language developersjimmy.schementi.comme<br />
  44. 44. ?<br />
  45. 45. IRON<br />Languages<br />Jimmy Schementi<br />jschementi@lab49.com<br />jimmy.schementi.com<br />Dynamic Languages for the .NET developer<br />github.com/IronLanguages<br />

×