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: extract code for compiling equal operator #614

Merged
merged 1 commit into from
Mar 28, 2024

Conversation

zhuliquan
Copy link
Contributor

Whether OpEqualInt and OpEqualInt need to be applied to the operator != when convert ast to bytecode, and if so, whether to consider extracting the code that handles equal into a function named equalBinnaryNode

@antonmedv
Copy link
Member

Why this code is needed?

@zhuliquan
Copy link
Contributor Author

Why this code is needed?

I'm not sure. I notice that you have applied three OpCode in equal operator case as below.

expr/compiler/compiler.go

Lines 412 to 418 in d523107

if l == r && l == reflect.Int && leftAndRightAreSimple {
c.emit(OpEqualInt)
} else if l == r && l == reflect.String && leftAndRightAreSimple {
c.emit(OpEqualString)
} else {
c.emit(OpEqual)
}

I'm guessing you want to make the most of the right and left type information in BinaryNode, so that the Bytecode generated later runs faster. Because. I notice that assert interface{} to int / string directly in case OpEqualInt/OpEqualString as below.

expr/vm/vm.go

Lines 165 to 173 in d523107

case OpEqualInt:
b := vm.pop()
a := vm.pop()
vm.push(a.(int) == b.(int))
case OpEqualString:
b := vm.pop()
a := vm.pop()
vm.push(a.(string) == b.(string))

Since this trick is already used in case of ==, why not use it in case of !=? Further, I think that if this trick can be used in case of !=, why not extract code of == case and apply it to both the case of == and the case of !=.

@antonmedv
Copy link
Member

Since this trick is already used in case of ==, why not use it in case of !=?

Got it. This makes sense.

@antonmedv antonmedv merged commit 7772ea0 into expr-lang:master Mar 28, 2024
10 checks passed
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.

2 participants