Permalink
Browse files

factorial sample, WIP

  • Loading branch information...
1 parent 3d5ab0b commit e50fdd23d3abd51b08f09c1868c50e57f279f6d9 @ajlopez committed Dec 24, 2012
Showing with 45 additions and 7 deletions.
  1. +7 −3 lib/cobolscript.js
  2. +19 −0 samples/factorial/factorial.cob
  3. +1 −0 samples/factorial/run.js
  4. +1 −1 samples/hellopgm/run.js
  5. +17 −3 test/compile.js
View
@@ -278,6 +278,8 @@ var cobolscript = (function() {
function ProcedureCommand(name, commands, args, locals)
{
+ name = normalizeName(name);
+
this.compile = function(program, context) {
var code = 'function ' + name + '(';
code += compileArguments();
@@ -326,16 +328,16 @@ var cobolscript = (function() {
};
function compileLocals() {
- if (!args)
+ if (!locals)
return '';
- var n = args.length;
+ var n = locals.length;
var code = 'var ';
for (var k = 0; k < n; k++) {
if (k)
code += ', ';
- code += args[k].getName();
+ code += locals[k].getName();
}
return code + ';';
@@ -345,6 +347,7 @@ var cobolscript = (function() {
function PerformCommand(name, options)
{
options = options || { };
+ name = normalizeName(name);
if (options.varying) {
if (options.from == null)
@@ -958,6 +961,7 @@ var cobolscript = (function() {
TokenType: TokenType,
Parser: Parser,
run: function(text, runtime, program) {
+ console.log(text);
var func = new Function("runtime", "program", "var ws = (program && program.data) ? program.data.working_storage : null;\r\n" + text);
return func(runtime, program);
}, compile: function(text) { var parser = new Parser(text); return parser.parseProgram(); }
@@ -0,0 +1,19 @@
+data division.
+working-storage section.
+01 n.
+
+procedure division.
+perform show-factorial varying n from 1 to 10.
+
+show-factorial locals result.
+perform factorial using n giving result.
+display n "! = " result.
+
+factorial using n locals 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.
+
+
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -172,14 +172,28 @@ assert.ok(text.indexOf('};') >= 0);
// perform procedure with giving many variables
-var text = compile('perform procedure1 giving k, j. procedure1. return 1.', { k: null, j: null });
-assert.ok(text.indexOf('var $aux = procedure1();') >= 0);
+var text = compile('perform procedure-1 giving k, j. procedure-1. return 1.', { k: null, j: null });
+assert.ok(text.indexOf('var $aux = procedure_1();') >= 0);
assert.ok(text.indexOf('ws.k = $aux;') >= 0);
assert.ok(text.indexOf('ws.j = $aux;') >= 0);
-assert.ok(text.indexOf('function procedure1() {') >= 0);
+assert.ok(text.indexOf('function procedure_1() {') >= 0);
assert.ok(text.indexOf('return 1;') >= 0);
assert.ok(text.indexOf('};') >= 0);
+// perform procedure with local
+
+var text = compile('\
+perform procedure-1.\r\n\
+\r\n\
+procedure-1 local a.\r\n\
+move 1 to a.\r\n\
+return a.'
+, { k: null, j: null });
+assert.ok(text.indexOf('procedure_1();') >= 0);
+assert.ok(text.indexOf('function procedure_1() {') >= 0);
+assert.ok(text.indexOf('var a;') >= 0);
+assert.ok(text.indexOf('};') >= 0);
+
// if
var text = compile('if a > 0 then move 0 to a.', { a: null });

0 comments on commit e50fdd2

Please sign in to comment.