Implement == and != for strings

This commit is contained in:
Ayke van Laethem
2018-08-22 00:56:11 +02:00
parent 2777f8464e
commit c3cb22030f
2 changed files with 33 additions and 4 deletions
+21 -4
View File
@@ -1542,10 +1542,27 @@ func (c *Compiler) parseBinOp(frame *Frame, binop *ssa.BinOp) (llvm.Value, error
// Go specific. Calculate "and not" with x & (~y)
inv := c.builder.CreateNot(y, "") // ~y
return c.builder.CreateAnd(x, inv, ""), nil
case token.EQL: // ==
return c.builder.CreateICmp(llvm.IntEQ, x, y, ""), nil
case token.NEQ: // !=
return c.builder.CreateICmp(llvm.IntNE, x, y, ""), nil
case token.EQL, token.NEQ: // ==, !=
switch typ := binop.X.Type().Underlying().(type) {
case *types.Basic:
if typ.Info()&types.IsInteger != 0 || typ.Kind() == types.UnsafePointer {
if binop.Op == token.EQL {
return c.builder.CreateICmp(llvm.IntEQ, x, y, ""), nil
} else {
return c.builder.CreateICmp(llvm.IntNE, x, y, ""), nil
}
} else if typ.Kind() == types.String {
result := c.builder.CreateCall(c.mod.NamedFunction("runtime.stringequal"), []llvm.Value{x, y}, "")
if binop.Op == token.NEQ {
result = c.builder.CreateNot(result, "")
}
return result, nil
} else {
return llvm.Value{}, errors.New("todo: equality operator on unknown basic type: " + typ.String())
}
default:
return llvm.Value{}, errors.New("todo: equality operator on unknown type: " + typ.String())
}
case token.LSS: // <
if signed {
return c.builder.CreateICmp(llvm.IntSLT, x, y, ""), nil
+12
View File
@@ -17,6 +17,18 @@ func GOMAXPROCS(n int) int {
return 1
}
func stringequal(x, y string) bool {
if len(x) != len(y) {
return false
}
for i := 0; i < len(x); i++ {
if x[i] != y[i] {
return false
}
}
return true
}
func _panic(message interface{}) {
printstring("panic: ")
printitf(message)