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) {
|
function executeCommand(args, std) {
|
||||||
console.log(args, std);
|
console.log(`args: ${args}, std: ${std}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function astSequence() {
|
function astSequence() {
|
||||||
|
@ -21,6 +21,7 @@
|
||||||
let args = [];
|
let args = [];
|
||||||
return {
|
return {
|
||||||
add: a => args.push(a),
|
add: a => args.push(a),
|
||||||
|
size: a => args.length,
|
||||||
execute: (std) => {
|
execute: (std) => {
|
||||||
let evaluatedArgs = args.map(a => a.evaluate());
|
let evaluatedArgs = args.map(a => a.evaluate());
|
||||||
executeCommand(evaluatedArgs, std);
|
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() {
|
function astValueCompound() {
|
||||||
let components = [];
|
let components = [];
|
||||||
return {
|
return {
|
||||||
|
@ -76,9 +87,10 @@
|
||||||
function parseCommands(content) {
|
function parseCommands(content) {
|
||||||
let line = 1;
|
let line = 1;
|
||||||
|
|
||||||
const PS_INIT = 0;
|
const PS_INIT = 0;
|
||||||
const PS_COMMENT = -1;
|
const PS_COMMENT = -1;
|
||||||
const PS_COMMAND = 1;
|
const PS_COMMAND = 1;
|
||||||
|
const PS_ASSIGN = 2;
|
||||||
|
|
||||||
let astRoot = astSequence();
|
let astRoot = astSequence();
|
||||||
let current = null;
|
let current = null;
|
||||||
|
@ -110,7 +122,11 @@
|
||||||
// check for escape and quotes
|
// check for escape and quotes
|
||||||
if (c == ';' || c == '\n') {
|
if (c == ';' || c == '\n') {
|
||||||
if (buffer) {
|
if (buffer) {
|
||||||
current.add(astValueString(buffer));
|
if (buffer[0] == '$') {
|
||||||
|
current.add(astValueVar(buffer.substring(1)));
|
||||||
|
} else {
|
||||||
|
current.add(astValueString(buffer));
|
||||||
|
}
|
||||||
buffer = "";
|
buffer = "";
|
||||||
}
|
}
|
||||||
astRoot.add(current);
|
astRoot.add(current);
|
||||||
|
@ -118,9 +134,39 @@
|
||||||
state = PS_INIT;
|
state = PS_INIT;
|
||||||
} else if (c == ' ' || c == '\t') {
|
} else if (c == ' ' || c == '\t') {
|
||||||
if (buffer) {
|
if (buffer) {
|
||||||
current.add(astValueString(buffer));
|
if (buffer[0] == '$') {
|
||||||
|
current.add(astValueVar(buffer.substring(1)));
|
||||||
|
} else {
|
||||||
|
current.add(astValueString(buffer));
|
||||||
|
}
|
||||||
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 {
|
} else {
|
||||||
buffer += c;
|
buffer += c;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue