Project

General

Profile

root / trunk / compiler / ooasCompiler / src / org / momut / ooas / codegen / prolog / OoaPrologIdentifier.java @ 9

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.prolog;
29

    
30
import org.momut.ooas.ast.identifiers.AttributeIdentifier;
31
import org.momut.ooas.ast.identifiers.EnumIdentifier;
32
import org.momut.ooas.ast.identifiers.ExpressionVariableIdentifier;
33
import org.momut.ooas.ast.identifiers.Identifier;
34
import org.momut.ooas.ast.identifiers.LocalVariableIdentifier;
35
import org.momut.ooas.ast.identifiers.MethodIdentifier;
36
import org.momut.ooas.ast.identifiers.NamedActionIdentifier;
37
import org.momut.ooas.ast.identifiers.ParameterIdentifier;
38
import org.momut.ooas.ast.identifiers.TypeIdentifier;
39
import org.momut.ooas.ast.types.FloatType;
40
import org.momut.ooas.ast.types.IntType;
41
import org.momut.ooas.codegen.OoasCodeEmitter;
42
import org.momut.ooas.utils.exceptions.NotImplementedException;
43
import org.momut.ooas.visitors.OoaIdentifierVisitor;
44

    
45
public final class OoaPrologIdentifier extends OoaIdentifierVisitor
46
{
47
        public static class Factory
48
        {
49
                public OoaPrologIdentifier create() { return new OoaPrologIdentifier(); }
50
        }
51

    
52

    
53
        OoasCodeEmitter m_emitter;
54

    
55
        private String getUniquePostFix(Identifier id) {
56
                return (id.line() > 0) && (id.column()>0)
57
                                ? String.format("%s_%s", id.line(), id.column())
58
                                : Integer.toString(id.hashCode()).replace("-", "m");
59
        }
60

    
61
        @Override
62
        public void visit(EnumIdentifier enumIdentifier)
63
        {
64
                assert(false); // handled in visit(IdentifierExpression identifierExpression)
65
                //AddTokenText(enumIdentifier);
66
        }
67

    
68
        @Override
69
        public void visit(AttributeIdentifier attributeIdentifier)
70
        {
71
                m_emitter.Append(String.format("attr_%s_%s", attributeIdentifier.tokenText(), getUniquePostFix(attributeIdentifier)));
72
        }
73

    
74
        @Override
75
        public void visit(ExpressionVariableIdentifier expressionVariableIdentifier)
76
        {
77
                m_emitter.Append(String.format("ExprVar_%s_%s", expressionVariableIdentifier, getUniquePostFix(expressionVariableIdentifier)));
78
        }
79

    
80
        @Override
81
        public void visit(ParameterIdentifier parameterIdentifier)
82
        {
83
                if (parameterIdentifier.tokenText().equals("result"))
84
                        m_emitter.Append("RESULT"); // methods are transformed into their own predicates, so they have their own scope..
85
                else
86
                        m_emitter.Append(String.format("_%s_%s", parameterIdentifier.tokenText(), getUniquePostFix(parameterIdentifier)));
87
        }
88

    
89
        @Override
90
        public void visit(LocalVariableIdentifier localVariableIdentifier)
91
        {
92
                m_emitter.Append(String.format("_%s_%s",
93
                                localVariableIdentifier.tokenText(), getUniquePostFix(localVariableIdentifier)));
94
        }
95

    
96
        @Override
97
        public void visit(TypeIdentifier typeIdentifier)
98
        {
99
                switch (typeIdentifier.type().kind())
100
                {
101
                case Any:          // not supported
102
                case Null:         // must have some type
103
                        throw new NotImplementedException();
104

    
105
                case OpaqueType:   // must be resolved at this point in time
106
                case FunctionType: // we do not support declaration of function types
107
                        assert(false);
108
                        throw new NotImplementedException();
109

    
110
                case FloatType:
111
                        final FloatType afloat = (FloatType)typeIdentifier.type();
112
                        m_emitter.Append(String.format("float_%s_%s_%s", afloat.low(), afloat.high(), afloat.precision()));
113
                        break;
114
                case IntType:
115
                        final IntType intType = (IntType)typeIdentifier.type();
116
                        m_emitter.Append(String.format("%s", intType.identifier().tokenText().toLowerCase().replace("-", "")));
117
                        break;
118

    
119
                case BoolType:
120
                        m_emitter.Append("bool");
121
                        break;
122

    
123
                case EnumeratedType:
124
                        m_emitter.Append(String.format("enum_%s%s", typeIdentifier.prefix, typeIdentifier.tokenText()));
125
                        break;
126

    
127
                case TupleType:
128
                        m_emitter.Append(String.format("tuple_%s%s", typeIdentifier.prefix, typeIdentifier.tokenText()));
129
                        break;
130

    
131
                case ListType:
132
                        m_emitter.Append(String.format("list_%s%s", typeIdentifier.prefix, typeIdentifier.tokenText()));
133
                        break;
134

    
135
                case QrType:
136
                case MapType:
137
                        throw new NotImplementedException();
138

    
139
                case OoActionSystemType:
140
                        m_emitter.Append(String.format("'%s'", typeIdentifier.tokenText()));
141
                        //AddTokenText(typeIdentifier);
142
                        break;
143

    
144
                default:
145
                        throw new NotImplementedException();
146
                }
147

    
148
        }
149

    
150
        @Override
151
        public void visit(MethodIdentifier methodIdentifier)
152
        {
153
                m_emitter.Append(String.format("'%s'", methodIdentifier.tokenText()));
154
        }
155

    
156
        @Override
157
        public void visit(NamedActionIdentifier namedActionIdentifier)
158
        {
159
                m_emitter.Append(String.format("'%s'", namedActionIdentifier.tokenText()));
160
        }
161

    
162

    
163
        @Override
164
        public String toString()
165
        {
166
                return m_emitter.toString();
167
        }
168

    
169
        private OoaPrologIdentifier()
170
        {
171
                m_emitter = new OoasCodeEmitter();
172
        }
173
}