summaryrefslogtreecommitdiffstats
path: root/awlsim/fupcompiler/fupcompiler_interf.py
blob: 7bef5a242cba983d84b867d6b3f15b38216c0cfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
#
# AWL simulator - FUP compiler - Interface
#
# Copyright 2016 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from __future__ import division, absolute_import, print_function, unicode_literals
from awlsim.common.compat import *

from awlsim.common.namevalidation import *
from awlsim.common.xmlfactory import *

from awlsim.fupcompiler.fupcompiler_base import *


class FupCompiler_InterfFactory(XmlFactory):
	def parser_open(self, tag=None):
		self.inSection = "interface"
		self.interf.reset()
		XmlFactory.parser_open(self, tag)

	def parser_beginTag(self, tag):
		interf = self.interf

		def mkField(tag):
			return FupCompiler_InterfField(
				name=tag.getAttr("name", ""),
				typeStr=tag.getAttr("type", ""),
				initValueStr=tag.getAttr("init", ""),
				comment=tag.getAttr("comment", ""))

		if self.inSection == "interface":
			if tag.name in {"inputs", "outputs", "inouts",
					"stats", "temps", "retval"}:
				self.inSection = tag.name
				return
		elif self.inSection == "inputs":
			if tag.name == "field":
				interf.inFields.append(mkField(tag))
				return
		elif self.inSection == "outputs":
			if tag.name == "field":
				interf.outFields.append(mkField(tag))
				return
		elif self.inSection == "inouts":
			if tag.name == "field":
				interf.inOutFields.append(mkField(tag))
				return
		elif self.inSection == "stats":
			if tag.name == "field":
				interf.statFields.append(mkField(tag))
				return
		elif self.inSection == "temps":
			if tag.name == "field":
				interf.tempFields.append(mkField(tag))
				return
		elif self.inSection == "retval":
			if tag.name == "field":
				interf.retValField = mkField(tag)
				return
		XmlFactory.parser_beginTag(self, tag)

	def parser_endTag(self, tag):
		if self.inSection == "interface":
			if tag.name == self.inSection:
				self.parser_finish()
				return
		else:
			if tag.name == self.inSection:
				self.inSection = "interface"
				return
			if tag.name == "field":
				return
		XmlFactory.parser_endTag(self, tag)

class FupCompiler_InterfField(object):
	def __init__(self, name="", typeStr="", initValueStr="", comment="", enableNameCheck=True):
		self.name = name
		self.typeStr = typeStr
		self.initValueStr = initValueStr
		self.comment = comment
		self.enableNameCheck = enableNameCheck

class FupCompiler_Interf(FupCompiler_BaseObj):
	factory = FupCompiler_InterfFactory

	def __init__(self, compiler):
		FupCompiler_BaseObj.__init__(self)
		self.compiler = compiler	# FupCompiler
		self.reset()

	def reset(self):
		self.inFields = []
		self.outFields = []
		self.inOutFields = []
		self.statFields = []
		self.tempFields = []
		self.retValField = None

	@property
	def allFields(self):
		for field in itertools.chain(self.inFields,
					     self.outFields,
					     self.inOutFields,
					     self.statFields,
					     self.tempFields,
					     [ self.retValField ]):
			if field:
				yield field

	def allocTEMP(self, dataTypeName="BOOL", name=None):
		"""Allocate an additional TEMP field.
		'dataTypeName' is the data type to create.
		'name' is the optional name of the new field.
		Returns the name string of the allocated field.
		"""
		field = FupCompiler_InterfField(
			name=name or ("_FUP_COMPILER_temp_%d" % len(self.tempFields)),
			typeStr=dataTypeName,
			initValueStr="",
			comment="Allocated by FUP compiler",
			enableNameCheck=False)
		self.tempFields.append(field)
		return field.name

	def __compileFields(self, declStr, fields):
		if not fields:
			return []
		awlLines = [ "\t" + declStr, ]
		for field in fields:
			varName = field.name
			typeStr = field.typeStr
			comment = field.comment
			if not AwlName.isValidVarName(varName) and\
			   field.enableNameCheck:
				raise AwlSimError("FupCompiler_Interf: Variable name "
					"'%s' contains invalid characters." %\
					varName)
			if not AwlName.mayBeValidType(typeStr):
				raise AwlSimError("FupCompiler_Interf: Variable type "
					"'%s' contains invalid characters." %\
					typeStr)
			if not AwlName.isValidComment(comment):
				raise AwlSimError("FupCompiler_Interf: Comment "
					"'%s' contains invalid characters." %\
					comment)
			awlLines.append("\t\t%s : %s;%s" %(
				varName, typeStr,
				("  // " + comment) if comment else ""))
		awlLines.append("\tEND_VAR")
		return awlLines

	def compile(self):
		"""Compile this FUP interface declaration to AWL.
		Returns a list of AWL lines.
		"""
		self.compileState = self.COMPILE_RUNNING
		awlLines = []

		awlLines.extend(self.__compileFields("VAR_INPUT", self.inFields))
		awlLines.extend(self.__compileFields("VAR_OUTPUT", self.outFields))
		awlLines.extend(self.__compileFields("VAR_IN_OUT", self.inOutFields))
		awlLines.extend(self.__compileFields("VAR", self.statFields))
		awlLines.extend(self.__compileFields("VAR_TEMP", self.tempFields))

		self.compileState = self.COMPILE_DONE
		return awlLines
bues.ch cgit interface