Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initializing conditional stack opcodes #32

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

okhaimie-dev
Copy link
Contributor

@okhaimie-dev okhaimie-dev commented Sep 11, 2024

Implement conditional opcodes and wip to enhance script code structure

Added the following opcodes with corresponding tests:

  • opIf
  • opNotIf
  • opElse
  • opEndIf

Others

  • opReserved
  • opVer

Improvements:

  • Structured opcode implementation to align with Shinigami's approach for better maintainability and collaboration
  • Introduced cond_stack for managing conditional execution

This commit addresses issue #29

src/script/cond_stack.zig Outdated Show resolved Hide resolved
src/script/cond_stack.zig Outdated Show resolved Hide resolved
src/script/cond_stack.zig Outdated Show resolved Hide resolved
src/script/cond_stack.zig Outdated Show resolved Hide resolved
src/script/cond_stack.zig Outdated Show resolved Hide resolved
src/script/cond_stack.zig Outdated Show resolved Hide resolved
src/script/cond_stack.zig Outdated Show resolved Hide resolved
src/script/cond_stack.zig Outdated Show resolved Hide resolved
src/script/cond_stack.zig Show resolved Hide resolved
if (self.stack.items.len == 0) {
return ConditionalStackError.EmptyConditionalStack;
}
_ = self.stack.pop();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you return the value?
Should it be named delete then? Not pop

src/script/cond_stack.zig Show resolved Hide resolved
src/script/cond_stack.zig Outdated Show resolved Hide resolved
src/script/engine.zig Outdated Show resolved Hide resolved
src/script/engine.zig Outdated Show resolved Hide resolved
src/script/opcodes/flow.zig Outdated Show resolved Hide resolved
src/script/opcodes/flow.zig Outdated Show resolved Hide resolved
src/script/opcodes/flow.zig Outdated Show resolved Hide resolved
src/script/opcodes/flow.zig Outdated Show resolved Hide resolved
src/script/cond_stack.zig Outdated Show resolved Hide resolved
src/script/engine.zig Outdated Show resolved Hide resolved
src/script/engine.zig Outdated Show resolved Hide resolved
src/script/engine.zig Outdated Show resolved Hide resolved
src/script/opcodes/flow.zig Outdated Show resolved Hide resolved
@tdelabro
Copy link
Collaborator

@okhaimie-dev don't forget to ask for a new review when you are finished pushing your changes

src/script/cond_stack.zig Outdated Show resolved Hide resolved
src/script/cond_stack.zig Outdated Show resolved Hide resolved
src/script/cond_stack.zig Outdated Show resolved Hide resolved
src/script/engine.zig Outdated Show resolved Hide resolved
src/script/engine.zig Outdated Show resolved Hide resolved
src/script/opcodes/flow_control.zig Outdated Show resolved Hide resolved
src/script/opcodes/flow_control.zig Outdated Show resolved Hide resolved
src/script/opcodes/flow_control.zig Outdated Show resolved Hide resolved
src/script/opcodes/flow_control.zig Outdated Show resolved Hide resolved
src/script/opcodes/flow_control.zig Outdated Show resolved Hide resolved
src/script/cond_stack.zig Outdated Show resolved Hide resolved
Comment on lines 55 to 58
///
/// Returns:
/// The popped value (u8) if the stack is not empty
/// ConditionalStackError.EmptyConditionalStack if the stack is empty
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we know what pop return. You can remove this long comment

/// Push a conditional value onto the stack
///
/// Args:
/// value: The conditional value to push (typically 0, 1, or 2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be something else than 0, 1 or 2? You comment suggest it can

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can you define an enum for more readability

	OpCondFalse = 0
	OpCondTrue  = 1
	OpCondSkip  = 2

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be something else than 0, 1 or 2? You comment suggest it can

Reading other implementations it looks like it cannot. So please change your comment to not mean that it could

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And ofcourse use those OpCond enum in your tests rather than 1, 2, 3

if (self.stack.items.len == 0) {
return ConditionalStackError.UnbalancedConditional;
}
_ = self.stack.pop();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you want to return the poped value?

@@ -391,3 +391,27 @@ pub fn isUnnamedPushNDataOpcode(opcode: Opcode) ?u8 {
const opcodeByte = opcode.toBytes();
return if (opcodeByte > 0x00 and opcodeByte <= 0x4b) opcodeByte else null;
}

/// Conditional execution constants for script operations.
pub const OpConditional = enum(u8) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are not Opcodes so I don't like the name.
What about "CondStackValue" ?

///
/// Returns:
/// Possible error if allocation fails
pub fn push(self: *Self, value: u8) !void {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if values are strictly limited, change the signature to reflect that:

pub fn push(self: *Self, value: CondStackValue) !void

const Self = @This();

/// Dynamic array to store conditional values
stack: std.ArrayList(u8),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std.ArrayList(CondStackValue) is more restrictive and more expressive

@tdelabro
Copy link
Collaborator

@okhaimie-dev are you still working on this?

@okhaimie-dev
Copy link
Contributor Author

@okhaimie-dev are you still working on this?

I'll send a fix tomorrow, I have been traveling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants