generic_language_constructs.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. import abc
  2. from visitor import Visitor, Visitable
  3. class GenericConstruct(Visitable):
  4. __metaclass__ = abc.ABCMeta
  5. # base class for constructs that are a collection of other constructs
  6. class AbstractList:
  7. __metaclass__ = abc.ABCMeta
  8. @abc.abstractmethod
  9. def add(self, generic_construct):
  10. pass
  11. class BlockEntry(GenericConstruct):
  12. __metaclass__ = abc.ABCMeta
  13. @abc.abstractmethod
  14. def isEmpty(self):
  15. pass
  16. class DeclarationBase:
  17. def __init__(self, identifier, description = None):
  18. self.identifier = identifier
  19. self.description = description # string describing declared artifact
  20. def getIdentifier(self):
  21. return self.identifier
  22. def getDescription(self):
  23. return self.description
  24. class Statement(BlockEntry):
  25. pass
  26. class Package(Statement, AbstractList, DeclarationBase):
  27. def __init__(self, identifier, description = None):
  28. DeclarationBase.__init__(self, identifier, description)
  29. self.declarations = []
  30. def add(self, item):
  31. self.declarations.append(MakeDeclaration(item))
  32. def getDeclarations(self):
  33. return self.declarations
  34. def isEmpty(self):
  35. return False
  36. class FormalParameters(GenericConstruct, AbstractList):
  37. def __init__(self, parameter_list = None):
  38. if parameter_list is None: parameter_list = []
  39. self.parameter_list = parameter_list
  40. def add(self, parameter):
  41. self.parameter_list.append(parameter)
  42. def getParameterList(self):
  43. return self.parameter_list
  44. class AST(GenericConstruct, AbstractList):
  45. def __init__(self):
  46. self.entries = []
  47. def add(self, entry):
  48. self.entries.append(MakeBlockEntry(entry))
  49. def getEntries(self):
  50. return self.entries
  51. class Block(AST):
  52. def __init__(self):
  53. AST.__init__(self)
  54. def isEmpty(self):
  55. for e in self.getEntries():
  56. if not e.isEmpty():
  57. return False
  58. return True
  59. class ForLoopBody(Block):
  60. def __init__(self, for_loop):
  61. Block.__init__(self)
  62. self.for_loop = for_loop
  63. def getForLoop(self):
  64. return self.for_loop
  65. class MethodBody(Block):
  66. def __init__(self, method):
  67. Block.__init__(self)
  68. self.method = method
  69. def getMethod(self):
  70. return self.method
  71. #class ConstructorBody(MethodBody):
  72. # def __init__(self, method):
  73. # MethodBody.__init__(self, method)
  74. #class DestructorBody(MethodBody):
  75. # def __init__(self, method):
  76. # MethodBody.__init__(self, method)
  77. class ClassMember(GenericConstruct, DeclarationBase):
  78. def __init__(self, c, identifier, description = None):
  79. DeclarationBase.__init__(self, identifier, description)
  80. self.c = c # Class
  81. def getClass(self):
  82. return self.c
  83. class MethodBase(ClassMember):
  84. def __init__(self, c, identifier, description = None):
  85. ClassMember.__init__(self, c, identifier, description)
  86. self.formal_parameters = FormalParameters()
  87. self.body = MethodBody(self)
  88. def getBody(self):
  89. return self.body
  90. def getFormalParameters(self):
  91. return self.formal_parameters
  92. class Method(MethodBase):
  93. def __init__(self, c, identifier, description = None):
  94. MethodBase.__init__(self, c, identifier, description)
  95. class Constructor(MethodBase):
  96. def __init__(self, c, description = None):
  97. MethodBase.__init__(self, c, None, description)
  98. class Destructor(MethodBase):
  99. def __init__(self, c, description = None):
  100. MethodBase.__init__(self, c, None, description)
  101. class Class(GenericConstruct, AbstractList, DeclarationBase):
  102. def __init__(self, identifier, super_class_identifier_list = None, description = None):
  103. DeclarationBase.__init__(self, identifier, description)
  104. self.super_class_identifier_list = super_class_identifier_list # string
  105. self.constructor = Constructor(self)
  106. self.destructor = Destructor(self)
  107. self.members = []
  108. def getSuperClassIdentifierList(self):
  109. return self.super_class_identifier_list
  110. def getConstructor(self):
  111. return self.constructor
  112. def getDestructor(self):
  113. return self.destructor
  114. def add(self, class_member):
  115. self.members.append(class_member)
  116. def getMembers(self):
  117. return self.members
  118. class AttributeBase(ClassMember):
  119. def __init__(self, c, identifier, init_value = None):
  120. ClassMember.__init__(self, c, identifier)
  121. self.init_value = MakeExpression(init_value)
  122. def getInitValue(self):
  123. return self.init_value
  124. class Attribute(AttributeBase):
  125. def __init__(self, c, identifier, init_value = None):
  126. AttributeBase.__init__(self, c, identifier, init_value)
  127. class StaticAttribute(AttributeBase):
  128. def __init__(self, c, name, init_value = None):
  129. AttributeBase.__init__(self, c, name, init_value)
  130. class FormalParameter(GenericConstruct, DeclarationBase):
  131. def __init__(self, identifier, default_value = None, description = None):
  132. DeclarationBase.__init__(self, identifier, description)
  133. #self.identifier = identifier
  134. self.default_value = MakeExpression(default_value)
  135. def getDefaultValue(self):
  136. return self.default_value
  137. class IncludeStatement(Statement):
  138. def __init__(self, module_path, imported_symbols = None):
  139. if imported_symbols is None: imported_symbols = []
  140. self.module_path = MakeExpressionList(module_path) # list of modules
  141. self.imported_symbols = imported_symbols
  142. def getModulePath(self):
  143. return self.module_path
  144. def getImportedSymbols(self):
  145. return self.imported_symbols
  146. def isEmpty(self):
  147. return False
  148. class ReturnStatement(Statement):
  149. def __init__(self, expr):
  150. self.expr = MakeExpression(expr)
  151. def getExpression(self):
  152. return self.expr
  153. def isEmpty(self):
  154. return False
  155. class BreakStatement(Statement):
  156. def isEmpty(self):
  157. return False
  158. class ThrowExceptionStatement(Statement):
  159. def __init__(self, expr):
  160. self.expr = MakeExpression(expr)
  161. def getExpression(self):
  162. return self.expr
  163. def isEmpty(self):
  164. return False
  165. class VSpace(BlockEntry):
  166. def isEmpty(self):
  167. return True
  168. class CommentBase(BlockEntry):
  169. def __init__(self, text):
  170. self.text = text
  171. def isEmpty(self):
  172. return True
  173. def getText(self):
  174. return self.text
  175. class SingleLineComment(CommentBase):
  176. def __init__(self, text):
  177. CommentBase.__init__(self,text)
  178. class MultiLineComment(CommentBase):
  179. def __init__(self, text):
  180. CommentBase.__init__(self,text)
  181. class ConditionalStatementBase(Statement, AbstractList):
  182. def __init__(self, body = None):
  183. if body is None: body = Block()
  184. self.body = body
  185. def add(self, stmt):
  186. self.body.add(stmt)
  187. def getBody(self):
  188. return self.body
  189. def isEmpty(self):
  190. return False
  191. class IfStatement(ConditionalStatementBase):
  192. def __init__(self, condition):
  193. ConditionalStatementBase.__init__(self)
  194. self.condition = MakeExpression(condition)
  195. def getCondition(self):
  196. return self.condition
  197. class ElseStatement(ConditionalStatementBase):
  198. def __init__(self):
  199. ConditionalStatementBase.__init__(self)
  200. class ElseIfStatement(IfStatement):
  201. def __init__(self, condition, is_first = False):
  202. IfStatement.__init__(self, condition)
  203. self.is_first = is_first
  204. # in a series of ElseIfStatements, the first ElseIfStatement will be a normal if statement
  205. def isFirst(self):
  206. return self.is_first
  207. class ForLoopIterateBase(ConditionalStatementBase):
  208. def __init__(self, collection_expr, iterator_identifier):
  209. ConditionalStatementBase.__init__(self, ForLoopBody(self))
  210. self.collection_expr = MakeExpression(collection_expr)
  211. self.iterator_identifier = iterator_identifier
  212. def getCollectionExpression(self):
  213. return self.collection_expr
  214. def getIteratorIdentifier(self):
  215. return self.iterator_identifier
  216. class ForLoopIterateArray(ForLoopIterateBase):
  217. def __init__(self, array_expr, iterator_identifier):
  218. ForLoopIterateBase.__init__(self, array_expr, iterator_identifier)
  219. class ForLoopIterateMapValues(ForLoopIterateBase):
  220. def __init__(self, map_expr, iterator_identifier):
  221. ForLoopIterateBase.__init__(self, map_expr, iterator_identifier)
  222. class ExpressionStatement(Statement):
  223. def __init__(self, expression):
  224. self.expression = expression
  225. def getExpression(self):
  226. return self.expression
  227. def isEmpty(self):
  228. return False
  229. # block of raw code
  230. class RawCode(BlockEntry):
  231. def __init__(self, text):
  232. self.text = text
  233. def getText(self):
  234. return self.text
  235. def isEmpty(self):
  236. return (len(self.text.strip()) == 0)
  237. # log message to console
  238. class LogStatement(Statement):
  239. def __init__(self, msg):
  240. self.msg = msg
  241. def getMessage(self):
  242. return self.msg
  243. def isEmpty(self):
  244. return False
  245. class Expression(GenericConstruct):
  246. __metaclass__ = abc.ABCMeta
  247. @abc.abstractmethod
  248. def isCompound(self):
  249. pass
  250. class SimpleExpression(Expression):
  251. def isCompound(self):
  252. return False
  253. class CompoundExpression(Expression):
  254. def isCompound(self):
  255. return True
  256. class RuntimeModuleIdentifier(SimpleExpression):
  257. pass
  258. # Not a real language construct, simply 'glues' expressions together.
  259. class Glue(SimpleExpression, AbstractList):
  260. def __init__(self):
  261. self.expression_list = []
  262. def add(self, expr):
  263. self.expression_list.append(MakeExpression(expr))
  264. def getExpressionList(self):
  265. return self.expression_list
  266. class ForLoopCurrentElement(SimpleExpression):
  267. def __init__(self, collection_expr, iterator_identifier):
  268. self.collection_expr = MakeExpression(collection_expr)
  269. self.iterator_identifier = iterator_identifier
  270. def getCollectionExpression(self):
  271. return self.collection_expr
  272. def getIteratorIdentifier(self):
  273. return self.iterator_identifier
  274. class Literal(SimpleExpression):
  275. def __init__(self, text):
  276. self.text = text
  277. def getText(self):
  278. return self.text
  279. class String(Literal):
  280. def __init__(self, text):
  281. Literal.__init__(self, text)
  282. class Property(SimpleExpression):
  283. def __init__(self, owner, prop):
  284. self.owner = MakeExpression(owner)
  285. self.prop = prop
  286. def getOwnerExpression(self):
  287. return self.owner
  288. def getProperty(self):
  289. return self.prop
  290. class MapIndexedExpression(SimpleExpression):
  291. def __init__(self, map_expr, key_expr):
  292. self.map_expr = MakeExpression(map_expr)
  293. self.key_expr = MakeExpression(key_expr)
  294. def getMapExpression(self):
  295. return self.map_expr
  296. def getKeyExpression(self):
  297. return self.key_expr
  298. class ArrayIndexedExpression(SimpleExpression):
  299. def __init__(self, array_expr, index_expr):
  300. self.array_expr = MakeExpression(array_expr)
  301. self.index_expr = MakeExpression(index_expr)
  302. def getArrayExpression(self):
  303. return self.array_expr
  304. def getIndexExpression(self):
  305. return self.index_expr
  306. class ActualParameters(GenericConstruct, AbstractList):
  307. def __init__(self, parameter_list = None):
  308. if parameter_list is None: parameter_list = []
  309. self.parameter_list = MakeExpressionList(parameter_list)
  310. def add(self, p):
  311. self.parameter_list.append(MakeExpression(p))
  312. pass
  313. def getParameterList(self):
  314. return self.parameter_list
  315. class FunctionCallBase(SimpleExpression):
  316. def __init__(self, actual_parameters = None):
  317. if actual_parameters is None: actual_parameters = ActualParameters()
  318. self.actual_parameters = MakeActualParameters(actual_parameters)
  319. def getActualParameters(self):
  320. return self.actual_parameters
  321. class FunctionCall(FunctionCallBase):
  322. def __init__(self, function_expr, actual_parameters = None):
  323. FunctionCallBase.__init__(self, actual_parameters)
  324. self.function_expr = MakeExpression(function_expr)
  325. def getFunctionExpression(self):
  326. return self.function_expr
  327. class SuperClassCallBase(FunctionCallBase):
  328. def __init__(self, super_class_identifier, actual_parameters = None):
  329. FunctionCallBase.__init__(self, actual_parameters)
  330. self.super_class_identifier = super_class_identifier
  331. def getSuperClassIdentifier(self):
  332. return self.super_class_identifier
  333. class SuperClassConstructorCall(SuperClassCallBase):
  334. def __init__(self, super_class_identifier, actual_parameters = None):
  335. SuperClassCallBase.__init__(self, super_class_identifier, actual_parameters)
  336. class SuperClassDestructorCall(SuperClassCallBase):
  337. def __init__(self, super_class_identifier):
  338. SuperClassCallBase.__init__(self, super_class_identifier)
  339. class SuperClassMethodCall(SuperClassCallBase):
  340. def __init__(self, super_class_identifier, method_identifier, actual_parameters = None):
  341. SuperClassCallBase.__init__(self, super_class_identifier, actual_parameters)
  342. self.method_identifier = method_identifier
  343. def getMethodIdentifier(self):
  344. return self.method_identifier
  345. class NewExpression(FunctionCallBase):
  346. def __init__(self, type_expr, actual_parameters = None):
  347. FunctionCallBase.__init__(self, actual_parameters)
  348. self.type_expr = MakeExpression(type_expr)
  349. def getTypeExpression(self):
  350. return self.type_expr
  351. class SelfExpression(SimpleExpression):
  352. pass
  353. class SelfProperty(Property):
  354. def __init__(self, prop):
  355. Property.__init__(self, SelfExpression(), prop)
  356. class Operator(GenericConstruct):
  357. pass
  358. class AndOperator(Operator):
  359. pass
  360. class OrOperator(Operator):
  361. pass
  362. class LessThanOperator(Operator):
  363. pass
  364. class GreaterThanOperator(Operator):
  365. pass
  366. class NotOperator(Operator):
  367. pass
  368. class EqualsOperator(Operator):
  369. pass
  370. class AssignmentOperator(Operator):
  371. pass
  372. class ProductOperator(Operator):
  373. pass
  374. class UnaryExpression(CompoundExpression):
  375. def __init__(self, operator, expr):
  376. self.operator = operator
  377. self.expr = MakeExpression(expr)
  378. def getExpression(self):
  379. return self.expr
  380. def getOperator(self):
  381. return self.operator
  382. class BinaryExpression(CompoundExpression):
  383. def __init__(self, lhs_expr, operator, rhs_expr):
  384. self.lhs_expr = MakeExpression(lhs_expr)
  385. self.operator = operator
  386. self.rhs_expr = MakeExpression(rhs_expr)
  387. def getLhsExpression(self):
  388. return self.lhs_expr
  389. def getRhsExpression(self):
  390. return self.rhs_expr
  391. def getOperator(self):
  392. return self.operator
  393. class NotExpression(UnaryExpression):
  394. def __init__(self, expr):
  395. UnaryExpression.__init__(self, NotOperator(), expr)
  396. class AndExpression(BinaryExpression):
  397. def __init__(self, lexpr = None, rexpr = None):
  398. BinaryExpression.__init__(self, lexpr, AndOperator(), rexpr)
  399. class OrExpression(BinaryExpression):
  400. def __init__(self, lexpr = None, rexpr = None):
  401. BinaryExpression.__init__(self, lexpr, OrOperator(), rexpr)
  402. class LessThanExpression(BinaryExpression):
  403. def __init__(self, lexpr = None, rexpr = None):
  404. BinaryExpression.__init__(self, lexpr, LessThanOperator(), rexpr)
  405. class GreaterThanExpression(BinaryExpression):
  406. def __init__(self, lexpr = None, rexpr = None):
  407. BinaryExpression.__init__(self, lexpr, GreaterThanOperator(), rexpr)
  408. class EqualsExpression(BinaryExpression):
  409. def __init__(self, lexpr = None, rexpr = None):
  410. BinaryExpression.__init__(self, lexpr, EqualsOperator(), rexpr)
  411. class AssignmentExpression(BinaryExpression):
  412. def __init__(self, lexpr = None, rexpr = None):
  413. BinaryExpression.__init__(self, lexpr, AssignmentOperator(), rexpr)
  414. class ProductExpression(BinaryExpression):
  415. def __init__(self, lexpr = None, rexpr = None):
  416. BinaryExpression.__init__(self, lexpr, ProductOperator(), rexpr)
  417. class FalseExpression(SimpleExpression):
  418. pass
  419. class TrueExpression(SimpleExpression):
  420. pass
  421. class LocalVariableDeclaration(Expression, DeclarationBase):
  422. def __init__(self, identifier, init_value = None, description = None):
  423. DeclarationBase.__init__(self, identifier, description)
  424. self.init_value = MakeExpression(init_value)
  425. def getInitValue(self):
  426. self.init_value
  427. def isCompound(self):
  428. return (self.init_value != None)
  429. class MapExpression(SimpleExpression):
  430. def __init__(self, elements = None):
  431. if elements is None: elements = {}
  432. self.elements = MakeExpressionMap(elements)
  433. def getElements(self):
  434. return self.elements
  435. class MapRemoveElement(Statement):
  436. def __init__(self, map_expr, key_expr):
  437. self.map_expr = MakeExpression(map_expr)
  438. self.key_expr = MakeExpression(key_expr)
  439. def getMapExpression(self):
  440. return self.map_expr
  441. def getKeyExpression(self):
  442. return self.key_expr
  443. def isEmpty(self):
  444. return False
  445. class ArrayExpression(SimpleExpression, AbstractList):
  446. def __init__(self, elements = None):
  447. if elements is None: elements = []
  448. self.elements = MakeExpressionList(elements)
  449. def add(self, element):
  450. self.elements.append(MakeExpression(element))
  451. def getElements(self):
  452. return self.elements
  453. class ArrayLength(SimpleExpression):
  454. def __init__(self, array_expr):
  455. self.array_expr = MakeExpression(array_expr)
  456. def getArrayExpression(self):
  457. return self.array_expr
  458. class ArrayElementOperation(Expression):
  459. def __init__(self, array_expr, elem_expr):
  460. self.array_expr = MakeExpression(array_expr)
  461. self.elem_expr = MakeExpression(elem_expr)
  462. def getArrayExpression(self):
  463. return self.array_expr
  464. def getElementExpression(self):
  465. return self.elem_expr
  466. class ArrayIndexOf(ArrayElementOperation, SimpleExpression):
  467. def __init__(self, array_expr, elem_expr):
  468. ArrayElementOperation.__init__(self, array_expr, elem_expr)
  469. class ArrayContains(ArrayElementOperation, CompoundExpression):
  470. def __init__(self, array_expr, elem_expr):
  471. ArrayElementOperation.__init__(self, array_expr, elem_expr)
  472. class ArrayPushBack(ArrayElementOperation, SimpleExpression):
  473. def __init__(self, array_expr, elem_expr):
  474. ArrayElementOperation.__init__(self, array_expr, elem_expr)
  475. class NoneExpression(SimpleExpression):
  476. pass
  477. # helpers
  478. def MakeExpression(expr):
  479. if isinstance(expr, Expression):
  480. return expr
  481. elif isinstance(expr, basestring):
  482. return Literal(expr)
  483. elif expr is None:
  484. return None
  485. else:
  486. raise Exception("Can't turn argument of type '" + str(type(expr)) + "' into Expression.")
  487. def MakeExpressionList(l):
  488. if not isinstance(l, list):
  489. raise Exception("Expected argument of type 'list'.")
  490. for i in range(len(l)):
  491. l[i] = MakeExpression(l[i])
  492. return l
  493. def MakeExpressionMap(m):
  494. if not isinstance(m, dict):
  495. raise Exception("Expected argument of type 'dict'.")
  496. for key in m.keys():
  497. m[key] = MakeExpression(m[key])
  498. return m
  499. def MakeBlockEntry(stmt):
  500. if isinstance(stmt, BlockEntry):
  501. return stmt
  502. elif isinstance(stmt, Expression):
  503. return ExpressionStatement(stmt)
  504. elif stmt is None:
  505. return None
  506. else:
  507. raise Exception("Can't turn argument of type '" + str(type(stmt)) + "' into BlockEntry.")
  508. def MakeDeclaration(obj):
  509. if isinstance(obj, DeclarationBase):
  510. return obj
  511. else:
  512. raise Exception("Can't turn argument of type '" + str(type(stmt)) + "' into DeclarationBase.")
  513. def MakeActualParameters(obj):
  514. if isinstance(obj, ActualParameters):
  515. return obj
  516. elif isinstance (obj, list):
  517. return ActualParameters(obj)
  518. else:
  519. raise Exception("Can't turn argument of type '" + str(type(obj)) + "' into ActualParameters.")
  520. """def MakeFormalParameter(parameter, default_value):
  521. if isinstance(parameter, FormalParameter):
  522. return parameter
  523. elif default_value:
  524. return FormalParameter(parameter, default_value)
  525. else:
  526. return FormalParameter(parameter)"""
  527. class GenericWriterBase(Visitor):
  528. __metaclass__ = abc.ABCMeta
  529. # overrides Visitor.generic_visit
  530. def generic_visit(self, node):
  531. raise Exception("Writer has no visit method for node of type '" + str(type(node)) + "'.")
  532. #### HELPERS ####
  533. def writeAll(self, l):
  534. for item in l:
  535. item.accept(self)
  536. def writeTuple(self, obj):
  537. self.out.extendWrite("(")
  538. self.writeCommaSeparated(obj)
  539. self.out.extendWrite(")")
  540. @abc.abstractmethod
  541. def writeComment(self, text):
  542. pass
  543. @abc.abstractmethod
  544. def writeMultiLineComment(self, text):
  545. pass
  546. def writeCommaSeparated(self, l):
  547. for i in range(len(l)):
  548. if i != 0:
  549. self.out.extendWrite(", ")
  550. l[i].accept(self)
  551. def writeDescription(self, decl):
  552. description = decl.getDescription()
  553. if description:
  554. self.writeComment(description)
  555. def writeCompoundExpr(self, expr):
  556. if expr.isCompound():
  557. self.out.extendWrite("(")
  558. expr.accept(self)
  559. if expr.isCompound():
  560. self.out.extendWrite(")")
  561. #### VISIT METHODS BASE IMPLEMENTATIONS ####
  562. def visit_ArrayIndexedExpression(self, i):
  563. a = i.getArrayExpression()
  564. index = i.getIndexExpression()
  565. a.accept(self)
  566. self.out.extendWrite("[")
  567. index.accept(self)
  568. self.out.extendWrite("]")
  569. def visit_ActualParameters(self, p):
  570. self.writeTuple(p.getParameterList())
  571. def visit_AssignmentOperator(self, assign):
  572. self.out.extendWrite(" = ")
  573. def visit_BinaryExpression(self, b):
  574. lhs = b.getLhsExpression()
  575. rhs = b.getRhsExpression()
  576. op = b.getOperator()
  577. self.writeCompoundExpr(lhs)
  578. op.accept(self)
  579. self.writeCompoundExpr(rhs)
  580. def visit_FormalParameters(self, p):
  581. self.writeTuple(p.getParameterList())
  582. def visit_FunctionCall(self, f):
  583. func = f.getFunctionExpression()
  584. params = f.getActualParameters()
  585. func.accept(self)
  586. params.accept(self)
  587. def visit_Glue(self, g):
  588. self.writeAll(g.getExpressionList())
  589. def visit_GreaterThanOperator(self, g):
  590. self.out.extendWrite(" > ")
  591. def visit_LessThanOperator(self, l):
  592. self.out.extendWrite(" < ")
  593. def visit_Literal(self, l):
  594. self.out.extendWrite(l.getText())
  595. def visit_MultiLineComment(self, c):
  596. self.writeMultiLineComment(c.getText())
  597. def visit_ProductOperator(self, p):
  598. self.out.extendWrite(" * ")
  599. def visit_Property(self, p):
  600. owner = p.getOwnerExpression()
  601. prop = p.getProperty()
  602. owner.accept(self)
  603. self.out.extendWrite("." + prop)
  604. def visit_RawCode(self, c):
  605. self.out.writeCodeCorrectIndent(c.getText())
  606. def visit_SingleLineComment(self, comment):
  607. self.writeComment(comment.getText())
  608. def visit_String(self, string):
  609. self.out.extendWrite("\"" + string.getText().replace("\"", "\\\"") + "\"")
  610. def visit_UnaryExpression(self, u):
  611. expr = u.getExpression()
  612. op = u.getOperator()
  613. op.accept(self)
  614. self.writeCompoundExpr(expr)
  615. def visit_VSpace(self, v):
  616. self.out.write()
  617. class CLikeWriterBase(GenericWriterBase):
  618. ### HELPERS ###
  619. def writeComment(self, text):
  620. self.out.write("// " + text)
  621. def writeMultiLineComment(self, text):
  622. self.out.write("/* " + text + "*/")
  623. ### VISIT METHODS ###
  624. def visit_AndOperator(self, a):
  625. self.out.extendWrite(" && ")
  626. def visit_Block(self, b):
  627. self.out.extendWrite(" {")
  628. self.out.indent()
  629. self.writeAll(b.getEntries())
  630. self.out.dedent()
  631. self.out.write("}")
  632. def visit_BreakStatement(self, b):
  633. self.out.write("break;")
  634. def visit_ElseStatement(self, else_stmt):
  635. self.out.extendWrite(" else ")
  636. else_stmt.getBody().accept(self)
  637. def visit_ElseIfStatement(self, else_if):
  638. condition = else_if.getCondition()
  639. body = else_if.getBody()
  640. if else_if.isFirst():
  641. self.out.write("if (")
  642. else:
  643. self.out.extendWrite(" else if (")
  644. condition.accept(self)
  645. self.out.extendWrite(")")
  646. body.accept(self)
  647. def visit_EqualsOperator(self, e):
  648. self.out.extendWrite(" == ")
  649. def visit_ExpressionStatement(self, stmt):
  650. self.out.write() # expressions never begin with a newline
  651. stmt.getExpression().accept(self)
  652. self.out.extendWrite(";")
  653. def visit_FalseExpression(self, f):
  654. self.out.extendWrite("false")
  655. def visit_IfStatement(self, if_stmt):
  656. condition = if_stmt.getCondition()
  657. body = if_stmt.getBody()
  658. self.out.write("if (")
  659. condition.accept(self)
  660. self.out.extendWrite(")")
  661. body.accept(self)
  662. def visit_NewExpression(self, new):
  663. type_expr = new.getTypeExpression()
  664. params = new.getActualParameters()
  665. self.out.extendWrite("new ")
  666. type_expr.accept(self)
  667. params.accept(self)
  668. def visit_NotOperator(self, n):
  669. self.out.extendWrite("!")
  670. def visit_OrOperator(self, o):
  671. self.out.extendWrite(" || ")
  672. def visit_ReturnStatement(self, r):
  673. self.out.write("return ")
  674. r.getExpression().accept(self)
  675. self.out.extendWrite(";")
  676. def visit_SelfExpression(self, s):
  677. self.out.extendWrite("this")
  678. def visit_TrueExpression(self, t):
  679. self.out.extendWrite("true")