Skip to content

Commit

Permalink
Refactor to commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlopez committed Aug 23, 2015
1 parent 63d7e23 commit 52b6705
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
32 changes: 32 additions & 0 deletions lib/commands.js
@@ -0,0 +1,32 @@

function ExpressionCommand(expr) {
this.execute = function (ctx) {
expr.evaluate(ctx);
};
}

function CompositeCommand(cmds) {
this.execute = function (ctx) {
cmds.forEach(function (cmd) {
cmd.execute(ctx);
});
};
}

function ProgramCommand(cmds) {
this.execute = function (ctx) {
ctx.set('$program', new CompositeCommand(cmds));
};
}

function ProcedureCommand(name, argnames, cmds) {
this.execute = function (ctx) {
ctx.set(name, new CompositeCommand(cmds));
};
}

module.exports = {
expression: function (expr) { return new ExpressionCommand(expr); },
program: function (cmds) { return new ProgramCommand(cmds); },
procedure: function (name, argnames, cmds) { return new ProcedureCommand(name, argnames, cmds); }
};
33 changes: 4 additions & 29 deletions lib/parsers.js
Expand Up @@ -2,32 +2,7 @@
var expressions = require('./expressions');
var lexers = require('./lexers');
var TokenType = lexers.TokenType;

function ExpressionCommand(expr) {
this.execute = function (ctx) {
expr.evaluate(ctx);
};
}

function CompositeCommand(cmds) {
this.execute = function (ctx) {
cmds.forEach(function (cmd) {
cmd.execute(ctx);
});
};
}

function ProgramCommand(cmds) {
this.execute = function (ctx) {
ctx.set('$program', new CompositeCommand(cmds));
};
}

function ProcedureCommand(name, argnames, cmds) {
this.execute = function (ctx) {
ctx.set(name, new CompositeCommand(cmds));
};
}
var commands = require('./commands');

function Parser(text) {
var lexer = lexers.lexer(text);
Expand All @@ -54,7 +29,7 @@ function Parser(text) {

tryGetToken(';', TokenType.Delimiter);

return new ExpressionCommand(expr);
return commands.expression(expr);
};

this.parseExpression = function () {
Expand Down Expand Up @@ -130,7 +105,7 @@ function Parser(text) {

getToken('}', TokenType.Delimiter);

return new ProgramCommand(cmds);
return commands.program(cmds);
}

function parseProcedure() {
Expand All @@ -155,7 +130,7 @@ function Parser(text) {

getToken('}', TokenType.Delimiter);

return new ProcedureCommand(name, argnames, cmds);
return commands.procedure(name, argnames, cmds);
}
}

Expand Down

0 comments on commit 52b6705

Please sign in to comment.