1 Star 0 Fork 0

issacsonjj/lox-py

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test_codegen.py 1.81 KB
一键复制 编辑 原始数据 按行查看 历史
issacsonjj 提交于 2022-12-21 16:01 . super的访问
import os
import unittest
from parse.codegen import CodeGenerator
from parse.lexer import Lexer
from parse.parser import Parser
from parse.resolver import Resolver
from vm.value import ObjType
class CodeGenTestCase(unittest.TestCase):
def gen_code(self, file):
with open(file, 'r') as f:
source = f.read()
lexer = Lexer()
tokens = lexer.parse(source)
parser = Parser(tokens)
stmts = parser.parse()
resolver = Resolver(stmts)
resolver.resolve()
for stmt in stmts:
print(stmt)
code_gen = CodeGenerator(stmts)
vm = code_gen.generate_code()
return vm
def setUp(self) -> None:
self.files = []
for root, _, files in os.walk('test_cases/codegen'):
for f in files:
self.files.append(os.path.join(root, f))
def exec_file(self, file: str):
files = filter(lambda f: f.endswith(file), self.files)
for file in files:
vm = self.gen_code(file)
for obj in vm.objects:
if obj.obj_type == ObjType.FUNCTION:
obj.func_value().chunk.disassemble()
vm.debug = False
vm.run()
def test_basic(self):
self.exec_file('basic.lox')
def test_while_loop(self):
self.exec_file('while.lox')
def test_function_basic(self):
self.exec_file('function_basic.lox')
def test_for_loop(self):
self.exec_file('for.lox')
def test_if(self):
self.exec_file('if.lox')
def test_closure(self):
self.exec_file('closure.lox')
def test_class(self):
self.exec_file('class.lox')
def test_inherit(self):
self.exec_file('inherit.lox')
if __name__ == '__main__':
unittest.main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/issasonjj/lox-py.git
git@gitee.com:issasonjj/lox-py.git
issasonjj
lox-py
lox-py
master

搜索帮助