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

Handle list tail and internal list representation

parent 5e253d8b
package ch.heiafr.prolograal.builtins;
import ch.heiafr.prolograal.runtime.ProloGraalAtom;
public final class ProloGraalBuiltinAtoms {
public static final ProloGraalAtom EMPTY_LIST = new ProloGraalAtom("[]");
public static final ProloGraalAtom DOT_OPERATOR = new ProloGraalAtom("'.'");
}
\ No newline at end of file
......@@ -58,9 +58,13 @@ fact |
composedClause
;
tail :
term
;
list :
LIST_START
term (SEPARATOR term)* (LIST_ENDING term)?
term (SEPARATOR term)* (LIST_ENDING tail)?
LIST_END
;
......
......@@ -155,6 +155,18 @@ public class ProloGraalBaseListener implements ProloGraalListener {
* <p>The default implementation does nothing.</p>
*/
@Override public void exitClause(ProloGraalParser.ClauseContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterTail(ProloGraalParser.TailContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitTail(ProloGraalParser.TailContext ctx) { }
/**
* {@inheritDoc}
*
......
......@@ -127,6 +127,16 @@ public interface ProloGraalListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitClause(ProloGraalParser.ClauseContext ctx);
/**
* Enter a parse tree produced by {@link ProloGraalParser#tail}.
* @param ctx the parse tree
*/
void enterTail(ProloGraalParser.TailContext ctx);
/**
* Exit a parse tree produced by {@link ProloGraalParser#tail}.
* @param ctx the parse tree
*/
void exitTail(ProloGraalParser.TailContext ctx);
/**
* Enter a parse tree produced by {@link ProloGraalParser#list}.
* @param ctx the parse tree
......
package ch.heiafr.prolograal.parser;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Stack;
import java.util.stream.Collectors;
import org.antlr.v4.runtime.ParserRuleContext;
......@@ -10,26 +12,11 @@ import ch.heiafr.prolograal.runtime.*;
public class ProloGraalListenerImpl extends ProloGraalBaseListener {
private static class DebugStack extends Stack<ProloGraalTerm<?>> {
@Override
public ProloGraalTerm<?> push(ProloGraalTerm<?> item) {
System.out.println("Pushing : " + item.toString());
return super.push(item);
}
@Override
public ProloGraalTerm<?> pop() {
ProloGraalTerm<?> e = super.pop();
System.out.println("Popping off : " + e.toString());
return e;
}
}
private Stack<ProloGraalTerm<?>> elements = new DebugStack();
private Stack<ProloGraalClause> clauses = new Stack<>();
private Deque<ProloGraalTerm<?>> elements = new ArrayDeque<>();
private Deque<ProloGraalClause> clauses = new ArrayDeque<>();
public void debug() {
System.out.println(clauses);
System.out.println(clauses.stream().map(x -> x.toString() + "\n").collect(Collectors.joining()));
}
private void throwParseError(ParserRuleContext ctx, String message) {
......@@ -199,6 +186,18 @@ public class ProloGraalListenerImpl extends ProloGraalBaseListener {
public void exitTerm(ProloGraalParser.TermContext ctx) {
}
@Override
public void enterTail(ProloGraalParser.TailContext ctx) {
}
private ProloGraalTerm<?> tail;
@Override
public void exitTail(ProloGraalParser.TailContext ctx) {
tail = elements.pop();
}
@Override
public void enterList(ProloGraalParser.ListContext ctx) {
elements.push(new ProloGraalList());
......@@ -223,6 +222,11 @@ public class ProloGraalListenerImpl extends ProloGraalBaseListener {
for(int i = items.size()-1; i >= 0; i--) {
list.addItem(items.get(i));
}
if(tail != null) {
list.setTail(tail);
tail = null;
}
list.buildInteralRepresentation();
}
}
\ No newline at end of file
......@@ -24,10 +24,10 @@ public class ProloGraalParser extends Parser {
RULE_prolograal = 0, RULE_atom = 1, RULE_number = 2, RULE_variable = 3,
RULE_functor = 4, RULE_composedTerm = 5, RULE_term = 6, RULE_head = 7,
RULE_fact = 8, RULE_goal = 9, RULE_composedClause = 10, RULE_clause = 11,
RULE_list = 12;
RULE_tail = 12, RULE_list = 13;
public static final String[] ruleNames = {
"prolograal", "atom", "number", "variable", "functor", "composedTerm",
"term", "head", "fact", "goal", "composedClause", "clause", "list"
"term", "head", "fact", "goal", "composedClause", "clause", "tail", "list"
};
private static final String[] _LITERAL_NAMES = {
......@@ -116,21 +116,21 @@ public class ProloGraalParser extends Parser {
try {
enterOuterAlt(_localctx, 1);
{
setState(29);
setState(31);
_errHandler.sync(this);
_la = _input.LA(1);
while (_la==LIST_START || _la==ATOM) {
{
{
setState(26);
setState(28);
clause();
}
}
setState(31);
setState(33);
_errHandler.sync(this);
_la = _input.LA(1);
}
setState(32);
setState(34);
match(EOF);
}
}
......@@ -167,22 +167,22 @@ public class ProloGraalParser extends Parser {
AtomContext _localctx = new AtomContext(_ctx, getState());
enterRule(_localctx, 2, RULE_atom);
try {
setState(37);
setState(39);
_errHandler.sync(this);
switch (_input.LA(1)) {
case ATOM:
enterOuterAlt(_localctx, 1);
{
setState(34);
setState(36);
match(ATOM);
}
break;
case LIST_START:
enterOuterAlt(_localctx, 2);
{
setState(35);
setState(37);
match(LIST_START);
setState(36);
setState(38);
match(LIST_END);
}
break;
......@@ -223,7 +223,7 @@ public class ProloGraalParser extends Parser {
try {
enterOuterAlt(_localctx, 1);
{
setState(39);
setState(41);
match(NUMBER);
}
}
......@@ -260,7 +260,7 @@ public class ProloGraalParser extends Parser {
try {
enterOuterAlt(_localctx, 1);
{
setState(41);
setState(43);
match(VARIABLE);
}
}
......@@ -299,7 +299,7 @@ public class ProloGraalParser extends Parser {
try {
enterOuterAlt(_localctx, 1);
{
setState(43);
setState(45);
atom();
}
}
......@@ -345,30 +345,30 @@ public class ProloGraalParser extends Parser {
try {
enterOuterAlt(_localctx, 1);
{
setState(45);
setState(47);
functor();
{
setState(46);
setState(48);
match(T__0);
setState(47);
setState(49);
term();
setState(52);
setState(54);
_errHandler.sync(this);
_la = _input.LA(1);
while (_la==SEPARATOR) {
{
{
setState(48);
setState(50);
match(SEPARATOR);
setState(49);
setState(51);
term();
}
}
setState(54);
setState(56);
_errHandler.sync(this);
_la = _input.LA(1);
}
setState(55);
setState(57);
match(T__1);
}
}
......@@ -418,41 +418,41 @@ public class ProloGraalParser extends Parser {
TermContext _localctx = new TermContext(_ctx, getState());
enterRule(_localctx, 12, RULE_term);
try {
setState(62);
setState(64);
_errHandler.sync(this);
switch ( getInterpreter().adaptivePredict(_input,3,_ctx) ) {
case 1:
enterOuterAlt(_localctx, 1);
{
setState(57);
setState(59);
composedTerm();
}
break;
case 2:
enterOuterAlt(_localctx, 2);
{
setState(58);
setState(60);
atom();
}
break;
case 3:
enterOuterAlt(_localctx, 3);
{
setState(59);
setState(61);
number();
}
break;
case 4:
enterOuterAlt(_localctx, 4);
{
setState(60);
setState(62);
variable();
}
break;
case 5:
enterOuterAlt(_localctx, 5);
{
setState(61);
setState(63);
list();
}
break;
......@@ -494,20 +494,20 @@ public class ProloGraalParser extends Parser {
HeadContext _localctx = new HeadContext(_ctx, getState());
enterRule(_localctx, 14, RULE_head);
try {
setState(66);
setState(68);
_errHandler.sync(this);
switch ( getInterpreter().adaptivePredict(_input,4,_ctx) ) {
case 1:
enterOuterAlt(_localctx, 1);
{
setState(64);
setState(66);
atom();
}
break;
case 2:
enterOuterAlt(_localctx, 2);
{
setState(65);
setState(67);
composedTerm();
}
break;
......@@ -549,9 +549,9 @@ public class ProloGraalParser extends Parser {
try {
enterOuterAlt(_localctx, 1);
{
setState(68);
setState(70);
head();
setState(69);
setState(71);
match(TERMINATOR);
}
}
......@@ -591,20 +591,20 @@ public class ProloGraalParser extends Parser {
GoalContext _localctx = new GoalContext(_ctx, getState());
enterRule(_localctx, 18, RULE_goal);
try {
setState(73);
setState(75);
_errHandler.sync(this);
switch ( getInterpreter().adaptivePredict(_input,5,_ctx) ) {
case 1:
enterOuterAlt(_localctx, 1);
{
setState(71);
setState(73);
atom();
}
break;
case 2:
enterOuterAlt(_localctx, 2);
{
setState(72);
setState(74);
composedTerm();
}
break;
......@@ -658,29 +658,29 @@ public class ProloGraalParser extends Parser {
try {
enterOuterAlt(_localctx, 1);
{
setState(75);
setState(77);
head();
setState(76);
setState(78);
match(CLAUSE_MARKER);
setState(77);
setState(79);
goal();
setState(82);
setState(84);
_errHandler.sync(this);
_la = _input.LA(1);
while (_la==SEPARATOR) {
{
{
setState(78);
setState(80);
match(SEPARATOR);
setState(79);
setState(81);
goal();
}
}
setState(84);
setState(86);
_errHandler.sync(this);
_la = _input.LA(1);
}
setState(85);
setState(87);
match(TERMINATOR);
}
}
......@@ -720,20 +720,20 @@ public class ProloGraalParser extends Parser {
ClauseContext _localctx = new ClauseContext(_ctx, getState());
enterRule(_localctx, 22, RULE_clause);
try {
setState(89);
setState(91);
_errHandler.sync(this);
switch ( getInterpreter().adaptivePredict(_input,7,_ctx) ) {
case 1:
enterOuterAlt(_localctx, 1);
{
setState(87);
setState(89);
fact();
}
break;
case 2:
enterOuterAlt(_localctx, 2);
{
setState(88);
setState(90);
composedClause();
}
break;
......@@ -750,6 +750,45 @@ public class ProloGraalParser extends Parser {
return _localctx;
}
public static class TailContext extends ParserRuleContext {
public TermContext term() {
return getRuleContext(TermContext.class,0);
}
public TailContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_tail; }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof ProloGraalListener ) ((ProloGraalListener)listener).enterTail(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof ProloGraalListener ) ((ProloGraalListener)listener).exitTail(this);
}
}
public final TailContext tail() throws RecognitionException {
TailContext _localctx = new TailContext(_ctx, getState());
enterRule(_localctx, 24, RULE_tail);
try {
enterOuterAlt(_localctx, 1);
{
setState(93);
term();
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
exitRule();
}
return _localctx;
}
public static class ListContext extends ParserRuleContext {
public TerminalNode LIST_START() { return getToken(ProloGraalParser.LIST_START, 0); }
public List<TermContext> term() {
......@@ -764,6 +803,9 @@ public class ProloGraalParser extends Parser {
return getToken(ProloGraalParser.SEPARATOR, i);
}
public TerminalNode LIST_ENDING() { return getToken(ProloGraalParser.LIST_ENDING, 0); }
public TailContext tail() {
return getRuleContext(TailContext.class,0);
}
public ListContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
......@@ -780,44 +822,44 @@ public class ProloGraalParser extends Parser {
public final ListContext list() throws RecognitionException {
ListContext _localctx = new ListContext(_ctx, getState());
enterRule(_localctx, 24, RULE_list);
enterRule(_localctx, 26, RULE_list);
int _la;
try {
enterOuterAlt(_localctx, 1);
{
setState(91);
setState(95);
match(LIST_START);
setState(92);
setState(96);
term();
setState(97);
setState(101);
_errHandler.sync(this);
_la = _input.LA(1);
while (_la==SEPARATOR) {
{
{
setState(93);
setState(97);
match(SEPARATOR);
setState(94);
setState(98);
term();
}
}
setState(99);
setState(103);
_errHandler.sync(this);
_la = _input.LA(1);
}
setState(102);
setState(106);
_errHandler.sync(this);
_la = _input.LA(1);
if (_la==LIST_ENDING) {
{
setState(100);
setState(104);
match(LIST_ENDING);
setState(101);
term();
setState(105);
tail();
}
}
setState(104);
setState(108);
match(LIST_END);
}
}
......@@ -833,32 +875,33 @@ public class ProloGraalParser extends Parser {
}
public static final String _serializedATN =
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\17m\4\2\t\2\4\3\t"+
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\17q\4\2\t\2\4\3\t"+
"\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t\13\4"+
"\f\t\f\4\r\t\r\4\16\t\16\3\2\7\2\36\n\2\f\2\16\2!\13\2\3\2\3\2\3\3\3\3"+
"\3\3\5\3(\n\3\3\4\3\4\3\5\3\5\3\6\3\6\3\7\3\7\3\7\3\7\3\7\7\7\65\n\7\f"+
"\7\16\78\13\7\3\7\3\7\3\b\3\b\3\b\3\b\3\b\5\bA\n\b\3\t\3\t\5\tE\n\t\3"+
"\n\3\n\3\n\3\13\3\13\5\13L\n\13\3\f\3\f\3\f\3\f\3\f\7\fS\n\f\f\f\16\f"+
"V\13\f\3\f\3\f\3\r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