代码拉取完成,页面将自动刷新
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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。