
var cobs = require('../..'),
    http = require('http'),
    fs = require('fs');

function compileFile(filename) {
    var content = fs.readFileSync(filename).toString();
    var code = cobs.compileTemplate(content);
    var parser = new cobs.Parser(code);
    var program = parser.parseProgram();
    program.text = program.command.compile(program);
    return program;
};

var program = compileFile('./factorial.cobp');

http.createServer(function(req, res) {
    var runtime = {
        display: function() {
            if (arguments && arguments.length)
                for (var k = 0; k < arguments.length; k++)
                    if (arguments[k])
                        res.write(arguments[k].toString());
                        
            res.write('\r\n');
        },
        write: function() {
            if (arguments && arguments.length)
                for (var k = 0; k < arguments.length; k++)
                    if (arguments[k])
                        res.write(arguments[k].toString());
        }
    }
    
    cobs.run(program.text, runtime, program);
    
    res.end();
}).listen(8000);

console.log('Server started, listening at port 8000');

var cobs = require('../..'),
    http = require('http'),
    fs = require('fs');

var program = cobs.compileTemplateFile('./factorial.cobp');

http.createServer(function(req, res) {
    var runtime = {
        display: function() {
            if (arguments && arguments.length)
                for (var k = 0; k < arguments.length; k++)
                    if (arguments[k])
                        res.write(arguments[k].toString());
                        
            res.write('\r\n');
        },
        write: function() {
            if (arguments && arguments.length)
                for (var k = 0; k < arguments.length; k++)
                    if (arguments[k])
                        res.write(arguments[k].toString());
        }
    }
    
    program.run(runtime);
    
    res.end();
}).listen(8000);

console.log('Server started, listening at port 8000');
Refactor template web sample to use compileTemplateFile