[stringtemplate-interest] Conditionals
Terence Parr
parrt at cs.usfca.edu
Fri Sep 16 10:31:41 PDT 2011
hi. My new rule of thumb is to avoid conditionals if possible. The way to do that is to define "do some int thing" and the others as separate templates. then, your code generator (the controller/model) then informs Foo it has another element to add:
Foo(type, a, b) ::= <<
class Foo {
<type> foo;
<statements>
}
>>
much cleaner
Ter
On Sep 1, 2011, at 3:35 PM, Jiwon Seo wrote:
Hi,
I'm using stringtemplate for code generation.
One thing I need to do is to generate different code based on the type
of a variable, like following:
Foo(type, a, b) ::= <<
class Foo {
<type> foo;
<if (type=="int")>
do some int thing
<elseif (type=="String">
do some string thing
<endif>
}
>>
So far, I could somehow use the map (such as, EqMap ::= ["int": "==" ,
default: ".equals"] ) but this is something that cannot be done using
the map. So I ended up using a lot of variables like following:
Foo(type, intType, stringType, ... a, b) ::= <<
class Foo {
<type> foo;
<if (intType)>
do some int thing
<elseif (stringType)>
do some string thing
<endif>
}
>>
Is there any other ways of doing this?
-Jiwon
_______________________________________________
stringtemplate-interest mailing list
stringtemplate-interest at antlr.org
http://www.antlr.org/mailman/listinfo/stringtemplate-interest
More information about the stringtemplate-interest
mailing list