PageRenderTime 25ms CodeModel.GetById 10ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

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