Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
Metaprogramming in Julia
Julia Taiwan發起人 杜岳華
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
pre-processor
compiler
assembler
linker
memory
loader
Runtime
Compile time
•
•
•
•
•
pre-processor
compiler
assembler
linker
memory
loader
Compile time
pre-processor
compiler
assembler
linker
Macro processing, augmentation,
file inclusion, language extension
Ge...
Runtime
memory
loader
Loader (part of OS) loads
programs and libraries
Execution
Compile time
pre-processer
compiler
assembler
linker
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate code
...
•
•
•
•
•
Compiler
Lexical analysis
Syntax analysis
Semantic analysis
scannerIdentify tokens
parser
Identify the grammar and
establi...
Compiler
Intermediate code
generation
Optimization
Code generation
Generate machine independent code for
common optimizati...
Source code
Tokens
AST
Lowered AST
Typed AST
LLVM IR
LLVM IR
Instructions
lexing
parsing
lowering
type inference
LLVM code...
•
•
julia> :foo
:foo
julia> :bar
:bar
julia> :foo == Symbol("foo")
true
julia> Symbol("func",10)
:func10
julia>
Symbol(:var,'_',"sym")
:var_sym
•
•
•
•
julia> ex1 = parse(“1 + 1”)
:(1 + 1)
julia> typeof(ex1)
Expr
julia> ex1.head
:call
julia> ex1.args
3-element Array{Any,1}:
:+
1
1
julia> ex1.typ
Any
julia> dump(ex1)
Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol +
2: Int64 1
3: Int64 1
typ: Any
julia> dump(ex)
Expr
head: Symbol call
args: Array{Any}((4,))
1: Symbol +
2: Symbol a
3: Expr
head: Symbol call
args: Arra...
julia> dump(ex)
Expr
head: Symbol call
args: Array{Any}((4,))
1: Symbol +
2: Symbol a
3: Expr
head: Symbol call
args: Arra...
•
julia> eval(:(1 + 2))
3
julia> a = 1
1
julia> ex = :a
:a
julia> eval(ex)
1
julia> a = 1;
julia> ex = :($a + b)
:(1 + b)
•
julia> macro sayhello()
return :( println("Hello, world!") )
end
@sayhello
julia> @sayhello
Hello, world!
julia> macro sayhello(name)
return :( println("Hello, ", $name) )
end
@sayhello
julia> @sayhello("Bob")
Hello, Bob
julia> ...
parse → expressions → macro → new expr. → compile
•
for op = (:+, :*, :&, :|, :$)
eval(quote
($op)(a,b,c) = ($op)(($op)(a,b),c)
end)
end
•
julia> macro decorator(dec, func)
name = func.args[1].args[1]
hiddenname = gensym()
func.args[1].args[1] = hiddenname
qu...
julia> foo(f) = x -> 2*f(x+10);
julia> @decorator foo function bar(x)
return x+1
end
julia> bar(0)
22
•
•
•
•
julia> @generated function foo(x)
println(x)
return :(x*x)
end
foo (generic function with 1 method)
julia> x = foo(2);
Int64
julia> x
4
julia> y = foo("bar");
String
julia> y
"barbar"
•
•
julia> @generated function bar(x)
if x <: Integer
return :(x^2)
else
return :(x)
end
end
bar (generic function with 1 me...
Source code
Tokens
AST
Lowered AST
Typed AST
LLVM IR
LLVM IR
Instructions
lexing
parsing
lowering
type inference
LLVM code...
•
•
•
•
•
•
•
•
201705 metaprogramming in julia
201705 metaprogramming in julia
201705 metaprogramming in julia
201705 metaprogramming in julia
201705 metaprogramming in julia
201705 metaprogramming in julia
201705 metaprogramming in julia
Nächste SlideShare
Wird geladen in …5
×

201705 metaprogramming in julia

360 Aufrufe

Veröffentlicht am

Julia Taiwan 5th meetup

Veröffentlicht in: Technologie
  • Als Erste(r) kommentieren

201705 metaprogramming in julia

  1. 1. Metaprogramming in Julia Julia Taiwan發起人 杜岳華
  2. 2. • • • • • •
  3. 3. • • • • • • • •
  4. 4.
  5. 5.
  6. 6. • • • • •
  7. 7. • •
  8. 8. • • • •
  9. 9. • •
  10. 10. • • • • • • • pre-processor compiler assembler linker memory loader
  11. 11. Runtime Compile time • • • • • pre-processor compiler assembler linker memory loader
  12. 12. Compile time pre-processor compiler assembler linker Macro processing, augmentation, file inclusion, language extension Generate target assembly code Generate relocatable machine code (object file) Links object files and generate executable machine code (binary file)
  13. 13. Runtime memory loader Loader (part of OS) loads programs and libraries Execution
  14. 14. Compile time pre-processer compiler assembler linker Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Optimization Code generation
  15. 15. • • • • •
  16. 16. Compiler Lexical analysis Syntax analysis Semantic analysis scannerIdentify tokens parser Identify the grammar and establish the parsing tree checkerType checking, declaration
  17. 17. Compiler Intermediate code generation Optimization Code generation Generate machine independent code for common optimization Optimization! Some magics…… Generate assembly code Interpreter
  18. 18. Source code Tokens AST Lowered AST Typed AST LLVM IR LLVM IR Instructions lexing parsing lowering type inference LLVM codegen optimize native codegen LLVM IRLLVM IRMacro expansion Generated function JIT
  19. 19. • • julia> :foo :foo julia> :bar :bar
  20. 20. julia> :foo == Symbol("foo") true julia> Symbol("func",10) :func10 julia> Symbol(:var,'_',"sym") :var_sym
  21. 21. • • • •
  22. 22. julia> ex1 = parse(“1 + 1”) :(1 + 1) julia> typeof(ex1) Expr
  23. 23. julia> ex1.head :call julia> ex1.args 3-element Array{Any,1}: :+ 1 1 julia> ex1.typ Any
  24. 24. julia> dump(ex1) Expr head: Symbol call args: Array{Any}((3,)) 1: Symbol + 2: Int64 1 3: Int64 1 typ: Any
  25. 25. julia> dump(ex) Expr head: Symbol call args: Array{Any}((4,)) 1: Symbol + 2: Symbol a 3: Expr head: Symbol call args: Array{Any}((3,)) 1: Symbol * 2: Symbol b 3: Symbol c typ: Any 4: Int64 1 typ: Any julia> ex = :(a + b * c + 1) :(a + b * c + 1)
  26. 26. julia> dump(ex) Expr head: Symbol call args: Array{Any}((4,)) 1: Symbol + 2: Symbol a 3: Expr head: Symbol call args: Array{Any}((3,)) 1: Symbol * 2: Symbol b 3: Symbol c typ: Any 4: Int64 1 typ: Any
  27. 27. • julia> eval(:(1 + 2)) 3
  28. 28. julia> a = 1 1 julia> ex = :a :a julia> eval(ex) 1
  29. 29. julia> a = 1; julia> ex = :($a + b) :(1 + b)
  30. 30. • julia> macro sayhello() return :( println("Hello, world!") ) end @sayhello julia> @sayhello Hello, world!
  31. 31. julia> macro sayhello(name) return :( println("Hello, ", $name) ) end @sayhello julia> @sayhello("Bob") Hello, Bob julia> @sayhello "Bob" Hello, Bob
  32. 32. parse → expressions → macro → new expr. → compile
  33. 33. • for op = (:+, :*, :&, :|, :$) eval(quote ($op)(a,b,c) = ($op)(($op)(a,b),c) end) end
  34. 34. • julia> macro decorator(dec, func) name = func.args[1].args[1] hiddenname = gensym() func.args[1].args[1] = hiddenname quote $func const $(esc(name)) = $dec($hiddenname) end end http://julia-programming-language.2336112.n4.nabble.com/Macro-as-decorators-td40521.html
  35. 35. julia> foo(f) = x -> 2*f(x+10); julia> @decorator foo function bar(x) return x+1 end julia> bar(0) 22
  36. 36. • • • •
  37. 37. julia> @generated function foo(x) println(x) return :(x*x) end foo (generic function with 1 method)
  38. 38. julia> x = foo(2); Int64 julia> x 4
  39. 39. julia> y = foo("bar"); String julia> y "barbar"
  40. 40.
  41. 41. • julia> @generated function bar(x) if x <: Integer return :(x^2) else return :(x) end end bar (generic function with 1 method) julia> bar(4) 16 julia> bar("baz") "baz"
  42. 42. Source code Tokens AST Lowered AST Typed AST LLVM IR LLVM IR Instructions lexing parsing lowering type inference LLVM codegen optimize native codegen LLVM IRLLVM IR Expr Symbol @code_lowered @code_typed @code_llvm @code_native @code_warntype
  43. 43. • • • •
  44. 44. • • • •

×