Angel \”Java\” Lopez on Blog

December 29, 2012

CobolScript (3) Templates

Previous Post
Next Post

I like to have a template engine in my languages, so, I added one in my open source CobolScript, COBOL compiler to JavaScript. The first sample:

https://github.com/ajlopez/CobolScript/tree/master/samples/template

The code:

<#
data division.
working-storage section.
01 n.

procedure division.
#>
Factorial
---------

<#
perform show-factorial varying n from 1 to 10.

show-factorial.
local result.
perform factorial using n giving result.
#>
${n}!= ${result}
<#
.
factorial using n.
local m.
if n = 1 then return n.
subtract 1 from n giving m.
perform factorial using m giving m.
multiply n by m.
return m.
#>

The template file is compiled to COBOL transforming every text to DISPLAY … WITH NO ADVANCING (the new lines are already in the text). The code between <# and #> is copy as is to the COBOL program. Every expression between ${ and } is expanded as another parameter to a DISPLAY command. Then, after compiling all the texto to COBOL, CobolScript compile it to JavaScript. The template syntax is a kind of syntax-sugar.

The output of the program:

Factorial
---------

1!= 1
2!= 2
3!= 6
4!= 24
5!= 120
6!= 720
7!= 5040
8!= 40320
9!= 362880
10!= 3628800

I could use the template support to generate text files, that is, to implement code generation in CobolScript, as I did in AjGenesis (classic, Ruby, Node.js). I already have a dynamic web page implementation based on this template engine (yes, dynamic web pages in CobolScript ;-). But that is a topic for another post.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

2 Comments »

  1. [...] Previous Post Next Post [...]

    Pingback by CobolScript (2) First Factorial Function « Angel ”Java” Lopez on Blog — December 29, 2012 @ 2:48 pm

  2. [...] Previous Post [...]

    Pingback by CobolScript (4) Web Pages with Templates « Angel ”Java” Lopez on Blog — January 19, 2013 @ 3:47 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 68 other followers

%d bloggers like this: