Skip to content

Commit

Permalink
Locals in procedure with test
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlopez committed Dec 24, 2012
1 parent 7d55040 commit 3d5ab0b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
60 changes: 44 additions & 16 deletions lib/cobolscript.js
Expand Up @@ -20,7 +20,7 @@ var cobolscript = (function() {
"or", "and",
"is", "not", "then",
"advancing",
"giving", "using",
"giving", "using", "locals", "local",
"than",
"varying",
"division", "section",
Expand Down Expand Up @@ -276,12 +276,13 @@ var cobolscript = (function() {
};
};

function ProcedureCommand(name, commands, args)
function ProcedureCommand(name, commands, args, locals)
{
this.compile = function(program, context) {
var code = 'function ' + name + '(';
code += compileArguments();
code += ') {';
code += compileLocals();
if (commands)
code += commands.compile(program, getContext());
code += '};';
Expand All @@ -291,13 +292,19 @@ var cobolscript = (function() {
function getContext() {
var context = { arguments: { }, locals: { } };

if (!args)
return context;

var n = args.length;

for (var k = 0; k < n; k++)
context.arguments[args[k].getName()] = null;
if (args) {
var n = args.length;

for (var k = 0; k < n; k++)
context.arguments[args[k].getName()] = null;
}

if (locals) {
var n = locals.length;

for (var k = 0; k < n; k++)
context.locals[locals[k].getName()] = null;
}

return context;
};
Expand All @@ -317,6 +324,22 @@ var cobolscript = (function() {

return code;
};

function compileLocals() {
if (!args)
return '';

var n = args.length;
var code = 'var ';

for (var k = 0; k < n; k++) {
if (k)
code += ', ';
code += args[k].getName();
}

return code + ';';
};
};

function PerformCommand(name, options)
Expand Down Expand Up @@ -358,10 +381,10 @@ var cobolscript = (function() {
if (!(options.giving instanceof Array))
code = options.giving.compile(program, context) + ' = ' + code;
else {
code = 'var aux = ' + code;
code = 'var $aux = ' + code;
var n = options.giving.length;
for (var k = 0; k < n; k++)
code += options.giving[k].compile(program, context) + ' = aux;';
code += options.giving[k].compile(program, context) + ' = $aux;';
}
}

Expand Down Expand Up @@ -433,15 +456,15 @@ var cobolscript = (function() {
if (from)
code = from.compile(program, context) + ' ' + oper + ' ' + code;

code = 'var aux = ' + code;
code = 'var $aux = ' + code;

var n = variable.length;

for (var k = 0; k < n; k++)
if (!giving)
code += variable[k].compile(program, context) + ' ' + oper + '= aux;';
code += variable[k].compile(program, context) + ' ' + oper + '= $aux;';
else
code += variable[k].compile(program, context) + ' = aux;';
code += variable[k].compile(program, context) + ' = $aux;';
return code; };

function compileTarget(program, context, target) {
Expand Down Expand Up @@ -525,12 +548,17 @@ var cobolscript = (function() {

if (tryParseName("using"))
arguments = parseVariables();

var locals = null;

if (tryParseName("locals") || tryParseName("local"))
locals = parseVariables();

parsePoint();

var cmds = this.parseCommands();

return new ProcedureCommand(token.value, cmds, arguments);
return new ProcedureCommand(token.value, cmds, arguments, locals);
}; this.parseCommands = function(noend) {
var command = this.parseCommand(noend); if (command == null) return null;
var commands = null;
Expand Down Expand Up @@ -930,7 +958,7 @@ var cobolscript = (function() {
TokenType: TokenType,
Parser: Parser,
run: function(text, runtime, program) {
var func = new Function("runtime", "program", "var ws = (program && program.data) ? program.data.working_storage : null; var aux = {};\r\n" + 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(); }
};
Expand Down
6 changes: 3 additions & 3 deletions test/compile.js
Expand Up @@ -173,9 +173,9 @@ 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);
assert.ok(text.indexOf('ws.k = aux;') >= 0);
assert.ok(text.indexOf('ws.j = aux;') >= 0);
assert.ok(text.indexOf('var $aux = procedure1();') >= 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('return 1;') >= 0);
assert.ok(text.indexOf('};') >= 0);
Expand Down
2 changes: 1 addition & 1 deletion test/perform.js
Expand Up @@ -65,6 +65,6 @@ assert.equal(ws.result, 4);
// perform factorial with auxiliary parameters

var ws = { result: 0 };
run('perform factorial using 3 giving result. factorial using n, 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.', ws);
run('perform factorial using 3 giving 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.', ws);
assert.equal(ws.result, 6);

0 comments on commit 3d5ab0b

Please sign in to comment.