Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
857 lines (796 sloc) 34.9 KB
/*
* generated by Xtext 2.12.0
*/
package com.euclideanspace.bootSyntax.generator
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.AbstractGenerator
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGeneratorContext
import com.euclideanspace.bootSyntax.editor.Model
import com.euclideanspace.bootSyntax.editor.Declaration
import com.euclideanspace.bootSyntax.editor.Package
import com.euclideanspace.bootSyntax.editor.Comment
import com.euclideanspace.bootSyntax.editor.Documentation
import com.euclideanspace.bootSyntax.editor.Defparameter
import com.euclideanspace.bootSyntax.editor.Defconstant
import com.euclideanspace.bootSyntax.editor.Defconst
import com.euclideanspace.bootSyntax.editor.Defvar
import com.euclideanspace.bootSyntax.editor.FunctionDef
import com.euclideanspace.bootSyntax.editor.GlobalVariable
//import com.euclideanspace.bootSyntax.editor.Block
//import com.euclideanspace.bootSyntax.editor.InnerBlock
import com.euclideanspace.bootSyntax.editor.Statement
//import com.euclideanspace.bootSyntax.editor.MultiAssign
//import com.euclideanspace.bootSyntax.editor.MultiDefine
//import com.euclideanspace.bootSyntax.editor.MultiExit
import com.euclideanspace.bootSyntax.editor.Loop
import com.euclideanspace.bootSyntax.editor.LoopCondition
import com.euclideanspace.bootSyntax.editor.While
import com.euclideanspace.bootSyntax.editor.Do
import com.euclideanspace.bootSyntax.editor.Until
import com.euclideanspace.bootSyntax.editor.For
import com.euclideanspace.bootSyntax.editor.Where
import com.euclideanspace.bootSyntax.editor.Expr
import com.euclideanspace.bootSyntax.editor.Expression
import com.euclideanspace.bootSyntax.editor.ExitExpression
import com.euclideanspace.bootSyntax.editor.OrExpression
import com.euclideanspace.bootSyntax.editor.AndExpression
import com.euclideanspace.bootSyntax.editor.EqualityExpression
import com.euclideanspace.bootSyntax.editor.RelationalExpression
import com.euclideanspace.bootSyntax.editor.IsExpression
import com.euclideanspace.bootSyntax.editor.InExpression
import com.euclideanspace.bootSyntax.editor.SegmentExpression
import com.euclideanspace.bootSyntax.editor.AdditiveExpression
import com.euclideanspace.bootSyntax.editor.ExquoExpression
import com.euclideanspace.bootSyntax.editor.DivisionExpression
import com.euclideanspace.bootSyntax.editor.QuoExpression
import com.euclideanspace.bootSyntax.editor.ModExpression
import com.euclideanspace.bootSyntax.editor.RemExpression
import com.euclideanspace.bootSyntax.editor.MultiplicativeExpression
import com.euclideanspace.bootSyntax.editor.ExponentExpression
import com.euclideanspace.bootSyntax.editor.MapExpression
import com.euclideanspace.bootSyntax.editor.AssignExpression
import com.euclideanspace.bootSyntax.editor.EltExpression
import com.euclideanspace.bootSyntax.editor.UnaryExpression
import com.euclideanspace.bootSyntax.editor.VarOrFunction
import com.euclideanspace.bootSyntax.editor.Tuple
import com.euclideanspace.bootSyntax.editor.Literal
import com.euclideanspace.bootSyntax.editor.LispLiteral
import com.euclideanspace.bootSyntax.editor.SubLispLiteral
import com.euclideanspace.bootSyntax.editor.AnnotatedSubLispLiteral
import com.euclideanspace.bootSyntax.editor.ListLiteral
import com.euclideanspace.bootSyntax.editor.ListElement
import com.euclideanspace.bootSyntax.editor.ListComprehension
import com.euclideanspace.bootSyntax.editor.WhereExpression
import com.euclideanspace.bootSyntax.editor.IfExpression
import com.euclideanspace.bootSyntax.editor.LambdaExpression
import com.euclideanspace.bootSyntax.editor.Block
/**
* Generates code from your model files on save.
*
* @see <a href="http://www.euclideanspace.com/prog/spad2aldor/boot/index.htm">compiling Boot code</a>
* @author Martin Baker
*/
class EditorGenerator extends AbstractGenerator {
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
fsa.generateFile(resource.className+".boot1", compile(0,0,resource.contents.head as Model))
}
def className(Resource res) {
var name = res.URI.lastSegment
return name.substring(0, name.indexOf('.'))
}
def CharSequence newline(int indent) {
var String s= System.lineSeparator()
for(var int i=0; i<indent; i++) {
s=s+" "
}
return s
}
/*
* cop (conditional open parenthesis)
* This inserts open parenthesis only if it is required by
* precidence rules.
*/
def String cop(int innerPrecidence,int outerPrecidence) {
if (innerPrecidence < outerPrecidence) return "(" else return ""
}
/*
* ccp (conditional close parenthesis)
* This inserts close parenthesis only if it is required by
* precidence rules.
*/
def String ccp(int innerPrecidence,int outerPrecidence) {
if (innerPrecidence < outerPrecidence) return ")" else return ""
}
/*
* some keywords are changed so that they are not taken as keywords
* in our language. This changes them back to what they should be.
*/
def String cleanID(String a) {
if (a.equals("if1")) return "if"
if (a.equals("and1")) return "and"
if (a.equals("or1")) return "or"
if (a.equals("not1")) return "not"
if (a.equals("is1")) return "is"
if (a.equals("package1")) return "package"
if (a.equals("local1")) return "local"
if (a.equals("until1")) return "until"
return a
}
def CharSequence compile(int indent,int precidence,Model model)
'''
«FOR x:model.declarations»«compile(indent,precidence,x)»«ENDFOR»'''
def CharSequence compile(int indent,int precidence,Declaration declaration)
'''
«newline(indent)»«
IF declaration instanceof Package»«
compile(indent,precidence,declaration as Package)»«ENDIF»«
IF declaration instanceof Comment»«
compile(indent,precidence,declaration as Comment)»«ENDIF»«
IF declaration instanceof Documentation»«
compile(indent,precidence,declaration as Documentation)»«ENDIF»«
IF declaration instanceof Defparameter»«
compile(indent,precidence,declaration as Defparameter)»«ENDIF»«
IF declaration instanceof Defconstant»«
compile(indent,precidence,declaration as Defconstant)»«ENDIF»«
IF declaration instanceof Defconst»«
compile(indent,precidence,declaration as Defconst)»«ENDIF»«
IF declaration instanceof Defvar»«
compile(indent,precidence,declaration as Defvar)»«ENDIF»«
IF declaration instanceof FunctionDef»«
compile(indent,precidence,declaration as FunctionDef)»«ENDIF»«
IF declaration instanceof Where»«
compile(indent,precidence,declaration as Where)»«ENDIF»«
IF declaration instanceof GlobalVariable»«
compile(indent,precidence,declaration as GlobalVariable)»«ENDIF»'''
def CharSequence compile(int indent,int precidence,Package package1)
'''
«IF package1.p !== null»)package «package1.p»«ENDIF»«
newline(indent)»'''
def CharSequence compile(int indent,int precidence,Comment comment)
'''
«IF comment.c !== null»«comment.c»«ENDIF»'''
/*
* Documentation:
* KW_CPAREN ('if' e=Expression | ei?='endif')
*/
def CharSequence compile(int indent,int precidence,Documentation documentation)
'''
«IF documentation.ei»)endif«
ELSEIF documentation.e !== null»)if «compile(indent,precidence,documentation.e)»«ENDIF»'''
/*
* Defparameter: 'DEFPARAMETER' KW_OPAREN name=TK_ID KW_COMMA e=Expression KW_CPAREN;
*/
def CharSequence compile(int indent,int precidence,Defparameter defparameter)
'''
DEFPARAMETER («
IF defparameter.name !== null»«defparameter.name»«ENDIF»«
IF defparameter.e !== null»,«compile(indent,precidence,defparameter.e)»«ENDIF»)'''
/*
* Defconstant: 'DEFCONSTANT' KW_OPAREN name=TK_ID KW_COMMA e=Expression KW_CPAREN;
*/
def CharSequence compile(int indent,int precidence,Defconstant defconstant)
'''
DEFCONSTANT («
IF defconstant.name !== null»«defconstant.name»«ENDIF»«
IF defconstant.e !== null»,«compile(indent,precidence,defconstant.e)»«ENDIF»)'''
/*
* Defconst: 'DEFCONST' KW_OPAREN name=TK_ID KW_COMMA e=Expression KW_CPAREN;
*/
def CharSequence compile(int indent,int precidence,Defconst defconst)
'''
DEFCONST («
IF defconst.name !== null»«defconst.name»«ENDIF»«
IF defconst.e !== null»,«compile(indent,precidence,defconst.e)»«ENDIF»)'''
/*
* Defvar: 'DEFVAR' KW_OPAREN name=TK_ID (KW_COMMA e=Expression)? KW_CPAREN;
*/
def CharSequence compile(int indent,int precidence,Defvar defvar)
'''
DEFVAR («
IF defvar.name !== null»«defvar.name»«ENDIF»«
IF defvar.e !== null»,«compile(indent,precidence,defvar.e)»«ENDIF»)'''
/* FunctionDef definition
Function:
name=TK_ID fp+=KW_PRIME*
(
(KW_OPAREN (params+=Expression (KW_COMMA params+=Expression)*)? KW_CPAREN)
| j=TK_ID // support juxtapose
)
((KW_EQ2|m?=KW_MARROW) b=Block)? // function may be abstract
*
*/
def CharSequence compile(int indent,int precidence,FunctionDef function)
'''
«var int ind = indent»«
var boolean loadCode = false»«null»«
IF function.name !== null»«
{ if (function.name.equals("loadInit")) {
ind = -1;loadCode=true
}
null
}»«
ENDIF»«
IF (!loadCode) »«
IF function.name !== null»«function.name»«ENDIF»«
FOR x:function.fp»'«ENDFOR»(«
IF function.j !== null»«function.j»«ELSE»«
var testparams=false»«
FOR x:function.params»«if(testparams)','»«compile(indent,precidence,x)»«{testparams=true;null}»«ENDFOR»«
ENDIF»)«
ENDIF»«
IF function.st !== null»«
IF (!loadCode) »«IF function.m» ==>«ELSE» ==«ENDIF»«ENDIF»«
compile(ind,precidence,function.st)»«
ENDIF»«
IF function.w !== null»«
newline(ind)»«
compile(ind,precidence,function.w)»«
ENDIF»«
newline(ind)»'''
/*
* name=TK_ID KW_ASSIGN e=Expression */
def CharSequence compile(int indent,int precidence,GlobalVariable globalVariable)
'''
«IF globalVariable.name !== null»«globalVariable.name»«ENDIF» :=«
IF globalVariable.e !== null»«compile(indent,precidence,globalVariable.e)»«ENDIF»'''
/*
* Statement:
( Comment | Loop | =>MultiAssign | =>MultiDefine | =>MultiExit | WhereExpression | Where | Do)
*/
def CharSequence compile(int indent,int precidence,Statement statement)
'''
«IF statement instanceof Comment»«
compile(indent,precidence,statement as Comment)»«ENDIF»«
IF statement instanceof Loop»«
compile(indent,precidence,statement as Loop)»«ENDIF»«
IF statement instanceof Do»«
compile(indent,precidence,statement as Do)»«ENDIF»«
IF statement instanceof Where»«
compile(indent,precidence,statement as Where)»«ENDIF»«
IF statement instanceof Expr»«
compile(indent,precidence,statement as Expr)»«ENDIF»'''
/*
* Loop:
* c+=LoopCondition* (KW_BAR e=Expression)? 'repeat' b=Block
*
* Put space after each condition.
* This code uses LoopCondition, While, For and Until directly rather
* than calling their 'compile' functions. This is because trailing spaces
* after newline are changed by auto-indent code.
*
*/
def CharSequence compile(int indent,int precidence,Loop loop)
'''
«FOR x:loop.c»«
IF x.f !== null»«
»for «IF x.f.e !== null»«compile(indent,0,x.f.e)» «ENDIF»«
ENDIF»«
IF x.w !== null»«
»while «IF x.w.e !== null»«compile(indent,0,x.w.e)»«
IF x.w.n»«newline(indent)» «ELSE» «ENDIF»«
ENDIF»«
ENDIF»«
IF x.u !== null»«
»until «IF x.u.e !== null»«compile(indent,0,x.u.e)»«
IF x.u.n»«newline(indent)» «ELSE» «ENDIF»«
ENDIF»«
ENDIF»«
ENDFOR»«
IF loop.e !== null»|«compile(indent,0,loop.e)» «ENDIF»repeat «
IF loop.b !== null»«
compile(indent,precidence,loop.b)»«
ENDIF»'''
/*
* Do:
* 'do' b=Block
*/
def CharSequence compile(int indent,int precidence,Do do1)
'''
do «IF do1.e !== null»«compile(indent,0,do1.e)»«ENDIF»'''
/*
* Where:
* 'where' b=Block
*/
def CharSequence compile(int indent,int precidence,Where where)
''' where «IF where.b !== null»«compile(indent,precidence,where.b)»«ENDIF»'''
/* Expr holds both Expression and WhereExpression
*
* WhereExpression returns Expr:
(Expression ({WhereExpression.left=current} w=Where)?)
|
c?=KW_OCURLY (st+=Statement NL)* KW_CCURLY (KW_EXIT eexp=Expression)?
|
(
'if' b1=InnerBlock
(
=>('then' b2i=InnerBlock 'else' b3=Block) |
('then' b2=Block)
)
)
*
* the eexp is for i-syscmd in function dewritify
*
PrimaryExpression returns Expr:
(
Literal
| p?=KW_OPAREN m?=KW_MINUS? (t4=WhereExpression (KW_COMMA t5+=WhereExpression)*)?
(
b?=BEGIN
(s+=Statement NL)*
END NL
)? KW_CPAREN
|
c2?=KW_OCURLY BEGIN (s+=Statement NL)* END NL KW_CCURLY
)
d=KW_2DOT? // for segment with no end part
*
* from PrimaryExpression - no indent:
* t4 is WhereExpression inside parenthesis
* t5 is WhereExpressions inside parenthesis after comma
* from PrimaryExpression - indent:
* b=true
*/
def CharSequence compile(int indent,int precidence,Expr expr)
'''
«IF expr instanceof WhereExpression»«compile(indent,precidence,expr as WhereExpression)»«ENDIF»«
IF expr instanceof IfExpression»«compile(indent,precidence,expr as IfExpression)»«ENDIF»«
IF expr instanceof Expression»«compile(indent,precidence,expr as Expression)»«ENDIF»«
IF expr instanceof OrExpression»«compile(indent,precidence,expr as OrExpression)»«ENDIF»«
IF expr instanceof AndExpression»«compile(indent,precidence,expr as AndExpression)»«ENDIF»«
IF expr instanceof EqualityExpression»«compile(indent,precidence,expr as EqualityExpression)»«ENDIF»«
IF expr instanceof RelationalExpression»«compile(indent,precidence,expr as RelationalExpression)»«ENDIF»«
IF expr instanceof IsExpression»«compile(indent,precidence,expr as IsExpression)»«ENDIF»«
IF expr instanceof InExpression»«compile(indent,precidence,expr as InExpression)»«ENDIF»«
IF expr instanceof SegmentExpression»«compile(indent,precidence,expr as SegmentExpression)»«ENDIF»«
IF expr instanceof AdditiveExpression»«compile(indent,precidence,expr as AdditiveExpression)»«ENDIF»«
IF expr instanceof ExquoExpression»«compile(indent,precidence,expr as ExquoExpression)»«ENDIF»«
IF expr instanceof DivisionExpression»«compile(indent,precidence,expr as DivisionExpression)»«ENDIF»«
IF expr instanceof QuoExpression»«compile(indent,precidence,expr as QuoExpression)»«ENDIF»«
IF expr instanceof ModExpression»«compile(indent,precidence,expr as ModExpression)»«ENDIF»«
IF expr instanceof RemExpression»«compile(indent,precidence,expr as RemExpression)»«ENDIF»«
IF expr instanceof MultiplicativeExpression»«compile(indent,precidence,expr as MultiplicativeExpression)»«ENDIF»«
IF expr instanceof ExponentExpression»«compile(indent,precidence,expr as ExponentExpression)»«ENDIF»«
IF expr instanceof MapExpression»«compile(indent,precidence,expr as MapExpression)»«ENDIF»«
IF expr instanceof LambdaExpression»«compile(indent,precidence,expr as LambdaExpression)»«ENDIF»«
IF expr instanceof AssignExpression»«compile(indent,precidence,expr as AssignExpression)»«ENDIF»«
IF expr instanceof ExitExpression»«compile(indent,precidence,expr as ExitExpression)»«ENDIF»«
IF expr instanceof EltExpression»«compile(indent,precidence,expr as EltExpression)»«ENDIF»«
IF expr instanceof UnaryExpression»«compile(indent,precidence,expr as UnaryExpression)»«ENDIF»«
IF expr instanceof VarOrFunction»«compile(indent,precidence,expr as VarOrFunction)»«ENDIF»«
IF expr instanceof Tuple»«compile(indent,precidence,expr as Tuple)»«ENDIF»«
IF expr instanceof Block»«compile(indent,precidence,expr as Block)»«ENDIF»«
IF expr instanceof Literal»«compile(indent,precidence,expr as Literal)»«ENDIF»«
IF expr.b1 !== null»if «
compile(indent,precidence,expr.b1)»«
IF expr.n1»«newline(indent)»«ELSE» «ENDIF»«
ENDIF»«
IF expr.b2 !== null»then «
compile(indent,precidence,expr.b2)» «
ENDIF»«
IF expr.b2i !== null»then «
compile(indent,precidence,expr.b2i)»«
IF expr.n2»«newline(indent)»«ELSE» «ENDIF»«
ENDIF»«
IF expr.b3 !== null»else «
compile(indent,precidence,expr.b3)»«ENDIF»'''
/* WhereExpression returns Expr:
(Expression ({WhereExpression.left=current} w=Where?))
|
c?=KW_OCURLY (st+=Statement NL)* KW_CCURLY
|
(
'if' b1=InnerBlock
(
=>('then' b2i=InnerBlock 'else' b3=Block) |
('then' b2=Block)
)
)
*/
def CharSequence compile(int indent,int precidence,WhereExpression whereExpression)
'''
«IF whereExpression.left !== null»«compile(indent,1,whereExpression.left)»«ENDIF»«
IF whereExpression.w !== null» «compile(indent,1,whereExpression.w)» «ENDIF»'''
def CharSequence compile(int indent,int precidence,IfExpression ifExpression)
'''
«IF ifExpression.i1 !== null»if «compile(indent,0,ifExpression.i1)» «ENDIF»«
IF ifExpression.i2 !== null»then «compile(indent,0,ifExpression.i2)» «ENDIF»«
IF ifExpression.i3 !== null»«
newline(indent)»else «
compile(indent,0,ifExpression.i3)» «
ENDIF»'''
/* This is top of expression tree except for WhereExpression
* combines statements using semicolon
*/
def CharSequence compile(int indent,int precidence,Expression expression)
'''
«cop(8,precidence)»«
IF expression.left !== null»«compile(indent,8,expression.left)»«ENDIF»«
IF expression.op !== null» «expression.op» «ENDIF»«
IF expression.right !== null»«compile(indent,8,expression.right)»«ENDIF»«
IF expression.right2 !== null»«compile(indent,8,expression.right2)»«ENDIF»«
ccp(8,precidence)»'''
def CharSequence compile(int indent,int precidence,MapExpression mapExpression)
'''
«cop(10,precidence)»«
IF mapExpression.left !== null»«compile(indent,10,mapExpression.left)»«ENDIF»«
IF mapExpression.op !== null» «mapExpression.op» «ENDIF»«
IF mapExpression.right !== null»«compile(indent,10,mapExpression.right)»«ENDIF»«
ccp(10,precidence)»'''
def CharSequence compile(int indent,int precidence,LambdaExpression lambdaExpression)
'''
«cop(12,precidence)»«
IF lambdaExpression.left !== null»«compile(indent,12,lambdaExpression.left)»«ENDIF»«
IF lambdaExpression.op !== null» «lambdaExpression.op» «ENDIF»«
IF lambdaExpression.right !== null»«compile(indent,12,lambdaExpression.right)»«ENDIF»«
ccp(12,precidence)»'''
/* This is top of expression tree except for WhereExpression
* Handles exit like: '=>' expr1 ';' expr2
*/
def CharSequence compile(int indent,int precidence,ExitExpression exitExpression)
'''
«cop(14,precidence)»«
IF exitExpression.left !== null»«compile(indent,14,exitExpression.left)»«ENDIF»«
IF exitExpression.op !== null» «exitExpression.op» «ENDIF»«
IF exitExpression.right !== null»«compile(indent,14,exitExpression.right)»«ENDIF»«
ccp(14,precidence)»'''
def CharSequence compile(int indent,int precidence,AssignExpression assignExpression)
'''
«cop(16,precidence)»«
IF assignExpression.left !== null»«compile(indent,16,assignExpression.left)»«ENDIF»«
IF assignExpression.op !== null» «assignExpression.op» «ENDIF»«
IF assignExpression.right !== null»«compile(indent,16,assignExpression.right)»«ENDIF»«
cop(16,precidence)»'''
def CharSequence compile(int indent,int precidence,OrExpression orExpression)
'''
«cop(18,precidence)»«
IF orExpression.left !== null»«compile(indent,18,orExpression.left)»«ENDIF»«
IF orExpression.op !== null» «orExpression.op» «ENDIF»«
IF orExpression.right !== null»«compile(indent,18,orExpression.right)»«ENDIF»«
ccp(18,precidence)»'''
def CharSequence compile(int indent,int precidence,AndExpression andExpression)
'''
«cop(20,precidence)»«
IF andExpression.left !== null»«compile(indent,20,andExpression.left)»«ENDIF»«
IF andExpression.op !== null» «andExpression.op» «ENDIF»«
IF andExpression.right !== null»«compile(indent,20,andExpression.right)»«ENDIF»«
ccp(20,precidence)»'''
def CharSequence compile(int indent,int precidence,EqualityExpression equalityExpression)
'''
«cop(22,precidence)»«
IF equalityExpression.left !== null»«compile(indent,22,equalityExpression.left)»«ENDIF»«
IF equalityExpression.op !== null» «equalityExpression.op» «ENDIF»«
IF equalityExpression.right !== null»«compile(indent,22,equalityExpression.right)»«ENDIF»«
ccp(22,precidence)»'''
def CharSequence compile(int indent,int precidence,RelationalExpression relationalExpression)
'''
«cop(24,precidence)»«
IF relationalExpression.left !== null»«compile(indent,24,relationalExpression.left)»«ENDIF»«
IF relationalExpression.op !== null» «relationalExpression.op» «ENDIF»«
IF relationalExpression.right !== null»«compile(indent,24,relationalExpression.right)»«ENDIF»«
ccp(24,precidence)»'''
def CharSequence compile(int indent,int precidence,IsExpression isExpression)
'''
«cop(26,precidence)»«
IF isExpression.left !== null»«compile(indent,26,isExpression.left)»«ENDIF»«
IF isExpression.op !== null» «isExpression.op» «ENDIF»«
IF isExpression.right !== null»«compile(indent,26,isExpression.right)»«ENDIF»«
ccp(26,precidence)»'''
def CharSequence compile(int indent,int precidence,InExpression inExpression)
'''
«cop(28,precidence)»«
IF inExpression.left !== null»«compile(indent,28,inExpression.left)»«ENDIF»«
IF inExpression.op !== null» «inExpression.op» «ENDIF»«
IF inExpression.right !== null»«compile(indent,28,inExpression.right)»«ENDIF»«
IF inExpression.r2 !== null» by «compile(indent,29,inExpression.r2)»«ENDIF»«
ccp(28,precidence)»'''
/*
* ..
* don't put spaces around segment */
def CharSequence compile(int indent,int precidence,SegmentExpression segmentExpression)
'''
«cop(30,precidence)»«
IF segmentExpression.left !== null»«compile(indent,30,segmentExpression.left)»«ENDIF»«
IF segmentExpression.op !== null»«segmentExpression.op»«ENDIF»«
IF segmentExpression.right !== null»«compile(indent,30,segmentExpression.right)»«ENDIF»«
ccp(30,precidence)»'''
def CharSequence compile(int indent,int precidence,AdditiveExpression additiveExpression)
'''
«cop(32,precidence)»«
IF additiveExpression.left !== null»«compile(indent,32,additiveExpression.left)»«ENDIF»«
IF additiveExpression.op !== null» «additiveExpression.op» «ENDIF»«
IF additiveExpression.right !== null»«compile(indent,32,additiveExpression.right)»«ENDIF»«
ccp(32,precidence)»'''
def CharSequence compile(int indent,int precidence,ExquoExpression exquoExpression)
'''
«cop(34,precidence)»«
IF exquoExpression.left !== null»«compile(indent,34,exquoExpression.left)»«ENDIF»«
IF exquoExpression.op !== null» «exquoExpression.op» «ENDIF»«
IF exquoExpression.right !== null»«compile(indent,34,exquoExpression.right)»«ENDIF»«
ccp(34,precidence)»'''
def CharSequence compile(int indent,int precidence,DivisionExpression divisionExpression)
'''
«cop(36,precidence)»«
IF divisionExpression.left !== null»«compile(indent,36,divisionExpression.left)»«ENDIF»«
IF divisionExpression.op !== null» «divisionExpression.op» «ENDIF»«
IF divisionExpression.right !== null»«compile(indent,36,divisionExpression.right)»«ENDIF»«
ccp(363,precidence)»'''
def CharSequence compile(int indent,int precidence,QuoExpression quoExpression)
'''
«cop(16,precidence)»«
IF quoExpression.left !== null»«compile(indent,16,quoExpression.left)»«ENDIF»«
IF quoExpression.op !== null» «quoExpression.op» «ENDIF»«
IF quoExpression.right !== null»«compile(indent,16,quoExpression.right)»«ENDIF»«
ccp(16,precidence)»'''
def CharSequence compile(int indent,int precidence,ModExpression modExpression)
'''
«cop(38,precidence)»«
IF modExpression.left !== null»«compile(indent,38,modExpression.left)»«ENDIF»«
IF modExpression.op !== null» «modExpression.op» «ENDIF»«
IF modExpression.right !== null»«compile(indent,38,modExpression.right)»«ENDIF»«
ccp(38,precidence)»'''
def CharSequence compile(int indent,int precidence,RemExpression remExpression)
'''
«cop(40,precidence)»«
IF remExpression.left !== null»«compile(indent,40,remExpression.left)»«ENDIF»«
IF remExpression.op !== null» «remExpression.op» «ENDIF»«
IF remExpression.right !== null»«compile(indent,40,remExpression.right)»«ENDIF»«
ccp(40,precidence)»'''
def CharSequence compile(int indent,int precidence,MultiplicativeExpression multiplicativeExpression)
'''
«cop(42,precidence)»«
IF multiplicativeExpression.left !== null»«compile(indent,42,multiplicativeExpression.left)»«ENDIF»«
IF multiplicativeExpression.op !== null» «multiplicativeExpression.op»«ENDIF»«
IF multiplicativeExpression.right !== null»«compile(indent,42,multiplicativeExpression.right)»«ENDIF»«
ccp(42,precidence)»'''
def CharSequence compile(int indent,int precidence,ExponentExpression exponentExpression)
'''
«cop(44,precidence)»«
IF exponentExpression.left !== null»«compile(indent,44,exponentExpression.left)»«ENDIF»«
IF exponentExpression.op !== null»«exponentExpression.op» «ENDIF»«
IF exponentExpression.right !== null»«compile(indent,44,exponentExpression.right)»«ENDIF»«
ccp(44,precidence)»'''
def CharSequence compile(int indent,int precidence,EltExpression eltExpression)
'''
«cop(46,precidence)»«
IF eltExpression.left !== null»«compile(indent,46,eltExpression.left)»«ENDIF»«
IF eltExpression.op !== null»«eltExpression.op»«ENDIF»«
IF eltExpression.right !== null»«compile(indent,46,eltExpression.right)»«ENDIF»«
ccp(46,precidence)»'''
/*
* UnaryExpression returns Expr:
PrimaryExpression |
({UnaryExpression} name=TK_ID =>p2?=KW_PRIME? expr=UnaryExpression?) |
({UnaryExpression} uop='not' expr=UnaryExpression) |
({UnaryExpression} uop=KW_COLON expr=UnaryExpression) |
({UnaryExpression} uop='or/' expr=UnaryExpression) |
({UnaryExpression} uop='and/' expr=UnaryExpression) |
({UnaryExpression} uop='+/' expr=UnaryExpression) |
({UnaryExpression} uop='*./' expr=UnaryExpression) | // needs . otherwise causes errors in xtext files
({UnaryExpression} uop='return' expr=UnaryExpression) |
({UnaryExpression} uop=KW_SHARP expr=UnaryExpression) |
({UnaryExpression} uop=KW_COLON loc?='local')
*
* Put a space between ID and expr but not $ and expr
*
*/
def CharSequence compile(int indent,int precidence,UnaryExpression unaryExpression)
'''
«cop(48,precidence)»«
IF unaryExpression.b1 !== null»«compile(indent,48,unaryExpression.b1)»«ENDIF»«
IF unaryExpression.b2 !== null»«unaryExpression.b2»«ENDIF»«
IF unaryExpression.b3 !== null»«compile(indent,48,unaryExpression.b3)»«ENDIF»«
IF unaryExpression.uop !== null»«
if (unaryExpression.uop.compareTo("*./")==0) "*/" else unaryExpression.uop»«
if (unaryExpression.uop.compareTo("not")==0) " "»«
if (unaryExpression.uop.compareTo("return")==0) " "»«
ENDIF»«
IF unaryExpression.loc»local«ENDIF»«
IF unaryExpression.expr !== null»«compile(indent,48,unaryExpression.expr)»«ENDIF»«
ccp(48,precidence)»'''
/*{Tuple} p?=KW_OPAREN m2?=KW_MINUS? (t3=WhereExpression NL? (KW_COMMA t5+=WhereExpression)*)? */
def CharSequence compile(int indent,int precidence,Tuple expr)
'''
«var Boolean removeBrackets =false»«
IF expr.m2»«
IF expr.t3 !== null»«
IF expr.t3 instanceof Literal»«{removeBrackets =true;null}»«ENDIF»«
ENDIF»«
ENDIF»«
IF (!removeBrackets) »(«ENDIF»«
IF expr.m2»-«ENDIF»«
IF expr.t3 !== null»«compile(indent,0,expr.t3)»«ENDIF»«
FOR x:expr.t5»,«compile(indent,0,x)»«ENDFOR»«
IF (!removeBrackets) »)«ENDIF»«
IF expr.d»..«ENDIF»'''
/*PrimaryExpression returns Expr:
(
Literal
|
({Tuple} p?=KW_OPAREN m2?=KW_MINUS? (t3=WhereExpression NL? (KW_COMMA t5+=WhereExpression)*)?
KW_CPAREN)
|
({Block} b?=BEGIN
(s+=Statement NL)*
END )
|
({Block} c2?=KW_OCURLY (s+=Statement NL)* KW_CCURLY )
|
({Block} c3?=KW_OCHEV m?=KW_MINUS? t4=WhereExpression KW_CCHEV )
)
d=KW_2DOT? // for segment with no end part */
def CharSequence compile(int indent,int precidence,Block expr)
'''
«IF expr.c2»(«
FOR x:expr.s»«
newline(indent+1)»«
compile(indent+1,0,x)»«
ENDFOR»)«
ENDIF»«
IF expr.b»«
FOR x:expr.s»«
newline(indent+1)»«
compile(indent+1,0,x)»«
ENDFOR»«
ENDIF»«
IF expr.c3»«
IF expr.m»-«ENDIF»«
IF expr.t4 !== null»«compile(indent,0,expr.t4)»«ENDIF»«
ENDIF»«
IF expr.d»..«ENDIF»'''
/* see UnaryExpression for syntax */
def CharSequence compile(int indent,int precidence,VarOrFunction varOrFunction)
'''
«cop(48,precidence)»«
cleanID(varOrFunction.name)»«
var Boolean addSpace =false»«
IF varOrFunction.expr !== null»«
IF !(varOrFunction.expr instanceof Tuple)»«{addSpace =true;null}»«ENDIF»«
ENDIF»«
if (addSpace) " " else ""»«
IF varOrFunction.expr !== null»«compile(indent,48,varOrFunction.expr)»«ENDIF»«
ccp(48,precidence)»'''
/*
* Literal:
* // numeric literal
* value=Numeric
* // boolean literals
* | bool='true'
* | bool='false'
* // null
* | nil='NIL'
* | nil='nil'
* // other
* | lst=ListLiteral
* | lsp=LispLiteral
* | str=TK_STRING
*/
def CharSequence compile(int indent,int precidence,Literal literal)
'''
«IF literal instanceof LispLiteral»«compile(indent,precidence,literal as LispLiteral)»«ENDIF»«
IF literal instanceof ListLiteral»[«compile(indent,precidence,literal as ListLiteral)»]«ENDIF»«
IF literal.value !== null»«
literal.value»«
IF literal.d»..«ENDIF»«
ENDIF»«
IF literal.bool !== null»«literal.bool»«ENDIF»«
IF literal.nil !== null»«literal.nil»«ENDIF»«
IF literal.str !== null»«literal.str»«ENDIF»'''
/*
* LispLiteral:
p+=KW_PRIME+ sll=SubLispLiteral
*
* Dont put space before prime(s) if first in a line
* Do put space before prime(s) if following
*/
def CharSequence compile(int indent,int precidence,LispLiteral lispLiteral)
'''
«FOR x:lispLiteral.pr»«x»«ENDFOR»«
IF lispLiteral.sll !== null»«compile(indent,precidence,lispLiteral.sll)»«ENDIF»'''
/*
* SubLispLiteral:
name=TK_ID // need to accept '%a
|
// allow keywords to be used in lisp literals
key=('not' | 'and' | 'or' | 'for' | 'while' | 'where' | 'local' | 'package' | 'exquo' |
'true' | 'false' | 'is' | 'isnt' | 'repeat' |'until' | 'DEFPARAMETER' | 'DEFCONST' |
'DEFCONSTANT' | 'DEFVAR' |
'NIL' | 'nil')
|
m?=KW_MINUS? num=Numeric
|
NL? st=TK_STRING
|
( NL? oparen=KW_OPAREN asl+=AnnotatedSubLispLiteral* KW_CPAREN)
*/
def CharSequence compile(int indent,int precidence,SubLispLiteral subLispLiteral)
'''
«IF subLispLiteral.name !== null»«cleanID(subLispLiteral.name)»«ENDIF»«
IF subLispLiteral.m»-«ENDIF»«
IF subLispLiteral.num !== null»«subLispLiteral.num»«ENDIF»«
IF subLispLiteral.st !== null»«subLispLiteral.st»«ENDIF»«
IF subLispLiteral.key !== null»«subLispLiteral.key»«ENDIF»«
var Boolean inParen = true»«var Boolean first = true»«
FOR x:subLispLiteral.asl»«
IF x.sl !== null»«
IF x.sl.m »«{inParen = false;null}»«ENDIF»«
ENDIF»«
{if (!first) inParen = true;null}»«
{first = false;null}»«
ENDFOR»«
IF subLispLiteral.oparen && inParen»(«ENDIF»«
FOR x:subLispLiteral.asl»«
compile(indent,0,x)» «
ENDFOR»«
IF subLispLiteral.oparen && inParen»)«ENDIF»'''
/* AnnotatedSubLispLiteral:
* p?=KW_PRIME? sl=SubLispLiteral d?=KW_DOT?
* */
def CharSequence compile(int indent,int precidence,AnnotatedSubLispLiteral annotatedSubLispLiteral)
'''
«IF annotatedSubLispLiteral.p»'«ENDIF»«
IF annotatedSubLispLiteral.sl !== null»«compile(indent,0,annotatedSubLispLiteral.sl)»«ENDIF»«
IF annotatedSubLispLiteral.d» .«ENDIF»'''
/*
* ListLiteral:
// may be empty list so ob ensures literal is created
ob?= KW_OBRACK
le+=ListElement?
(KW_COMMA NL? le+=ListElement)*
sl+=ListComprehension*
KW_CBRACK
*/
def CharSequence compile(int indent,int precidence,ListLiteral listLiteral)
'''
«var testparams=false»«
FOR x:listLiteral.le»«if(testparams)','»«compile(indent,0,x)»«{testparams=true;null}»«ENDFOR»«
FOR x:listLiteral.sl»«compile(indent,0,x)»«ENDFOR»'''
/*
* ListElement:
(
(c?=KW_COLON? e?=KW_EQ)? l2=IfExpression |
c2?=KW_COLON? d?=KW_DOT
)
*/
def CharSequence compile(int indent,int precidence,ListElement listElement)
'''
«IF listElement.c»:«
IF listElement.e» «ENDIF»«
ENDIF»«
IF listElement.e»=«ENDIF»«
IF listElement.l2 !== null»«compile(indent,0,listElement.l2)» «ENDIF»«
IF listElement.c2»:«ENDIF»«
IF listElement.d».«ENDIF»'''
/*
* ListComprehension:
(sl1=('for'|'while'|'where'|KW_BAR) sl2=Expression
|
r?='repeat'
)
*/
def CharSequence compile(int indent,int precidence,ListComprehension listComprehension)
'''
«IF listComprehension.sl1 !== null» «listComprehension.sl1» «ENDIF»«
IF listComprehension.sl2 !== null»«compile(indent,0,listComprehension.sl2)»«ENDIF»«
IF listComprehension.r» repeat «ENDIF»'''
}
/* Code Generator Template Notes
* -----------------------------
* Template commands:
*
* FOR
* ENDFOR
* IF
* ELSE
* ENDIF
* ELSEIF
*
* To display filename use:
* «IF function.eContainer !== null»
* function «function.eResource.className»;
* «ENDIF»
*
* To use Booleans:
*
* «IF block.b»true«ELSE»false«ENDIF»
*/