Skip to content

Commit

Permalink
feat: import only
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrouge committed Dec 18, 2023
1 parent 8ef386c commit 3fb09d9
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 25 deletions.
5 changes: 2 additions & 3 deletions packages/beize_compiler/example/project/main.beize
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "./benchmark.beize" as benchmark;
import "./benchmark.beize" as benchmarask;
import "./benchmark.beize" only createBenchmarker;

benchmarker := benchmark.createBenchmarker();
benchmarker := createBenchmarker();
benchmarker.start();

increment := -> i {
Expand Down
32 changes: 25 additions & 7 deletions packages/beize_compiler/lib/compiler/parser/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,6 @@ abstract class BeizeParser {
moduleIndex = i;
}
}
compiler.consume(BeizeTokens.asKw);
compiler.consume(BeizeTokens.identifier);
final int asIndex = parseIdentifierConstant(compiler);
compiler.consume(BeizeTokens.semi);
compiler.emitOpCode(BeizeOpCodes.opImport);
if (moduleIndex == -1) {
moduleIndex = compiler.modules.length;
final int nameIndex = compiler.makeConstant(moduleName);
Expand All @@ -235,8 +230,31 @@ abstract class BeizeParser {
compiler.modules.add(functionIndex);
await moduleCompiler.compile();
}
compiler.emitCode(moduleIndex);
compiler.emitCode(asIndex);
if (compiler.match(BeizeTokens.onlyKw)) {
bool cont = true;
while (cont && !compiler.check(BeizeTokens.semi)) {
compiler.emitOpCode(BeizeOpCodes.opImport);
compiler.emitCode(moduleIndex);
compiler.consume(BeizeTokens.identifier);
final int onlyIndex = parseIdentifierConstant(compiler);
compiler.emitOpCode(BeizeOpCodes.opConstant);
compiler.emitCode(onlyIndex);
compiler.emitOpCode(BeizeOpCodes.opGetProperty);
compiler.emitOpCode(BeizeOpCodes.opDeclare);
compiler.emitCode(onlyIndex);
cont = compiler.match(BeizeTokens.comma);
}
compiler.consume(BeizeTokens.semi);
} else {
compiler.emitOpCode(BeizeOpCodes.opImport);
compiler.emitCode(moduleIndex);
compiler.consume(BeizeTokens.asKw);
compiler.consume(BeizeTokens.identifier);
final int asIndex = parseIdentifierConstant(compiler);
compiler.consume(BeizeTokens.semi);
compiler.emitOpCode(BeizeOpCodes.opDeclare);
compiler.emitCode(asIndex);
}
}

static void parseMatchableStatement(
Expand Down
6 changes: 2 additions & 4 deletions packages/beize_compiler/lib/disassembler/disassembler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,15 @@ class BeizeDisassembler {

case BeizeOpCodes.opImport:
final int moduleIndex = chunk.codeAt(ip + 1);
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(
opCode,
ip,
chunk.lineAt(ip),
'{so}(module [$moduleIndex] = $moduleName, as [$asIndex] = $importName)',
'{so}(module [$moduleIndex] = $moduleName)',
);
return 2;
return 1;

default:
writeInstruction(opCode, ip, chunk.lineAt(ip));
Expand Down
1 change: 1 addition & 0 deletions packages/beize_compiler/lib/scanner/rules/identifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ abstract class BeizeIdentifierScanner {
BeizeTokens.forKw,
BeizeTokens.asyncKw,
BeizeTokens.awaitKw,
BeizeTokens.onlyKw,
};

static final Map<String, BeizeTokens> keywordsMap = keywords.asNameMap().map(
Expand Down
2 changes: 2 additions & 0 deletions packages/beize_compiler/lib/token/tokens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ enum BeizeTokens {
forKw, // for
asyncKw, // async
awaitKw, // await
onlyKw, // only
}

const Map<BeizeTokens, String> _tokensCodeMap = <BeizeTokens, String>{
Expand Down Expand Up @@ -159,6 +160,7 @@ const Map<BeizeTokens, String> _tokensCodeMap = <BeizeTokens, String>{
BeizeTokens.forKw: 'for',
BeizeTokens.asyncKw: 'async',
BeizeTokens.awaitKw: 'await',
BeizeTokens.onlyKw: 'only',
};

extension OutreTokensUtils on BeizeTokens {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import '../utils.dart';
Future<void> main() async {
const String title = '[Statement] If-Else (1)';
final BeizeProgramConstant program = await compileTestScript(
'if-else_statement',
'if-else_1.beize',
'if_else_statement',
'if_else_1.beize',
);

test('$title - Channel', () async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import '../utils.dart';
Future<void> main() async {
const String title = '[Statement] If-Else (2)';
final BeizeProgramConstant program = await compileTestScript(
'if-else_statement',
'if-else_2.beize',
'if_else_statement',
'if_else_2.beize',
);

test('$title - Channel', () async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import '../utils.dart';
Future<void> main() async {
const String title = '[Statement] If-ElseIf-Else (1)';
final BeizeProgramConstant program = await compileTestScript(
'if-elseif-else_statement',
'if-elseif-else_1.beize',
'if_elseif_else_statement',
'if_elseif_else_1.beize',
);

test('$title - Channel', () async {
Expand Down
8 changes: 3 additions & 5 deletions packages/beize_vm/lib/vm/interpreter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -456,22 +456,20 @@ class BeizeInterpreter {

case BeizeOpCodes.opImport:
final int moduleIndex = chunk.codeAt(frame.ip);
final int asIndex = chunk.codeAt(frame.ip + 1);
final String name = frame.vm.program.constantAt(asIndex) as String;
frame.ip += 2;
frame.ip++;
final BeizeModuleValue? pValue = frame.vm.lookupModule(moduleIndex);
if (pValue != null) {
namespace.declare(name, pValue);
stack.push(pValue);
} else {
final BeizePreparedModule module = frame.vm.prepareModule(
moduleIndex,
isEntrypoint: false,
);
namespace.declare(name, module.value);
final BeizeInterpreterResult result = frame.vm.loadModule(module);
if (result.isFailure) {
return handleException(result.error);
}
stack.push(module.value);
}

default:
Expand Down

0 comments on commit 3fb09d9

Please sign in to comment.