123 lines
3.8 KiB
C#
123 lines
3.8 KiB
C#
namespace Bts.DataStreamExpressions;
|
|
|
|
using BCG;
|
|
|
|
using System.Collections;
|
|
using System.Collections.Immutable;
|
|
|
|
public class PrimitiveExpression : StatementExpression {
|
|
private string myName;
|
|
IList<Argument> myArgs;
|
|
|
|
public PrimitiveExpression(string name) {
|
|
myName = name;
|
|
myArgs = new List<Argument>(0);
|
|
}
|
|
|
|
public PrimitiveExpression(string name, ImmutableList<Argument> args) {
|
|
myName = name;
|
|
myArgs = args;
|
|
}
|
|
|
|
public override string print(int startCol, IList<string> activeBindings) {
|
|
string prelude = "new " + PrimitiveManager.instance.find(myName) + " (";
|
|
|
|
return prelude + myPrintArgs(startCol + prelude.Length, activeBindings) + ")";
|
|
}
|
|
|
|
public override string printView(int startCol) {
|
|
string prelude = myName + " (";
|
|
|
|
return prelude + myPrintViewArgs(startCol + prelude.Length) + ")";
|
|
}
|
|
|
|
public override ViewStream myGetView(IDictionary<string, StatementExpression> env) {
|
|
ArrayList resolvedArgs = new ArrayList();
|
|
|
|
foreach (var arg in myArgs) {
|
|
if (arg.StatementExp is not null) {
|
|
resolvedArgs.Add(arg.StatementExp.myGetView(env));
|
|
continue;
|
|
}
|
|
if (arg.QuotedView is not null) {
|
|
resolvedArgs.Add(arg.QuotedView.Value);
|
|
continue;
|
|
}
|
|
resolvedArgs.Add(arg.Obj);
|
|
}
|
|
return PrimitiveManager.instance.build(myName, resolvedArgs);
|
|
}
|
|
|
|
string myPrintSingleArg(int atColumn, object? o, IList<string> activeBindings) {
|
|
/// Unwrap <see cref="Argument"/> to get its underlying value
|
|
while (o is Argument arg) {
|
|
o = arg.Obj;
|
|
// Safeguard against infinite loop
|
|
if (ReferenceEquals(o, arg)) {
|
|
throw new ArgumentException(
|
|
$"Detected circular reference in argument " +
|
|
$"('{nameof(arg)}.{nameof(arg.Obj)}' is '{nameof(arg)}' itself)");
|
|
}
|
|
}
|
|
|
|
string result = new string(' ', atColumn);
|
|
if (o is null) {
|
|
return result;
|
|
}
|
|
if (o is StatementExpression)
|
|
return new string(' ', atColumn) +
|
|
((o as StatementExpression)?.print(atColumn, activeBindings) ?? string.Empty);
|
|
else {
|
|
if (o is string)
|
|
result += makeCSString((string)o);
|
|
else if (o is DateTime)
|
|
result += "DateTime.parse(\"" + ((DateTime)o).ToString() + "\")";
|
|
else if (o is QuotedView)
|
|
result += "\"" + ((QuotedView)o).Value + "\"";
|
|
else if (o is bool)
|
|
result += o.Equals(true) ? "true" : "false";
|
|
else
|
|
result += o.ToString();
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
string myPrintArgs(int atColumn, IList<string> activeBindings) {
|
|
string result = "";
|
|
for (int i = 0; i < myArgs.Count; i++)
|
|
if (i == 0)
|
|
result += myPrintSingleArg(0, myArgs[i], activeBindings);
|
|
else
|
|
result += ",\n" + myPrintSingleArg(atColumn, myArgs[i], activeBindings);
|
|
return result;
|
|
}
|
|
|
|
string myPrintViewArgs(int atColumn) {
|
|
string result = "";
|
|
for (int i = 0; i < myArgs.Count; i++)
|
|
if (i == 0)
|
|
result += myPrintSingleViewArg(0, myArgs[i]);
|
|
else
|
|
result += ",\n" + myPrintSingleViewArg(atColumn, myArgs[i]);
|
|
return result;
|
|
}
|
|
|
|
string myPrintSingleViewArg(int atColumn, object o) {
|
|
if (o is StatementExpression exp) {
|
|
return new string(' ', atColumn) + exp.printView(atColumn);
|
|
}
|
|
var result = new string(' ', atColumn);
|
|
if (o is string str) {
|
|
result += new CSharpString(str).ToString();
|
|
} else if (o is QuotedView quotedView) {
|
|
result += "[" + quotedView.Value + "]";
|
|
} else if (o is bool b) {
|
|
result += b ? "true" : "false";
|
|
} else {
|
|
result += o.ToString();
|
|
}
|
|
return result;
|
|
}
|
|
}
|