Project

General

Profile

Revision 7

Added by Willibald K. over 8 years ago

changing java, cpp, hpp files to unix line endings

View differences:

CadpIdentifier.java
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
  */

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 26

  
27 27

  
28
package org.momut.ooas.codegen.cadp;

29

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

  
30 30
import org.momut.ooas.ast.AstNodeTypeEnum;
31 31
import org.momut.ooas.ast.IAst;
32 32
import org.momut.ooas.ast.identifiers.AttributeIdentifier;
......
53 53
import org.momut.ooas.ast.types.UlyssesType;
54 54
import org.momut.ooas.codegen.OoasCodeEmitter;
55 55
import org.momut.ooas.visitors.OoaCompleteAstTraversalVisitor;
56

  
57
/// <summary>
58
/// Knows how to construct a C-CADP identifier from an AST-Identifier
59
/// (enums, attributes, variables, parameters supported)
60
/// </summary>
61
public final class CadpIdentifier extends OoaCompleteAstTraversalVisitor
62
{
63
	private final OoasCodeEmitter m_emitter = new OoasCodeEmitter();
64
	private final String m_stateVariablePrefix;
65

  
66
	/// <summary>
67
	/// enums are mapped to ints, so an enum identifier simply gets an integer.
68
	/// </summary>
69
	@Override
70
	public  void visit(EnumIdentifier enumIdentifier)
71
	{
72
		if (enumIdentifier.HaveValue())
73
		{
74
			m_emitter.Append(enumIdentifier.Value());
75
			m_emitter.Append(" /* ");
76
			m_emitter.Append(enumIdentifier.tokenText());
77
			m_emitter.Append(" */ ");
78
		}
79
		else
80
		{
81
			final EnumType anEnum = (EnumType)enumIdentifier.type();
82
			m_emitter.Append(anEnum.listOfEnumSymbols().indexOf(enumIdentifier));
83
			m_emitter.Append(" /* ");
84
			m_emitter.Append(enumIdentifier.tokenText());
85
			m_emitter.Append(" */ ");
86
		}
87
	}
88
	@Override
89
	public  void visit(LandmarkIdentifier landmarkIdentifier)
90
	{
91
		throw new UnsupportedOperationException();
92
	}
93
	@Override
94
	public  void visit(AttributeIdentifier attributeIdentifier)
95
	{
96
		if (m_stateVariablePrefix != null && !m_stateVariablePrefix.equals(""))
97
		{
98
			m_emitter.Append(m_stateVariablePrefix);
99
			m_emitter.Append("->");
100
		}
101
		m_emitter.Append(attributeIdentifier.tokenText());
102
	}
103
	@Override
104
	public  void visit(ExpressionVariableIdentifier expressionVariableIdentifier)
105
	{
106
		// expression vars are local..
107
		m_emitter.Append(expressionVariableIdentifier.tokenText());
108
	}
109
	@Override
110
	public  void visit(ParameterIdentifier parameterIdentifier)
111
	{
112
		/* handed in as parameter to named action, hence no state ref. necessary. */
113
		if (parameterIdentifier.tokenText().equals("result"))
114
			m_emitter.Append(String.format("*param_%1$s", parameterIdentifier.tokenText()));
115
		else
116
			m_emitter.Append(String.format("param_%1$s", parameterIdentifier.tokenText()));
117
	}
118
	@Override
119
	public  void visit(LocalVariableIdentifier localVariableIdentifier)
120
	{
121
		m_emitter.Append(localVariableIdentifier.tokenText());
122
	}
123
	@Override
124
	public  void visit(TypeIdentifier typeIdentifier)
125
	{
126
		m_emitter.Append("aType_");
127
		if (typeIdentifier.type().kind() == TypeKind.ListType)
128
		{
129
			m_emitter.Append("list_");
130
			final ListType alist = (ListType)typeIdentifier.type();
131
			if (alist.maxNumberOfElements() >= 0)
132
				m_emitter.Append(String.format("%1$s_", alist.maxNumberOfElements()));
133
			else
134
				m_emitter.Append(String.format("dyn_", alist.maxNumberOfElements()));
135

  
136
			visit(alist.innerType().identifier());
137
		}
138
		else if (typeIdentifier.type().kind() == TypeKind.FunctionType)
139
		{
140
			final FunctionType funType = (FunctionType) typeIdentifier.type();
141
			m_emitter.Append(typeIdentifier.tokenText());
142
			for (final UlyssesType x : funType.parameter())
143
			{
144
				m_emitter.Append("_");
145
				m_emitter.Append(x.toString());
146
			}
147
			if (funType.returnType() != null)
148
			{
149
				m_emitter.Append("__");
150
				m_emitter.Append(funType.returnType().toString());
151
			}
152
		}
153
		else
154
			m_emitter.Append(typeIdentifier.tokenText());
155
	}
156

  
157
	@Override
158
	public  void visit(SelfTypeIdentifier aself)
159
	{
160
		m_emitter.Append(aself.tokenText());
161
	}
162

  
163
	@Override
164
	public  void visit(MethodIdentifier methodIdentifier)
165
	{
166
		m_emitter.Append(methodIdentifier.tokenText());
167
	}
168
	@Override
169
	public  void visit(NamedActionIdentifier namedActionIdentifier)
170
	{
171
		m_emitter.Append(namedActionIdentifier.tokenText());
172
	}
173
	@Override
174
	public  void visit(MainModule mainModule)
175
	{
176
		throw new UnsupportedOperationException();
177
	}
178
	@Override
179
	public  void visit(Module module)
180
	{
181
		throw new UnsupportedOperationException();
182
	}
183
	@Override
184
	public  void visit(NondetIdentifierList nondetIdentifierList)
185
	{
186
		throw new UnsupportedOperationException();
187
	}
188
	@Override
189
	public  void visit(SeqIdentifierList seqIdentifierList)
190
	{
191
		throw new UnsupportedOperationException();
192
	}
193
	@Override
194
	public  void visit(PrioIdentifierList prioIdentifierList)
195
	{
196
		throw new UnsupportedOperationException();
197
	}
198
	@Override
199
	public  void visit(UnspecIdentifierList unspecIdentifierList)
200
	{
201
		throw new UnsupportedOperationException();
202
	}
203

  
204
	@Override
205
	protected  void VisitAstElement(IAst element, IAst parent)
206
	{
207
		if (element.nodeType() == AstNodeTypeEnum.identifier)
208
			super.VisitAstElement(element, parent);
209
	}
210

  
211
	@Override
212
	public  String toString()
213
	{
214
		return m_emitter.toString();
215
	}
216

  
217
	public CadpIdentifier(String stateVariablePrefix)
218
	{
219
		super(null);
220
		m_stateVariablePrefix = stateVariablePrefix;
221
	}
222

  
223

  
224
	public static String GetIdentifierString(TypeIdentifier typeIdentifier)
225
	{
226
		final CadpIdentifier ident = new CadpIdentifier("");
227
		ident.visit(typeIdentifier);
228
		return ident.toString();
229
	}
230
	public static String GetIdentifierString(ParameterIdentifier aParam)
231
	{
232
		final CadpIdentifier anid = new CadpIdentifier("");
233
		anid.visit(aParam);
234
		return anid.toString();
235
	}
236
	public static Object GetIdentifierString(Identifier x)
237
	{
238
		final CadpIdentifier anid = new CadpIdentifier(OoaCADPVisitor.StateVariablePrefix);
239
		x.Accept(anid);
240
		return anid.toString();
241
	}
242
}
56

  
57
/// <summary>
58
/// Knows how to construct a C-CADP identifier from an AST-Identifier
59
/// (enums, attributes, variables, parameters supported)
60
/// </summary>
61
public final class CadpIdentifier extends OoaCompleteAstTraversalVisitor
62
{
63
	private final OoasCodeEmitter m_emitter = new OoasCodeEmitter();
64
	private final String m_stateVariablePrefix;
65

  
66
	/// <summary>
67
	/// enums are mapped to ints, so an enum identifier simply gets an integer.
68
	/// </summary>
69
	@Override
70
	public  void visit(EnumIdentifier enumIdentifier)
71
	{
72
		if (enumIdentifier.HaveValue())
73
		{
74
			m_emitter.Append(enumIdentifier.Value());
75
			m_emitter.Append(" /* ");
76
			m_emitter.Append(enumIdentifier.tokenText());
77
			m_emitter.Append(" */ ");
78
		}
79
		else
80
		{
81
			final EnumType anEnum = (EnumType)enumIdentifier.type();
82
			m_emitter.Append(anEnum.listOfEnumSymbols().indexOf(enumIdentifier));
83
			m_emitter.Append(" /* ");
84
			m_emitter.Append(enumIdentifier.tokenText());
85
			m_emitter.Append(" */ ");
86
		}
87
	}
88
	@Override
89
	public  void visit(LandmarkIdentifier landmarkIdentifier)
90
	{
91
		throw new UnsupportedOperationException();
92
	}
93
	@Override
94
	public  void visit(AttributeIdentifier attributeIdentifier)
95
	{
96
		if (m_stateVariablePrefix != null && !m_stateVariablePrefix.equals(""))
97
		{
98
			m_emitter.Append(m_stateVariablePrefix);
99
			m_emitter.Append("->");
100
		}
101
		m_emitter.Append(attributeIdentifier.tokenText());
102
	}
103
	@Override
104
	public  void visit(ExpressionVariableIdentifier expressionVariableIdentifier)
105
	{
106
		// expression vars are local..
107
		m_emitter.Append(expressionVariableIdentifier.tokenText());
108
	}
109
	@Override
110
	public  void visit(ParameterIdentifier parameterIdentifier)
111
	{
112
		/* handed in as parameter to named action, hence no state ref. necessary. */
113
		if (parameterIdentifier.tokenText().equals("result"))
114
			m_emitter.Append(String.format("*param_%1$s", parameterIdentifier.tokenText()));
115
		else
116
			m_emitter.Append(String.format("param_%1$s", parameterIdentifier.tokenText()));
117
	}
118
	@Override
119
	public  void visit(LocalVariableIdentifier localVariableIdentifier)
120
	{
121
		m_emitter.Append(localVariableIdentifier.tokenText());
122
	}
123
	@Override
124
	public  void visit(TypeIdentifier typeIdentifier)
125
	{
126
		m_emitter.Append("aType_");
127
		if (typeIdentifier.type().kind() == TypeKind.ListType)
128
		{
129
			m_emitter.Append("list_");
130
			final ListType alist = (ListType)typeIdentifier.type();
131
			if (alist.maxNumberOfElements() >= 0)
132
				m_emitter.Append(String.format("%1$s_", alist.maxNumberOfElements()));
133
			else
134
				m_emitter.Append(String.format("dyn_", alist.maxNumberOfElements()));
135

  
136
			visit(alist.innerType().identifier());
137
		}
138
		else if (typeIdentifier.type().kind() == TypeKind.FunctionType)
139
		{
140
			final FunctionType funType = (FunctionType) typeIdentifier.type();
141
			m_emitter.Append(typeIdentifier.tokenText());
142
			for (final UlyssesType x : funType.parameter())
143
			{
144
				m_emitter.Append("_");
145
				m_emitter.Append(x.toString());
146
			}
147
			if (funType.returnType() != null)
148
			{
149
				m_emitter.Append("__");
150
				m_emitter.Append(funType.returnType().toString());
151
			}
152
		}
153
		else
154
			m_emitter.Append(typeIdentifier.tokenText());
155
	}
156

  
157
	@Override
158
	public  void visit(SelfTypeIdentifier aself)
159
	{
160
		m_emitter.Append(aself.tokenText());
161
	}
162

  
163
	@Override
164
	public  void visit(MethodIdentifier methodIdentifier)
165
	{
166
		m_emitter.Append(methodIdentifier.tokenText());
167
	}
168
	@Override
169
	public  void visit(NamedActionIdentifier namedActionIdentifier)
170
	{
171
		m_emitter.Append(namedActionIdentifier.tokenText());
172
	}
173
	@Override
174
	public  void visit(MainModule mainModule)
175
	{
176
		throw new UnsupportedOperationException();
177
	}
178
	@Override
179
	public  void visit(Module module)
180
	{
181
		throw new UnsupportedOperationException();
182
	}
183
	@Override
184
	public  void visit(NondetIdentifierList nondetIdentifierList)
185
	{
186
		throw new UnsupportedOperationException();
187
	}
188
	@Override
189
	public  void visit(SeqIdentifierList seqIdentifierList)
190
	{
191
		throw new UnsupportedOperationException();
192
	}
193
	@Override
194
	public  void visit(PrioIdentifierList prioIdentifierList)
195
	{
196
		throw new UnsupportedOperationException();
197
	}
198
	@Override
199
	public  void visit(UnspecIdentifierList unspecIdentifierList)
200
	{
201
		throw new UnsupportedOperationException();
202
	}
203

  
204
	@Override
205
	protected  void VisitAstElement(IAst element, IAst parent)
206
	{
207
		if (element.nodeType() == AstNodeTypeEnum.identifier)
208
			super.VisitAstElement(element, parent);
209
	}
210

  
211
	@Override
212
	public  String toString()
213
	{
214
		return m_emitter.toString();
215
	}
216

  
217
	public CadpIdentifier(String stateVariablePrefix)
218
	{
219
		super(null);
220
		m_stateVariablePrefix = stateVariablePrefix;
221
	}
222

  
223

  
224
	public static String GetIdentifierString(TypeIdentifier typeIdentifier)
225
	{
226
		final CadpIdentifier ident = new CadpIdentifier("");
227
		ident.visit(typeIdentifier);
228
		return ident.toString();
229
	}
230
	public static String GetIdentifierString(ParameterIdentifier aParam)
231
	{
232
		final CadpIdentifier anid = new CadpIdentifier("");
233
		anid.visit(aParam);
234
		return anid.toString();
235
	}
236
	public static Object GetIdentifierString(Identifier x)
237
	{
238
		final CadpIdentifier anid = new CadpIdentifier(OoaCADPVisitor.StateVariablePrefix);
239
		x.Accept(anid);
240
		return anid.toString();
241
	}
242
}

Also available in: Unified diff