summaryrefslogblamecommitdiff
path: root/scripts/addfile.py
blob: 2de21c940ded1912b319897e5e3227ca6a8b8a30 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11





                                  
                  



                              

                                        


                                     

                                         



                                          


                                      





                                           

                               

                                 

                               




                          













                                                                                                     
                                                               

                                                                 




                                                                




                                                                 

                                 

                                                                                                  
                                  



                                                               




                                 
     
                  


                                                
                 
                                  



                                                    
                                             
     
                    

                                  
                     
                          



                                                             
 
                       
                                                                       
                         
                                                                                                                  

                           


                                                           
                                                                   

                                                  

                                                                                              
#! /usr/bin/env python2
# -*- coding: utf8 -*-
#
# Copyright (C) 2015  Andrei Karas

import sys
import subprocess;

def readFile(path):
    with open(path, "r") as f:
        return f.read()
def writeFile(fileName, data, opts="w"):
    with open(fileName, opts) as w:
        w.write(data)
def firstBigLetter(text):
    return text[0].upper() + text[1:]
def gitAddFile(path):
    subprocess.call(["git", "add", path])

if len(sys.argv) < 2 or len(sys.argv) > 3:
    print "Usage:"
    print "  addfile.py [dir] nodename"
    print "Examples:"
    print "  addfile.py expr TestExpr"
    print "  addfile.py TestNode"
    exit(1)

nodeTemplate = readFile("tpl/node.tpl")
parserTemplate = readFile("tpl/parser.tpl")
if len(sys.argv) == 2:
    dirName = ""
    typeName = sys.argv[1]
    nodeName = typeName.lower()
else:
    dirName = sys.argv[1].lower()
    typeName = sys.argv[2]
    nodeName = typeName.lower()
suffixSize = 0
parserAdditionalCode1 = ""
parserAdditionalCode2 = ""
parserBaseInclude = ""

if dirName != "":
    if nodeName[-4:] == "decl":
        suffixSize = 4
        parserAdditionalCode1 = "    fillLocation(node);\n    fillDeclLabel(node);\n"
        parserAdditionalCode2 = "\n    fillDeclAutoGenerated(node);\n    fillDeclAttributes(node);\n"
        parserBaseInclude = "#include \"parsers/base/decl.h\"\n"
    elif nodeName[-4:] == "type":
        suffixSize = 4
        parserAdditionalCode2 = "\n    fillTypeName(node);\n    fillTypeAttributes(node);\n"
        parserBaseInclude = "#include \"parsers/base/type.h\"\n"
    elif nodeName[-3:] == "cst":
        suffixSize = 3
    elif nodeName[-4:] == "expr":
        suffixSize = 4
        parserAdditionalCode1 = "    fillExprLocation(node);\n"
        parserAdditionalCode2 = "\n    fillExprOperands(node);\n"
        parserBaseInclude = "#include \"parsers/base/expr.h\"\n"
    elif nodeName[-3:] == "ref":
        suffixSize = 3
        parserAdditionalCode1 = "    fillRefLocation(node);\n"
        parserAdditionalCode2 = "\n    fillRefOperands(node);\n"
        parserBaseInclude = "#include \"parsers/base/ref.h\"\n"
    elif nodeName[-5:] == "block":
        suffixSize = 5
        parserAdditionalCode1 = "    fillExprLocation(node);\n"
        parserAdditionalCode2 = "\n    fillExprOperands(node);\n"
        parserBaseInclude = "#include \"parsers/base/expr.h\"\n"
    elif nodeName[-4:] == "stmt":
        suffixSize = 4
        parserAdditionalCode1 = "    fillExprLocation(node);\n"
        parserBaseInclude = "#include \"parsers/base/expr.h\"\n#include \"parsers/base/stmt.h\"\n"
        parserAdditionalCode2 = ""
    elif nodeName[-3:] == "vec":
        suffixSize = 3
        parserBaseInclude = "#include \"parsers/base/vec.h\"\n"
        parserAdditionalCode2 = ""
    elif nodeName[-4:] == "list":
        suffixSize = 4
    else:
        print "unknown nodename"
        exit(1)
else:
    suffixSize = 0

word1 = nodeName[0 : len(nodeName) - suffixSize]
word2 = nodeName[- suffixSize:]
if dirName != "":
    fileName = word1 + "_" + word2
    guardHeader = (dirName + "_" + nodeName).upper()
    baseName = dirName
    baseTypeName = firstBigLetter(dirName)
    nodeInclude = dirName + "/" + fileName
    enumValue = (word1 + "_" + word2).upper()
else:
    fileName = word1
    guardHeader = nodeName.upper()
    baseName = "node"
    baseTypeName = ""
    nodeInclude = fileName
    enumValue = word1.upper()

nodeFileName = "../src/nodes/{0}.h".format(nodeInclude)
parserFileName = "../src/parsers/{0}.cpp".format(nodeInclude)

writeFile(nodeFileName,
    nodeTemplate.format(guardHeader, baseName, typeName, baseTypeName))
writeFile(parserFileName,
    parserTemplate.format(typeName, nodeInclude, parserBaseInclude, parserAdditionalCode1, parserAdditionalCode2))
gitAddFile(nodeFileName);
gitAddFile(parserFileName);
writeFile("../src/includes/nodeincludes.h",
    "#include \"nodes/{0}.h\"\n".format(nodeInclude), "a");
writeFile("../src/includes/nodeshandling.inc",
    "handleNodeType({0}, {1})\n".format(enumValue, typeName), "a");
writeFile("../src/includes/parserdefines.inc",
    "parserDefine({0});\n".format(typeName), "a");
writeFile("../src/Makefile.files",
    " \\\n      nodes/{0}.h \\\n      parsers/{0}.cpp".format(nodeInclude, nodeInclude), "a");