root / trunk / compiler / ooasCompiler / src / org / momut / ooas / codegen / prolog / OoaPrologIdentifier.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.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.LandmarkIdentifier; |
35 |
import org.momut.ooas.ast.identifiers.LocalVariableIdentifier; |
36 |
import org.momut.ooas.ast.identifiers.MethodIdentifier; |
37 |
import org.momut.ooas.ast.identifiers.NamedActionIdentifier; |
38 |
import org.momut.ooas.ast.identifiers.ParameterIdentifier; |
39 |
import org.momut.ooas.ast.identifiers.TypeIdentifier; |
40 |
import org.momut.ooas.ast.types.FloatType; |
41 |
import org.momut.ooas.ast.types.IntType; |
42 |
import org.momut.ooas.codegen.OoasCodeEmitter; |
43 |
import org.momut.ooas.utils.exceptions.NotImplementedException; |
44 |
import org.momut.ooas.visitors.OoaIdentifierVisitor; |
45 |
|
46 |
public final class OoaPrologIdentifier extends OoaIdentifierVisitor |
47 |
{ |
48 |
public static class Factory |
49 |
{ |
50 |
public OoaPrologIdentifier create() { return new OoaPrologIdentifier(); } |
51 |
} |
52 |
|
53 |
|
54 |
OoasCodeEmitter m_emitter; |
55 |
|
56 |
private void AddTokenText(Identifier anId) |
57 |
{ |
58 |
m_emitter.Append(anId.tokenText()); |
59 |
} |
60 |
|
61 |
private String getUniquePostFix(Identifier id) { |
62 |
return (id.line() > 0) && (id.column()>0) |
63 |
? String.format("%s_%s", id.line(), id.column()) |
64 |
: Integer.toString(id.hashCode()).replace("-", "m"); |
65 |
} |
66 |
|
67 |
@Override
|
68 |
public /*override*/ void visit(EnumIdentifier enumIdentifier) |
69 |
{ |
70 |
assert(false); // handled in visit(IdentifierExpression identifierExpression) |
71 |
//AddTokenText(enumIdentifier);
|
72 |
} |
73 |
|
74 |
@Override
|
75 |
public /*override*/ void visit(LandmarkIdentifier landmarkIdentifier) |
76 |
{ |
77 |
AddTokenText(landmarkIdentifier); |
78 |
} |
79 |
|
80 |
@Override
|
81 |
public /*override*/ void visit(AttributeIdentifier attributeIdentifier) |
82 |
{ |
83 |
m_emitter.Append(String.format("attr_%s_%s", attributeIdentifier.tokenText(), getUniquePostFix(attributeIdentifier))); |
84 |
} |
85 |
|
86 |
@Override
|
87 |
public /*override*/ void visit(ExpressionVariableIdentifier expressionVariableIdentifier) |
88 |
{ |
89 |
m_emitter.Append(String.format("ExprVar_%s_%s", expressionVariableIdentifier, getUniquePostFix(expressionVariableIdentifier))); |
90 |
} |
91 |
|
92 |
@Override
|
93 |
public /*override*/ void visit(ParameterIdentifier parameterIdentifier) |
94 |
{ |
95 |
//m_emitter.Append(parameterIdentifier.tokenText().ToUpper());
|
96 |
if (parameterIdentifier.tokenText().equals("result")) |
97 |
m_emitter.Append("RESULT"); // methods are transformed into their own predicates, so they have their own scope.. |
98 |
else
|
99 |
m_emitter.Append(String.format("_%s_%s", parameterIdentifier.tokenText(), getUniquePostFix(parameterIdentifier))); |
100 |
} |
101 |
|
102 |
@Override
|
103 |
public /*override*/ void visit(LocalVariableIdentifier localVariableIdentifier) |
104 |
{ |
105 |
m_emitter.Append(String.format("_%s_%s", |
106 |
localVariableIdentifier.tokenText(), getUniquePostFix(localVariableIdentifier))); |
107 |
} |
108 |
|
109 |
@Override
|
110 |
public /*override*/ void visit(TypeIdentifier typeIdentifier) |
111 |
{ |
112 |
switch (typeIdentifier.type().kind())
|
113 |
{ |
114 |
case Any: // not supported |
115 |
case Null: // must have some type |
116 |
throw new NotImplementedException(); |
117 |
|
118 |
case OpaqueType: // must be resolved at this point in time |
119 |
case FunctionType: // we do not support declaration of function types |
120 |
assert(false); |
121 |
throw new NotImplementedException(); |
122 |
|
123 |
case FloatType:
|
124 |
final FloatType afloat = (FloatType)typeIdentifier.type();
|
125 |
m_emitter.Append(String.format("float_%s_%s_%s", afloat.low(), afloat.high(), afloat.precision())); |
126 |
break;
|
127 |
case IntType:
|
128 |
final IntType intType = (IntType)typeIdentifier.type();
|
129 |
m_emitter.Append(String.format("%s", intType.identifier().tokenText().toLowerCase().replace("-", ""))); |
130 |
break;
|
131 |
|
132 |
case BoolType:
|
133 |
m_emitter.Append("bool");
|
134 |
break;
|
135 |
|
136 |
case EnumeratedType:
|
137 |
m_emitter.Append(String.format("enum_%s%s", typeIdentifier.prefix, typeIdentifier.tokenText())); |
138 |
break;
|
139 |
|
140 |
case TupleType:
|
141 |
m_emitter.Append(String.format("tuple_%s%s", typeIdentifier.prefix, typeIdentifier.tokenText())); |
142 |
break;
|
143 |
|
144 |
case ListType:
|
145 |
m_emitter.Append(String.format("list_%s%s", typeIdentifier.prefix, typeIdentifier.tokenText())); |
146 |
break;
|
147 |
|
148 |
case QrType:
|
149 |
case MapType:
|
150 |
throw new NotImplementedException(); |
151 |
|
152 |
case OoActionSystemType:
|
153 |
m_emitter.Append(String.format("'%s'", typeIdentifier.tokenText())); |
154 |
//AddTokenText(typeIdentifier);
|
155 |
break;
|
156 |
|
157 |
default:
|
158 |
throw new NotImplementedException(); |
159 |
} |
160 |
|
161 |
} |
162 |
|
163 |
@Override
|
164 |
public /*override*/ void visit(MethodIdentifier methodIdentifier) |
165 |
{ |
166 |
m_emitter.Append(String.format("'%s'", methodIdentifier.tokenText())); |
167 |
} |
168 |
|
169 |
@Override
|
170 |
public /*override*/ void visit(NamedActionIdentifier namedActionIdentifier) |
171 |
{ |
172 |
m_emitter.Append(String.format("'%s'", namedActionIdentifier.tokenText())); |
173 |
} |
174 |
|
175 |
|
176 |
@Override
|
177 |
public /*override*/ String toString() |
178 |
{ |
179 |
return m_emitter.toString();
|
180 |
} |
181 |
|
182 |
private OoaPrologIdentifier()
|
183 |
{ |
184 |
m_emitter = new OoasCodeEmitter();
|
185 |
} |
186 |
} |