use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.
the class SLLanguage method parse.
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
Source source = request.getSource();
Map<TruffleString, RootCallTarget> functions;
/*
* Parse the provided source. At this point, we do not have a SLContext yet. Registration of
* the functions with the SLContext happens lazily in SLEvalRootNode.
*/
if (request.getArgumentNames().isEmpty()) {
functions = SimpleLanguageParser.parseSL(this, source);
} else {
StringBuilder sb = new StringBuilder();
sb.append("function main(");
String sep = "";
for (String argumentName : request.getArgumentNames()) {
sb.append(sep);
sb.append(argumentName);
sep = ",";
}
sb.append(") { return ");
sb.append(source.getCharacters());
sb.append(";}");
String language = source.getLanguage() == null ? ID : source.getLanguage();
Source decoratedSource = Source.newBuilder(language, sb.toString(), source.getName()).build();
functions = SimpleLanguageParser.parseSL(this, decoratedSource);
}
RootCallTarget main = functions.get(SLStrings.MAIN);
RootNode evalMain;
if (main != null) {
/*
* We have a main function, so "evaluating" the parsed source means invoking that main
* function. However, we need to lazily register functions into the SLContext first, so
* we cannot use the original SLRootNode for the main function. Instead, we create a new
* SLEvalRootNode that does everything we need.
*/
evalMain = new SLEvalRootNode(this, main, functions);
} else {
/*
* Even without a main function, "evaluating" the parsed source needs to register the
* functions into the SLContext.
*/
evalMain = new SLEvalRootNode(this, null, functions);
}
return evalMain.getCallTarget();
}
use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.
the class SLNodeFactory method createAssignment.
/**
* Returns an {@link SLWriteLocalVariableNode} for the given parameters.
*
* @param nameNode The name of the variable being assigned
* @param valueNode The value to be assigned
* @param argumentIndex null or index of the argument the assignment is assigning
* @return An SLExpressionNode for the given parameters. null if nameNode or valueNode is null.
*/
public SLExpressionNode createAssignment(SLExpressionNode nameNode, SLExpressionNode valueNode, Integer argumentIndex) {
if (nameNode == null || valueNode == null) {
return null;
}
TruffleString name = ((SLStringLiteralNode) nameNode).executeGeneric(null);
Integer frameSlot = lexicalScope.find(name);
boolean newVariable = false;
if (frameSlot == null) {
frameSlot = frameDescriptorBuilder.addSlot(FrameSlotKind.Illegal, name, argumentIndex);
lexicalScope.locals.put(name, frameSlot);
newVariable = true;
}
final SLExpressionNode result = SLWriteLocalVariableNodeGen.create(valueNode, frameSlot, nameNode, newVariable);
if (valueNode.hasSource()) {
final int start = nameNode.getSourceCharIndex();
final int length = valueNode.getSourceEndIndex() - start;
result.setSourceSection(start, length);
}
if (argumentIndex == null) {
result.addExpressionTag();
}
return result;
}
use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.
the class SLNodeFactory method createRead.
/**
* Returns a {@link SLReadLocalVariableNode} if this read is a local variable or a
* {@link SLFunctionLiteralNode} if this read is global. In SL, the only global names are
* functions.
*
* @param nameNode The name of the variable/function being read
* @return either:
* <ul>
* <li>A SLReadLocalVariableNode representing the local variable being read.</li>
* <li>A SLFunctionLiteralNode representing the function definition.</li>
* <li>null if nameNode is null.</li>
* </ul>
*/
public SLExpressionNode createRead(SLExpressionNode nameNode) {
if (nameNode == null) {
return null;
}
TruffleString name = ((SLStringLiteralNode) nameNode).executeGeneric(null);
final SLExpressionNode result;
final Integer frameSlot = lexicalScope.find(name);
if (frameSlot != null) {
/* Read of a local variable. */
result = SLReadLocalVariableNodeGen.create(frameSlot);
} else {
/* Read of a global name. In our language, the only global names are functions. */
result = new SLFunctionLiteralNode(name);
}
result.setSourceSection(nameNode.getSourceCharIndex(), nameNode.getSourceLength());
result.addExpressionTag();
return result;
}
use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.
the class SLStackTraceBuiltin method createStackTrace.
@TruffleBoundary
private static TruffleString createStackTrace() {
final TruffleStringBuilder str = TruffleStringBuilder.create(SLLanguage.STRING_ENCODING);
Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Integer>() {
// skip stack trace builtin
private int skip = 1;
@Override
public Integer visitFrame(FrameInstance frameInstance) {
if (skip > 0) {
skip--;
return null;
}
CallTarget callTarget = frameInstance.getCallTarget();
Frame frame = frameInstance.getFrame(FrameAccess.READ_ONLY);
RootNode rn = ((RootCallTarget) callTarget).getRootNode();
// ignore internal or interop stack frames
if (rn.isInternal() || rn.getLanguageInfo() == null) {
return 1;
}
if (str.byteLength() > 0) {
str.appendStringUncached(SLStrings.fromJavaString(System.getProperty("line.separator")));
}
str.appendStringUncached(FRAME);
str.appendStringUncached(getRootNodeName(rn));
FrameDescriptor frameDescriptor = frame.getFrameDescriptor();
int count = frameDescriptor.getNumberOfSlots();
for (int i = 0; i < count; i++) {
str.appendStringUncached(SEPARATOR);
str.appendStringUncached((TruffleString) frameDescriptor.getSlotName(i));
str.appendStringUncached(EQUALS);
str.appendStringUncached(SLStrings.fromObject(frame.getValue(i)));
}
return null;
}
});
return str.toStringUncached();
}
use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.
the class TStringSwitchEncodingTest method testAll.
@Test
public void testAll() throws Exception {
EnumSet<TruffleString.Encoding> reducedEncodingSet = EnumSet.allOf(TruffleString.Encoding.class);
reducedEncodingSet.removeIf(e -> e.name().startsWith("IBM") || e.name().startsWith("Windows") || e.name().startsWith("ISO_8859_"));
forAllStrings(true, (a, array, codeRange, isValid, encoding, codepoints, byteIndices) -> {
if (encoding == TruffleString.Encoding.UTF8_SoftBank || encoding == TruffleString.Encoding.CP51932) {
// https://github.com/jruby/jcodings/issues/42
return;
}
for (TruffleString.Encoding targetEncoding : reducedEncodingSet) {
TruffleString b = node.execute(a, targetEncoding);
MutableTruffleString bMutable = nodeMutable.execute(a, targetEncoding);
if (a instanceof TruffleString && (encoding == targetEncoding || !isDebugStrictEncodingChecks() && codeRange == TruffleString.CodeRange.ASCII && isAsciiCompatible(targetEncoding))) {
Assert.assertSame(a, b);
}
if (a instanceof MutableTruffleString && encoding == targetEncoding) {
Assert.assertSame(a, bMutable);
}
if (isUTF(encoding) && isUTF(targetEncoding) && isValid) {
assertCodePointsEqual(b, targetEncoding, codepoints);
}
}
});
}
Aggregations