1 / 19

Internal Domain-Specific Languages

Internal Domain-Specific Languages. Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010. Example internal DSLs. jQuery rfig rake. jQuery. Before jQuery var nodes = document.getElementsByTagName ('a');

geordi
Download Presentation

Internal Domain-Specific Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Internal Domain-Specific Languages Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

  2. Example internal DSLs • jQuery • rfig • rake

  3. jQuery Before jQuery var nodes = document.getElementsByTagName('a'); for (vari = 0; i < nodes.length; i++) { var a = nodes[i]; a.addEventListener('mouseover', function(event) { event.target.style.backgroundColor=‘orange'; }, false ); a.addEventListener('mouseout', function(event) { event.target.style.backgroundColor=‘white'; }, false ); } jQuery abstracts iteration and events jQuery('a').hover( function() { jQuery(this).css('background-color', 'orange'); }, function() { jQuery(this).css('background-color', 'white'); } );

  4. rfig

  5. rfig: slide authoring DSL Embedding a domain-specific language (DSL) into Ruby. see slide 8 in http://cs164fa09.pbworks.com/f/01-rfig-tutorial.pdf …

  6. The animation in rfig, a Ruby-based language slide!('Overlays', 'Using overlays, we can place things on top of each other.', 'The pivot specifies the relative positions', 'that should be used to align the objects in the overlay.', overlay('0 = 1', hedge.color(red).thickness(2)).pivot(0, 0), staggeredOverlay(true, # True means that old objects disappear 'the elements', 'in this', 'overlay should be centered', nil).pivot(0, 0), cr, pause, # pivot(x, y): -1 = left, 0 = center, +1 = right staggeredOverlay(true, 'whereas the ones', 'here', 'should be right justified', nil).pivot(1, 0), nil) { |slide| slide.label('overlay').signature(8) } rfig was developed by Percy Liang, now a Berkeley student

  7. rake

  8. rake rake: an internal DSL, embedded in Ruby Author: Jim Weirich functionality similar to make • has nice extensions, and flexibility, since it's embedded • ie can use any ruby commands even the syntax is close (perhaps better): • embedded in Ruby, so all syntax is legal Ruby http://martinfowler.com/articles/rake.html

  9. Example rake file task :codeGen do # do the code generation end task :compile => :codeGen do #do the compilation end task :dataLoad => :codeGen do # load the test data end task :test => [:compile, :dataLoad] do # run the tests end

  10. How to implement internal DSLs First, define simple OO programming in 164. Then we show how to build a jQuery like language

  11. Initial OO extensions to 164 defmyObj = {} # build an object myObj.field = 3 myObj.foo = lambda(self, a, b) { print self.field + a } myObj.foo(myObj,1,2) myObj:foo(1,2) p:foo(x,y) desugars to p.foo(p,x,y) p.foodesugarsto p[“foo”]

  12. 164Query # usage: q('myklass'):fill('ivory2'):font('Helvetica 8') def Q = {} # empty Query object Q.each= lambda(self,f) { for (n in preorder(window)) { if ("klass" in n) { if (n.klass == self.klass) { f(n) } } } # return the query object to allow call chaining self } def q(clss) { def query = copy(Q) query.klass=clss query }

  13. Continued Q.fill = lambda(self,color) { self:each(lambda(node) { node.fill= color }) } Q.font = lambda(self,font) { self:each(lambda(node) { for (n in preorder(node)) { if (n:class() == Word) { n.font = font } } }) }

  14. Example rake file task :codeGen do # do the code generation end task :compile => :codeGen do #do the compilation end task :dataLoad => :codeGen do # load the test data end task :test => [:compile, :dataLoad] do # run the tests end

  15. How is rake legal ruby? Deconstructing rake (teaches us a lot about Ruby): task :dataLoad => :codeGen do # load the test data end task :test => [:compile, :dataLoad] do # run the tests end

  16. Two kinds of tasks File task: dependences between files (like in main) file 'build/dev/rake.html' => 'dev/rake.xml' do |t| require 'paper' maker = PaperMaker.newt.prerequisites[0], t.name maker.run end

  17. Two kinds of tasks Rake task: dependences between jobs task :build_refact => [:clean] do target = SITE_DIR + 'refact/' mkdir_p target, QUIET require 'refactoringHome' OutputCapturer.new.run {run_refactoring} end

  18. Orthogonalize dependences and rules task :second do #second's body end task :first do #first's body end task :second => :first

  19. General rules Sort of like make's %.c : %.c BLIKI = build('bliki/index.html') FileList['bliki/*.xml'].each do |src| file BLIKI => src end file BLIKI do #code to build the bliki end

More Related