almost have C
This commit is contained in:
1 parent
53990297e8
commit
708e63e36e
10 files changed
+171
-111
No files matched your search
Vendored
+1
-1
@@ -104,7 +104,7 @@
|
||||
"request": "launch",
|
||||
"program": "C:/Program Files/Microsoft Office/Office14/EXCEL.EXE",
|
||||
"args": [
|
||||
"${workspaceRoot}/build/Debug/bin/net471/spark_excel-addin64-packed.xll"
|
||||
"${workspaceRoot}/build/Debug/bin/net471/spark_excel-addin64.xll"
|
||||
],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"stopAtEntry": false,
|
||||
|
||||
@@ -216,9 +216,20 @@ namespace bts.excel
|
||||
/// <returns>Unique key</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return $"'{SheetName}'!{LocalAddress}";
|
||||
if(!string.IsNullOrEmpty(SheetName))
|
||||
return $"'{SheetName}'!{LocalAddress}";
|
||||
else
|
||||
return $"{LocalAddress}";
|
||||
}
|
||||
|
||||
public string to_name()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(SheetName))
|
||||
return $"{SheetName}_{LocalAddress}".ToLowerInvariant();
|
||||
else
|
||||
return $"{LocalAddress}".ToLowerInvariant();
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return ToString().GetHashCode();
|
||||
|
||||
@@ -4,7 +4,6 @@ using ExcelDna.Integration;
|
||||
using spark_excel.grammar;
|
||||
|
||||
using Antlr4.Runtime;
|
||||
using Antlr4.Runtime.Tree;
|
||||
|
||||
namespace bts.excel
|
||||
{
|
||||
@@ -37,13 +36,13 @@ namespace bts.excel
|
||||
public class Generate
|
||||
{
|
||||
[ExcelFunction(IsMacroType = true)]
|
||||
static public string bts_generate_code_from_sink(string name, [ExcelArgument(AllowReference=true)] object sink)
|
||||
static public string bts_generate_code_from_sink([ExcelArgument(AllowReference=true)] object sink)
|
||||
{
|
||||
ExcelReference rref = (ExcelReference)sink;
|
||||
var formula = XlCall.Excel(XlCall.xlfGetCell, 41, rref);
|
||||
|
||||
string sheetname = (string)XlCall.Excel(XlCall.xlSheetNm, rref);
|
||||
//var formabs = XlCall.Excel(XlCall.xlfFormulaConvert, form, ExcelDna.
|
||||
var sheetid = rref.SheetId;
|
||||
|
||||
var antlrstream = new AntlrInputStream(formula.ToString());
|
||||
var lexer = new ExcelFormulaLexer(antlrstream);
|
||||
@@ -59,10 +58,13 @@ namespace bts.excel
|
||||
throw new ExcelFormulaException(error_listener.ErrorLocation, error_listener.ErrorMessage);
|
||||
|
||||
//AbstractParseTreeVisitor
|
||||
PreGeneratorVisitor visitor = new PreGeneratorVisitor(rref, sheetname);
|
||||
GeneratorVisitor visitor = new GeneratorVisitor(rref, sheetname, false);
|
||||
object val = visitor.VisitMain(parsetree);
|
||||
|
||||
return val.ToString();
|
||||
string ret = string.Empty;
|
||||
visitor.get_defs().ForEach(x => ret += x + Environment.NewLine);
|
||||
ret += val.ToString();
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,91 +1,104 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
using spark_excel.grammar;
|
||||
using Antlr4.Runtime;
|
||||
|
||||
using ExcelDna.Integration;
|
||||
using Excel = Microsoft.Office.Interop.Excel;
|
||||
|
||||
namespace bts.excel
|
||||
{
|
||||
internal class PreGeneratorVisitor : ExcelFormulaBaseVisitor<object>
|
||||
internal class GeneratorVisitor : ExcelFormulaBaseVisitor<object>
|
||||
{
|
||||
ExcelReference _ref;
|
||||
string _sheetname;
|
||||
string _sheetname, _simple_sheetname;
|
||||
bool _factor;
|
||||
functions.lib.FunctionLibrary _library = new functions.lib.FunctionLibrary();
|
||||
public PreGeneratorVisitor(ExcelReference rref, string sheetname)
|
||||
ExcelFormulaErrorListener _error_listener = new ExcelFormulaErrorListener();
|
||||
List<string> _defs = new List<string>();
|
||||
|
||||
public GeneratorVisitor(ExcelReference rref, string sheetname, bool factor)
|
||||
{
|
||||
_ref = rref;
|
||||
_sheetname = sheetname;
|
||||
_factor = factor;
|
||||
int open = _sheetname.IndexOf('[');
|
||||
int close = _sheetname.IndexOf(']', open + 1);
|
||||
_simple_sheetname = _sheetname.Substring(0, open) + _sheetname.Substring(close+1);
|
||||
|
||||
_library.add_standard_functions();
|
||||
}
|
||||
|
||||
public List<string> get_defs()
|
||||
{
|
||||
return _defs.Distinct().ToList();
|
||||
}
|
||||
|
||||
#region Context visitors overrides
|
||||
|
||||
public override object VisitMain(ExcelFormulaParser.MainContext context)
|
||||
public override object VisitCell(ExcelFormulaParser.CellContext context)
|
||||
{
|
||||
return context.expr().Accept(this);
|
||||
return context.@ref().Accept(this);
|
||||
}
|
||||
|
||||
public override object VisitCellRef(ExcelFormulaParser.CellRefContext context)
|
||||
{
|
||||
var colRow = context.colRow.Text;
|
||||
var (col1, row1) = CellRef.ConvertAddressToCoord(colRow);
|
||||
var sheetname = context.sheet?.Text ?? _simple_sheetname;
|
||||
|
||||
var rref = new ExcelReference(row1 - 1, row1 - 1, col1 - 1, col1 - 1, sheetname);
|
||||
return process_cell(rref, sheetname);
|
||||
}
|
||||
|
||||
public override object VisitMulDiv(ExcelFormulaParser.MulDivContext context)
|
||||
public override object VisitName(ExcelFormulaParser.NameContext context)
|
||||
{
|
||||
var expr = context.expr();
|
||||
|
||||
var arg1 = expr[0].Accept(this);
|
||||
if (arg1 is ErrorValue)
|
||||
return arg1;
|
||||
|
||||
var arg2 = expr[1].Accept(this);
|
||||
if (arg2 is ErrorValue)
|
||||
return arg2;
|
||||
|
||||
string code = $"{arg1} {context.op.Text} {arg2}\n";
|
||||
return code;
|
||||
var name = context.GetText();
|
||||
var lookup = name;
|
||||
if(!lookup.Contains("!"))
|
||||
lookup = "!" + lookup;
|
||||
object value = XlCall.Excel(XlCall.xlfGetName, lookup);
|
||||
if(value != null && !(value is ExcelError))
|
||||
{
|
||||
string[] par = value.ToString().Split(new[] {'=', 'R', 'C'}, StringSplitOptions.RemoveEmptyEntries);
|
||||
var cellref = new CellRef(_simple_sheetname, (int.Parse(par[1]), int.Parse(par[0])));
|
||||
_defs.Add($"XLVal& {name} = {cellref.to_name()} ;");
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
public override object VisitAddSub(ExcelFormulaParser.AddSubContext context)
|
||||
|
||||
private object process_cell(ExcelReference rref, string sheetname)
|
||||
{
|
||||
var expr = context.expr();
|
||||
var formula = XlCall.Excel(XlCall.xlfGetCell, 41, rref);
|
||||
|
||||
var formstring = formula.ToString();
|
||||
if(string.IsNullOrEmpty(formstring))
|
||||
formstring = "0.0";
|
||||
if (!formstring.StartsWith("="))
|
||||
formstring = "=" + formstring;
|
||||
|
||||
var arg1 = expr[0].Accept(this);
|
||||
if (arg1 is ErrorValue)
|
||||
return arg1;
|
||||
var cellref = new CellRef(sheetname, (rref.ColumnFirst + 1, rref.RowFirst + 1));
|
||||
string refname = cellref.to_name();
|
||||
// var cellname = XlCall.Excel(XlCall.xlfGetDef, rref);
|
||||
// if (cellname is ExcelError)
|
||||
// cellname = null;
|
||||
// if(cellname != null)
|
||||
// refname = cellname.ToString();
|
||||
|
||||
var arg2 = expr[1].Accept(this);
|
||||
if (arg2 is ErrorValue)
|
||||
return arg2;
|
||||
|
||||
string code = $"{arg1} {context.op.Text} {arg2}\n";
|
||||
return code;
|
||||
var antlrstream = new AntlrInputStream(formstring);
|
||||
var lexer = new ExcelFormulaLexer(antlrstream);
|
||||
var tokens = new CommonTokenStream(lexer);
|
||||
var parser = new ExcelFormulaParser(tokens);
|
||||
parser.AddErrorListener(_error_listener);
|
||||
var parsetree = parser.main();
|
||||
if (!_error_listener.IsValid)
|
||||
throw new ExcelFormulaException(_error_listener.ErrorLocation, _error_listener.ErrorMessage);
|
||||
|
||||
_defs.Add($"XLVal {refname} = {this.VisitMain(parsetree)} ;");
|
||||
return $"( {refname} ) ";
|
||||
}
|
||||
|
||||
public override object VisitPercentage(ExcelFormulaParser.PercentageContext context)
|
||||
{
|
||||
var expr = context.expr();
|
||||
|
||||
var arg1 = expr.Accept(this);
|
||||
if (arg1 is ErrorValue)
|
||||
return arg1;
|
||||
|
||||
return $"{arg1} / 100.0\n";
|
||||
}
|
||||
|
||||
public override object VisitRelational(ExcelFormulaParser.RelationalContext context)
|
||||
{
|
||||
var expr = context.expr();
|
||||
|
||||
var arg1 = expr[0].Accept(this);
|
||||
if (arg1 is ErrorValue)
|
||||
return arg1;
|
||||
|
||||
var arg2 = expr[1].Accept(this);
|
||||
if (arg2 is ErrorValue)
|
||||
return arg2;
|
||||
|
||||
return $"{arg1} {context.op.Text} {arg2}\n";
|
||||
}
|
||||
|
||||
public override object VisitCellList(ExcelFormulaParser.CellListContext context)
|
||||
{
|
||||
var result = new List<CellRef>();
|
||||
@@ -112,8 +125,72 @@ namespace bts.excel
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public override object VisitMain(ExcelFormulaParser.MainContext context)
|
||||
{
|
||||
return context.expr().Accept(this);
|
||||
}
|
||||
|
||||
public override object VisitMulDiv(ExcelFormulaParser.MulDivContext context)
|
||||
{
|
||||
var expr = context.expr();
|
||||
|
||||
var arg1 = expr[0].Accept(this);
|
||||
if (arg1 is ErrorValue)
|
||||
return arg1;
|
||||
|
||||
var arg2 = expr[1].Accept(this);
|
||||
if (arg2 is ErrorValue)
|
||||
return arg2;
|
||||
|
||||
string code = $"{arg1} {context.op.Text} {arg2}";
|
||||
return code;
|
||||
}
|
||||
|
||||
public override object VisitAddSub(ExcelFormulaParser.AddSubContext context)
|
||||
{
|
||||
var expr = context.expr();
|
||||
|
||||
var arg1 = expr[0].Accept(this);
|
||||
if (arg1 is ErrorValue)
|
||||
return arg1;
|
||||
|
||||
var arg2 = expr[1].Accept(this);
|
||||
if (arg2 is ErrorValue)
|
||||
return arg2;
|
||||
|
||||
string code = $"{arg1} {context.op.Text} {arg2}";
|
||||
return code;
|
||||
}
|
||||
|
||||
public override object VisitPercentage(ExcelFormulaParser.PercentageContext context)
|
||||
{
|
||||
var expr = context.expr();
|
||||
|
||||
var arg1 = expr.Accept(this);
|
||||
if (arg1 is ErrorValue)
|
||||
return arg1;
|
||||
|
||||
return $"( {arg1} / 100.0 )";
|
||||
}
|
||||
|
||||
public override object VisitRelational(ExcelFormulaParser.RelationalContext context)
|
||||
{
|
||||
var expr = context.expr();
|
||||
|
||||
var arg1 = expr[0].Accept(this);
|
||||
if (arg1 is ErrorValue)
|
||||
return arg1;
|
||||
|
||||
var arg2 = expr[1].Accept(this);
|
||||
if (arg2 is ErrorValue)
|
||||
return arg2;
|
||||
|
||||
return $"{arg1} {context.op.Text} {arg2}";
|
||||
}
|
||||
|
||||
|
||||
public override object VisitError(ExcelFormulaParser.ErrorContext context)
|
||||
{
|
||||
return new ErrorValue(context.GetText());
|
||||
@@ -125,11 +202,6 @@ namespace bts.excel
|
||||
return str.Substring(1, str.Length - 2);
|
||||
}
|
||||
|
||||
public override object VisitRange(ExcelFormulaParser.RangeContext context)
|
||||
{
|
||||
return context.rng().Accept(this);
|
||||
}
|
||||
|
||||
public override object VisitUnary(ExcelFormulaParser.UnaryContext context)
|
||||
{
|
||||
var expr = context.expr();
|
||||
@@ -139,12 +211,6 @@ namespace bts.excel
|
||||
return expr.Accept(this);
|
||||
}
|
||||
|
||||
public override object VisitName(ExcelFormulaParser.NameContext context)
|
||||
{
|
||||
var name = context.GetText();
|
||||
return name;
|
||||
}
|
||||
|
||||
public override object VisitFunction(ExcelFormulaParser.FunctionContext context)
|
||||
{
|
||||
var name = context.name.Text;
|
||||
@@ -168,6 +234,8 @@ namespace bts.excel
|
||||
public override object VisitNumber(ExcelFormulaParser.NumberContext context)
|
||||
{
|
||||
var num = context.GetText();
|
||||
if(double.TryParse(num, out double res))
|
||||
return res.ToString("0.0############");
|
||||
return num;
|
||||
}
|
||||
|
||||
@@ -180,6 +248,11 @@ namespace bts.excel
|
||||
return $"{arg1}+{arg2}";
|
||||
}
|
||||
|
||||
public override object VisitRange(ExcelFormulaParser.RangeContext context)
|
||||
{
|
||||
return context.rng().Accept(this);
|
||||
}
|
||||
|
||||
public override object VisitRangeList(ExcelFormulaParser.RangeListContext context)
|
||||
{
|
||||
var result = new List<RangeRef>();
|
||||
@@ -207,31 +280,6 @@ namespace bts.excel
|
||||
return result;
|
||||
}
|
||||
|
||||
public override object VisitCell(ExcelFormulaParser.CellContext context)
|
||||
{
|
||||
return context.@ref().Accept(this);
|
||||
}
|
||||
|
||||
public override object VisitCellRef(ExcelFormulaParser.CellRefContext context)
|
||||
{
|
||||
// Column and row number for the cell.
|
||||
var colRow = context.colRow.Text;
|
||||
var (col1, row1) = CellRef.ConvertAddressToCoord(colRow);
|
||||
|
||||
var cellSheetName = context.sheet?.Text;
|
||||
|
||||
// Remove '!' from end of name.
|
||||
cellSheetName = cellSheetName?.Substring(0, cellSheetName.Length - 1);
|
||||
|
||||
// Remove possible ' quotes from sheet name.
|
||||
if (!string.IsNullOrEmpty(cellSheetName) && cellSheetName[0] == '\'')
|
||||
{
|
||||
cellSheetName = cellSheetName.Substring(1, cellSheetName.Length - 2);
|
||||
}
|
||||
|
||||
return new CellRef(cellSheetName, (col1, row1));
|
||||
}
|
||||
|
||||
public override object VisitRangeRef(ExcelFormulaParser.RangeRefContext context)
|
||||
{
|
||||
var obj = context.@ref().Accept(this);
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace bts.excel.functions.lib
|
||||
|
||||
if (arg is ErrorValue)
|
||||
return arg;
|
||||
code += arg + " && \n";
|
||||
code += arg + " && " + Environment.NewLine;
|
||||
}
|
||||
code += "true";
|
||||
return code;
|
||||
|
||||
@@ -20,17 +20,17 @@ namespace bts.excel.functions.lib
|
||||
|
||||
var numbers = new List<double>();
|
||||
|
||||
string code = "auto avg = 0.0; \n";
|
||||
string code = "auto avg = 0.0; " + Environment.NewLine;
|
||||
int count = 0;
|
||||
foreach (var arg in args)
|
||||
{
|
||||
var argv = arg.evaluate();
|
||||
if (argv is ErrorValue)
|
||||
return argv;
|
||||
code += $"avg += {argv};\n";
|
||||
code += $"avg += {argv};" + Environment.NewLine;
|
||||
}
|
||||
if(count > 0)
|
||||
code += "avg /= {count};\n";
|
||||
code += "avg /= {count};" + Environment.NewLine;
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,11 +23,11 @@ namespace bts.excel.functions.lib
|
||||
string code = string.Empty;
|
||||
if (args.Length == 3)
|
||||
{
|
||||
code += $"( {arg} ? { args[1].evaluate() } : {args[2].evaluate() };";
|
||||
code += $"( {arg} ? { args[1].evaluate() } : {args[2].evaluate()} ) ";
|
||||
}
|
||||
else
|
||||
{
|
||||
code += $"( {arg} ? { args[1].evaluate() } : false;";
|
||||
code += $"( {arg} ? { args[1].evaluate() } : false) ";
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace bts.excel.functions.lib
|
||||
if (arg is ErrorValue)
|
||||
return arg;
|
||||
|
||||
string code = $"std::log({arg});\n";
|
||||
string code = $"log({arg});";
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace bts.excel.functions.lib
|
||||
if (arg is ErrorValue)
|
||||
return arg;
|
||||
|
||||
string code = $"std::log10({arg});\n";
|
||||
string code = $"std::log10({arg});";
|
||||
return code;
|
||||
|
||||
case 2:
|
||||
|
||||
@@ -45,7 +45,6 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="StringTemplate4" Version="4.0.8" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in new issue
Block a user