/scripts/parse.lsp

http://github.com/blackdog66/bdog-gtk · Lisp · 119 lines · 91 code · 28 blank · 0 comment · 0 complexity · 1251f9b94dbf4037fc9cd78723527da3 MD5 · raw file

  1. (setq f (read-file "cfg"))
  2. (define (getGtkFunctions)
  3. (find-all "FUNCTION_NAME = gtk_(.*)" f $1))
  4. (define (getGladeFunctions)
  5. (find-all "FUNCTION_NAME = glade_(.*)" f $1))
  6. (define (cam-case n)
  7. (join (map title-case (parse n "_"))))
  8. (define (change-types l)
  9. (map (fn (el)
  10. (cond
  11. ((= el "NONE") "Void")
  12. ((= el "VOID") "Void")
  13. ((= el "WIDGET") "String")
  14. ((= el "STRING") "String")
  15. ((= el "INT") "Int")
  16. ((= el "BOOL") "Bool")
  17. ((= el "BASE64") "String")
  18. ((= el "NULL") "Widget")
  19. ((or (= el "DOUBLE") (= el "FLOAT")) "Float")
  20. ("Dynamic")))
  21. l))
  22. (define (wrap el)
  23. (cond
  24. ((= el "String") (string "'" el "'"))
  25. ((el))))
  26. (define (add-param-conversion el)
  27. (string "Server.in" (first el) "(p" (el 1) ")"))
  28. (define (body-params p)
  29. (if (> (length p) 0)
  30. (0 -6 (join
  31. (map (fn (el) (string "+" (add-param-conversion el) " + \" \""))
  32. (zip p (sequence 1 (length p))))))
  33. ""))
  34. (define (get-prefix apiType)
  35. (if (= apiType "Gtk") "gtk_" "glade_"))
  36. (define (body f r p apiType)
  37. (letn ((fncall (string "Server.send(\""
  38. (get-prefix apiType)
  39. f " \"" (body-params p) (if (= r "Void") "" ",fn") ")")))
  40. (string fncall ";")))
  41. (define (zip)
  42. (transpose (args)))
  43. (define (head-params p)
  44. (join
  45. (if (> (length p) 0)
  46. (map (fn (el) (string "p" (first el) ":" (el 1)))
  47. (zip (sequence 1 (length p)) p))
  48. (list)) ","))
  49. (define (first-char-lower s)
  50. (string (lower-case (first s)) (1 s)))
  51. (define (comma? s)
  52. (if (= (length s) 0) "" ","))
  53. (define (header n r p)
  54. (letn ((hps (head-params p)))
  55. (append "public static function "
  56. (first-char-lower (cam-case n))
  57. "(" hps (if (= r "Void") ")" (append (comma? hps) "fn:" r "->Void)"))
  58. ":"
  59. "Void"
  60. " {\n" )))
  61. (define (genApi generator apiType)
  62. (map (fn (f)
  63. (letn (
  64. (p (map trim (parse f ",")))
  65. (fs (change-types p))
  66. (fnname (p 0))
  67. (retType (fs 2))
  68. (prms (4 fs)))
  69. (append
  70. (header fnname retType prms)
  71. (body fnname retType prms apiType)
  72. "\n}"
  73. )))
  74. (generator)))
  75. (define (fheader cl)
  76. (string
  77. "
  78. package gtk;
  79. import ui.Widget;
  80. import gtk.Server;
  81. import haxe.io.Bytes;
  82. class " cl " {\n"))
  83. (setq out-dir "./")
  84. (define (gen className generator)
  85. (write-file (append out-dir "/" className ".hx")
  86. (string
  87. (fheader className)
  88. (join (genApi generator className) "\n")
  89. "\n}")))
  90. (gen "Gtk" getGtkFunctions)
  91. (gen "Glade" getGladeFunctions)