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

Parser rewrite wip

parent 3b6c679f
......@@ -23,37 +23,39 @@ functor :
atom
;
composed_term :
composedTerm :
functor ('(' term (',' term)* ')')
;
term :
composed_term |
composedTerm |
atom |
number |
variable |
list
;
fact :
term TERMINATOR
head :
atom |
composedTerm
;
goal :
term
fact :
head TERMINATOR
;
head :
composed_term
goal :
atom |
composedTerm
;
composed_clause :
composedClause :
head CLAUSE_MARKER goal (SEPARATOR goal)* TERMINATOR
;
clause :
fact |
composed_clause
composedClause
;
list :
......
......@@ -76,13 +76,13 @@ public class ProloGraalBaseListener implements ProloGraalListener {
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterComposed_term(ProloGraalParser.Composed_termContext ctx) { }
@Override public void enterComposedTerm(ProloGraalParser.ComposedTermContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitComposed_term(ProloGraalParser.Composed_termContext ctx) { }
@Override public void exitComposedTerm(ProloGraalParser.ComposedTermContext ctx) { }
/**
* {@inheritDoc}
*
......@@ -100,49 +100,61 @@ public class ProloGraalBaseListener implements ProloGraalListener {
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterFact(ProloGraalParser.FactContext ctx) { }
@Override public void enterCallable(ProloGraalParser.CallableContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitFact(ProloGraalParser.FactContext ctx) { }
@Override public void exitCallable(ProloGraalParser.CallableContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterGoal(ProloGraalParser.GoalContext ctx) { }
@Override public void enterHead(ProloGraalParser.HeadContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitGoal(ProloGraalParser.GoalContext ctx) { }
@Override public void exitHead(ProloGraalParser.HeadContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterHead(ProloGraalParser.HeadContext ctx) { }
@Override public void enterFact(ProloGraalParser.FactContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitHead(ProloGraalParser.HeadContext ctx) { }
@Override public void exitFact(ProloGraalParser.FactContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterGoal(ProloGraalParser.GoalContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitGoal(ProloGraalParser.GoalContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterComposed_clause(ProloGraalParser.Composed_clauseContext ctx) { }
@Override public void enterComposedClause(ProloGraalParser.ComposedClauseContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitComposed_clause(ProloGraalParser.Composed_clauseContext ctx) { }
@Override public void exitComposedClause(ProloGraalParser.ComposedClauseContext ctx) { }
/**
* {@inheritDoc}
*
......
......@@ -58,15 +58,15 @@ public interface ProloGraalListener extends ParseTreeListener {
*/
void exitFunctor(ProloGraalParser.FunctorContext ctx);
/**
* Enter a parse tree produced by {@link ProloGraalParser#composed_term}.
* Enter a parse tree produced by {@link ProloGraalParser#composedTerm}.
* @param ctx the parse tree
*/
void enterComposed_term(ProloGraalParser.Composed_termContext ctx);
void enterComposedTerm(ProloGraalParser.ComposedTermContext ctx);
/**
* Exit a parse tree produced by {@link ProloGraalParser#composed_term}.
* Exit a parse tree produced by {@link ProloGraalParser#composedTerm}.
* @param ctx the parse tree
*/
void exitComposed_term(ProloGraalParser.Composed_termContext ctx);
void exitComposedTerm(ProloGraalParser.ComposedTermContext ctx);
/**
* Enter a parse tree produced by {@link ProloGraalParser#term}.
* @param ctx the parse tree
......@@ -77,6 +77,26 @@ public interface ProloGraalListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitTerm(ProloGraalParser.TermContext ctx);
/**
* Enter a parse tree produced by {@link ProloGraalParser#callable}.
* @param ctx the parse tree
*/
void enterCallable(ProloGraalParser.CallableContext ctx);
/**
* Exit a parse tree produced by {@link ProloGraalParser#callable}.
* @param ctx the parse tree
*/
void exitCallable(ProloGraalParser.CallableContext ctx);
/**
* Enter a parse tree produced by {@link ProloGraalParser#head}.
* @param ctx the parse tree
*/
void enterHead(ProloGraalParser.HeadContext ctx);
/**
* Exit a parse tree produced by {@link ProloGraalParser#head}.
* @param ctx the parse tree
*/
void exitHead(ProloGraalParser.HeadContext ctx);
/**
* Enter a parse tree produced by {@link ProloGraalParser#fact}.
* @param ctx the parse tree
......@@ -98,25 +118,15 @@ public interface ProloGraalListener extends ParseTreeListener {
*/
void exitGoal(ProloGraalParser.GoalContext ctx);
/**
* Enter a parse tree produced by {@link ProloGraalParser#head}.
* @param ctx the parse tree
*/
void enterHead(ProloGraalParser.HeadContext ctx);
/**
* Exit a parse tree produced by {@link ProloGraalParser#head}.
* @param ctx the parse tree
*/
void exitHead(ProloGraalParser.HeadContext ctx);
/**
* Enter a parse tree produced by {@link ProloGraalParser#composed_clause}.
* Enter a parse tree produced by {@link ProloGraalParser#composedClause}.
* @param ctx the parse tree
*/
void enterComposed_clause(ProloGraalParser.Composed_clauseContext ctx);
void enterComposedClause(ProloGraalParser.ComposedClauseContext ctx);
/**
* Exit a parse tree produced by {@link ProloGraalParser#composed_clause}.
* Exit a parse tree produced by {@link ProloGraalParser#composedClause}.
* @param ctx the parse tree
*/
void exitComposed_clause(ProloGraalParser.Composed_clauseContext ctx);
void exitComposedClause(ProloGraalParser.ComposedClauseContext ctx);
/**
* Enter a parse tree produced by {@link ProloGraalParser#clause}.
* @param ctx the parse tree
......
......@@ -8,117 +8,123 @@ import ch.heiafr.prolograal.runtime.*;
public class ProloGraalListenerImpl extends ProloGraalBaseListener {
private List<ProloGraalTerm<?>> clauses;
private List<List<ProloGraalVariable>> variablesForEachClause;
private Stack<ProloGraalAtom> functors;
private Stack<List<ProloGraalTerm<?>>> structArgs;
private int depth;
private Stack<ProloGraalClause> clauses = new Stack<>();
public void debug() {
System.out.println("Clauses : ");
System.out.println(clauses);
System.out.println("Variables :");
System.out.println(variablesForEachClause);
/*
* System.out.println("Functors : "); System.out.println(functors);
* System.out.println("Struct args : "); System.out.println(structArgs);
*/
}
private void add(ProloGraalTerm<?> term) {
if (depth == 0)
clauses.add(term);
else
structArgs.peek().add(term);
@Override
public void enterProlograal(ProloGraalParser.ProlograalContext ctx) {
}
public List<ProloGraalTerm<?>> getClauses() {
return clauses;
@Override
public void exitProlograal(ProloGraalParser.ProlograalContext ctx) {
}
public List<List<ProloGraalVariable>> getVariables() {
return variablesForEachClause;
}
@Override
public void enterProlograal(ProloGraalParser.ProlograalContext ctx) {
clauses = new ArrayList<>();
structArgs = new Stack<>();
functors = new Stack<>();
variablesForEachClause = new ArrayList<>();
depth = 0;
public void enterAtom(ProloGraalParser.AtomContext ctx) {
}
@Override
public void enterTerm(ProloGraalParser.TermContext ctx) {
if (ctx.getChildCount() > 1)
depth++;
// System.out.println(depth + " enterTerm : " + ctx.getText());
public void exitAtom(ProloGraalParser.AtomContext ctx) {
}
@Override
public void exitTerm(ProloGraalParser.TermContext ctx) {
if (ctx.getChildCount() > 1) {
depth--;
ProloGraalStructure struct = new ProloGraalStructure(functors.pop(), structArgs.pop());
add(struct);
}
// System.out.println(depth + " exitTerm : " + ctx.getText());
public void enterNumber(ProloGraalParser.NumberContext ctx) {
}
@Override
public void enterAtom(ProloGraalParser.AtomContext ctx) {
if (ctx.parent instanceof ProloGraalParser.FunctorContext) { // if the parent is a functor
return; // already handled
}
add(new ProloGraalAtom(ctx.getText()));
// System.out.println("enterAtom : " + ctx.getText());
public void exitNumber(ProloGraalParser.NumberContext ctx) {
}
@Override
public void enterVariable(ProloGraalParser.VariableContext ctx) {
if(depth == 0)
ProloGraalParserImpl.throwParseError(ProloGraalParserImpl.source, ctx.getStart().getLine(), 0, ctx.getStart(), "Head is a variable !");
ProloGraalVariable var = new ProloGraalVariable(ctx.getText());
final List<ProloGraalVariable> variablesForThisClause = variablesForEachClause.get(variablesForEachClause.size() - 1);
final ProloGraalVariable var2 = var;
// if the variable was previsouly encountered in this scope, do not add a new one
var = variablesForThisClause.stream().filter(x -> x.equals(var2)).findFirst().orElseGet(() -> {
variablesForThisClause.add(var2);
return var2;
});
add(var);
}
@Override
public void enterNumber(ProloGraalParser.NumberContext ctx) {
String n = ctx.getText();
try {
add(new ProloGraalIntegerNumber(Integer.parseInt(n)));
// System.out.println("enterNumberInt : " + n);
} catch (NumberFormatException ex) {
add(new ProloGraalDoubleNumber(Double.parseDouble(n)));
// System.out.println("enterNumberDouble : " + n);
}
public void exitVariable(ProloGraalParser.VariableContext ctx) {
}
@Override
public void enterFunctor(ProloGraalParser.FunctorContext ctx) {
functors.push(new ProloGraalAtom(ctx.getText()));
structArgs.push(new ArrayList<>());
// System.out.println("enterFunctor : " + ctx.getText());
}
@Override
public void exitFunctor(ProloGraalParser.FunctorContext ctx) {
}
@Override
public void enterComposedTerm(ProloGraalParser.ComposedTermContext ctx) {
}
@Override
public void exitComposedTerm(ProloGraalParser.ComposedTermContext ctx) {
}
@Override
public void enterTerm(ProloGraalParser.TermContext ctx) {
}
@Override
public void exitTerm(ProloGraalParser.TermContext ctx) {
}
@Override
public void enterFact(ProloGraalParser.FactContext ctx) {
// System.out.println("enterFact : " + ctx.getText());
}
@Override
public void exitFact(ProloGraalParser.FactContext ctx) {
}
@Override
public void enterGoal(ProloGraalParser.GoalContext ctx) {
}
@Override
public void exitGoal(ProloGraalParser.GoalContext ctx) {
}
@Override
public void enterCallable(ProloGraalParser.CallableContext ctx) {
}
@Override
public void exitCallable(ProloGraalParser.CallableContext ctx) {
}
@Override
public void enterHead(ProloGraalParser.HeadContext ctx) {
}
@Override
public void exitHead(ProloGraalParser.HeadContext ctx) {
}
@Override
public void enterComposedClause(ProloGraalParser.ComposedClauseContext ctx) {
}
@Override
public void exitComposedClause(ProloGraalParser.ComposedClauseContext ctx) {
}
@Override
public void enterClause(ProloGraalParser.ClauseContext ctx) {
variablesForEachClause.add(new ArrayList<>());
}
@Override
public void exitClause(ProloGraalParser.ClauseContext ctx) {
}
@Override
public void enterList(ProloGraalParser.ListContext ctx) {
}
@Override
public void exitList(ProloGraalParser.ListContext ctx) {
}
}
\ No newline at end of file
......@@ -57,6 +57,6 @@ public class ProloGraalParserImpl {
if(ProloGraalLanguage.DEBUG_MODE)
listener.debug();
return new ProloGraalRuntime(listener.getClauses(), listener.getVariables());
return new ProloGraalRuntime(null, null);
}
}
\ No newline at end of file
package ch.heiafr.prolograal.runtime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ProloGraalClause {
private ProloGraalTerm<?> head;
private List<ProloGraalTerm<?>> goals;
private Set<ProloGraalVariable> variables;
public ProloGraalClause(ProloGraalTerm<?> head) {
this.head = head;
goals = new ArrayList<>();
variables = new HashSet<>();
}
public void addGoal(ProloGraalTerm<?> goal) {
goals.add(goal);
}
public void addVariable(ProloGraalVariable var) {
variables.add(var);
}
public ProloGraalTerm<?> getLastGoal() {
return goals.get(goals.size()-1);
}
}
\ No newline at end of file
% this file is a show of some atrocities that may or may not compile in gprolog
% valid
a :-
test(X).
a.
b :-
test(X),
5,
a.
test([a,b, f(X)]).
% illegal but not fatal
a,b :- test(X).
a,b :- test(X), d.
[a, X] :-
test(X).
[a,b,c].
% fatal
% test([test(X) :- test(X), b]).
% 8 :- test(X).
% 5.
% A.
\ No newline at end of file
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