very basic support for variables

This commit is contained in:
overflowerror 2022-04-06 22:23:41 +02:00
parent e5679c765d
commit 2680edc356

View file

@ -6,7 +6,7 @@
function executeCommand(args, std) {
console.log(args, std);
console.log(`args: ${args}, std: ${std}`);
}
function astSequence() {
@ -21,6 +21,7 @@
let args = [];
return {
add: a => args.push(a),
size: a => args.length,
execute: (std) => {
let evaluatedArgs = args.map(a => a.evaluate());
executeCommand(evaluatedArgs, std);
@ -28,6 +29,16 @@
};
}
function astAssignment(name) {
let value = astValueString("");
return {
setValue: v => { value = v; },
execute: (std) => {
variables[name] = value.evaluate();
}
};
}
function astValueCompound() {
let components = [];
return {
@ -76,9 +87,10 @@
function parseCommands(content) {
let line = 1;
const PS_INIT = 0;
const PS_COMMENT = -1;
const PS_COMMAND = 1;
const PS_INIT = 0;
const PS_COMMENT = -1;
const PS_COMMAND = 1;
const PS_ASSIGN = 2;
let astRoot = astSequence();
let current = null;
@ -110,7 +122,11 @@
// check for escape and quotes
if (c == ';' || c == '\n') {
if (buffer) {
current.add(astValueString(buffer));
if (buffer[0] == '$') {
current.add(astValueVar(buffer.substring(1)));
} else {
current.add(astValueString(buffer));
}
buffer = "";
}
astRoot.add(current);
@ -118,9 +134,39 @@
state = PS_INIT;
} else if (c == ' ' || c == '\t') {
if (buffer) {
current.add(astValueString(buffer));
if (buffer[0] == '$') {
current.add(astValueVar(buffer.substring(1)));
} else {
current.add(astValueString(buffer));
}
buffer = "";
}
} else if (c == '=' && current.size() == 0) {
current = astAssignment(buffer);
state = PS_ASSIGN;
buffer = "";
} else {
buffer += c;
}
break;
case PS_ASSIGN:
// check for escape and quotes
if (c == ';' || c == '\n') {
if (buffer[0] == "$") {
current.setValue(astValueVar(buffer.substring(1)));
} else {
current.setValue(astValueString(buffer));
}
buffer = "";
astRoot.add(current);
current = null;
state = PS_INIT;
} else if (c == ' ' || c == '\t') {
// would normale set exported variable for command but we don't support that anyway
// current.setValue(astValueString(buffer));
buffer = "";
current = null;
state = PS_INIT;
} else {
buffer += c;
}