mirror of
https://github.com/sigmasternchen/sh.js
synced 2025-03-15 07:28:55 +00:00
very basic support for variables
This commit is contained in:
parent
e5679c765d
commit
2680edc356
1 changed files with 52 additions and 6 deletions
|
@ -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 {
|
||||
|
@ -79,6 +90,7 @@
|
|||
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) {
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue