Access Control is now enabled for Gitlab Pages so you can now restrict Pages to authorized users only. If needed, make your website publicly available : Navigate to your project's Settings > General > Visibility and select Everyone in pages section.

Commit bb4ea2be authored by Tony Licata's avatar Tony Licata

Builtin clauses are now built while parsing, and so permit to instantiate...

Builtin clauses are now built while parsing, and so permit to instantiate builtins with arguments not being ProloGraalVariables
parent 64563b7c
package ch.heiafr.prolograal.builtins.predicates;
import ch.heiafr.prolograal.ProloGraalTypes;
import ch.heiafr.prolograal.nodes.ProloGraalBuiltinHeadNode;
import ch.heiafr.prolograal.nodes.ProloGraalTermNode;
import ch.heiafr.prolograal.nodes.*;
import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.TypeSystemReference;
......@@ -13,7 +12,9 @@ import com.oracle.truffle.api.instrumentation.ProbeNode;
import com.oracle.truffle.api.nodes.NodeInfo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
/**
* Abstract base class for built-ins.
......@@ -28,6 +29,23 @@ import java.util.Map;
@NodeChild(value = "head", type = ProloGraalBuiltinHeadNode.class)
public abstract class ProloGraalBuiltinClause extends ProloGraalClause implements InstrumentableNode {
private static final Map<String, Function<ProloGraalTerm, ProloGraalBuiltinStructure>> builtinsInitializers = fillBuiltinsInitializers();
private static Map<String, Function<ProloGraalTerm, ProloGraalBuiltinStructure>> fillBuiltinsInitializers(){
Map<String, Function<ProloGraalTerm, ProloGraalBuiltinStructure>> initializers = new HashMap<>();
initializers.put("is", ProloGraalBuiltinClause::initializeIs);
return initializers;
}
public static ProloGraalBuiltinStructure initializeIs(ProloGraalTerm isTerm){
System.out.println(isTerm);
if( !(isTerm.isStructure() && isTerm.asStructure().getArity() == 2) )
return null;
List<ProloGraalTerm<?>> isArgs = isTerm.asStructure().getArguments();
ProloGraalIsHeadNode isBuiltinHead = ProloGraalIsHeadNodeGen.create(isTerm,new ProloGraalSimpleTermNode(isArgs.get(0)),new ProloGraalSimpleTermNode(isArgs.get(1)));
return new ProloGraalBuiltinStructure(isTerm.asStructure(),isBuiltinHead);
}
public ProloGraalBuiltinClause() {
super(new HashMap<>());
}
......@@ -53,6 +71,11 @@ public abstract class ProloGraalBuiltinClause extends ProloGraalClause implement
//public abstract ProloGraalObject executeGeneric(VirtualFrame frame);
public static Map<String, Function<ProloGraalTerm, ProloGraalBuiltinStructure>> getBuiltinsInitializers() {
return builtinsInitializers;
}
/**
* We want the built-ins to override this because otherwise they will lose their "ProloGraalBultinClause" status.
* @return a copy of the built-in
......
......@@ -122,6 +122,10 @@ public abstract class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
return null;
}
public static Map<ProloGraalAtom, Function<List<ProloGraalNumber>, ProloGraalNumber>> getOperations() {
return operations;
}
// override the default copy so we do not lose the built-in status
@Override
public ProloGraalClause copy() {
......
......@@ -74,7 +74,8 @@ public final class ProloGraalEvalRootNode extends RootNode {
for (int i = 0; i < request.getGoals().size(); i++) {
goalsArray[i] = new ProloGraalSimpleTermNode(request.getGoals().get(i));
}
ProloGraalClause goal = ProloGraalClauseNodeGen.create(request.getVariables(), new ProloGraalSimpleTermNode(head),goalsArray);
ProloGraalClause goal;
goal = new ProloGraalClause(request.getVariables());//ProloGraalClauseNodeGen.create(request.getVariables(), new ProloGraalSimpleTermNode(head),goalsArray);
goal.setHead(head);
request.getGoals().forEach(proloGraalTerm -> goal.addGoal(proloGraalTerm));
ProloGraalRuntime runtime;
......
package ch.heiafr.prolograal.nodes;
import ch.heiafr.prolograal.builtins.predicates.ProloGraalIsBuiltin;
import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeInfo;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
@NodeInfo(shortName = "ProloGraalIsHeadNode")
public abstract class ProloGraalIsHeadNode extends ProloGraalBinaryHeadNode {
......@@ -22,21 +23,15 @@ public abstract class ProloGraalIsHeadNode extends ProloGraalBinaryHeadNode {
return new ProloGraalSuccess();
}*/
@Specialization(guards = "left.isNumber()")
public ProloGraalBoolean returnValue(VirtualFrame frame, ProloGraalVariable left, ProloGraalVariable right) {
@Specialization(guards = "right.isStructure()")
public ProloGraalBoolean returnValue(VirtualFrame frame, ProloGraalVariable left, ProloGraalIntegerNumber right) {
System.out.println("@Specialization: returnValue");
return left.unify(right)||right.unify(left)?new ProloGraalSuccess():new ProloGraalFailure();
}
@Specialization(guards = "left.isAtom()")
public ProloGraalBoolean executeIs(VirtualFrame frame, ProloGraalVariable left, ProloGraalVariable right) {
System.out.println("@Specialization: executeIs");
return left.unify(right)||right.unify(left)?new ProloGraalSuccess():new ProloGraalFailure();
ProloGraalNumber res = ProloGraalIsBuiltin.getOperations().get(right.asStructure().getFunctor()).apply(right.asStructure().getArguments().stream().map(x -> x.asNumber()).collect(Collectors.toList()));
return left.unify(res)?new ProloGraalSuccess():new ProloGraalFailure();
}
@Specialization
public ProloGraalBoolean testVariable(VirtualFrame frame, ProloGraalVariable left, ProloGraalVariable right) {
public ProloGraalBoolean testVariable(VirtualFrame frame, ProloGraalTerm left, ProloGraalTerm right) {
System.out.println("@Specialization: testVariable");
return left.unify(right)||right.unify(left)?new ProloGraalSuccess():new ProloGraalFailure();
}
......
......@@ -106,6 +106,13 @@ public class ProloGraalProofTreeNode extends Node {
+ TRACE_BETWEEN_WHITE_SPACE + TRACE_QUESTION_MARK);
}
if(currentGoal instanceof ProloGraalBuiltinStructure){
System.out.println("resolving builtin term");
returnedValue = ((ProloGraalBuiltinStructure)currentGoal).getBuiltin().executeBuiltin(frame);
executeState = ExecuteState.RETURN;
break;
}
// get the list of all possible clauses based on the name of the predicate
List<ProloGraalClause> possibleClauses = clauses.get(currentGoal);
if (possibleClauses == null || possibleClauses.isEmpty()) // if no match, throw an error
......
package ch.heiafr.prolograal.parser;
import ch.heiafr.prolograal.ProloGraalLanguage;
import ch.heiafr.prolograal.builtins.predicates.ProloGraalBuiltinClause;
import ch.heiafr.prolograal.nodes.ProloGraalSimpleTermNode;
import ch.heiafr.prolograal.nodes.ProloGraalTermNode;
import ch.heiafr.prolograal.runtime.*;
......@@ -109,12 +110,12 @@ public class ProloGraalListenerImpl extends ProloGraalBaseListener {
}
private ProloGraalTermNode createHead(){
return new ProloGraalSimpleTermNode(new ProloGraalVariable(new HashMap<ProloGraalVariable, ProloGraalVariable>(),"A"));
return new ProloGraalSimpleTermNode(new ProloGraalVariable(new HashMap<>(),"A"));
}
private ProloGraalTermNode[] createGoals(){
ProloGraalTermNode[] goals = new ProloGraalTermNode[1];
goals[0] = new ProloGraalSimpleTermNode(new ProloGraalVariable(new HashMap<ProloGraalVariable, ProloGraalVariable>(),"B"));
goals[0] = new ProloGraalSimpleTermNode(new ProloGraalVariable(new HashMap<>(),"B"));
return goals;
}
......@@ -125,7 +126,7 @@ public class ProloGraalListenerImpl extends ProloGraalBaseListener {
if(!parsingClauses) {
queries.push(new ProloGraalQuery());
}else{
clauses.push(ProloGraalClauseNodeGen.create(new HashMap<>(),createHead(),createGoals()));
clauses.push(new ProloGraalClause(new HashMap<>()));
}
}
......@@ -139,7 +140,7 @@ public class ProloGraalListenerImpl extends ProloGraalBaseListener {
// when we enter a clause, we need to create a new "context" for it.
// Clauses and queries are the "top-level" of Prolog.
if(parsingClauses) {
clauses.push(ProloGraalClauseNodeGen.create(new HashMap<>(),createHead(),createGoals()));
clauses.push(new ProloGraalClause(new HashMap<>()));
}else{
queries.push(new ProloGraalQuery());
}
......@@ -237,6 +238,15 @@ public class ProloGraalListenerImpl extends ProloGraalBaseListener {
for (int i = subterms.size() - 1; i >= 0; i--) {
struct.addSubterm(subterms.get(i));
}
// check if the current structure is a builtin, and initialize it as a builtin if it is one
if(ProloGraalBuiltinClause.getBuiltinsInitializers().containsKey(struct.getFunctor().getName())){
ProloGraalBuiltinStructure builtinStructure = ProloGraalBuiltinClause.getBuiltinsInitializers().get(struct.getFunctor().getName()).apply(struct);
if(builtinStructure != null){
elements.pop();
elements.push(builtinStructure);
}
}
// no need to do anything else since it was already in the element stack (we do not remove it here)
}
......
package ch.heiafr.prolograal.runtime;
import ch.heiafr.prolograal.nodes.ProloGraalBuiltinHeadNode;
import java.util.HashMap;
import java.util.Map;
public class ProloGraalBuiltinStructure extends ProloGraalStructure{
private final ProloGraalBuiltinHeadNode builtin;
public ProloGraalBuiltinStructure(ProloGraalBuiltinHeadNode builtin){
super(new HashMap<>());
this.builtin = builtin;
}
public ProloGraalBuiltinStructure(ProloGraalStructure copiedStruct, ProloGraalBuiltinHeadNode builtin) {
super(copiedStruct);
this.builtin = builtin;
}
public ProloGraalBuiltinHeadNode getBuiltin(){
return builtin;
}
@Override
public ProloGraalBuiltinStructure copy(Map<ProloGraalVariable, ProloGraalVariable> variables) {
ProloGraalBuiltinStructure struct = new ProloGraalBuiltinStructure(builtin);
struct.setFunctor(this.functor.copy(variables));
this.subterms.forEach(x -> struct.addSubterm(x.copy(variables)));
return struct;
}
}
\ No newline at end of file
......@@ -17,13 +17,13 @@ import java.util.stream.Collectors;
@ExportLibrary(InteropLibrary.class)
public class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructure> {
// the functor of this structure
private ProloGraalAtom functor;
protected ProloGraalAtom functor;
// the sub terms of this structure
private final List<ProloGraalTerm<?>> subterms;
protected final List<ProloGraalTerm<?>> subterms;
// Variables contained in this structure only (children only)
private final Map<ProloGraalVariable, ProloGraalVariable> subVariables;
protected final Map<ProloGraalVariable, ProloGraalVariable> subVariables;
// arity of this structure
private int arity;
protected int arity;
public ProloGraalStructure(Map<ProloGraalVariable, ProloGraalVariable> variables) {
super(variables);
......@@ -31,6 +31,14 @@ public class ProloGraalStructure extends ProloGraalTerm<ProloGraalStructure> {
this.subVariables = new HashMap<>();
}
protected ProloGraalStructure(ProloGraalStructure copiedStruct){
super(copiedStruct.getVariables());
functor = copiedStruct.functor;
subterms = copiedStruct.subterms;
subVariables = copiedStruct.subVariables;
arity = copiedStruct.arity;
}
/**
* Add a subterm to this structure.
* In case the subterm is a variable or a structure, correctly adds it to the various variable lists.
......
Markdown is supported
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