Skip to content

Commit

Permalink
feat(vm): classes
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrouge committed Dec 9, 2024
1 parent 3fb09d9 commit a6fc775
Show file tree
Hide file tree
Showing 79 changed files with 1,181 additions and 755 deletions.
1 change: 1 addition & 0 deletions packages/beize_compiler/example/project/dev.beize
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ main := -> async {
};

main().await;
print();
2 changes: 1 addition & 1 deletion packages/beize_compiler/tests/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Future<List<String>> executeTestScript(
final BeizeVM vm = BeizeVM(program, BeizeVMOptions());
final List<String> output = <String>[];
final BeizeNativeFunctionValue out = BeizeNativeFunctionValue.sync(
(final BeizeNativeFunctionCall call) {
(final BeizeCallableCall call) {
final BeizeStringValue value = call.argumentAt(0).cast();
output.add(value.value);
return BeizeNullValue.value;
Expand Down
23 changes: 23 additions & 0 deletions packages/beize_shared/lib/bytecode/constants/class.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import '../chunk.dart';
import 'constant.dart';

class BeizeClassConstant {
BeizeClassConstant({
required this.moduleIndex,
required this.chunk,
});

factory BeizeClassConstant.deserialize(
final BeizeSerializedConstant serialized,
) =>
BeizeClassConstant(
moduleIndex: serialized[0] as int,
chunk: BeizeChunk.deserialize(serialized[1] as BeizeSerializedConstant),
);

final int moduleIndex;
final BeizeChunk chunk;

BeizeSerializedConstant serialize() =>
<dynamic>[moduleIndex, chunk.serialize()];
}
1 change: 1 addition & 0 deletions packages/beize_shared/lib/bytecode/constants/exports.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export 'class.dart';
export 'constant.dart';
export 'function.dart';
export 'program.dart';
4 changes: 1 addition & 3 deletions packages/beize_shared/lib/bytecode/constants/function.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class BeizeFunctionConstant {
moduleIndex: serialized[0] as int,
isAsync: serialized[1] == 1,
arguments: (serialized[2] as List<dynamic>).cast<int>(),
chunk: BeizeChunk.deserialize(
serialized[3] as BeizeSerializedConstant,
),
chunk: BeizeChunk.deserialize(serialized[3] as BeizeSerializedConstant),
);

final int moduleIndex;
Expand Down
31 changes: 0 additions & 31 deletions packages/beize_vm/lib/values/boolean.dart

This file was deleted.

29 changes: 29 additions & 0 deletions packages/beize_vm/lib/values/boolean/boolean.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import '../../vm/exports.dart';
import '../exports.dart';

class BeizeBooleanValue extends BeizeNativeObjectValue {
BeizeBooleanValue(this.value);

final bool value;

@override
final BeizeValueKind kind = BeizeValueKind.boolean;

@override
BeizeBooleanValue kClone() => BeizeBooleanValue(value);

@override
String kToString() => value.toString();

@override
BeizeClassValue? kClassInternal(final BeizeVM vm) => vm.globals.booleanClass;

@override
BeizeClassValue get kClass => throw UnimplementedError();

@override
bool get isTruthy => value;

@override
int get kHashCode => value.hashCode;
}
21 changes: 21 additions & 0 deletions packages/beize_vm/lib/values/boolean/boolean_class.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import '../exports.dart';

class BeizeBooleanClassValue extends BeizeNativeClassValue {
@override
BeizeBooleanValue kInstantiate(final BeizeCallableCall call) {
final BeizeValue value = call.argumentAt(0);
return BeizeBooleanValue(value.isTruthy);
}

@override
BeizeBooleanClassValue kClone() => this;

@override
String kToString() => '<boolean class>';

@override
bool get isTruthy => true;

@override
int get kHashCode => hashCode;
}
2 changes: 2 additions & 0 deletions packages/beize_vm/lib/values/boolean/exports.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'boolean.dart';
export 'boolean_class.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import '../../errors/exports.dart';
import '../../vm/exports.dart';
import '../exports.dart';

class BeizeNativeFunctionCall {
BeizeNativeFunctionCall({
class BeizeCallableCall {
BeizeCallableCall({
required this.frame,
required this.arguments,
});
Expand Down
6 changes: 6 additions & 0 deletions packages/beize_vm/lib/values/callable/callable.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import '../../vm/exports.dart';
import '../exports.dart';

abstract class BeizeCallableValue extends BeizeObjectValue {
BeizeInterpreterResult kCall(final BeizeCallableCall call);
}
2 changes: 2 additions & 0 deletions packages/beize_vm/lib/values/callable/exports.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'call.dart';
export 'callable.dart';
16 changes: 16 additions & 0 deletions packages/beize_vm/lib/values/class/class.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import '../../vm/exports.dart';
import '../exports.dart';

abstract class BeizeClassValue extends BeizeNativeObjectValue
implements BeizeCallableValue {
BeizeObjectValue kInstantiate(final BeizeCallableCall call);

@override
BeizeInterpreterResult kCall(final BeizeCallableCall call) {
final BeizeObjectValue value = kInstantiate(call);
return BeizeInterpreterResult.success(value);
}

@override
BeizeClassValue get kClass => this;
}
31 changes: 31 additions & 0 deletions packages/beize_vm/lib/values/class/class_class.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import '../exports.dart';

// what the fuck is this naming
class BeizeClassClassValue extends BeizeNativeClassValue {
BeizeClassClassValue() {
set(
BeizeStringValue('of'),
BeizeNativeFunctionValue.sync(
(final BeizeCallableCall call) {
final BeizeObjectValue value = call.argumentAt(0);
return value.kClassInternal(call.frame.vm) ?? value.kClass;
},
),
);
}

@override
BeizeClassValue kInstantiate(final BeizeCallableCall call) {
final BeizeClassValue value = call.argumentAt(0);
return value;
}

@override
BeizeClassClassValue kClone() => this;

@override
String kToString() => '<class>';

@override
BeizeClassValue get kClass => this;
}
4 changes: 4 additions & 0 deletions packages/beize_vm/lib/values/class/exports.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export 'class.dart';
export 'class_class.dart';
export 'native_class.dart';
export 'vm_class.dart';
15 changes: 15 additions & 0 deletions packages/beize_vm/lib/values/class/native_class.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import '../exports.dart';

abstract class BeizeNativeClassValue extends BeizeClassValue {
@override
final BeizeValueKind kind = BeizeValueKind.nativeClazz;

@override
String kToString() => '<native class>';

@override
bool get isTruthy => true;

@override
int get kHashCode => hashCode;
}
28 changes: 28 additions & 0 deletions packages/beize_vm/lib/values/class/vm_class.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import '../../bytecode.dart';
import '../exports.dart';

class BeizeVMClassValue extends BeizeClassValue {
BeizeVMClassValue(this.constant);

final BeizeClassConstant constant;

@override
final BeizeValueKind kind = BeizeValueKind.clazz;

@override
BeizeObjectValue kInstantiate(final BeizeCallableCall call) {
throw UnimplementedError();
}

@override
BeizeVMClassValue kClone() => BeizeVMClassValue(constant);

@override
String kToString() => '<class>';

@override
bool get isTruthy => true;

@override
int get kHashCode => constant.hashCode;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'exports.dart';
import '../../vm/exports.dart';
import '../exports.dart';

class BeizeExceptionValue extends BeizePrimitiveObjectValue {
class BeizeExceptionValue extends BeizeNativeObjectValue {
BeizeExceptionValue(this.message, this.stackTrace, [this.dartStackTrace]);

final String message;
Expand Down Expand Up @@ -54,6 +55,12 @@ class BeizeExceptionValue extends BeizePrimitiveObjectValue {
@override
String toString() => toCustomString();

@override
BeizeClassValue kClassInternal(final BeizeVM vm) => vm.globals.exceptionClass;

@override
BeizeClassValue get kClass => throw UnimplementedError();

@override
bool get isTruthy => true;

Expand Down
27 changes: 27 additions & 0 deletions packages/beize_vm/lib/values/exception/exception_class.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import '../exports.dart';

class BeizeExceptionClassValue extends BeizeNativeClassValue {
@override
BeizeExceptionValue kInstantiate(final BeizeCallableCall call) {
final BeizeStringValue message = call.argumentAt(0);
final BeizeValue stackTrace = call.argumentAt(1);
return BeizeExceptionValue(
message.value,
stackTrace is BeizeNullValue
? call.frame.getStackTrace()
: stackTrace.kToString(),
);
}

@override
BeizeExceptionClassValue kClone() => this;

@override
String kToString() => '<exception class>';

@override
bool get isTruthy => true;

@override
int get kHashCode => hashCode;
}
2 changes: 2 additions & 0 deletions packages/beize_vm/lib/values/exception/exports.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'exception.dart';
export 'exception_class.dart';
19 changes: 11 additions & 8 deletions packages/beize_vm/lib/values/exports.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
export 'boolean.dart';
export 'exception.dart';
export 'boolean/exports.dart';
export 'callable/exports.dart';
export 'class/exports.dart';
export 'exception/exports.dart';
export 'function/exports.dart';
export 'kind.dart';
export 'list.dart';
export 'module.dart';
export 'list/exports.dart';
export 'map/exports.dart';
export 'module/exports.dart';
export 'null.dart';
export 'number.dart';
export 'object.dart';
export 'primitive_object.dart';
export 'string.dart';
export 'number/exports.dart';
export 'object/exports.dart';
export 'string/exports.dart';
export 'unawaited/exports.dart';
export 'value.dart';
3 changes: 0 additions & 3 deletions packages/beize_vm/lib/values/function/callable.dart

This file was deleted.

6 changes: 2 additions & 4 deletions packages/beize_vm/lib/values/function/exports.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export 'call.dart';
export 'callable.dart';
export 'function.dart';
export 'native_function.dart';
export 'unawaited.dart';
export 'function_class.dart';
export 'native/exports.dart';
export 'utils.dart';
Loading

0 comments on commit a6fc775

Please sign in to comment.