Advertisement
Guest User

Untitled

a guest
May 31st, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1.  
  2. /*
  3. * generated by Xtext 2.9.1
  4. */
  5. package org.xtext.example.tim.generator
  6.  
  7. import org.eclipse.emf.ecore.resource.Resource
  8. import org.eclipse.xtext.generator.AbstractGenerator
  9. import org.eclipse.xtext.generator.IFileSystemAccess2
  10. import org.eclipse.xtext.generator.IGeneratorContext
  11. import org.xtext.example.tim.timlang.AbstractElement
  12. import org.xtext.example.tim.timlang.AssignmentExpression
  13. import org.xtext.example.tim.timlang.Conditional
  14. import org.xtext.example.tim.timlang.CreationExpression
  15. import org.xtext.example.tim.timlang.ForLoop
  16. import org.xtext.example.tim.timlang.Model
  17. import org.xtext.example.tim.timlang.PrintExpression
  18. import org.xtext.example.tim.timlang.UnaryExpression
  19. import org.xtext.example.tim.timlang.impl.AssignmentExpressionImpl
  20. import org.xtext.example.tim.timlang.impl.ConditionalImpl
  21. import org.xtext.example.tim.timlang.impl.CreationExpressionImpl
  22. import org.xtext.example.tim.timlang.impl.ForLoopImpl
  23. import org.xtext.example.tim.timlang.impl.PrintExpressionImpl
  24. import org.xtext.example.tim.timlang.impl.UnaryExpressionImpl
  25. import org.xtext.example.tim.timlang.ExprBody
  26. import org.xtext.example.tim.timlang.FactorExpr
  27.  
  28.  
  29. import org.xtext.example.tim.timlang.FunctionCall
  30. import org.xtext.example.tim.timlang.FunctionReturn
  31. import org.xtext.example.tim.timlang.impl.FunctionParameterImpl
  32. import org.xtext.example.tim.timlang.impl.ParameterLiteralImpl
  33. import org.xtext.example.tim.timlang.ParameterLiteral
  34. import org.xtext.example.tim.timlang.impl.FunctionCallLiteralImpl
  35. import org.xtext.example.tim.timlang.FunctionCallLiteral
  36. import org.xtext.example.tim.timlang.impl.FunctionCallImpl
  37. import org.xtext.example.tim.timlang.FunctionDeclaration
  38. import org.xtext.example.tim.timlang.impl.FunctionDeclarationImpl
  39. import org.xtext.example.tim.timlang.FunctionParameter
  40.  
  41. /**
  42. * Generates code from your model files on save.
  43. *
  44. * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
  45. */
  46. class TimlangGenerator extends AbstractGenerator {
  47.  
  48. override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
  49.  
  50.  
  51.  
  52.  
  53.  
  54. fsa.generateFile(resource.className+".java", toJavaCode(resource.contents.head as Model))
  55.  
  56.  
  57.  
  58. }
  59.  
  60.  
  61. def className(Resource res) {
  62. var name = res.URI.lastSegment
  63. name.substring(0, name.indexOf('.'))
  64. }
  65.  
  66.  
  67. def toJavaCode(Model m) '''
  68. public class «m.eResource.className» {
  69.  
  70.  
  71. «FOR be : m.elements»
  72. «compileFunctionDeclaration(be)»
  73. «ENDFOR»
  74.  
  75. public static void main(String[] args) {
  76. «FOR ae : m.elements»
  77. «compileAbstractElement(ae)»
  78. «ENDFOR»
  79. }
  80.  
  81. }
  82. '''
  83.  
  84. def compileFunctionDeclaration(AbstractElement ae) '''
  85. «IF ae.class == FunctionDeclarationImpl»
  86. «compileFuncDeclaration(ae as FunctionDeclaration)»
  87.  
  88.  
  89. «ENDIF»
  90.  
  91. '''
  92.  
  93. def compileAbstractElement(AbstractElement ae) '''
  94. «IF ae.class == UnaryExpressionImpl»
  95. «compileUnaryExpression(ae as UnaryExpression, true)»
  96. «ELSEIF ae.class == CreationExpressionImpl»
  97. «compileCreationExpression(ae as CreationExpression)»
  98. «ELSEIF ae.class == AssignmentExpressionImpl»
  99. «compileAssignmentExpression(ae as AssignmentExpression)»
  100. «ELSEIF ae.class == ConditionalImpl»
  101. «compileCondition(ae as Conditional)»
  102. «ELSEIF ae.class == ForLoopImpl»
  103. «compileForLoop(ae as ForLoop)»
  104. «ELSEIF ae.class == PrintExpressionImpl»
  105. «compilePrintExpression(ae as PrintExpression)»
  106. «ELSEIF ae.class == FunctionCallLiteral»
  107. «compileFunctionCall(ae as FunctionCall)»
  108. «ELSEIF ae.class == FunctionCallLiteralImpl»
  109. «compileFunctionCallLiteral(ae as FunctionCallLiteral)»
  110. «ELSEIF ae.class == FunctionParameterImpl»
  111. «compileFuncParam(ae as FunctionParameter)»
  112. «ELSEIF ae.class == FunctionCallImpl»
  113. «compileFunctionCall(ae as FunctionCall)»
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120. «ENDIF»
  121. '''
  122.  
  123.  
  124. def compileFuncDeclaration(FunctionDeclaration func)
  125. ''' public static «func.type» «func.name» («FOR arg : func.parameters SEPARATOR ','»«arg.type» «arg.name»«ENDFOR»){
  126. «FOR ae: func.statements»
  127. «compileAbstractElement(ae)»
  128. «ENDFOR»
  129. «func.returnStatement.compileReturn»
  130. }; '''
  131.  
  132.  
  133. def compileReturn(FunctionReturn returnSt)
  134. '''return «returnSt.expression.compileAbstractElement»'''
  135.  
  136.  
  137. def compileFuncParam(FunctionParameter param)
  138. '''«param.name»'''
  139.  
  140.  
  141.  
  142.  
  143. def compileFunctionCallLiteral(FunctionCallLiteral func)
  144. '''«func.value.compileFunctionCall»'''
  145.  
  146.  
  147. def compileFunctionCall(FunctionCall call)
  148. '''«call.name»(«FOR argument : call.arguments»«argument.name»«ENDFOR»);'''
  149.  
  150. def compilePrintExpression(PrintExpression pe) '''
  151. System.out.println(«IF pe.what.^var != null»«pe.what.^var.name»«ELSE»«pe.what.^val»«ENDIF»);
  152. '''
  153.  
  154. def compileAssignmentExpression(AssignmentExpression ae)
  155. '''«ae.name.name» = «compileExprBody(ae.^val)»;'''
  156.  
  157. def compileUnaryExpression(UnaryExpression ue, boolean solo) '''«IF solo && ue.^val.^var == null»//[int]++ or [int]-- needs to be assigned to variable«ELSE»«IF ue.up != null»«ue.up»«ENDIF»«IF ue.^val.^var != null»«ue.^val.^var.name»«ELSE»«ue.^val.^val»«ENDIF»«IF ue.us != null»«IF ue.^val.^val != null»«IF ue.us == "++"»+ 1 «ELSE»- 1«ENDIF»«ELSE»«ue.us» «ENDIF»«ENDIF»«IF solo»;«ENDIF»«ENDIF»'''
  158.  
  159. def compileCreationExpression(CreationExpression ce) '''«IF ce.type == 'string'»String«ELSE»«ce.type»«ENDIF» «ce.name.name»«IF ce.^val != null» = «compileExprBody(ce.^val)»«ENDIF»;'''
  160.  
  161. def compileExprBody(ExprBody eb)'''«FOR ex : eb.expr»«compileFactorExpr(ex)»«ENDFOR»'''
  162.  
  163. def compileFactorExpr(FactorExpr fe)'''«IF fe.l.class == UnaryExpressionImpl»«compileUnaryExpression(fe.l as UnaryExpression, false)»«ELSE»(«compileFactorExpr(fe.l as FactorExpr)»)«ENDIF»«FOR ar : fe.a»«IF ar.r.class == UnaryExpressionImpl»«ar.m»«compileUnaryExpression(ar.r as UnaryExpression, false)»«ELSE»«ar.m»(«compileFactorExpr(ar.r as FactorExpr)»)«ENDIF»«ENDFOR»'''
  164.  
  165. def compileForLoop(ForLoop fp) '''
  166. for(«IF fp.start != null» «compileAssignmentExpression(fp.start)»«ELSE»;«ENDIF» «compileExprBody(fp.end.left)» «fp.end.op» «compileExprBody(fp.end.right)»;«IF fp.iteration != null»«IF fp.class == AssignmentExpressionImpl»«compileAssignmentExpression(fp.iteration as AssignmentExpression)»«ELSE»«compileUnaryExpression(fp.iteration as UnaryExpression, false)»«ENDIF»«ENDIF») {
  167. «FOR ae: fp.loop»
  168. «compileAbstractElement(ae)»
  169. «ENDFOR»}
  170. '''
  171.  
  172. def compileCondition(Conditional c) '''
  173. if(«compileExprBody(c.cond.left)» «c.cond.op» «compileExprBody(c.cond.right)») {
  174. «FOR ae: c.then»
  175. «compileAbstractElement(ae)»
  176. «ENDFOR»
  177. }
  178. «IF !c.^else.isEmpty»
  179. else {
  180. «FOR ae: c.^else»
  181. «compileAbstractElement(ae)»
  182. «ENDFOR»
  183. }
  184. «ENDIF»
  185. '''
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement