Project

General

Profile

root / trunk / compiler / ooasCompiler / src / org / momut / ooas / codegen / prologsymbolic / OoaPrologSymbolicStatement.java @ 7

1
/**
2
  *
3
  *                      OOAS Compiler
4
  *
5
  *       Copyright 2015, AIT Austrian Institute of Technology.
6
  * This code is based on the C# Version of the OOAS Compiler, which is
7
  * copyright 2015 by the Institute of Software Technology, Graz University
8
  * of Technology with portions copyright by the AIT Austrian Institute of
9
  * Technology. All rights reserved.
10
  *
11
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
12
  *
13
  * If you modify the file please update the list of contributors below to in-
14
  * clude your name. Please also stick to the coding convention of using TABs
15
  * to do the basic (block-level) indentation and spaces for anything after
16
  * that. (Enable the display of special chars and it should be pretty obvious
17
  * what this means.) Also, remove all trailing whitespace.
18
  *
19
  * Contributors:
20
  *               Willibald Krenn (AIT)
21
  *               Stephan Zimmerer (AIT)
22
  *               Markus Demetz (AIT)
23
  *               Christoph Czurda (AIT)
24
  *
25
  */
26

    
27

    
28
package org.momut.ooas.codegen.prologsymbolic;
29

    
30
import java.util.ArrayList;
31
import java.util.Iterator;
32

    
33
import org.momut.ooas.ast.expressions.AccessExpression;
34
import org.momut.ooas.ast.expressions.Expression;
35
import org.momut.ooas.ast.expressions.ExpressionKind;
36
import org.momut.ooas.ast.expressions.IdentifierExpression;
37
import org.momut.ooas.ast.identifiers.Identifier;
38
import org.momut.ooas.ast.identifiers.IdentifierKind;
39
import org.momut.ooas.ast.statements.Assignment;
40
import org.momut.ooas.ast.statements.SeqBlock;
41
import org.momut.ooas.ast.statements.Statement;
42
import org.momut.ooas.codegen.prolog.OoaPrologExpression;
43
import org.momut.ooas.codegen.prolog.OoaPrologIdentifier;
44
import org.momut.ooas.codegen.prolog.OoaPrologStatement;
45
import org.momut.ooas.codegen.prolog.OoaPrologType;
46
import org.momut.ooas.codegen.prolog.Scratchbook;
47
import org.momut.ooas.utils.exceptions.NotImplementedException;
48

    
49
public class OoaPrologSymbolicStatement extends OoaPrologStatement
50
{
51
        public static class Factory extends OoaPrologStatement.Factory
52
        {
53
                @Override
54
                public /*override*/ OoaPrologStatement create(
55
                                OoaPrologExpression.Factory exprFactory,
56
                                OoaPrologIdentifier.Factory idFactory,
57
                                OoaPrologType.Factory typeFactory,
58
                                Scratchbook scratchbook)
59
                {
60
                        return new OoaPrologSymbolicStatement(exprFactory, idFactory, typeFactory, scratchbook);
61
                }
62
        }
63

    
64
        @Override
65
        public /*override*/ void visit(Assignment assignment)
66
        {
67
                if (assignment.nondetExpression() != null)
68
                        throw new NotImplementedException();
69

    
70
                final Iterator<Expression> aPlaceIt = assignment.places().iterator();
71
                final Iterator<Expression> aValueIt = assignment.values().iterator();
72
                final ArrayList<String> assignments = new ArrayList<String>();
73

    
74
                while (aPlaceIt.hasNext())
75
                {
76
                        final Expression aPlace = aPlaceIt.next();
77
                        final Expression aValue = aValueIt.next();
78

    
79
                        final OoaPrologExpression prologPlace = createExpressionVisitor(true);
80
                        aPlace.Accept(prologPlace);
81
                        assert(prologPlace.tmpVariables().size() == 1);
82

    
83
                        final OoaPrologExpression prologValue = createExpressionVisitor();
84
                        aValue.Accept(prologValue);
85
                        assert(prologValue.tmpVariables().size() == 1);
86

    
87
                        m_emitter.Append(prologValue.toString());
88

    
89
                        if (aPlace.kind() == ExpressionKind.Access &&
90
                                        ((AccessExpression)aPlace).right().kind() == ExpressionKind.Identifier &&
91
                                        ((IdentifierExpression)((AccessExpression)aPlace).right()).identifier().kind() == IdentifierKind.AttributeIdentifier)
92
                        {
93
                                //access to attribute is always 'self.XY'...
94
                                assignments.add(String.format("%s := %s", prologPlace.tmpVariables().get(0), prologValue.tmpVariables().get(0)));
95
                        }
96
                        else
97
                                if (prologPlace.tmpVariables().get(0).equals("RESULT"))
98
                                        assignments.add(String.format("unify( RESULT = %s)", prologValue.tmpVariables().get(0)));
99
                                else
100
                                        assignments.add(String.format("%s = %s", prologPlace.tmpVariables().get(0), prologValue.tmpVariables().get(0)));
101

    
102
                        if (prologPlace.toString().length() > 0)
103
                        {
104
                                String place = prologPlace.toString();
105
                                place = place.trim();
106
                                if (place.endsWith(","))
107
                                        place = place.substring(0, place.length() - 1);
108

    
109
                                assignments.add(place);
110
                        }
111
                }
112

    
113
                int pos = 0;
114
                for (final String s: assignments)
115
                {
116
                        if (pos != 0)
117
                                m_emitter.Append(",");
118
                        pos++;
119
                        m_emitter.Append(s);
120
                }
121
        }
122

    
123
        @Override
124
        public /*override*/ void visit(SeqBlock seqBlock)
125
        {
126
                int y = 0;
127
                if (seqBlock.symbols().symbolList().size() > 0)
128
                {
129
                        m_emitter.Append(" [");
130
                        for (final Identifier sym: seqBlock.symbols().symbolList())
131
                        {
132
                                if (y != 0)
133
                                        m_emitter.AppendLine(", ");
134
                                else
135
                                        y++;
136

    
137
                                final OoaPrologType atype = createTypeVisitor();
138
                                sym.type().Accept(atype);
139
                                final OoaPrologIdentifier anIdent = createIdentifierVisitor();
140
                                sym.Accept(anIdent);
141
                                m_emitter.Append(String.format("%s:%s", anIdent.toString(), atype.toString()));
142
                        }
143
                        m_emitter.Append("]: ( ");
144
                }
145

    
146
                y = 0;
147
                for (final Statement x: seqBlock.statements())
148
                {
149
                        if (y != 0)
150
                                m_emitter.AppendLine(", ");
151
                        else
152
                                y++;
153
                        m_emitter.Append("(");
154
                        VisitSub(x, seqBlock);
155
                        m_emitter.Append(")");
156
                }
157

    
158
                if (seqBlock.symbols().symbolList().size() > 0)
159
                        m_emitter.Append(")");
160
        }
161

    
162

    
163
        protected OoaPrologSymbolicStatement(
164
                        OoaPrologExpression.Factory exprFactory,
165
                        OoaPrologIdentifier.Factory idFactory,
166
                        OoaPrologType.Factory typeFactory,
167
                        Scratchbook scratchbook)
168
        {
169
                super (exprFactory, idFactory, typeFactory, scratchbook);
170
        }
171
}