Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / codegen / PrologSymbolic / ooaPrologSymbolicStatement.cs

1 3 krennw
/**
2
  *
3
  *                      OOAS Compiler (Deprecated)
4
  *
5
  * Copyright 2015, Institute for Software Technology, Graz University of
6
  * Technology. Portions are copyright 2015 by the AIT Austrian Institute
7
  * of Technology. All rights reserved.
8
  *
9
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
10
  *
11
  * Please notice that this version of the OOAS compiler is considered de-
12
  * precated. Only the Java version is maintained.
13
  *
14
  * Contributors:
15
  *               Willibald Krenn (TU Graz/AIT)
16
  *               Stefan Tiran (TU Graz/AIT)
17
  */
18
19
using System;
20
using System.Collections.Generic;
21
using System.Text;
22
using TUG.Mogentes.Codegen.Prolog;
23
24
namespace TUG.Mogentes.Codegen.PrologSymbolic
25
{
26
    public class OoaPrologSymbolicStatement : OoaPrologStatement
27
    {
28
        public new class Factory : OoaPrologStatement.Factory
29
        {
30
            public override OoaPrologStatement create(
31
                OoaPrologExpression.Factory exprFactory,
32
                OoaPrologIdentifier.Factory idFactory,
33
                OoaPrologType.Factory typeFactory)
34
            {
35
                return new OoaPrologSymbolicStatement(exprFactory, idFactory, typeFactory);
36
            }
37
        }
38
39
        public override void visit(Assignment assignment)
40
        {
41
            if (assignment.nondetExpression != null)
42
                throw new NotImplementedException();
43
44
            LinkedListNode<Expression> aPlace = assignment.places.First;
45
            LinkedListNode<Expression> aValue = assignment.values.First;
46
            List<string> assignments = new List<string>();
47
48
            while (aPlace != null)
49
            {
50
                OoaPrologExpression prologPlace = createExpressionVisitor(true);
51
                aPlace.Value.Accept(prologPlace);
52
                System.Diagnostics.Debug.Assert(prologPlace.tmpVariables.Count == 1);
53
54
                OoaPrologExpression prologValue = createExpressionVisitor();
55
                aValue.Value.Accept(prologValue);
56
                System.Diagnostics.Debug.Assert(prologValue.tmpVariables.Count == 1);
57
58
                m_emitter.Append(prologValue.ToString());
59
60
                if (aPlace.Value.kind == ExpressionKind.Access &&
61
                    ((AccessExpression)aPlace.Value).right.kind == ExpressionKind.Identifier &&
62
                    ((IdentifierExpression)((AccessExpression)aPlace.Value).right).identifier.kind == IdentifierKind.AttributeIdentifier)
63
                {
64
                    //access to attribute is always 'self.XY'...
65
                    assignments.Add(String.Format("{0} := {1}", prologPlace.tmpVariables[0], prologValue.tmpVariables[0]));
66
                }
67
                else
68
                    if (prologPlace.tmpVariables[0] == "RESULT")
69
                        assignments.Add(String.Format("unify( RESULT = {0})", prologValue.tmpVariables[0]));
70
//                    else if (aPlace.Value.type.kind == TypeKind.IntType)
71
//                        assignments.Add(String.Format("{0} #= {1}", prologPlace.tmpVariables[0], prologValue.tmpVariables[0]));
72
                    else
73
                        assignments.Add(String.Format("{0} = {1}", prologPlace.tmpVariables[0], prologValue.tmpVariables[0]));
74
75
                if (prologPlace.ToString().Length > 0)
76
                {
77
                    string place = prologPlace.ToString();
78
                    place = place.Trim();
79
                    if (place.EndsWith(","))
80
                        place = place.Substring(0, place.Length - 1);
81
82
                    assignments.Add(place);
83
                }
84
85
                aPlace = aPlace.Next;
86
                aValue = aValue.Next;
87
            }
88
89
            int pos = 0;
90
            foreach (var s in assignments)
91
            {
92
                if (pos != 0)
93
                    m_emitter.Append(",");
94
                pos++;
95
                m_emitter.Append(s);
96
            }
97
        }
98
99
        public override void visit(SeqBlock seqBlock)
100
        {
101
            int y = 0;
102
            if (seqBlock.symbols.symbolList.Count > 0)
103
            {
104
                m_emitter.Append(" [");
105
                foreach (Identifier sym in seqBlock.symbols.symbolList)
106
                {
107
                    if (y != 0)
108
                        m_emitter.AppendLine(", ");
109
                    else
110
                        y++;
111
112
                    OoaPrologType atype = createTypeVisitor();
113
                    sym.type.Accept(atype);
114
                    OoaPrologIdentifier anIdent = createIdentifierVisitor();
115
                    sym.Accept(anIdent);
116
                    m_emitter.Append(String.Format("{0}:{1}", anIdent.ToString(), atype.ToString()));
117
                }
118
                m_emitter.Append("]: ( ");
119
            }
120
121
            y = 0;
122
            foreach (Statement x in seqBlock.statements)
123
            {
124
                if (y != 0)
125
                    m_emitter.AppendLine(", ");
126
                else
127
                    y++;
128
                m_emitter.Append("(");
129
                VisitSub(x, seqBlock);
130
                m_emitter.Append(")");
131
            }
132
            if (seqBlock.symbols.symbolList.Count > 0)
133
                m_emitter.Append(")");
134
        }
135
136
137
        protected OoaPrologSymbolicStatement(
138
                OoaPrologExpression.Factory exprFactory,
139
                OoaPrologIdentifier.Factory idFactory,
140
                OoaPrologType.Factory typeFactory)
141
            : base(exprFactory, idFactory, typeFactory)
142
        {
143
        }
144
    }
145
}