created namespace for ast related constructors

This commit is contained in:
overflowerror 2022-04-11 20:29:02 +02:00
parent 741ff1c347
commit 333eb79820

View file

@ -4,12 +4,12 @@
var functions = [];
var file = "[anonymous]";
function executeCommand(args, std) {
console.log(`args: ${args}, std: ${std}`);
}
function astSequence() {
const ast = {
sequence: function() {
let commands = [];
return {
add: c => commands.push(c),
@ -21,9 +21,9 @@
.map(a => a.join("\n"))
.join(",\n") + "\n}",
}
}
},
function astCommand() {
command: function() {
let args = [];
return {
add: a => args.push(a),
@ -39,10 +39,10 @@
.map(a => a.join("\n"))
.join(",\n") + "\n}",
};
}
},
function astAssignment(name) {
let value = astValueString("");
assignment: function(name) {
let value = ast.value.string("");
return {
setValue: v => { value = v; },
execute: (std) => {
@ -50,9 +50,10 @@
},
toString: () => `assign '${name}'=\n${value.toString().split('\n').map(l => " " + l).join("\n")}`,
};
}
},
function astValueCompound() {
value: {
compound: function() {
let components = [];
const self = {
add: c => components.push(c),
@ -76,39 +77,41 @@
},
};
return self;
}
},
function astValueString(str) {
string: function(str) {
return {
evaluate: () => str,
toString: () => "'" + str + "'",
};
}
},
function astValueVar(name) {
variable: function(name) {
return {
// add support for multiple arguments in one variable
evaluate: () => variables[name],
toString: () => `var '${name}'`,
}
}
},
function astValueCommandSubstitution(ast) {
commandSubstitution: function(ast) {
return {
evaluate: () => {
// TODO
},
toString: () => "not implemented",
};
}
},
function astValueProcessSubstitution(ast) {
processSubstitution: function(ast) {
return {
evaluate: () => {
// TODO
},
toString: () => "not implemented",
};
},
},
}
function panic(line, message) {
@ -172,7 +175,7 @@
function doubleQuoteToAst(quoteContent, line) {
const length = quoteContent.length;
let astRoot = astValueCompound();
let astRoot = ast.value.compound();
let buffer = "";
@ -189,7 +192,7 @@
switch(state) {
case QS_INIT:
if (c == '$') {
astRoot.add(astValueString(buffer));
astRoot.add(ast.value.string(buffer));
buffer = "";
if (i < length - 1 && quoteContent[i + 1] == '(') {
state = QS_SUBSTITUTION;
@ -203,7 +206,7 @@
break;
case QS_VARIABLE:
if (!("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".includes(c))) {
astRoot.add(astValueVar(buffer));
astRoot.add(ast.value.variable(buffer));
buffer = "";
state = QS_INIT;
i--;
@ -223,10 +226,10 @@
if (buffer) {
switch(state) {
case QS_INIT:
astRoot.add(astValueString(buffer));
astRoot.add(ast.value.string(buffer));
break;
case QS_VARIABLE:
astRoot.add(astValueVar(buffer));
astRoot.add(ast.value.variable(buffer));
break;
case QS_SUBSTITUTION:
throw "not implemented";
@ -248,7 +251,7 @@
const PS_COMMAND = 1;
const PS_ASSIGN = 2;
let astRoot = astSequence();
let astRoot = ast.sequence();
let current = null;
let value = null;
@ -265,7 +268,7 @@
} else if (c == '#') {
state = PS_COMMENT;
} else {
current = astCommand();
current = ast.command();
buffer = c;
state = PS_COMMAND;
}
@ -281,9 +284,9 @@
if (value) {
if (buffer) {
if (buffer[0] == '$') {
value.add(astValueVar(buffer.substring(1)));
value.add(ast.value.variable(buffer.substring(1)));
} else {
value.add(astValueString(buffer.replaceAll('\\$', '$')));
value.add(ast.value.string(buffer.replaceAll('\\$', '$')));
}
buffer = "";
}
@ -291,9 +294,9 @@
value = null;
} else if (buffer) {
if (buffer[0] == '$') {
current.add(astValueVar(buffer.substring(1)));
current.add(ast.value.variable(buffer.substring(1)));
} else {
current.add(astValueString(buffer.replaceAll('\\$', '$')));
current.add(ast.value.string(buffer.replaceAll('\\$', '$')));
}
buffer = "";
}
@ -304,9 +307,9 @@
if (value) {
if (buffer) {
if (buffer[0] == '$') {
value.add(astValueVar(buffer.substring(1)));
value.add(ast.value.variable(buffer.substring(1)));
} else {
value.add(astValueString(buffer.replaceAll('\\$', '$')));
value.add(ast.value.string(buffer.replaceAll('\\$', '$')));
}
buffer = "";
}
@ -314,26 +317,26 @@
value = null;
} else if (buffer) {
if (buffer[0] == '$') {
current.add(astValueVar(buffer.substring(1)));
current.add(ast.value.variable(buffer.substring(1)));
} else {
current.add(astValueString(buffer.replaceAll('\\$', '$')));
current.add(ast.value.string(buffer.replaceAll('\\$', '$')));
}
buffer = "";
}
} else if (c == '"') {
if (!value) {
value = astValueCompound();
value = ast.value.compound();
}
if (buffer) {
value.add(astValueString(buffer));
value.add(ast.value.string(buffer));
buffer = "";
}
const end = findSymbolInScope(content, line, '"', i + 1, length);
const [ast, _line] = doubleQuoteToAst(content.substring(i + 1, end), line);
const [_ast, _line] = doubleQuoteToAst(content.substring(i + 1, end), line);
line = _line;
value.add(ast);
value.add(_ast);
i = end;
} else if (c == "'") {
@ -345,7 +348,7 @@
}
i = end;
} else if (c == '=' && current.size() == 0) {
current = astAssignment(buffer);
current = ast.assignment(buffer);
state = PS_ASSIGN;
buffer = "";
} else {
@ -358,21 +361,21 @@
if (value) {
if (buffer) {
if (buffer[0] == "$") {
value.add(astValueVar(buffer.substring(1)));
value.add(ast.value.variable(buffer.substring(1)));
} else {
value.add(astValueString(buffer));
value.add(ast.value.string(buffer));
}
}
current.setValue(value.reduce());
value = null;
} else if (buffer) {
if (buffer[0] == "$") {
current.setValue(astValueVar(buffer.substring(1)));
current.setValue(ast.value.variable(buffer.substring(1)));
} else {
current.setValue(astValueString(buffer));
current.setValue(ast.value.string(buffer));
}
} else {
current.setValue(astValueString(""));
current.setValue(ast.value.string(""));
}
buffer = "";
astRoot.add(current);
@ -380,18 +383,18 @@
state = PS_INIT;
} else if (c == '"') {
if (!value) {
value = astValueCompound();
value = ast.value.compound();
}
if (buffer) {
value.add(astValueString(buffer));
value.add(ast.value.string(buffer));
buffer = "";
}
const end = findSymbolInScope(content, line, '"', i + 1, length);
const [ast, _line] = doubleQuoteToAst(content.substring(i + 1, end), line);
const [_ast, _line] = doubleQuoteToAst(content.substring(i + 1, end), line);
line = _line;
value.add(ast);
value.add(_ast);
i = end;
} else if (c == "'") {