Commit f4898d13 authored by Martin Spoto's avatar Martin Spoto
Browse files

Working variables (may require bugfixes)

parent 472ea272
......@@ -3,11 +3,9 @@ package ch.heiafr.prolograal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.TruffleLanguage.ContextPolicy;
......
package ch.heiafr.prolograal.nodes;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.Frame;
import com.oracle.truffle.api.nodes.Node;
public abstract class ProloGraalClauseNode extends Node {
......
......@@ -19,7 +19,7 @@ import ch.heiafr.prolograal.runtime.ProloGraalVariable;
public final class ProloGraalEvalRootNode extends RootNode {
private final ProloGraalLanguage language;
private final List<ProloGraalTerm> clauses;
private final List<ProloGraalTerm<?>> clauses;
private final List<List<ProloGraalVariable>> variables;
@CompilationFinal
private boolean registered;
......@@ -29,7 +29,7 @@ public final class ProloGraalEvalRootNode extends RootNode {
@Child
private DirectCallNode mainCallNode;
public ProloGraalEvalRootNode(ProloGraalLanguage language, List<ProloGraalTerm> clauses, List<List<ProloGraalVariable>> variables) {
public ProloGraalEvalRootNode(ProloGraalLanguage language, List<ProloGraalTerm<?>> clauses, List<List<ProloGraalVariable>> variables) {
super(null); // internal frame
this.clauses = clauses;
this.variables = variables;
......
package ch.heiafr.prolograal.nodes;
import java.io.IOException;
import java.util.List;
import javax.naming.spi.DirStateFactory.Result;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.RootNode;
......@@ -21,7 +18,6 @@ import ch.heiafr.prolograal.parser.ProloGraalParseError;
import ch.heiafr.prolograal.parser.ProloGraalParseResult;
import ch.heiafr.prolograal.parser.ProloGraalParserImpl;
import ch.heiafr.prolograal.runtime.ProloGraalContext;
import ch.heiafr.prolograal.runtime.ProloGraalFailure;
import ch.heiafr.prolograal.runtime.ProloGraalSuccess;
import ch.heiafr.prolograal.runtime.ProloGraalTerm;
......@@ -45,7 +41,7 @@ public class ProloGraalInterpreterNode extends RootNode {
Source source = Source.newBuilder("pl", line, null).build();
try {
ProloGraalParseResult result = ProloGraalParserImpl.parseProloGraal(language, source);
List<ProloGraalTerm> terms = result.getTerms();
List<ProloGraalTerm<?>> terms = result.getTerms();
if(terms.size() == 1) {
if(terms.get(0).toString().equals("exit")) break;
......
package ch.heiafr.prolograal.nodes;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.NodeInfo;
......
package ch.heiafr.prolograal.nodes;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.sound.sampled.SourceDataLine;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.RootNode;
import ch.heiafr.prolograal.ProloGraalLanguage;
import ch.heiafr.prolograal.exceptions.ProloGraalExistenceError;
import ch.heiafr.prolograal.parser.ProloGraalParseResult;
import ch.heiafr.prolograal.runtime.ProloGraalBoolean;
import ch.heiafr.prolograal.runtime.ProloGraalContext;
import ch.heiafr.prolograal.runtime.ProloGraalFailure;
import ch.heiafr.prolograal.runtime.ProloGraalSuccess;
import ch.heiafr.prolograal.runtime.ProloGraalTerm;
import ch.heiafr.prolograal.runtime.ProloGraalVariable;
......@@ -20,7 +18,7 @@ import ch.heiafr.prolograal.runtime.ProloGraalVariable;
public final class ProloGraalResolverNode extends RootNode {
private final ProloGraalContext context;
private final List<ProloGraalTerm> terms;
private final List<ProloGraalTerm<?>> terms;
private final List<List<ProloGraalVariable>> variables;
protected ProloGraalResolverNode(ProloGraalLanguage language) {
......@@ -33,24 +31,43 @@ public final class ProloGraalResolverNode extends RootNode {
@Override
public Object execute(VirtualFrame frame) {
ProloGraalParseResult result = (ProloGraalParseResult) frame.getArguments()[0];
ProloGraalTerm goal = result.getTerms().get(0);
ProloGraalParseResult goal = result;
System.out.println("Executing : " + goal);
System.out.println("Executing : " + goal.getTerms().get(0));
List<ProloGraalTerm> filteredTerms = terms.stream().filter(x -> goal.equals(x)).collect(Collectors.toList());
/*List<ProloGraalTerm> filteredTerms = terms.stream().filter(x -> goal.equals(x)).collect(Collectors.toList());
if(filteredTerms.size() == 0)
throw new ProloGraalExistenceError();
throw new ProloGraalExistenceError();*/
return resolve(goal, filteredTerms);
return resolve(goal);
}
public ProloGraalBoolean resolve(ProloGraalTerm goal, List<ProloGraalTerm> clauses) {
System.out.println("Current goal : " + goal);
System.out.println("Current candidates : " + clauses);
public ProloGraalBoolean resolve(ProloGraalParseResult goalResult) {
//System.out.println("Current goal : " + goal);
//System.out.println("Current candidates : " + terms);
ProloGraalTerm<?> goal = goalResult.getTerms().get(0);
List<ProloGraalVariable> goalVariables = goalResult.getVariables().get(0);
List<ProloGraalSuccess> successes = new ArrayList<>();
for(int i = 0; i < terms.size(); i++) {
ProloGraalTerm<?> clause = terms.get(i);
if(clause.isUnifiable(goal)) {
System.out.println("Success with clause : " + clause.toString());
successes.add(new ProloGraalSuccess(clause, variables.get(i)));
}
variables.get(i).forEach(ProloGraalVariable::unbind);
goalVariables.forEach(ProloGraalVariable::unbind);
}
if(successes.size() > 0) {
return new ProloGraalSuccess(successes);
}
return new ProloGraalSuccess();
return new ProloGraalFailure();
}
}
\ No newline at end of file
package ch.heiafr.prolograal.nodes;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.Node;
public abstract class ProloGraalTermNode extends ProloGraalNode {
}
\ No newline at end of file
......@@ -4,18 +4,14 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import javax.sound.sampled.SourceDataLine;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import ch.heiafr.prolograal.runtime.*;
public class ProloGraalListenerImpl extends ProloGraalBaseListener {
private List<ProloGraalTerm> clauses;
private List<ProloGraalTerm<?>> clauses;
private List<List<ProloGraalVariable>> variablesForEachClause;
private Stack<ProloGraalAtom> functors;
private Stack<List<ProloGraalTerm>> structArgs;
private Stack<List<ProloGraalTerm<?>>> structArgs;
private int depth;
......@@ -30,14 +26,14 @@ public class ProloGraalListenerImpl extends ProloGraalBaseListener {
*/
}
private void add(ProloGraalTerm term) {
private void add(ProloGraalTerm<?> term) {
if (depth == 0)
clauses.add(term);
else
structArgs.peek().add(term);
}
public List<ProloGraalTerm> getClauses() {
public List<ProloGraalTerm<?>> getClauses() {
return clauses;
}
......
......@@ -6,15 +6,15 @@ import ch.heiafr.prolograal.runtime.ProloGraalTerm;
import ch.heiafr.prolograal.runtime.ProloGraalVariable;
public final class ProloGraalParseResult {
private final List<ProloGraalTerm> terms;
private final List<ProloGraalTerm<?>> terms;
private List<List<ProloGraalVariable>> variables;
public ProloGraalParseResult(List<ProloGraalTerm> terms, List<List<ProloGraalVariable>> variables) {
public ProloGraalParseResult(List<ProloGraalTerm<?>> terms, List<List<ProloGraalVariable>> variables) {
this.terms = terms;
this.variables = variables;
}
public List<ProloGraalTerm> getTerms() {
public List<ProloGraalTerm<?>> getTerms() {
return terms;
}
......
package ch.heiafr.prolograal.runtime;
public final class ProloGraalAtom extends ProloGraalTerm {
public final class ProloGraalAtom extends ProloGraalTerm<ProloGraalAtom> {
private final String name;
public ProloGraalAtom(String name) {
......@@ -22,7 +22,7 @@ public final class ProloGraalAtom extends ProloGraalTerm {
}
@Override
public boolean isUnifiable(ProloGraalTerm other) {
public boolean isUnifiable(ProloGraalTerm<?> other) {
if(this.equals(other)) {
return true;
} else if(other instanceof ProloGraalVariable) {
......@@ -30,4 +30,9 @@ public final class ProloGraalAtom extends ProloGraalTerm {
}
return false;
}
@Override
public ProloGraalAtom copy() {
return new ProloGraalAtom(this.name);
}
}
\ No newline at end of file
......@@ -7,7 +7,6 @@ import java.util.List;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.TruffleLanguage.Env;
import com.oracle.truffle.api.nodes.RootNode;
import ch.heiafr.prolograal.ProloGraalLanguage;
import ch.heiafr.prolograal.nodes.ProloGraalInterpreterNode;
......@@ -15,7 +14,7 @@ import ch.heiafr.prolograal.nodes.ProloGraalResolverNode;
public class ProloGraalContext {
private List<ProloGraalTerm> clauses;
private List<ProloGraalTerm<?>> clauses;
private List<List<ProloGraalVariable>> variables;
private ProloGraalInterpreterNode interpreterNode;
private ProloGraalResolverNode resolverNode;
......@@ -41,7 +40,7 @@ public class ProloGraalContext {
return output;
}
public void registerClauses(List<ProloGraalTerm> clauses) {
public void registerClauses(List<ProloGraalTerm<?>> clauses) {
this.clauses = clauses;
}
......@@ -57,7 +56,7 @@ public class ProloGraalContext {
this.resolverNode = resolverNode;
}
public List<ProloGraalTerm> getClauses() {
public List<ProloGraalTerm<?>> getClauses() {
return clauses;
}
......
package ch.heiafr.prolograal.runtime;
public final class ProloGraalDoubleNumber extends ProloGraalNumber {
public final class ProloGraalDoubleNumber extends ProloGraalNumber<ProloGraalDoubleNumber> {
final double value;
public ProloGraalDoubleNumber(double value) {
......@@ -25,5 +25,10 @@ public final class ProloGraalDoubleNumber extends ProloGraalNumber {
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof ProloGraalDoubleNumber && this.value == ((ProloGraalDoubleNumber)obj).value;
}
}
@Override
public ProloGraalDoubleNumber copy() {
return new ProloGraalDoubleNumber(this.value);
}
}
\ No newline at end of file
package ch.heiafr.prolograal.runtime;
public final class ProloGraalIntegerNumber extends ProloGraalNumber {
public final class ProloGraalIntegerNumber extends ProloGraalNumber<ProloGraalIntegerNumber> {
final int value;
public ProloGraalIntegerNumber(int value) {
......@@ -26,4 +26,9 @@ public final class ProloGraalIntegerNumber extends ProloGraalNumber {
public boolean equals(Object obj) {
return obj != null && obj instanceof ProloGraalIntegerNumber && this.value == ((ProloGraalIntegerNumber)obj).value;
}
@Override
public ProloGraalIntegerNumber copy() {
return new ProloGraalIntegerNumber(this.value);
}
}
\ No newline at end of file
package ch.heiafr.prolograal.runtime;
public abstract class ProloGraalNumber extends ProloGraalTerm {
public abstract class ProloGraalNumber<T extends ProloGraalTerm<T>> extends ProloGraalTerm<T> {
public abstract double asDouble();
public abstract int asInt();
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof ProloGraalNumber && ((ProloGraalNumber)obj).asDouble() == this.asDouble() && ((ProloGraalNumber)obj).asInt() == this.asInt();
return obj != null && obj instanceof ProloGraalNumber && ((ProloGraalNumber<?>)obj).asDouble() == this.asDouble() && ((ProloGraalNumber<?>)obj).asInt() == this.asInt();
}
@Override
public boolean isUnifiable(ProloGraalTerm other) {
return this.equals(other);
public boolean isUnifiable(ProloGraalTerm<?> other) {
if(other instanceof ProloGraalVariable)
return ((ProloGraalVariable)other).isUnifiable(this);
else
return this.equals(other);
}
}
\ No newline at end of file
package ch.heiafr.prolograal.runtime;
import java.util.ArrayList;
import java.util.List;
public final class ProloGraalStructure extends ProloGraalTerm {
public final class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructure> {
protected final ProloGraalAtom functor;
protected final List<ProloGraalTerm> subterms;
protected final List<ProloGraalTerm<?>> subterms;
protected final int arity;
public ProloGraalStructure(ProloGraalAtom functor, List<ProloGraalTerm> subterms) {
public ProloGraalStructure(ProloGraalAtom functor, List<ProloGraalTerm<?>> subterms) {
this.functor = functor;
this.arity = subterms.size();
this.subterms = subterms;
......@@ -17,7 +18,7 @@ public final class ProloGraalStructure extends ProloGraalTerm {
return this.functor;
}
public List<ProloGraalTerm> getArguments() {
public List<ProloGraalTerm<?>> getArguments() {
return this.subterms;
}
......@@ -40,7 +41,7 @@ public final class ProloGraalStructure extends ProloGraalTerm {
}
@Override
public boolean isUnifiable(ProloGraalTerm other) {
public boolean isUnifiable(ProloGraalTerm<?> other) {
if(this.equals(other)) {
for(int i = 0; i < this.arity; i++) {
if(!this.subterms.get(i).isUnifiable(((ProloGraalStructure)other).subterms.get(i))) {
......@@ -53,4 +54,11 @@ public final class ProloGraalStructure extends ProloGraalTerm {
}
return false;
}
@Override
public ProloGraalStructure copy() {
List<ProloGraalTerm<?>> subterms = new ArrayList<>(this.subterms.size());
this.subterms.forEach(x -> subterms.add(x.copy()));
return new ProloGraalStructure(this.functor.copy(), subterms);
}
}
\ No newline at end of file
package ch.heiafr.prolograal.runtime;
import java.util.ArrayList;
import java.util.List;
public class ProloGraalSuccess extends ProloGraalBoolean {
@Override
private ProloGraalTerm<?> clause;
private List<ProloGraalVariable> variables;
private List<ProloGraalSuccess> successes;
public ProloGraalSuccess() {
}
public ProloGraalSuccess(ProloGraalTerm<?> clause, List<ProloGraalVariable> variables) {
this.clause = clause.copy();
this.variables = new ArrayList<>(variables.size());
for(ProloGraalVariable variable : variables) {
this.variables.add(variable.copy());
}
}
public ProloGraalSuccess(List<ProloGraalSuccess> successes) {
this.successes = successes;
}
@Override
public String toString() {
return "yes";
}
......
......@@ -2,9 +2,11 @@ package ch.heiafr.prolograal.runtime;
import com.oracle.truffle.api.interop.TruffleObject;
public abstract class ProloGraalTerm implements TruffleObject {
public abstract class ProloGraalTerm<T extends ProloGraalTerm<T>> implements TruffleObject {
@Override
public abstract boolean equals(Object obj);
public abstract boolean isUnifiable(ProloGraalTerm other);
public abstract boolean isUnifiable(ProloGraalTerm<?> other);
public abstract T copy();
}
\ No newline at end of file
package ch.heiafr.prolograal.runtime;
public final class ProloGraalVariable extends ProloGraalTerm {
public final class ProloGraalVariable extends ProloGraalTerm<ProloGraalVariable> {
private static int anonymousVariableSuffix = 0;
private final String name;
private boolean isBound = false;
private ProloGraalTerm boundValue;
private ProloGraalTerm<?> boundValue;
public ProloGraalVariable(String name) {
if(name.startsWith("_")) {
......@@ -14,14 +14,21 @@ public final class ProloGraalVariable extends ProloGraalTerm {
this.name = name;
}
public ProloGraalTerm getValue() {
public ProloGraalTerm<?> getValue() {
if(isBound)
return boundValue;
else
return this;
}
public void bind(ProloGraalTerm other) {
public void bind(ProloGraalTerm<?> other) {
if(other instanceof ProloGraalVariable) {
ProloGraalVariable otherVar = (ProloGraalVariable)other;
if(otherVar.isBound && otherVar.boundValue == this) {
//throw new RuntimeException("Recursive binding for variables " + this.name + " and " + otherVar.name);
return;
}
}
this.isBound = true;
this.boundValue = other;
System.out.println(name + " = " + other);
......@@ -43,9 +50,35 @@ public final class ProloGraalVariable extends ProloGraalTerm {
}
@Override
public boolean isUnifiable(ProloGraalTerm other) {
// TODO Auto-generated method stub
return false;
public boolean isUnifiable(ProloGraalTerm<?> other) {
if(other instanceof ProloGraalVariable && (ProloGraalVariable)other == this) // can always unify to itself
return true;
if(this.isBound) {
if(other instanceof ProloGraalVariable) {
ProloGraalVariable otherVar = (ProloGraalVariable)other;
if(otherVar.isBound) {
return this.boundValue.isUnifiable(otherVar.boundValue); // FIXME maybe this will cause infinite loops
} else {
otherVar.bind(this);
return true;
}
} else {
return this.boundValue.isUnifiable(other);
}
} else {
this.bind(other);
return true;
}
}
@Override
public ProloGraalVariable copy() {
ProloGraalVariable var = new ProloGraalVariable(this.name);
var.isBound = this.isBound;
if(this.isBound) {
var.boundValue = this.boundValue.copy();
}
return var;
}
}
\ No newline at end of file
......@@ -7,7 +7,6 @@ import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.PolyglotException;
......@@ -58,15 +57,6 @@ public final class ProloGraalMain {
try {
context.eval(source);
/*String query = "";
Scanner scanner = new Scanner(System.in);
while(!query.equals("exit.")) {
System.out.print("?- ");
query = scanner.nextLine(); // TODO do something with the query we receive
}
scanner.close();*/ // not doing that here after all
return 0;
} catch (PolyglotException ex) {
if (ex.isInternalError()) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment