Angel \”Java\” Lopez on Blog

January 19, 2013

CobolScript (4) Web Pages with Templates

Filed under: COBOL, CobolScript, JavaScript, NodeJs, Open Source Projects — ajlopez @ 3:47 pm

Previous Post

In the previous post I presented CobolScript generating output using templates. It can be used to generate web page output, too. The sample is at:

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

The launch program in JavaScript is simple:

var cobs = require('../..'),
    http = require('http'),
    fs = require('fs');

var program = cobs.compileTemplateFile('./factorial.cobp');

http.createServer(function(req, res) {
    program.run(cobs.getRuntime({ request: req, response: res }));
}).listen(8000);

console.log('Server started, listening at port 8000');

The key part is the call to compile the file template. It produce a compiled JavaScript function to be invoked. The call of program.run executes the template, with a giving runtime context. The runtime context is build giving the request and response objects of the current incoming request. That runtime derives all the output of the CobolScript program to the response output. So, the template doesn’t know about web request and response. You can see the runtime object as a service provider to the compiled CobolScript program. Its properties can be accessed if you define a LINKAGE SECTION as in classic COBOL. But this feature was not used in this simple sample.

The template file

<h1>Factorial</h1>
<p>Page generated by CobolScript, using templates</p>
<table>
<tr><th align='right'>n</th><th align='right'>n!
</th>
</tr>
<#
local n.
perform show-factorial using n varying n from 1 to 10.
#>
</table>
<#
.
stop run.

show-factorial section using n.
local result.
perform factorial using n giving result.
#>
<tr>
<td align='right'>${n}</td><td align='right'>${result}
</td>
</tr>
<#
.

factorial section 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.
#>

Launch the server with

node server

Navigate to localhost:8000, the result:

Next post: a dynamic web site accessing MySQL database, using CobolScript.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

January 12, 2013

Code Katas in JavaScript/Node.js using TDD

These past weeks, I was working in JavaScript/Node.js modules, using TDD at each step. Practice, practice, practice, the journey to mastery.

You can see my progress, reviewing the commits I did at each new test. This is a summary of that work:

CobolScript: See my posts, an implementation of COBOL as a compiler to JavaScript, having console program samples, dynamic web pages and access to Node.js modules. See web sample, using MySQL, and SimpleWeb.

SimplePipes: A way to define message-passing using ‘pipes’ to connect different defined nodes/functions. I want to extend it to have distributed process.

SimpleBoggle: Boggle solver, it is better than me! See console sample.

SimpleMemolap: Multidimensional OLAP-like processing, with in-memory model, and SimpleWeb site see sample:

SimpleChess: Work in progress, define a board using SimpleBoard, and make moves. I’m working on SimpleGo, too, to have a board, game, and evaluators.

SimpleRules: forward-chaing rule engine. I should add rule compilation to JavaScript. The engine works a la Rete-2, detecting the changes in the current state, and triggering actions.

SimpleScript: see post, my simple language, compiled to JavaScript. See posts. WIP.

Py2Script: Python language compiler to JavaScript, first step. WIP.

SimpleWeb: web middleware, a la Connect, with web sample.

BasicScript: My first steps to compile Basic to JavaScript. I want to use it to program and compile a game.

SimplePermissions: Today code kata. It implements subjects, roles, and permissions, granted by context.

SimpleFunc: Serialization of functions.

SimpleMapReduce: Exploring the implementation of a Map-Reduce algorithm.

SimpleTuring: Turing machine implentation.

Cellular: Cellular automata implementation, including a Game of Life console sample.

I will work on:

NodeDelicious: To retrieve my links from my Delicious account, now the site was revamped and no more pagination.

SimpleDatabase: In-memory database, maybe I will add file persistence.

SimpleSudoku: Rewrite of my AjSudoku solver, from scratch.

I’m having a lot of fun, as usual ;-)

Keep tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

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

December 24, 2012

CobolScript (2) First Factorial Function

Previous Post
Next Post

JavaScript is butter in my hands ;-). I was working on CobolScript, my COBOL compiler to JavaScript.

I added the support for user-defined functions, parameters, arguments and local variables. The first example, factorial:

https://github.com/ajlopez/CobolScript/blob/master/samples/factorial/factorial.cob

You can run it, executing command line in that folder:

node run factorial.cob

data division.
working-storage section.
01 n.

procedure division.
perform show-factorial varying n from 1 to 10.

show-factorial local result.
perform factorial using n giving result.
display 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 new syntax features:

- performusing … You can call a local procedure passing arguments.

- <proc> using …  The procedure declares its arguments.

- <proc> local(s) … The procedure declares its local variables.

- performgiving <var>…  You can specify that a perform has a return value, to be saved in one or more variables.

- return (expr)  The procedure can return at any point. A return value could be specified.

I just added web page support, but that is a topic for another post ;-)

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

Theme: Shocking Blue Green. Get a free blog at WordPress.com

Follow

Get every new post delivered to your Inbox.

Join 67 other followers