Advertisement
prat3492

Untitled

Nov 5th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. hmm, but then we lose out on embedding within C.
  2. IMHO we can bikeshed for the name later :D
  3.  
  4. I have been thinking a bit about grammar for writing the DSL.
  5. We can build LL(1) parser for it (no need to make it powerful i suppose since DSL's mostly have simple syntax).
  6.  
  7. My main concern is how do users expect our toolkit to be ?
  8. They have their API, and now they want to write a DSL using our tool.
  9.  
  10. If i were a user, I would expect to write the DSL spec in a BNF-like notation and provide equivalent C or C++ code
  11. in "semantic action" for the lowering.
  12.  
  13. for instance, suppose I have a graphics lib which uses following sequence to draw a red line:
  14.  
  15. struct line *l = create_line ();
  16. set_color (l, COLOR_RED);
  17. draw_line (l);
  18.  
  19. Instead I want to do it with following DSL stmt:
  20. draw red line;
  21.  
  22. So I would expect to be able to express "draw red line" in BNF notation,
  23. and me to provide the lowering:
  24. SOmething like:
  25.  
  26. %line_nonterminal<struct line *>: "draw" "red" "line"
  27.  
  28. { // starts a semantic action
  29. $$ = create_line ();
  30. set_color ($$, COLOR_RED);
  31. draw_line ($$);
  32. }
  33.  
  34. %queryfunc = DRAWFOO
  35.  
  36. and I use the DSL in following C code:
  37.  
  38. #include "graphics.h"
  39.  
  40. int main (void)
  41. {
  42. DRAWFOO("draw red line");
  43. return 0;
  44. }
  45.  
  46. Our clang plugin would be given the above c code and the grammar description (i don't know how it can find the grammar description but let's leave that aside for moment).
  47. The tool would then do the following:
  48.  
  49. a) Create parser for the grammar on the fly.
  50. b) Look for the queryfunc string, and parse it using our built parser.
  51. c) if a production matches, then replace the queryfunc (string) with the AST form of C code present in semantic actions.
  52.  
  53. Alternatively we could also compile down to plain C by replacing the queryfunc(string) with the corresponding C code in the actions.
  54.  
  55. For instance instead of plugin, we will have a clang tool that will take above code as input and produce following c code as output:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement