[tlox] Implement String concatenation
This commit is contained in:
@@ -2,6 +2,7 @@ package xyz.ctsk.lox.nodes.expr;
|
||||
|
||||
import com.oracle.truffle.api.dsl.Fallback;
|
||||
import com.oracle.truffle.api.dsl.Specialization;
|
||||
import com.oracle.truffle.api.strings.TruffleString;
|
||||
import xyz.ctsk.lox.runtime.LoxException;
|
||||
import xyz.ctsk.lox.nodes.LoxBinaryNode;
|
||||
|
||||
@@ -11,6 +12,11 @@ public abstract class LoxAddNode extends LoxBinaryNode {
|
||||
return left + right;
|
||||
}
|
||||
|
||||
@Specialization
|
||||
public TruffleString add(TruffleString left, TruffleString right) {
|
||||
return TruffleString.ConcatNode.create().execute(left, right, TruffleString.Encoding.UTF_16, false);
|
||||
}
|
||||
|
||||
@Fallback
|
||||
protected Object typeError(Object left, Object right) {
|
||||
throw LoxException.typeError(this, left, right);
|
||||
|
||||
@@ -12,7 +12,8 @@ public class LoxNodeFactory {
|
||||
}
|
||||
|
||||
public static LoxStringLiteralNode createStringLiteral(Token literalToken) {
|
||||
var value = TruffleString.fromJavaStringUncached(literalToken.getText(), TruffleString.Encoding.UTF_16);
|
||||
var text = literalToken.getText();
|
||||
var value = TruffleString.fromJavaStringUncached(text.substring(1, text.length() - 1), TruffleString.Encoding.UTF_16);
|
||||
return new LoxStringLiteralNode(value);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user