# gl2lua **Repository Path**: sharkbaby/gl2lua ## Basic Information - **Project Name**: gl2lua - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2021-02-21 - **Last Updated**: 2021-02-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 项目详情 ## 目录 `antlr`:`antlr.jar文件,g4文件与antlr生成的代码` `eval_visitor_method`:`对应文法的处理方法目录` `luafiles`:`暂时的gl2lua转换测试目录` `test`:`测试文件目录` `utlis`:`辅助功能函数目录` `auto_test.py`:`自动测试文件` `gl_to_gua`:`gualang转lua的主文件` #### 其中,eval_visitor_method目录下的文件 `arithmetic`:`算术运算相关方法` `common`:`公共方法` `dict`:`字典相关方法` `function`:`函数相关方法` `guaclass`:`类相关方法` `ifelse`:`if条件相关方法` `list`:`数组相关方法` `literal`:`基本类型数据相关方法` `loop`:`循环相关方法` ## 代码解释 # 如何使用G4 ## 安装 ### 1. 首先你需要安装java和python #### java java windows 与 mac 的安装文件在群文件 https://www.runoob.com/w3cnote/windows10-java-setup.html win10 用这个安装 java, jdk 就用群里刚才发的就行, #### python mac: https://www.python.org/ftp/python/3.8.1/python-3.8.1-macosx10.9.pkg windows: https://www.python.org/ftp/python/3.8.1/python-3.8.1-amd64.exe ### 2. 然后需要安装一个antlr-runtime库 ```bash pip3 install antlr4-python3-runtime llvmlite -i https://pypi.doubanio.com/simple/ ``` ## 使用 ### 1.用 antlr 读取 Gua.g4 语法文件生成 Python3 的解析器代码 java -jar antlr.jar -visitor -no-listener -Dlanguage=Python3 Gua.g4 ### 2.编写visitor程序 看g4文件,修改`EvalVisitor`下对应的方法,比如: g4文件中的 `#LabelExpressionLiteralInt`,对应`EvalVisitor`中的`visitLabelExpressionLiteralInt ` ```python class EvalVisitor(GuaVisitor): def __init__(self): self.types = { } self.d = {} self.scope = 0 self.ans_string = "" def build_ans_string(self, output): if self.scope == 1: self.ans_string = self.ans_string + output + "\n" self.scope -= 1 def visitLabelExpressionLiteralInt(self, ctx): self.scope += 1 output = labelExpressionLiteralInt(ctx) self.build_ans_string(output) return output ``` 此外`build_ans_string`方法是为了处理以下这种情况: ```js var a = 1 var b = 2 const main = function() { } ``` 加一个计数,保证`var a = 1`和`var b = 2`返回的结果也能够被保存,避免最终保存下来的只有最后一个`const main ...` ### 3.测试 需要安装nose和watchdog pip3 install watchdog pip3 install nose