Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

of

201705 metaprogramming in julia Slide 1 201705 metaprogramming in julia Slide 2 201705 metaprogramming in julia Slide 3 201705 metaprogramming in julia Slide 4 201705 metaprogramming in julia Slide 5 201705 metaprogramming in julia Slide 6 201705 metaprogramming in julia Slide 7 201705 metaprogramming in julia Slide 8 201705 metaprogramming in julia Slide 9 201705 metaprogramming in julia Slide 10 201705 metaprogramming in julia Slide 11 201705 metaprogramming in julia Slide 12 201705 metaprogramming in julia Slide 13 201705 metaprogramming in julia Slide 14 201705 metaprogramming in julia Slide 15 201705 metaprogramming in julia Slide 16 201705 metaprogramming in julia Slide 17 201705 metaprogramming in julia Slide 18 201705 metaprogramming in julia Slide 19 201705 metaprogramming in julia Slide 20 201705 metaprogramming in julia Slide 21 201705 metaprogramming in julia Slide 22 201705 metaprogramming in julia Slide 23 201705 metaprogramming in julia Slide 24 201705 metaprogramming in julia Slide 25 201705 metaprogramming in julia Slide 26 201705 metaprogramming in julia Slide 27 201705 metaprogramming in julia Slide 28 201705 metaprogramming in julia Slide 29 201705 metaprogramming in julia Slide 30 201705 metaprogramming in julia Slide 31 201705 metaprogramming in julia Slide 32 201705 metaprogramming in julia Slide 33 201705 metaprogramming in julia Slide 34 201705 metaprogramming in julia Slide 35 201705 metaprogramming in julia Slide 36 201705 metaprogramming in julia Slide 37 201705 metaprogramming in julia Slide 38 201705 metaprogramming in julia Slide 39 201705 metaprogramming in julia Slide 40 201705 metaprogramming in julia Slide 41 201705 metaprogramming in julia Slide 42 201705 metaprogramming in julia Slide 43 201705 metaprogramming in julia Slide 44 201705 metaprogramming in julia Slide 45 201705 metaprogramming in julia Slide 46 201705 metaprogramming in julia Slide 47 201705 metaprogramming in julia Slide 48 201705 metaprogramming in julia Slide 49 201705 metaprogramming in julia Slide 50 201705 metaprogramming in julia Slide 51
Upcoming SlideShare
What to Upload to SlideShare
Next
Download to read offline and view in fullscreen.

4 Likes

Share

Download to read offline

201705 metaprogramming in julia

Download to read offline

Julia Taiwan 5th meetup

Related Books

Free with a 30 day trial from Scribd

See all

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. • • • •
  • appleparan

    Sep. 5, 2017
  • DubaiEscortBunniesUAE

    May. 26, 2017
  • edhsu14

    May. 21, 2017
  • mosuchiang

    May. 21, 2017

Julia Taiwan 5th meetup

Views

Total views

427

On Slideshare

0

From embeds

0

Number of embeds

55

Actions

Downloads

6

Shares

0

Comments

0

Likes

4

×