lua源码到c的简单转换

import glob,re

for f in glob.glob("*.lua"):
    fout = file("%s.out"%f[:-4],"w+")
    for line in file(f):
        if re.match(r"^\s*$", line):
            fout.write(line)
            continue
        if re.match(r"^\s*--", line):
            fout.write("//"+line)
            continue
        line=line.rstrip()
        line=re.sub(r"\bif\b", 'if (', line)
        line=re.sub(r"\bthen\b", ' ) {', line)
        line=re.sub(r"\bend\b", '}', line)
        line=re.sub(r"\belse\b", '} else {', line)
        line=re.sub(r"\belseif\b", '} else if (', line)
        if re.match(r".*\{.*\}", line):
            line=re.sub(r"\}", '; }', line)
        if not re.match(r".*[{}]$", line):
            line+=";"
        fout.write(line+"\n")
        
很不完善,仅支持单个函数体内转换,临时变量声明没有处理

你可能感兴趣的:(算法)