44
IRON Languages Dynamic Languages for the .NET developer github.com/ IronLanguages Jimmy Schementi [email protected] jimmy.schementi.com

Iron Languages - NYC CodeCamp 2/19/2011

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Iron Languages - NYC CodeCamp 2/19/2011

IRONLanguages

Dynamic Languages for the .NET developer

github.com/IronLanguages

Jimmy [email protected]

Page 2: Iron Languages - NYC CodeCamp 2/19/2011

JavaScriptSmalltalk

RubyTcl

MATLAB

Page 3: Iron Languages - NYC CodeCamp 2/19/2011

xkcd.com

Page 4: Iron Languages - NYC CodeCamp 2/19/2011

Executes many common behaviors, at runtime, that other languages might perform during compilation, if at all.

Page 5: Iron Languages - NYC CodeCamp 2/19/2011

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.

Page 6: Iron Languages - NYC CodeCamp 2/19/2011

why?

Page 7: Iron Languages - NYC CodeCamp 2/19/2011

Simple enough for non-programmers, capable enough for programmers

[1,2,3].ea

ch do |i|

puts i

end

print File.read("foo

.txt")

name = "Jimmy"

a.downcase rescue "No name"

class Foo def method_missing(m)

puts "called: #{m}"

endendFoo.new.dfhajsdhfl

"-" *

79

Page 8: Iron Languages - NYC CodeCamp 2/19/2011

Scripting Languages

Dynamic Languages

Page 9: Iron Languages - NYC CodeCamp 2/19/2011

“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 ThamerCivilization IV development team

Page 10: Iron Languages - NYC CodeCamp 2/19/2011
Page 11: Iron Languages - NYC CodeCamp 2/19/2011

http://www.unreal.com/media/banners/kismet1.jpg

Page 12: Iron Languages - NYC CodeCamp 2/19/2011

http://logo.twentygototen.org/

Page 13: Iron Languages - NYC CodeCamp 2/19/2011

Interactive>>> 2 + 24

Page 14: Iron Languages - NYC CodeCamp 2/19/2011

.NET?

Page 15: Iron Languages - NYC CodeCamp 2/19/2011

Dynamic Languages on .NET

Consumers

Page 16: Iron Languages - NYC CodeCamp 2/19/2011

C#

Python Ruby

Keep it Simpledef fact n    return 1 if n == 0    n * fact(n-1)end

puts fact 13

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)); }}

def fact(n): if n == 0: return 1 return n * fact(n-1)

print fact(13)

Page 17: Iron Languages - NYC CodeCamp 2/19/2011

Scripting the .NET frameworkDriving .NET code from scriptsDomain-specific languages

Compiler 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

Page 18: Iron Languages - NYC CodeCamp 2/19/2011

DemoScripting .NET

Page 19: Iron Languages - NYC CodeCamp 2/19/2011

rb> puts 2 + 2 4 # => nil

Page 20: Iron Languages - NYC CodeCamp 2/19/2011

def add(): return 2 + a

traditional compiler front-end

return

Syntax Tree

return

Scan

2 + a

Token stream

add

Named(a)Const(2)

Parse

Page 21: Iron Languages - NYC CodeCamp 2/19/2011

IronPython: Hand-written LL(1) parser

IronRuby: Gardens Point Parser Generator LALR(1)

Page 22: Iron Languages - NYC CodeCamp 2/19/2011

compiler back-end on CLR

Syntax Tree

return

add

Named(a)Const(2)

ldc.i4.2 // load 2box [mscorlib]System.Int32ldarg.0 // load “a”call object LangHelpers::Add(object, object)ret

IL

public static object Add (object x, object y) { ... }

Runtime Library

Generate IL

Page 23: Iron Languages - NYC CodeCamp 2/19/2011

compiler back-end on DLR

To Expression TreeLinq ExpressionTree

Return

MethodCallLangHelpers.Add

BoundExpression

ConstantExpression2

ConvertToObject

Variablea: Object

Syntax Tree

return

add

Named(a)Const(2)

Page 24: Iron Languages - NYC CodeCamp 2/19/2011

.Dynamic puts(.S,1) @1( $#scope, $#self, .Call IronRuby.Runtime.RubyOps.CreateMutableStringL( "hi", .Constant<IronRuby.Builtins.RubyEncoding>(US-ASCII)))

puts 2 + 2 -> Expression tree

Page 25: Iron Languages - NYC CodeCamp 2/19/2011

internal static Delegate/*!*/ CompileLambda( LambdaExpression/*!*/ lambda, bool debugMode, bool noAdaptiveCompilation, int compilationThreshold) {

if (debugMode) { return CompileDebug(lambda); } else if (noAdaptiveCompilation) { return lambda.Compile(); } else { return lambda.LightCompile(compilationThreshold); } }

IronRuby.Runtime.RubyScriptCode

Page 26: Iron Languages - NYC CodeCamp 2/19/2011

static vs. dynamic dispatch

def yo (name): "hello " + nameprint yo("jimmy")

MethodCallExpression

Method : {RuntimeMethodInfo {Name: "Print"}}Arguments : [0] ActionExpression

ActionExpressionyo("jimmy")

Action : CallActionArguments : [0] {BoundExpression {Variable: Local{yo}}} [1] {ConstantExpression {"jimmy"}}

Page 27: Iron Languages - NYC CodeCamp 2/19/2011

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);}

Page 28: Iron Languages - NYC CodeCamp 2/19/2011

print yo(1)

Page 29: Iron Languages - NYC CodeCamp 2/19/2011

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);}

Page 31: Iron Languages - NYC CodeCamp 2/19/2011

Dynamic Language Runtime

Infrastructure for creating languagesFocus on dynamic compiler back-end.

Dynamic-lookup protocolDynamicObject: shared protocol between languages

Lightweight hosting APIOne API for all DLR languages

Page 32: Iron Languages - NYC CodeCamp 2/19/2011

HostingHostingScriptRuntime

ScriptScope ScriptEngine

ScriptSource

Page 33: Iron Languages - NYC CodeCamp 2/19/2011

var engine = Ruby.CreateEngine(); engine.Execute("puts 2 + 2");

Page 34: Iron Languages - NYC CodeCamp 2/19/2011

var engine = Python.CreateEngine(); dynamic scope = engine.CreateScope(); scope.page = this; engine.Execute( "page.Message.Text = 'Hello from Python!'", scope);

Page 35: Iron Languages - NYC CodeCamp 2/19/2011

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);

Page 36: Iron Languages - NYC CodeCamp 2/19/2011

require 'IronPython'require 'Microsoft.Scripting'include Microsoft::Scripting::Hostinginclude IronPython::Hosting

python = Python.create_enginescope = python.create_scopepython.execute "class Foo(object):    def bar(self):        print 'Look ma, white-space-sensitivity!'", scopepython.execute "Foo().bar()", scope

Page 37: Iron Languages - NYC CodeCamp 2/19/2011

# 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

Page 38: Iron Languages - NYC CodeCamp 2/19/2011

DemoHosting

Page 39: Iron Languages - NYC CodeCamp 2/19/2011

Hosting best-practices• Store scripts where you want with PlatformAdaptationLayer–Makes script file-system operations use database, source-control, whatever …

• Pick isolation level for scripts– In-App-Domain: you totally control– Out-App-Domain: limit permission level– Out of process: total isolation

Page 40: Iron Languages - NYC CodeCamp 2/19/2011

Project Status• IronRuby is working towards 1.9 compat – Rails 3, FFI, static type system integration• IronPython working towards 2.7/3.0 compat – Django, IronClad, and other libraries.• Tooling– IronRuby Gems/Rake support– Debugging w/REPL

• Fully open source– Contributions welcome!

Page 41: Iron Languages - NYC CodeCamp 2/19/2011

How you can participate• Use it at your company, and tell us about it!– Ask the mailing lists and stackoverflow for help– Log any bugs you find

• Contributing to the project– Even if you’re not a compiler hacker … – but hackers welcome!– samples, documentation, blogs, and talks are all welcome also

Page 42: Iron Languages - NYC CodeCamp 2/19/2011

ironpython.net IronPython website & download

dlr.codeplex.com DLR documentation for hosters and language developers

jimmy.schementi.com me

ironruby.net

IronRuby website & download

Page 43: Iron Languages - NYC CodeCamp 2/19/2011

?

Page 44: Iron Languages - NYC CodeCamp 2/19/2011

IRONLanguages

Dynamic Languages for the .NET developer

github.com/IronLanguages

Jimmy [email protected]