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 ff7e921e authored by Tony Licata's avatar Tony Licata

atoms surrounded with ' will now be parsed without them in their names, is/2...

atoms surrounded with ' will now be parsed without them in their names, is/2 predicate now return integers if it is possible
parent 42d63dc3
......@@ -10,5 +10,5 @@ import ch.heiafr.prolograal.runtime.ProloGraalAtom;
*/
public final class ProloGraalBuiltinAtoms {
public static final ProloGraalAtom EMPTY_LIST = new ProloGraalAtom(null, "[]");
public static final ProloGraalAtom DOT_OPERATOR = new ProloGraalAtom(null, "'.'");
public static final ProloGraalAtom DOT_OPERATOR = new ProloGraalAtom(null, ".");
}
\ No newline at end of file
......@@ -42,7 +42,6 @@ public final class ProloGraalConsultStringBuiltin extends ProloGraalBuiltinClaus
ProloGraalTerm consultContentTerm = arg.getRootValue();
if(consultContentTerm instanceof ProloGraalAtom){
String consultContentString = ((ProloGraalAtom) consultContentTerm).getName();
consultContentString = consultContentString.substring(1,consultContentString.length()-1);
Source source = Source.newBuilder(ProloGraalLanguage.ID, consultContentString, null).build();
//we filter clauses to remove goal lists from returned clauses
List<ProloGraalClause> clauses = ProloGraalParserImpl.parseClauses(source);
......
......@@ -127,17 +127,26 @@ public final class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
private ProloGraalNumber add(List<ProloGraalNumber> args){
if(args.size()!=2)return null;
return new ProloGraalDoubleNumber(getVariables(),args.get(0).asDouble()+args.get(1).asDouble());
ProloGraalNumber arg0 = args.get(0), arg1 = args.get(1);
if(areAllIntegers(arg0,arg1))
return new ProloGraalIntegerNumber(getVariables(),arg0.asInteger()+arg1.asInteger());
return new ProloGraalDoubleNumber(getVariables(),arg0.asDouble()+arg1.asDouble());
}
private ProloGraalNumber substract(List<ProloGraalNumber> args){
if(args.size()!=2)return null;
return new ProloGraalDoubleNumber(getVariables(),args.get(0).asDouble()-args.get(1).asDouble());
ProloGraalNumber arg0 = args.get(0), arg1 = args.get(1);
if(areAllIntegers(arg0,arg1))
return new ProloGraalIntegerNumber(getVariables(),arg0.asInteger()-arg1.asInteger());
return new ProloGraalDoubleNumber(getVariables(),arg0.asDouble()-arg1.asDouble());
}
private ProloGraalNumber multiply(List<ProloGraalNumber> args){
if(args.size()!=2)return null;
return new ProloGraalDoubleNumber(getVariables(),args.get(0).asDouble()*args.get(1).asDouble());
ProloGraalNumber arg0 = args.get(0), arg1 = args.get(1);
if(areAllIntegers(arg0,arg1))
return new ProloGraalIntegerNumber(getVariables(),arg0.asInteger()*arg1.asInteger());
return new ProloGraalDoubleNumber(getVariables(),arg0.asDouble()*arg1.asDouble());
}
private ProloGraalNumber divide(List<ProloGraalNumber> args){
......@@ -147,7 +156,10 @@ public final class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
private ProloGraalNumber pow(List<ProloGraalNumber> args){
if(args.size()!=2)return null;
return new ProloGraalDoubleNumber(getVariables(),Math.pow(args.get(0).asDouble(),args.get(1).asDouble()));
ProloGraalNumber arg0 = args.get(0), arg1 = args.get(1);
if(areAllIntegers(arg0,arg1) && Math.abs(Math.pow(arg0.asInteger(),arg1.asInteger()))<Integer.MAX_VALUE)
return new ProloGraalIntegerNumber(getVariables(),(int)Math.pow(arg0.asInteger(),arg1.asInteger()));
return new ProloGraalDoubleNumber(getVariables(),Math.pow(arg0.asDouble(),arg1.asDouble()));
}
private ProloGraalNumber sqrt(List<ProloGraalNumber> args){
......@@ -157,14 +169,20 @@ public final class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
private ProloGraalNumber rem(List<ProloGraalNumber> args){
if(args.size()!=2)return null;
return new ProloGraalDoubleNumber(getVariables(),args.get(0).asDouble()%args.get(1).asDouble());
ProloGraalNumber arg0 = args.get(0), arg1 = args.get(1);
if(areAllIntegers(arg0,arg1))
return new ProloGraalIntegerNumber(getVariables(),arg0.asInteger()%arg1.asInteger());
return new ProloGraalDoubleNumber(getVariables(),arg0.asDouble()%arg1.asDouble());
}
private ProloGraalNumber mod(List<ProloGraalNumber> args){
if(args.size()!=2)return null;
double ad = args.get(0).asDouble();
double bd = args.get(1).asDouble();
ProloGraalNumber arg0 = args.get(0), arg1 = args.get(1);
double ad = arg0.asDouble();
double bd = arg1.asDouble();
double res = ((ad % bd + bd) % bd);
if(areAllIntegers(arg0,arg1))
return new ProloGraalIntegerNumber(getVariables(),(int)res);
return new ProloGraalDoubleNumber(getVariables(),res);
}
......@@ -174,7 +192,7 @@ public final class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
int res = 0;
if(val>0) res = 1;
if(val<0) res = -1;
return new ProloGraalDoubleNumber(getVariables(),res);
return new ProloGraalIntegerNumber(getVariables(),res);
}
private ProloGraalNumber exp(List<ProloGraalNumber> args){
......@@ -224,7 +242,10 @@ public final class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
private ProloGraalNumber abs(List<ProloGraalNumber> args){
if(args.size()!=1)return null;
return new ProloGraalDoubleNumber(getVariables(),Math.abs(args.get(0).asDouble()));
ProloGraalNumber arg0 = args.get(0);
if(areAllIntegers(arg0))
return new ProloGraalIntegerNumber(getVariables(),Math.abs(arg0.asInteger()));
return new ProloGraalDoubleNumber(getVariables(),Math.abs(arg0.asDouble()));
}
private void fillOperations(Map<ProloGraalAtom, Function<List<ProloGraalNumber>,ProloGraalNumber>> operations){
......@@ -257,6 +278,11 @@ public final class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
String atomName,
Function<List<ProloGraalNumber>,ProloGraalNumber> function){
operations.put(new ProloGraalAtom(getVariables(),atomName), function);
operations.put(new ProloGraalAtom(getVariables(),"'"+atomName+"'"), function);
}
private boolean areAllIntegers(ProloGraalNumber<?>... args){
for (ProloGraalNumber number : args)
if(!number.isInteger())return false;
return true;
}
}
......@@ -19,6 +19,8 @@ import java.util.stream.Collectors;
*/
public class ProloGraalListenerImpl extends ProloGraalBaseListener {
private static String[] atomDelimiters = {"'","~"};
// both deque are used as stack so we do not need to reverse everything, and because it is encouraged to use
// deque in the stack javadoc
......@@ -150,7 +152,14 @@ public class ProloGraalListenerImpl extends ProloGraalBaseListener {
@Override
public void enterAtom(ProloGraalParser.AtomContext ctx) {
// create an atom an push it to the element stack
ProloGraalAtom atom = new ProloGraalAtom(getVariables(), ctx.getText());
String atomName = ctx.getText();
for (String atomDelimiter: atomDelimiters) {
if(atomName.startsWith(atomDelimiter) && atomName.endsWith(atomDelimiter)){
atomName = atomName.substring(1,atomName.length()-1);
break;
}
}
ProloGraalAtom atom = new ProloGraalAtom(getVariables(), atomName);
elements.push(atom);
}
......
package ch.heiafr.prolograal.runtime;
import ch.heiafr.prolograal.exceptions.ProloGraalTypeCastingError;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.ExportLibrary;
......@@ -36,6 +37,18 @@ public final class ProloGraalDoubleNumber extends ProloGraalNumber<ProloGraalDou
return new ProloGraalDoubleNumber(variables, this.value);
}
@Override
public int asInteger(){
if(isInteger())
return (int)value;
throw new ProloGraalTypeCastingError(this, "integer");
}
@Override
public boolean isInteger(){
return (value*10%10==0 && fitsInInt());
}
@Override
@ExportMessage
public boolean isNumber() {
......
......@@ -36,6 +36,16 @@ public final class ProloGraalIntegerNumber extends ProloGraalNumber<ProloGraalIn
return new ProloGraalIntegerNumber(variables, this.value);
}
@Override
public int asInteger(){
return value;
}
@Override
public boolean isInteger(){
return true;
}
@Override
@ExportMessage
public boolean isNumber() {
......
......@@ -23,6 +23,11 @@ public abstract class ProloGraalNumber<T extends ProloGraalTerm<T>> extends Prol
return d >= -INT_MAX_SAFE_FLOAT && d <= INT_MAX_SAFE_FLOAT;
}
public abstract boolean isInteger();
public abstract int asInteger();
public abstract boolean isNumber();
public abstract double asDouble();
......
......@@ -19,7 +19,7 @@ X = [a, b]
yes
% concatDL('-'([a, b, c | Xs], Xs), '-'([d, e, f | Ys], Ys), R).
Xs = [d, e, f | Ys]
R = '-'/2([a, b, c, d, e, f | Ys], Ys)
R = -/2([a, b, c, d, e, f | Ys], Ys)
yes
% listSuffix([a,b,c], [b,c]).
yes
......
......@@ -4,13 +4,13 @@ yes
A = 10
yes
% is(A,'**'(2,10)).
A = 1024.0
A = 1024
yes
% is(2,'+'(1,1)).
yes
% test(B), is(C,'*'(B,B)).
B = 10
C = 100.0
C = 100
yes
% EOF
yes
\ No newline at end of file
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