Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Javascript assignment, can someone help with the two functions? const TARGET_PAT

ID: 672933 • Letter: J

Question

Javascript assignment, can someone help with the two functions?

const TARGET_PAT = /(.*):s*(.*)/,
PHONY_PAT = /.PHONYs*:s*(.*)/,
CMD_PAT = /^ (.*)/,
COMMENT_PAT = /#.*/,
ASSIGN_PAT = /s*(.+?)s*=s*(.+)/,
VAR_PAT = /${(.*?)}/;

var fs = require('fs'),
process = require('process'),
eol = require('os').EOL,
execSync = require('child_process').execSync,
targets,
args,
makeFileName = "Makefile",
vars = {};

/**
* Constructor function for a make target.
* Every target has a name, a list of dependencies (that is, targets that
* must be executed before this target), and a list of commands that must
* be executed when the target is called.
*
* A phony target is invoked any time that it is called, unless it has
* already been invoked for this call to make. Other targets will be called
* only when a file of the same name as the target does not exist, or if
* the timestamp on the file is older than the timestamp of any file dependencies.
*
* Any command beginning with a '-' is tolerant of errors; if an error is encountered,
* subsequent commands should still be executed. If an error is encountered for
* other commands, execution should terminate.
*/
function Target(name, dependencies, commands, phony) {
this.name = name;
this.dependencies = dependencies || [];
this.commands = commands || [];
this.phony = phony || false;
}

// Recursively call every dependency of this target, and then apply
// the commands associated with the target. You may find it worthwhile
// to add arguments to this function -- feel free to do so.
Target.prototype.invoke = function() {
//
// ***YOUR CODE HERE***
//
}

Target.prototype.addCommand = function(cmd) {
//
// ***YOUR CODE HERE***
//
}

// Read in a file and return a hash of targets.
function parse(fileName) {
var i, lines, line, cmd, first, m, deps, target, targetName,
phonies = [],
makeTargets = {};
lines = fs.readFileSync(fileName).toString().split(eol);
for (i=0; i<lines.length; i++) {
line = lines[i].replace(COMMENT_PAT, '');
if (line.match(PHONY_PAT)) {
phonies = line.match(PHONY_PAT)[1].split(/s+/);
} else if (line.match(ASSIGN_PAT)) {
m = line.match(ASSIGN_PAT);
vars[m[1]] = m[2];
} else if (line.match(CMD_PAT)) {
if (!target) {
console.error(fileName + ":" + (i+1) +
": *** commands commence before first target. Stop.");
process.exit(1);
}
cmd = line.match(CMD_PAT)[1];
target.addCommand(cmd);
} else if (line.match(TARGET_PAT)) {
// Add previous target and start a new one
if (target) makeTargets[target.name] = target;
m = line.match(TARGET_PAT);
targetName = m[1];
deps = m[2].split(/s+/);
if (!first) first = targetName;
target = new Target(targetName, deps);
}
}
if (target) makeTargets[target.name] = target;
else {
console.error("jsmake: *** No targets. Stop.");
process.exit(1);
}
return [makeTargets, first];
}

// Handling command-line arguments
args = process.argv
if (args[0].match(/node/)) args.shift(); // Drop node command
args.shift(); // drop script name
if (args[0] === '-f') {
args.shift(); // drop '-f'
makeFileName = args.shift();
}

fs.exists(makeFileName, function(exists) {
var first, res, i, t;
if (exists) {
res = parse(makeFileName);
targets = res[0];
first = res[1];
if (args.length === 0) {
targets[first].invoke();
} else {
for (i in args) {
t = args[i];
if (targets.hasOwnProperty(t)) targets[t].invoke();
else console.error("jsmake: *** No rule to make target `" + t + "'. Stop.");
}
}
} else {
console.error('jsmake: *** No targets specified and no makefile found. Stop.');
}
});

Explanation / Answer

I updated the code. Essentially, now able to get the program to create the Employee.class file

const TARGET_PAT = /(.*):s*(.*)/,
PHONY_PAT = /.PHONYs*:s*(.*)/,
CMD_PAT = /^ (.*)/,
COMMENT_PAT = /#.*/,
ASSIGN_PAT = /s*(.+?)s*=s*(.+)/,
VAR_PAT = /${(.*?)}/;

var fs = require('fs'),
process = require('process'),
eol = require('os').EOL,
execSync = require('child_process').execSync,
targets,
args,
makeFileName = "Makefile",
vars = {};

/**
* Constructor function for a make target.
* Every target has a name, a list of dependencies (that is, targets that
* must be executed before this target), and a list of commands that must
* be executed when the target is called.
*
* A phony target is invoked any time that it is called, unless it has
* already been invoked for this call to make. Other targets will be called
* only when a file of the same name as the target does not exist, or if
* the timestamp on the file is older than the timestamp of any file dependencies.
*
* Any command beginning with a '-' is tolerant of errors; if an error is encountered,
* subsequent commands should still be executed. If an error is encountered for
* other commands, execution should terminate.
*/
function Target(name, dependencies, commands, phony) {
this.name = name;
this.dependencies = dependencies || [];
this.commands = commands || [];
this.phony = phony || false;
}

// Recursively call every dependency of this target, and then apply
// the commands associated with the target. You may find it worthwhile
// to add arguments to this function -- feel free to do so.
Target.prototype.invoke = function() { //name, dependencies, commands, phony
//
// ***YOUR CODE HERE***
//
console.log("this.dependencies = " + this.dependencies);
console.log("this.dependencies.length = " + this.dependencies.length);
if(this.dependencies.length == 0) return this;
for(var i = 0; i < this.dependencies.length; i++){
console.log("this.commands = " + this.commands);
console.log(execSync(this.commands));
//this.invoke(this.dependencies.slice(i + 1, this.dependencies.length));
}
}

Target.prototype.addCommand = function(cmd) {
//
// ***YOUR CODE HERE***
//
//this.invoke(cmd);
//cmd.execute();
console.log("cmd here = " + cmd);
this.commands = cmd;
}

// Read in a file and return a hash of targets.
function parse(fileName) {
var i, lines, line, cmd, first, m, deps, target, targetName,
phonies = [],
makeTargets = {};
lines = fs.readFileSync(fileName).toString().split(eol);
for (i=0; i<lines.length; i++) {
line = lines[i].replace(COMMENT_PAT, '');
if (line.match(PHONY_PAT)) {
phonies = line.match(PHONY_PAT)[1].split(/s+/);
} else if (line.match(ASSIGN_PAT)) {
m = line.match(ASSIGN_PAT);
vars[m[1]] = m[2];
} else if (line.match(CMD_PAT)) {
if (!target) {
console.error(fileName + ":" + (i+1) +
": *** commands commence before first target. Stop.");
process.exit(1);
}
cmd = line.match(CMD_PAT)[1];
console.log("cmd = " + cmd);
target.addCommand(cmd);
} else if (line.match(TARGET_PAT)) {
// Add previous target and start a new one
if (target) makeTargets[target.name] = target;
m = line.match(TARGET_PAT);
targetName = m[1];
deps = m[2].split(/s+/);
if (!first) first = targetName;
target = new Target(targetName, deps); //constructor call
}
}
if (target) makeTargets[target.name] = target;
else {
console.error("jsmake: *** No targets. Stop.");
process.exit(1);
}
return [makeTargets, first];
}

// Handling command-line arguments
args = process.argv
if (args[0].match(/node/)) args.shift(); // Drop node command
args.shift(); // drop script name
if (args[0] === '-f') {
args.shift(); // drop '-f'
makeFileName = args.shift();
}

fs.exists(makeFileName, function(exists) {
var first, res, i, t;
if (exists) {
res = parse(makeFileName);
targets = res[0];
first = res[1];
console.log("first = " + first);
if (args.length === 0) {
console.log("targets[first] = " + targets[first]);
targets[first].invoke();
} else {
for (i in args) {
t = args[i];
console.log("t = " + t);
if (targets.hasOwnProperty(t)) targets[t].invoke();
else console.error("jsmake: *** No rule to make target `" + t + "'. Stop.");
}
}
} else {
console.error('jsmake: *** No targets specified and no makefile found. Stop.');
}
});

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote