diff --git a/packages/beize_compiler/example/project/main.beize b/packages/beize_compiler/example/project/main.beize index bd7595f..032cc4f 100644 --- a/packages/beize_compiler/example/project/main.beize +++ b/packages/beize_compiler/example/project/main.beize @@ -1,4 +1,5 @@ import "./benchmark.beize" as benchmark; +import "./benchmark.beize" as benchmarask; benchmarker := benchmark.createBenchmarker(); benchmarker.start(); diff --git a/packages/beize_compiler/lib/compiler/compiler.dart b/packages/beize_compiler/lib/compiler/compiler.dart index 1ff15c3..65ebe17 100644 --- a/packages/beize_compiler/lib/compiler/compiler.dart +++ b/packages/beize_compiler/lib/compiler/compiler.dart @@ -278,8 +278,9 @@ class BeizeCompiler { compiler.prepare(isAsync: true); final int nameIndex = compiler.makeConstant(compiler.resolveRelativePath(fullPath)); - compiler.makeConstant(compiler.currentFunction); + final int functionIndex = compiler.makeConstant(compiler.currentFunction); compiler.modules.add(nameIndex); + compiler.modules.add(functionIndex); await compiler.compile(); return BeizeProgramConstant( modules: compiler.modules, diff --git a/packages/beize_compiler/lib/compiler/parser/parser.dart b/packages/beize_compiler/lib/compiler/parser/parser.dart index b6926c0..b707548 100644 --- a/packages/beize_compiler/lib/compiler/parser/parser.dart +++ b/packages/beize_compiler/lib/compiler/parser/parser.dart @@ -210,10 +210,10 @@ abstract class BeizeParser { compiler.resolveImportPath(compiler.previousToken.literal as String); final String moduleName = compiler.resolveRelativePath(modulePath); int moduleIndex = -1; - for (int i = 0; i < compiler.modules.length; i++) { + for (int i = 0; i < compiler.modules.length; i += 2) { final int x = compiler.modules[i]; if (compiler.constants[x] == moduleName) { - moduleIndex = x; + moduleIndex = i; } } compiler.consume(BeizeTokens.asKw); @@ -229,8 +229,10 @@ abstract class BeizeParser { modulePath, isAsync: false, ); - compiler.makeConstant(moduleCompiler.currentFunction); + final int functionIndex = + compiler.makeConstant(moduleCompiler.currentFunction); compiler.modules.add(nameIndex); + compiler.modules.add(functionIndex); await moduleCompiler.compile(); } compiler.emitCode(moduleIndex); diff --git a/packages/beize_compiler/lib/disassembler/disassembler.dart b/packages/beize_compiler/lib/disassembler/disassembler.dart index 989ba4f..9e59d8f 100644 --- a/packages/beize_compiler/lib/disassembler/disassembler.dart +++ b/packages/beize_compiler/lib/disassembler/disassembler.dart @@ -88,8 +88,8 @@ class BeizeDisassembler { case BeizeOpCodes.opImport: final int moduleIndex = chunk.codeAt(ip + 1); - final int nameIndex = program.moduleAt(moduleIndex); final int asIndex = chunk.codeAt(ip + 2); + final int nameIndex = program.modules[moduleIndex]; final String moduleName = program.constantAt(nameIndex) as String; final BeizeConstant importName = program.constantAt(asIndex); writeInstruction( @@ -124,11 +124,14 @@ class BeizeDisassembler { static void disassembleProgram(final BeizeProgramConstant program) { final BeizeDisassemblerOutput output = BeizeDisassemblerConsoleOutput(); - for (final int nameIndex in program.modules) { + for (int i = 0; i < program.modules.length; i += 2) { + final int nameIndex = program.modules[i]; + final int functionIndex = program.modules[i + 1]; final String moduleName = program.constantAt(nameIndex) as String; final BeizeFunctionConstant function = - program.constantAt(nameIndex + 1) as BeizeFunctionConstant; - output.write('> $moduleName ${nameIndex == 0 ? "(entrypoint)" : ""}'); + program.constantAt(functionIndex) as BeizeFunctionConstant; + output.write( + '> $moduleName (constant [$nameIndex]) at constant [$functionIndex] ${nameIndex == 0 ? "(entrypoint)" : ""}'); final BeizeDisassembler disassembler = BeizeDisassembler(program, function.chunk, output); disassembler.dissassemble(); diff --git a/packages/beize_compiler/tests/utils.dart b/packages/beize_compiler/tests/utils.dart index b62ebd5..e609054 100644 --- a/packages/beize_compiler/tests/utils.dart +++ b/packages/beize_compiler/tests/utils.dart @@ -40,22 +40,23 @@ class BeizeTestProgram { BeizeTestChunk addModule( final int moduleIndex, - final int nameIndex, + final int moduleNameIndex, final String moduleName, + final int functionIndex, ) { int missingCount = moduleIndex - modules.length + 1; while (missingCount > 0) { modules.add(0); missingCount--; } - modules[moduleIndex] = nameIndex; + modules[moduleIndex] = moduleNameIndex; final BeizeTestChunk chunk = BeizeTestChunk( this, BeizeChunk.empty(moduleIndex), ); - addConstant(nameIndex, moduleName); + addConstant(moduleNameIndex, moduleName); addConstant( - nameIndex + 1, + functionIndex, BeizeFunctionConstant( isAsync: true, arguments: [], @@ -104,13 +105,14 @@ String buildExpectedProgramCode( final StringBuffer output = buffer ?? StringBuffer(); output.write('final BeizeTestProgram expectedProgram = BeizeTestProgram();'); for (int i = 0; i < program.modules.length; i++) { - final int nameIndex = program.moduleAt(i); - final String moduleName = program.constantAt(i) as String; + final int moduleNameIndex = program.modules[i]; + final int functionIndex = program.modules[i + 1]; + final String moduleName = program.constantAt(moduleNameIndex) as String; final BeizeFunctionConstant function = - program.constantAt(nameIndex + 1) as BeizeFunctionConstant; + program.constantAt(functionIndex) as BeizeFunctionConstant; final String variableName = 'expectedModule$i'; output.write( - "final BeizeTestChunk $variableName = expectedProgram.addModule($i, $nameIndex, '${escapedString(moduleName, "'")}');", + "final BeizeTestChunk $variableName = expectedProgram.addModule($i, $moduleNameIndex, '${escapedString(moduleName, "'")}', $functionIndex);", ); buildExpectedChunkCode( program, @@ -188,8 +190,7 @@ String buildExpectedChunkCode( case BeizeOpCodes.opImport: final int moduleIndex = chunk.codeAt(ip + 1); final int asIndex = chunk.codeAt(ip + 2); - final int nameIndex = program.moduleAt(moduleIndex); - final String name = program.constantAt(nameIndex) as String; + final String name = program.constantAt(asIndex) as String; output.writeln('$variableName.addCode($moduleIndex);'); output.writeln("$variableName.addConstant($asIndex, '$name');"); bump += 2;