Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Hs2Py / GenOpCodes.hs @ 9d049fb4

History | View | Annotate | Download (2.6 kB)

1 f3e38e89 Jose A. Lopes
{-| GenOpCodes handles Python opcode generation.
2 f3e38e89 Jose A. Lopes
3 f3e38e89 Jose A. Lopes
GenOpCodes contains the helper functions that generate the Python
4 f3e38e89 Jose A. Lopes
opcodes as strings from the Haskell opcode description.
5 f3e38e89 Jose A. Lopes
6 f3e38e89 Jose A. Lopes
-}
7 f3e38e89 Jose A. Lopes
8 f3e38e89 Jose A. Lopes
{-
9 f3e38e89 Jose A. Lopes
10 f3e38e89 Jose A. Lopes
Copyright (C) 2011, 2012, 2013 Google Inc.
11 f3e38e89 Jose A. Lopes
12 f3e38e89 Jose A. Lopes
This program is free software; you can redistribute it and/or modify
13 f3e38e89 Jose A. Lopes
it under the terms of the GNU General Public License as published by
14 f3e38e89 Jose A. Lopes
the Free Software Foundation; either version 2 of the License, or
15 f3e38e89 Jose A. Lopes
(at your option) any later version.
16 f3e38e89 Jose A. Lopes
17 f3e38e89 Jose A. Lopes
This program is distributed in the hope that it will be useful, but
18 f3e38e89 Jose A. Lopes
WITHOUT ANY WARRANTY; without even the implied warranty of
19 f3e38e89 Jose A. Lopes
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 f3e38e89 Jose A. Lopes
General Public License for more details.
21 f3e38e89 Jose A. Lopes
22 f3e38e89 Jose A. Lopes
You should have received a copy of the GNU General Public License
23 f3e38e89 Jose A. Lopes
along with this program; if not, write to the Free Software
24 f3e38e89 Jose A. Lopes
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 f3e38e89 Jose A. Lopes
02110-1301, USA.
26 f3e38e89 Jose A. Lopes
27 f3e38e89 Jose A. Lopes
-}
28 f3e38e89 Jose A. Lopes
29 f3e38e89 Jose A. Lopes
module Ganeti.Hs2Py.GenOpCodes (showPyClasses) where
30 f3e38e89 Jose A. Lopes
31 f3e38e89 Jose A. Lopes
import Data.List (intercalate, zipWith4)
32 f3e38e89 Jose A. Lopes
33 f3e38e89 Jose A. Lopes
import Ganeti.OpCodes
34 f3e38e89 Jose A. Lopes
import Ganeti.THH
35 f3e38e89 Jose A. Lopes
36 f3e38e89 Jose A. Lopes
-- | Generates the Python class docstring.
37 f3e38e89 Jose A. Lopes
pyClassDoc :: String -> String
38 f3e38e89 Jose A. Lopes
pyClassDoc doc
39 f3e38e89 Jose A. Lopes
  | length (lines doc) > 1 =
40 f3e38e89 Jose A. Lopes
    "  \"\"\"" ++ doc ++ "\n\n" ++ "  \"\"\"" ++ "\n"
41 f3e38e89 Jose A. Lopes
  | otherwise =
42 f3e38e89 Jose A. Lopes
    "  \"\"\"" ++ doc ++ "\"\"\"" ++ "\n"
43 f3e38e89 Jose A. Lopes
44 f3e38e89 Jose A. Lopes
-- | Generates an opcode parameter in Python.
45 f3e38e89 Jose A. Lopes
pyClassField :: String -> String -> Maybe PyValueEx -> String -> String
46 f3e38e89 Jose A. Lopes
pyClassField name typ Nothing doc =
47 f3e38e89 Jose A. Lopes
  "(" ++ intercalate ", " [show name, "None", typ, show doc] ++ ")"
48 f3e38e89 Jose A. Lopes
  
49 f3e38e89 Jose A. Lopes
pyClassField name typ (Just (PyValueEx def)) doc =
50 f3e38e89 Jose A. Lopes
  "(" ++ intercalate ", " [show name, showValue def, typ, show doc] ++ ")"
51 f3e38e89 Jose A. Lopes
  
52 f3e38e89 Jose A. Lopes
-- | Comma intercalates and indents opcode parameters in Python.
53 f3e38e89 Jose A. Lopes
intercalateIndent :: [String] -> String
54 f3e38e89 Jose A. Lopes
intercalateIndent xs = intercalate "," (map ("\n    " ++) xs)
55 f3e38e89 Jose A. Lopes
56 f3e38e89 Jose A. Lopes
-- | Generates an opcode as a Python class.
57 f3e38e89 Jose A. Lopes
showPyClass :: OpCodeDescriptor -> String
58 f3e38e89 Jose A. Lopes
showPyClass (name, typ, doc, fields, types, defs, docs, dsc) =
59 f3e38e89 Jose A. Lopes
  let
60 f3e38e89 Jose A. Lopes
    baseclass
61 f3e38e89 Jose A. Lopes
      | name == "OpInstanceMultiAlloc" = "OpInstanceMultiAllocBase"
62 f3e38e89 Jose A. Lopes
      | otherwise = "OpCode"
63 f3e38e89 Jose A. Lopes
    opDscField
64 f3e38e89 Jose A. Lopes
      | null dsc = ""
65 f3e38e89 Jose A. Lopes
      | otherwise = "  OP_DSC_FIELD = " ++ show dsc ++ "\n"
66 f3e38e89 Jose A. Lopes
    withLU
67 f3e38e89 Jose A. Lopes
      | name == "OpTestDummy" = "\n  WITH_LU = False"
68 f3e38e89 Jose A. Lopes
      | otherwise = ""
69 f3e38e89 Jose A. Lopes
  in
70 f3e38e89 Jose A. Lopes
   "class " ++ name ++ "(" ++ baseclass ++ "):" ++ "\n" ++
71 f3e38e89 Jose A. Lopes
   pyClassDoc doc ++
72 f3e38e89 Jose A. Lopes
   opDscField ++
73 f3e38e89 Jose A. Lopes
   "  OP_PARAMS = [" ++
74 f3e38e89 Jose A. Lopes
   intercalateIndent (zipWith4 pyClassField fields types defs docs) ++
75 f3e38e89 Jose A. Lopes
   "\n    ]" ++ "\n" ++
76 f3e38e89 Jose A. Lopes
   "  OP_RESULT = " ++ typ ++
77 f3e38e89 Jose A. Lopes
   withLU ++ "\n\n"
78 f3e38e89 Jose A. Lopes
79 f3e38e89 Jose A. Lopes
-- | Generates all opcodes as Python classes.
80 f3e38e89 Jose A. Lopes
showPyClasses :: String
81 f3e38e89 Jose A. Lopes
showPyClasses = concatMap showPyClass pyClasses