From 3d5ab0be5e9825726c202ba18fb24a1d5ba0526f Mon Sep 17 00:00:00 2001 From: ajlopez Date: Mon, 24 Dec 2012 10:50:16 -0300 Subject: [PATCH] Locals in procedure with test --- lib/cobolscript.js | 60 +++++++++++++++++++++++++++++++++------------- test/compile.js | 6 ++--- test/perform.js | 2 +- 3 files changed, 48 insertions(+), 20 deletions(-) diff --git a/lib/cobolscript.js b/lib/cobolscript.js index 3825a9d..476fdf4 100644 --- a/lib/cobolscript.js +++ b/lib/cobolscript.js @@ -20,7 +20,7 @@ var cobolscript = (function() { "or", "and", "is", "not", "then", "advancing", - "giving", "using", + "giving", "using", "locals", "local", "than", "varying", "division", "section", @@ -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 += '};'; @@ -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; }; @@ -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) @@ -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;'; } } @@ -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) { @@ -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; @@ -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(); } }; diff --git a/test/compile.js b/test/compile.js index 167ac98..7214770 100644 --- a/test/compile.js +++ b/test/compile.js @@ -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); diff --git a/test/perform.js b/test/perform.js index 94ada1f..25b1df9 100644 --- a/test/perform.js +++ b/test/perform.js @@ -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);