Want more features on Pastebin? Sign Up, it's FREE!

AST.hs

By: VictorCacciari on Feb 16th, 2012  |  syntax: Haskell  |  size: 3.38 KB  |  views: 83  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. {-
  2.         ASTCG (Abstract Syntax Tree Code Generator).
  3.         This is a small program that aims to use a very simple grammar
  4.         to generate data structures (for AST obviously) in a variety of target languages.
  5.        
  6.         IMPORTANT:
  7.                 This is a pre-release version of the code, there is no parser yet.
  8.                 The AST must be built inside the haskell interpreter using the data types defined
  9.                 in this module.
  10.        
  11.        
  12.     Copyright (C) 2012 Victor Cacciari Miraldo
  13.  
  14.     This program is free software: you can redistribute it and/or modify
  15.     it under the terms of the GNU General Public License as published by
  16.     the Free Software Foundation, either version 3 of the License, or
  17.     (at your option) any later version.
  18.  
  19.     This program is distributed in the hope that it will be useful,
  20.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.     GNU General Public License for more details.
  23.  
  24.     You should have received a copy of the GNU General Public License
  25.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  26. -}
  27. module AST where
  28.  
  29. {-
  30.         A ASTField is something that contains data. It must have a name and a type.
  31.         The type must be compilant in the target language.
  32. -}
  33. data ASTField = ASTField String String deriving Show
  34.  
  35. {-
  36.         Well, one node of the same type can have different (and disjoint) cases.
  37.         For instance, one atom can be a variable or a number.
  38.         In C, this will correspond to a union.
  39.        
  40.         A "kind" is called by it's name, and itself can be a composite field (struct)
  41.         or a single value.
  42. -}
  43. data ASTKind = ASTKind String [ASTField]
  44.                         |  ASTAtomKind ASTField
  45.  
  46. -- Since ASTKind can assume different forms, we should provide some interface functions
  47. -- for making the implementation of the target translators easier.
  48. kindName :: ASTKind -> String
  49. kindName (ASTKind n _) = n
  50. kindName (ASTAtomKind (ASTField _ n)) = n
  51.  
  52. kindFields :: ASTKind -> [ASTField]
  53. kindFields (ASTKind _ f) = f
  54. kindFields (ASTAtomKind f) = [f]
  55.  
  56. {-
  57.         A ASTNode will correspond to a struct/class in the target language,
  58.         It must have a name, a list of fields and a list of kind-specific fields,
  59.         which will correspond to a union (in C).
  60. -}
  61. data ASTNode = ASTNode String [ASTField] [ASTKind]
  62.  
  63. {-
  64.         Finally, a complete grammar needs different nodes, so one ASTProgram
  65.         will be a list of nodes, a header and a footer.
  66.        
  67.         Both the header and the footer must be compilant code in the target language,
  68.         they'll be added before and after the declaration of the AST created data types.
  69.        
  70.         TODO: Add the possibility to use options.
  71. -}
  72. data ASTProgram = ASTProgram String String [ASTNode]
  73.  
  74. {-
  75.         TODO: the parsing interface to generate the ASTProgram.
  76. -}
  77.  
  78.  
  79. {-
  80.         Example program.
  81.         Used for testing purposes only.
  82. -}
  83. expKinds = [
  84.         ASTKind "binop" [(ASTField "int" "op"), (ASTField "Exp*" "lh"), (ASTField "Exp*" "rh")],
  85.         ASTAtomKind (ASTField "Symbol*" "builtin"),
  86.         ASTKind "app" [(ASTField "Symbol*" "f"), (ASTField "Exp*" "arg")]
  87.         ]
  88.        
  89. expNode = ASTNode "Exp" [(ASTField "int" "line")] expKinds
  90.                        
  91.                        
  92. varNode = ASTNode "VarDecl" [(ASTField "int" "line"), (ASTField "Symbol*" "name")] []
  93.  
  94. myProg = ASTProgram
  95.         "#ifndef EXP_TEST_H\n#define EXP_TEST_H\n\n#include \"test.h\"\n\n"    
  96.         "#endif\n\n\n/* I M P L E M E N T A T I O N */\n#include \"exp_test.h\"\n#define ALLOC(type) (type*)my_allocator(sizeof(type))\n\n"
  97.         [expNode, varNode]
clone this paste RAW Paste Data