Angel \”Java\” Lopez on Blog

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

2 Comments »

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

    Pingback by CobolScript (3) Templates « Angel ”Java” Lopez on Blog — December 29, 2012 @ 2:46 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. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 57 other followers

%d bloggers like this: