compilation errors fix (project can be built without parser)

This commit is contained in:
ProgramSnail 2025-06-16 21:43:53 +03:00
parent 0edc46ab92
commit e39eaeb6ef
31 changed files with 219 additions and 105 deletions

42
pom.xml
View file

@ -9,13 +9,51 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<properties> <properties>
<maven.compiler.source>23</maven.compiler.source> <maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>23</maven.compiler.target> <maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<graalvm.version>23.1.0</graalvm.version> <graalvm.version>23.1.0</graalvm.version>
<antlr.version>4.12.0</antlr.version> <antlr.version>4.12.0</antlr.version>
</properties> </properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<argLine>
--add-exports=java.base/jdk.internal.module=truffle.tck
</argLine>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.0</version>
<configuration>
<source>21</source>
<target>21</target>
<annotationProcessorPaths>
<path>
<groupId>org.graalvm.truffle</groupId>
<artifactId>truffle-dsl-processor</artifactId>
<version>${graalvm.version}</version>
</path>
</annotationProcessorPaths>
<excludes>
<exclude>parser/**</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<finalName>truffle-lama-from-scratch</finalName>
</build>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.graalvm.polyglot</groupId> <groupId>org.graalvm.polyglot</groupId>

View file

@ -6,6 +6,7 @@ import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.library.ExportLibrary; import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage; import com.oracle.truffle.api.library.ExportMessage;
import org.programsnail.truffle_lama.nodes.FunctionDispatchNode; import org.programsnail.truffle_lama.nodes.FunctionDispatchNode;
import org.programsnail.truffle_lama.nodes.FunctionDispatchNodeGen;
import org.programsnail.truffle_lama.runtime.LamaException; import org.programsnail.truffle_lama.runtime.LamaException;
@ExportLibrary(InteropLibrary.class) @ExportLibrary(InteropLibrary.class)
@ -14,10 +15,10 @@ public final class FunctionObject implements TruffleObject {
public final int argumentCount; public final int argumentCount;
public final FunctionDispatchNode functionDispatchNode; public final FunctionDispatchNode functionDispatchNode;
public FunctionObject(CallTarget callTarget, int argumentCount, FunctionDispatchNode functionDispatchNode) { public FunctionObject(CallTarget callTarget, int argumentCount) {
this.callTarget = callTarget; this.callTarget = callTarget;
this.argumentCount = argumentCount; this.argumentCount = argumentCount;
this.functionDispatchNode = functionDispatchNode; this.functionDispatchNode = FunctionDispatchNodeGen.create();
} }
@ExportMessage @ExportMessage

View file

@ -2,6 +2,7 @@ package org.programsnail.truffle_lama;
import com.oracle.truffle.api.TruffleLanguage; import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
import com.oracle.truffle.api.interop.TruffleObject; import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnknownIdentifierException; import com.oracle.truffle.api.interop.UnknownIdentifierException;
import com.oracle.truffle.api.library.ExportLibrary; import com.oracle.truffle.api.library.ExportLibrary;
@ -88,11 +89,11 @@ public final class GlobalScopeObject implements TruffleObject {
} }
@ExportLibrary(InteropLibrary.class) @ExportLibrary(InteropLibrary.class)
final class GlobalVariablesNamesObject implements TruffleObject { final class GlobalVariableNamesObject implements TruffleObject {
private final List<String> names; private final List<String> names;
GlobalVariablesNamesObject(List<String> names) { GlobalVariableNamesObject(Set<String> names) {
this.names = names; this.names = new ArrayList<>(names);
} }
@ExportMessage @ExportMessage

View file

@ -17,7 +17,7 @@ import com.oracle.truffle.api.strings.TruffleString;
import org.programsnail.truffle_lama.builtins.LamaBuiltinNode; import org.programsnail.truffle_lama.builtins.LamaBuiltinNode;
import org.programsnail.truffle_lama.nodes.LamaExpressionNode; import org.programsnail.truffle_lama.nodes.LamaExpressionNode;
import org.programsnail.truffle_lama.nodes.LamaRootNode; import org.programsnail.truffle_lama.nodes.LamaRootNode;
import org.programsnail.truffle_lama.parser.LamaParser; //import org.programsnail.truffle_lama.parser.LamaParser;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -42,9 +42,11 @@ public class LamaLanguage extends TruffleLanguage<LamaContext> {
@Override @Override
protected CallTarget parse(ParsingRequest request) throws Exception { protected CallTarget parse(ParsingRequest request) throws Exception {
var exprNode = LamaParser.parseLama(this, request.getSource()); // var exprNode = LamaParser.parseLama(this, request.getSource());
var rootNode = new LamaRootNode(this, exprNode); // var rootNode = new LamaRootNode(this, exprNode);
return rootNode.getCallTarget(); // return rootNode.getCallTarget();
// TODO
return null;
} }
@Override @Override

View file

@ -7,6 +7,7 @@ import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.UnexpectedResultException; import com.oracle.truffle.api.nodes.UnexpectedResultException;
import org.programsnail.truffle_lama.nodes.LamaExpressionNode; import org.programsnail.truffle_lama.nodes.LamaExpressionNode;
import org.programsnail.truffle_lama.runtime.LamaException;
@NodeChild(value = "arguments", type = LamaExpressionNode[].class) @NodeChild(value = "arguments", type = LamaExpressionNode[].class)
@ -32,10 +33,5 @@ public abstract class LamaBuiltinNode extends LamaExpressionNode {
return super.executeLong(frame); return super.executeLong(frame);
} }
@Override
public final void executeVoid(VirtualFrame frame) {
super.executeVoid(frame);
}
protected abstract Object execute(VirtualFrame frame); protected abstract Object execute(VirtualFrame frame);
} }

View file

@ -15,15 +15,14 @@ public abstract class FunctionDispatchNode extends Node {
@Specialization(guards = "function.callTarget == directCallNode.getCallTarget()", limit = "2") @Specialization(guards = "function.callTarget == directCallNode.getCallTarget()", limit = "2")
protected static Object dispatchDirectly(FunctionObject function, protected static Object dispatchDirectly(FunctionObject function,
Object[] arguments, Object[] arguments,
@Cached("create(function.callTarget)") @Cached("create(function.callTarget)") DirectCallNode directCallNode) {
DirectCallNode directCallNode) {
return directCallNode.call(arguments); // TODO: check arguments count ?? return directCallNode.call(arguments); // TODO: check arguments count ??
} }
@Specialization(replaces = "dispatchDirectly") @Specialization(replaces = "dispatchDirectly")
protected static Object dispatchIndirectly(FunctionObject function, protected static Object dispatchIndirectly(FunctionObject function,
Object[] arguments, Object[] arguments,
IndirectCallNode indirectCallNode) { @Cached IndirectCallNode indirectCallNode) {
return indirectCallNode.call(function.callTarget, arguments); // TODO: check arguments count ?? return indirectCallNode.call(function.callTarget, arguments); // TODO: check arguments count ??
} }

View file

@ -12,6 +12,11 @@ import org.programsnail.truffle_lama.LamaLanguage;
@TypeSystemReference(LamaTypes.class) @TypeSystemReference(LamaTypes.class)
@NodeInfo(description = "The abstract base node for all expressions") @NodeInfo(description = "The abstract base node for all expressions")
public abstract class LamaExpressionNode extends Node { public abstract class LamaExpressionNode extends Node {
private static final int NO_SOURCE = -1;
private int sourceCharIndex = NO_SOURCE;
private int sourceLength = 0;
protected final LamaLanguage currentTruffleLanguage() { protected final LamaLanguage currentTruffleLanguage() {
return LamaLanguage.get(this); return LamaLanguage.get(this);
} }
@ -21,21 +26,50 @@ public abstract class LamaExpressionNode extends Node {
} }
public abstract Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException; public abstract Object executeGeneric(VirtualFrame frame);
/* // ---
* Execute methods for specialized types. They all follow the same pattern: they call the
* generic execution method and then expect a result of their return type. Type-specialized
* subclasses overwrite the appropriate methods.
*/
// LamaTypesGen is generated from LamaTypes public final void setSourceSection(int charIndex, int length) {
assert sourceCharIndex == NO_SOURCE : "source must only be set once";
if (charIndex < 0) {
throw new IllegalArgumentException("charIndex < 0");
} else if (length < 0) {
throw new IllegalArgumentException("length < 0");
}
this.sourceCharIndex = charIndex;
this.sourceLength = length;
}
// ---
public final boolean hasSource() {
return sourceCharIndex != NO_SOURCE;
}
public final int getSourceCharIndex() {
return sourceCharIndex;
}
public final int getSourceEndIndex() {
return sourceCharIndex + sourceLength;
}
public final int getSourceLength() {
return sourceLength;
}
// ---
// LamaTypesGen is generated from LamaTypes ??
// TODO // TODO
public long executeLong(VirtualFrame frame) throws UnexpectedResultException { public long executeLong(VirtualFrame frame) throws UnexpectedResultException {
return LamaTypesGen.expectLong(executeGeneric(frame)); return LamaTypesGen.expectLong(executeGeneric(frame));
} }
// TODO
public boolean executeBoolean(VirtualFrame frame) throws UnexpectedResultException { public boolean executeBoolean(VirtualFrame frame) throws UnexpectedResultException {
return LamaTypesGen.expectBoolean(executeGeneric(frame)); return LamaTypesGen.expectBoolean(executeGeneric(frame));
} }

View file

@ -28,7 +28,7 @@ public final class LamaCaseNode extends LamaExpressionNode {
} }
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
Object value = this.valueNode.executeGeneric(frame); Object value = this.valueNode.executeGeneric(frame);
for (int i = 0; i < this.patterns.length; ++i) { for (int i = 0; i < this.patterns.length; ++i) {
var patternMatch = this.patterns[i].match(value); var patternMatch = this.patterns[i].match(value);

View file

@ -20,7 +20,7 @@ public final class LamaDoWhileNode extends LamaExpressionNode {
} }
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
this.loopNode.execute(frame); this.loopNode.execute(frame);
return LamaUnit.INSTANCE; return LamaUnit.INSTANCE;
} }

View file

@ -4,6 +4,7 @@ import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.UnexpectedResultException; import com.oracle.truffle.api.nodes.UnexpectedResultException;
import com.oracle.truffle.api.profiles.ConditionProfile; import com.oracle.truffle.api.profiles.ConditionProfile;
import org.programsnail.truffle_lama.nodes.LamaExpressionNode; import org.programsnail.truffle_lama.nodes.LamaExpressionNode;
import org.programsnail.truffle_lama.runtime.LamaException;
import org.programsnail.truffle_lama.runtime.LamaUnit; import org.programsnail.truffle_lama.runtime.LamaUnit;
import java.util.Objects; import java.util.Objects;
@ -21,10 +22,14 @@ public final class LamaIfNode extends LamaExpressionNode {
private final ConditionProfile condition = ConditionProfile.create(); private final ConditionProfile condition = ConditionProfile.create();
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
try {
if (this.condition.profile(this.conditionExpr.executeBoolean(frame))) { if (this.condition.profile(this.conditionExpr.executeBoolean(frame))) {
return this.thenExpr.executeGeneric(frame); return this.thenExpr.executeGeneric(frame);
} }
} catch (UnexpectedResultException e) {
throw new LamaException("If node: condition can't be evaluated as boolean");
}
// TODO: or throw ?? // TODO: or throw ??
return Objects.requireNonNullElse(this.elseExpr, LamaUnit.INSTANCE); return Objects.requireNonNullElse(this.elseExpr, LamaUnit.INSTANCE);
} }

View file

@ -20,7 +20,7 @@ public final class LamaWhileNode extends LamaExpressionNode {
} }
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
this.loopNode.execute(frame); this.loopNode.execute(frame);
return LamaUnit.INSTANCE; return LamaUnit.INSTANCE;
} }

View file

@ -14,7 +14,7 @@ public final class LamaArrayNode extends LamaExpressionNode {
} }
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
Object[] elements = new Object[elementNodes.length]; Object[] elements = new Object[elementNodes.length];
for (int i = 0; i < elements.length; ++i) { for (int i = 0; i < elements.length; ++i) {
elements[i] = elementNodes[i].executeGeneric(frame); elements[i] = elementNodes[i].executeGeneric(frame);

View file

@ -9,8 +9,6 @@ import org.programsnail.truffle_lama.runtime.LamaException;
import org.programsnail.truffle_lama.runtime.LamaUnit; import org.programsnail.truffle_lama.runtime.LamaUnit;
import org.programsnail.truffle_lama.runtime.LamaGlobalVarRef; import org.programsnail.truffle_lama.runtime.LamaGlobalVarRef;
@NodeChild("leftNode")
@NodeChild("rightNode")
public final class LamaAssignNode extends LamaExpressionNode { public final class LamaAssignNode extends LamaExpressionNode {
@Child @Child
private LamaExpressionNode leftNode, rightNode; private LamaExpressionNode leftNode, rightNode;
@ -20,14 +18,13 @@ public final class LamaAssignNode extends LamaExpressionNode {
this.rightNode = rightNode; this.rightNode = rightNode;
} }
@Specialization public Object executeGeneric(VirtualFrame frame) {
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException {
var leftValue = leftNode.executeGeneric(frame); var leftValue = leftNode.executeGeneric(frame);
var rightValue = rightNode.executeGeneric(frame); var rightValue = rightNode.executeGeneric(frame);
if (leftValue instanceof LamaGlobalVarRef) { if (leftValue instanceof LamaGlobalVarRef varRef) {
if (!((LamaGlobalVarRef) leftValue).assign(rightValue, frame)) { if (!varRef.assign(rightValue, frame)) {
throw new LamaException("Can't update variable by identifier '" + name + "'"); throw new LamaException("Can't update variable by identifier '" + varRef.getName() + "'");
} }
} else { } else {
throw new LamaException("Can't assign not to ref", this); throw new LamaException("Can't assign not to ref", this);

View file

@ -1,5 +1,6 @@
package org.programsnail.truffle_lama.nodes.expression; package org.programsnail.truffle_lama.nodes.expression;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.NodeChild; import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeField; import com.oracle.truffle.api.dsl.NodeField;
import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.dsl.Specialization;
@ -7,13 +8,15 @@ import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.UnexpectedResultException; import com.oracle.truffle.api.nodes.UnexpectedResultException;
import org.programsnail.truffle_lama.nodes.LamaExpressionNode; import org.programsnail.truffle_lama.nodes.LamaExpressionNode;
@NodeChild("leftNode") @NodeChild(value = "leftNode", type = LamaExpressionNode.class)
@NodeChild("rightNode") @NodeChild(value = "rightNode", type = LamaExpressionNode.class)
@NodeField(name = "op", type = String.class) @NodeField(name = "op", type = String.class)
public abstract class LamaBinopNode extends LamaExpressionNode { public abstract class LamaBinopNode extends LamaExpressionNode {
protected abstract String getOp();
@Specialization(rewriteOn = IllegalStateException.class) @Specialization(rewriteOn = IllegalStateException.class)
protected long numOp(long leftValue, long rightValue) { protected long numOp(long leftValue, long rightValue) {
return switch (op) { return switch (getOp()) {
case "+" -> leftValue + rightValue; case "+" -> leftValue + rightValue;
case "-" -> leftValue - rightValue; case "-" -> leftValue - rightValue;
case "/" -> leftValue / rightValue; case "/" -> leftValue / rightValue;
@ -24,7 +27,7 @@ public abstract class LamaBinopNode extends LamaExpressionNode {
@Specialization(replaces = "numOp") @Specialization(replaces = "numOp")
protected boolean compOp(long leftValue, long rightValue) { protected boolean compOp(long leftValue, long rightValue) {
return switch (op) { return switch (getOp()) {
case "<" -> leftValue < rightValue; case "<" -> leftValue < rightValue;
case "<=" -> leftValue <= rightValue; case "<=" -> leftValue <= rightValue;
case ">" -> leftValue > rightValue; case ">" -> leftValue > rightValue;
@ -37,13 +40,15 @@ public abstract class LamaBinopNode extends LamaExpressionNode {
@Specialization @Specialization
protected boolean boolOp(boolean leftValue, boolean rightValue) { protected boolean boolOp(boolean leftValue, boolean rightValue) {
return switch (op) { return switch (getOp()) {
case "&&" -> leftValue && rightValue; case "&&" -> leftValue && rightValue;
case "!!" -> leftValue || rightValue; case "!!" -> leftValue || rightValue;
default -> throw new IllegalStateException(); default -> throw new IllegalStateException();
}; };
} }
String op; @Fallback
protected boolean noOp(Object leftValue, Object rightValue) {
throw new IllegalStateException(); // TODO
}
} }

View file

@ -22,7 +22,7 @@ public final class LamaCallNode extends LamaExpressionNode {
} }
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
Object targetFunction = this.functionNode.executeGeneric(frame); Object targetFunction = this.functionNode.executeGeneric(frame);
Object[] callArguments = new Object[callArgumentNodes.length]; Object[] callArguments = new Object[callArgumentNodes.length];

View file

@ -5,9 +5,9 @@ import com.oracle.truffle.api.nodes.UnexpectedResultException;
import org.programsnail.truffle_lama.nodes.LamaExpressionNode; import org.programsnail.truffle_lama.nodes.LamaExpressionNode;
public final class LamaConstNode extends LamaExpressionNode { public final class LamaConstNode extends LamaExpressionNode {
private final int value; private final long value;
public LamaConstNode(int value) { public LamaConstNode(long value) {
this.value = value; this.value = value;
} }

View file

@ -13,7 +13,7 @@ public final class LamaGlobalRefNode extends LamaExpressionNode {
} }
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
return new LamaGlobalVarRef(this.name, this.currentLanguageContext()); return new LamaGlobalVarRef(this.name, this.currentLanguageContext());
} }
} }

View file

@ -9,7 +9,7 @@ public final class LamaGlobalVarNode extends LamaExpressionNode {
String name; String name;
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
this.currentLanguageContext().globalScopeObject.newVariable(name, LamaUnit.INSTANCE, false); this.currentLanguageContext().globalScopeObject.newVariable(name, LamaUnit.INSTANCE, false);
return LamaUnit.INSTANCE; return LamaUnit.INSTANCE;
} }

View file

@ -14,7 +14,7 @@ public final class LamaIgnoreNode extends LamaExpressionNode {
} }
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
node.executeGeneric(frame); node.executeGeneric(frame);
return LamaUnit.INSTANCE; return LamaUnit.INSTANCE;
} }

View file

@ -5,6 +5,7 @@ import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.UnexpectedResultException; import com.oracle.truffle.api.nodes.UnexpectedResultException;
import org.programsnail.truffle_lama.FunctionObject; import org.programsnail.truffle_lama.FunctionObject;
import org.programsnail.truffle_lama.LamaLanguage; import org.programsnail.truffle_lama.LamaLanguage;
import org.programsnail.truffle_lama.nodes.FunctionRootNode;
import org.programsnail.truffle_lama.nodes.LamaExpressionNode; import org.programsnail.truffle_lama.nodes.LamaExpressionNode;
public final class LamaLambdaNode extends LamaExpressionNode { public final class LamaLambdaNode extends LamaExpressionNode {
@ -24,7 +25,7 @@ public final class LamaLambdaNode extends LamaExpressionNode {
} }
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
LamaLanguage language = this.currentTruffleLanguage(); LamaLanguage language = this.currentTruffleLanguage();
FunctionRootNode functionRootNode = new FunctionRootNode(language, this.frameDescriptor, this.bodyNode); FunctionRootNode functionRootNode = new FunctionRootNode(language, this.frameDescriptor, this.bodyNode);
return new FunctionObject(functionRootNode.getCallTarget(), this.argumentsCount); return new FunctionObject(functionRootNode.getCallTarget(), this.argumentsCount);

View file

@ -13,7 +13,7 @@ public final class LamaLocalRefNode extends LamaExpressionNode {
} }
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
return new LamaLocalVarRef(this.id, frame); // TODO: correct frame ?? return new LamaLocalVarRef(this.id); // TODO: correct frame ??
} }
} }

View file

@ -18,7 +18,7 @@ public final class LamaLocalVarNode extends LamaExpressionNode {
} }
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
frame.getFrameDescriptor().setSlotKind(this.frameSlot, FrameSlotKind.Object); frame.getFrameDescriptor().setSlotKind(this.frameSlot, FrameSlotKind.Object);
return LamaUnit.INSTANCE; return LamaUnit.INSTANCE;
} }

View file

@ -14,7 +14,7 @@ public final class LamaSeqNode extends LamaExpressionNode {
} }
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
leftNode.executeGeneric(frame); leftNode.executeGeneric(frame);
return rightNode.executeGeneric(frame); return rightNode.executeGeneric(frame);
} }

View file

@ -2,22 +2,23 @@ package org.programsnail.truffle_lama.nodes.expression;
import com.oracle.truffle.api.frame.VirtualFrame; import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.UnexpectedResultException; import com.oracle.truffle.api.nodes.UnexpectedResultException;
import com.oracle.truffle.api.strings.TruffleString;
import org.programsnail.truffle_lama.nodes.LamaExpressionNode; import org.programsnail.truffle_lama.nodes.LamaExpressionNode;
import org.programsnail.truffle_lama.runtime.LamaSexp; import org.programsnail.truffle_lama.runtime.LamaSexp;
public final class LamaSexpNode extends LamaExpressionNode { public final class LamaSexpNode extends LamaExpressionNode {
private String tag; private TruffleString tag;
@Children @Children
private LamaExpressionNode[] elementNodes; private LamaExpressionNode[] elementNodes;
public LamaSexpNode(String tag, LamaExpressionNode[] elementNodes) { public LamaSexpNode(TruffleString tag, LamaExpressionNode[] elementNodes) {
this.tag = tag; this.tag = tag;
this.elementNodes = elementNodes; this.elementNodes = elementNodes;
} }
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
Object[] elements = new Object[elementNodes.length]; Object[] elements = new Object[elementNodes.length];
for (int i = 0; i < elements.length; ++i) { for (int i = 0; i < elements.length; ++i) {
elements[i] = elementNodes[i].executeGeneric(frame); elements[i] = elementNodes[i].executeGeneric(frame);

View file

@ -10,7 +10,7 @@ public final class LamaSkipNode extends LamaExpressionNode {
} }
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
return LamaUnit.INSTANCE; return LamaUnit.INSTANCE;
} }
} }

View file

@ -13,7 +13,7 @@ public final class LamaStringNode extends LamaExpressionNode {
} }
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
return this.value; return this.value;
} }
} }

View file

@ -7,7 +7,7 @@ import org.programsnail.truffle_lama.runtime.LamaUnit;
public final class LamaUnitNode extends LamaExpressionNode { public final class LamaUnitNode extends LamaExpressionNode {
@Override @Override
public Object executeGeneric(VirtualFrame frame) throws UnexpectedResultException { public Object executeGeneric(VirtualFrame frame) {
return LamaUnit.INSTANCE; return LamaUnit.INSTANCE;
} }
} }

View file

@ -4,9 +4,13 @@ import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.frame.FrameDescriptor; import com.oracle.truffle.api.frame.FrameDescriptor;
import com.oracle.truffle.api.source.Source; import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.strings.TruffleString; import com.oracle.truffle.api.strings.TruffleString;
import org.antlr.v4.runtime.Token;
import org.programsnail.truffle_lama.LamaLanguage; import org.programsnail.truffle_lama.LamaLanguage;
import org.programsnail.truffle_lama.LamaStrings; import org.programsnail.truffle_lama.LamaStrings;
import org.programsnail.truffle_lama.nodes.LamaExpressionNode; import org.programsnail.truffle_lama.nodes.LamaExpressionNode;
import org.programsnail.truffle_lama.nodes.expression.LamaConstNode;
import org.programsnail.truffle_lama.nodes.expression.LamaSeqNode;
import org.programsnail.truffle_lama.nodes.expression.LamaStringNode;
import org.programsnail.truffle_lama.nodes.pattern.LamaPattern; import org.programsnail.truffle_lama.nodes.pattern.LamaPattern;
import java.util.HashMap; import java.util.HashMap;
@ -72,142 +76,163 @@ public class LamaNodeFactory {
// --- // ---
public LamaExpressionNode defineFunction(String name, LamaExpressionNode[] args, LamaExpressionNode body) { public LamaExpressionNode defineFunction(String name, LamaExpressionNode[] args, LamaExpressionNode body) {
return null; // TODO
} }
// --- // ---
public LamaExpressionNode createStringNode(String value) { public LamaExpressionNode createStringNode(Token token) {
String text = token.getText();
return addSrcFromToken(new LamaStringNode(
LamaStrings.fromJavaString(text.substring(1, text.length() - 2))), token); // TODO: special chars
} }
public LamaExpressionNode createConstNode(long value) { // TODO: use in grammar
public LamaExpressionNode createConstCharNode(Token token) {
return addSrcFromToken(new LamaConstNode(token.getText().charAt(1)), token); // TODO: special chars
}
public LamaExpressionNode createConstNode(Token token) {
return addSrcFromToken(new LamaConstNode(Long.parseLong(token.getText())), token);
} }
public LamaExpressionNode createVarNode(String name, LamaExpressionNode value) { public LamaExpressionNode createVarNode(String name, LamaExpressionNode value) {
return null; // TODO
} }
public LamaExpressionNode createSeqNode(LamaExpressionNode left, LamaExpressionNode ripht) { public LamaExpressionNode createSeqNode(LamaExpressionNode left, LamaExpressionNode right) {
return addSrcFromNodes(new LamaSeqNode(left, right), left, right);
} }
public LamaExpressionNode createBinopNode(String op, LamaExpressionNode left, LamaExpressionNode ripht) { public LamaExpressionNode createBinopNode(String op, LamaExpressionNode left, LamaExpressionNode right) {
// return addSrcFromNodes(new LamaBinopNo, left, right);
return null; // TODO
} }
// TODO: decide Elem or ElemRef // TODO: decide Elem or ElemRef
public LamaExpressionNode createElemNode(LamaExpressionNode array, LamaExpressionNode index) { public LamaExpressionNode createElemNode(LamaExpressionNode array, LamaExpressionNode index) {
return null; // TODO
} }
public LamaExpressionNode createCallNode(LamaExpressionNode func, LamaExpressionNode[] args) { public LamaExpressionNode createCallNode(LamaExpressionNode func, LamaExpressionNode[] args) {
return null; // TODO
} }
public LamaExpressionNode createRefNode(String name) { public LamaExpressionNode createRefNode(String name) {
return null; // TODO
} }
public LamaExpressionNode createClosureNode(LamaExpressionNode[] args, LamaExpressionNode body) { public LamaExpressionNode createClosureNode(LamaExpressionNode[] args, LamaExpressionNode body) {
return null; // TODO
} }
public LamaExpressionNode createSkipNode() { public LamaExpressionNode createSkipNode() {
return null; // TODO
} }
public LamaExpressionNode createArrayNode(LamaExpressionNode[] elems) { public LamaExpressionNode createArrayNode(LamaExpressionNode[] elems) {
return null; // TODO
} }
// sexps-pairs with tag 'cons' // sexps-pairs with tag 'cons'
public LamaExpressionNode createListSexpNode(LamaExpressionNode[] elems) { public LamaExpressionNode createListSexpNode(LamaExpressionNode[] elems) {
return null; // TODO
} }
public LamaExpressionNode createSexpNode(String tag, LamaExpressionNode[] elems) { public LamaExpressionNode createSexpNode(String tag, LamaExpressionNode[] elems) {
return null; // TODO
} }
public LamaExpressionNode createItNode(LamaExpressionNode condition, LamaExpressionNode doThen, LamaExpressionNode doElse) { public LamaExpressionNode createItNode(LamaExpressionNode condition, LamaExpressionNode doThen, LamaExpressionNode doElse) {
return null; // TODO
} }
public LamaExpressionNode createWhileNode(LamaExpressionNode condition, LamaExpressionNode body) { public LamaExpressionNode createWhileNode(LamaExpressionNode condition, LamaExpressionNode body) {
return null; // TODO
} }
public LamaExpressionNode createDoWhileNode(LamaExpressionNode condition, LamaExpressionNode body) { public LamaExpressionNode createDoWhileNode(LamaExpressionNode condition, LamaExpressionNode body) {
return null; // TODO
} }
public LamaExpressionNode createCaseNode(LamaExpressionNode value, LamaPattern[] patterns, LamaExpressionNode[] exprs) { public LamaExpressionNode createCaseNode(LamaExpressionNode value, LamaPattern[] patterns, LamaExpressionNode[] exprs) {
return null; // TODO
} }
// --- // ---
public LamaPattern createWildcardPattern() { public LamaPattern createWildcardPattern() {
return null; // TODO
} }
public LamaPattern createSexpPattern(String name, LamaPattern[] elems) { public LamaPattern createSexpPattern(String name, LamaPattern[] elems) {
return null; // TODO
} }
public LamaPattern createArrayPattern(LamaPattern[] elems) { public LamaPattern createArrayPattern(LamaPattern[] elems) {
return null; // TODO
} }
// sexps-pairs with tag 'cons' // sexps-pairs with tag 'cons'
public LamaPattern createListSexpPattern(LamaPattern[] elems) { public LamaPattern createListSexpPattern(LamaPattern[] elems) {
return null; // TODO
} }
// TODO: fix name pattern: add possibility of associated pattern // TODO: fix name pattern: add possibility of associated pattern
public LamaPattern createNamedPattern(String name, LamaPattern pattern) { public LamaPattern createNamedPattern(String name, LamaPattern pattern) {
return null; // TODO
} }
public LamaPattern createConstPattern(long value) { public LamaPattern createConstPattern(long value) {
return null; // TODO
} }
public LamaPattern createNegativeConstPattern(long value) { public LamaPattern createNegativeConstPattern(long value) {
return null; // TODO
} }
public LamaPattern createStringPattern(String value) { public LamaPattern createStringPattern(String value) {
return null; // TODO
} }
public LamaPattern createBoxedPattern() { public LamaPattern createBoxedPattern() {
return null; // TODO
} }
public LamaPattern createUnBoxedPattern() { public LamaPattern createUnBoxedPattern() {
return null; // TODO
} }
public LamaPattern createStringTagPattern() { public LamaPattern createStringTagPattern() {
return null; // TODO
} }
public LamaPattern createArrayTagPattern() { public LamaPattern createArrayTagPattern() {
return null; // TODO
} }
public LamaPattern createSexpTagPattern() { public LamaPattern createSexpTagPattern() {
return null; // TODO
} }
public LamaPattern createClosureTagPattern() { public LamaPattern createClosureTagPattern() {
return null; // TODO
} }
// ---= // ---
private static LamaExpressionNode addSrcFromToken(LamaExpressionNode node, Token token) {
node.setSourceSection(token.getStartIndex(), token.getText().length());
return node;
}
private static LamaExpressionNode addSrcFromNodes(LamaExpressionNode node, LamaExpressionNode begin, LamaExpressionNode end) {
int beginIndex = begin.getSourceCharIndex();
node.setSourceSection(beginIndex, end.getSourceEndIndex() - beginIndex);
return node;
}
// ---
public LamaExpressionNode getRootExpr() { public LamaExpressionNode getRootExpr() {
// TODO // TODO

View file

@ -11,4 +11,9 @@ public class LamaException extends AbstractTruffleException {
public LamaException(String message, Node location) { public LamaException(String message, Node location) {
super(message, location); super(message, location);
} }
public static LamaException typeError(Node node, Object... values) {
// TODO
return new LamaException("Type error");
}
} }

View file

@ -7,6 +7,10 @@ public final class LamaGlobalVarRef extends LamaObjRef {
private String name; private String name;
private LamaContext context; private LamaContext context;
public String getName() {
return this.name;
}
public LamaGlobalVarRef(String name, LamaContext context) { public LamaGlobalVarRef(String name, LamaContext context) {
this.name = name; this.name = name;
this.context = context; this.context = context;

View file

@ -7,10 +7,10 @@ import org.programsnail.truffle_lama.nodes.LamaRootNode;
public class BasicNodesTest { public class BasicNodesTest {
@Test @Test
public void basic_test() { public void basic_test() {
LamaExpressionNode exprNode = null; // TODO // LamaExpressionNode exprNode = null; // TODO
var rootNode = new LamaRootNode(exprNode); // var rootNode = new LamaRootNode(exprNode);
CallTarget callTarget = rootNode.getCallTarget(); // CallTarget callTarget = rootNode.getCallTarget();
var result = callTarget.call(); // var result = callTarget.call();
// TODO: check // TODO: check
} }