public static FormulaValue Today(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, FormulaValue[] args) { // $$$ timezone? var date = DateTime.Today; return(new DateValue(irContext, date)); }
private static IEnumerable <DValue <RecordValue> > LazyFilter(EvalVisitor runner, SymbolContext context, IEnumerable <DValue <RecordValue> > sources, LambdaFormulaValue filter) { foreach (var row in sources) { if (row.IsValue) { var childContext = context.WithScopeValues(row.Value); // Filter evals to a boolean var result = filter.Eval(runner, childContext); bool include = false; if (result is BooleanValue booleanValue) { include = booleanValue.Value; } if (include) { yield return(row); } } } }
// https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-text public static FormulaValue Text(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, FormulaValue[] args) { // only DateValue and DateTimeValue are supported for now with custom format strings. if (args.Length > 1 && args[0] is StringValue) { return(CommonErrors.NotYetImplementedError(irContext, "Text() doesn't support format args for type StringValue")); } string resultString = null; string formatString = null; if (args.Length > 1 && args[1] is StringValue fs) { formatString = fs.Value; } CultureInfo suppliedCulture = null; if (args.Length > 2 && args[2] is StringValue locale) { suppliedCulture = new CultureInfo(locale.Value); } switch (args[0]) { case NumberValue num: resultString = num.Value.ToString(formatString ?? "g", suppliedCulture ?? runner.CultureInfo); break; case StringValue s: resultString = s.Value; break; case DateValue d: formatString = ExpandDateTimeFormatSpecifiers(formatString, suppliedCulture ?? runner.CultureInfo); resultString = d.Value.ToString(formatString ?? "M/d/yyyy", suppliedCulture ?? runner.CultureInfo); break; case DateTimeValue dt: formatString = ExpandDateTimeFormatSpecifiers(formatString, suppliedCulture ?? runner.CultureInfo); resultString = dt.Value.ToString(formatString ?? "g", suppliedCulture ?? runner.CultureInfo); break; case TimeValue t: formatString = ExpandDateTimeFormatSpecifiers(formatString, suppliedCulture ?? runner.CultureInfo); resultString = _epoch.Add(t.Value).ToString(formatString ?? "t", suppliedCulture ?? runner.CultureInfo); break; default: break; } if (resultString != null) { return(new StringValue(irContext, resultString)); } return(CommonErrors.NotYetImplementedError(irContext, $"Text format for {args[0]?.GetType().Name}")); }
public void LockedModeShouldBlockAccess() { const string expectedMessage = "Properties of the symbol context should not be accessed until name binding is completed."; var compilation = new Compilation(TestTypeHelper.CreateEmptyProvider(), SyntaxTreeGroupingFactory.CreateFromText("")); var bindings = new Dictionary <SyntaxBase, Symbol>(); var cyclesBySymbol = new Dictionary <DeclaredSymbol, ImmutableArray <DeclaredSymbol> >(); var context = new SymbolContext(compilation, compilation.GetEntrypointSemanticModel()); Action byName = () => { var tm = context.TypeManager; }; byName.Should().Throw <InvalidOperationException>().WithMessage(expectedMessage); Action byNode = () => { var b = context.Compilation; }; byNode.Should().Throw <InvalidOperationException>().WithMessage(expectedMessage); context.Unlock(); context.TypeManager.Should().NotBeNull(); context.Compilation.Should().NotBeNull(); }
//public event CopyEventHandler CopyEvent; public MainWindow() { InitializeComponent(); // Setup the logger RosDiagnostics.SetupLogger(); RosDiagnostics.DebugTrace(RosDiagnostics.TraceType.Info, "Initialising application"); mSymbolContext = new SymbolContext(); RegisterControl(m_RegView); RegisterControl(m_BackTrace); RegisterControl(m_RawTraffic); RegisterControl(m_Locals); RegisterControl(m_MemoryWindow); RegisterControl(m_ProcThread); RegisterControl(m_Modules); RegisterControl(m_Breakpoints); m_Locals.Show(dockPanel, DockState.DockRight); m_RegView.Show(dockPanel, DockState.DockRight); m_Breakpoints.Show(dockPanel, DockState.DockRight); m_RegView.Activate(); m_BackTrace.Show(dockPanel, DockState.DockBottom); m_RawTraffic.Show(dockPanel); m_Modules.Show(dockPanel); m_ProcThread.Show(dockPanel); ReactOSWeb web = new ReactOSWeb(); web.Show(dockPanel); }
public SymbolSymbol( SymbolContext context, Scope scope, Dictionary <string, ParserRuleContext> meta = null) : base(context, scope, meta) { }
public void LockedModeShouldBlockAccess() { const string expectedMessage = "Properties of the symbol context should not be accessed until name binding is completed."; var bindings = new Dictionary <SyntaxBase, Symbol>(); var cyclesBySyntax = new Dictionary <SyntaxBase, ImmutableArray <DeclaredSymbol> >(); var context = new SymbolContext(new TypeManager(bindings, cyclesBySyntax), bindings); Action byName = () => { var tm = context.TypeManager; }; byName.Should().Throw <InvalidOperationException>().WithMessage(expectedMessage); Action byNode = () => { var b = context.Bindings; }; byNode.Should().Throw <InvalidOperationException>().WithMessage(expectedMessage); context.Unlock(); context.TypeManager.Should().NotBeNull(); context.Bindings.Should().NotBeNull(); }
public void LockedModeShouldBlockAccess() { const string expectedMessage = "Properties of the symbol context should not be accessed until name binding is completed."; var compilation = new Compilation(TestResourceTypeProvider.Create(), SyntaxFactory.CreateFromText("")); var bindings = new Dictionary <SyntaxBase, Symbol>(); var cyclesBySymbol = new Dictionary <DeclaredSymbol, ImmutableArray <DeclaredSymbol> >(); var context = new SymbolContext(new TypeManager(TestResourceTypeProvider.Create(), bindings, cyclesBySymbol, new SyntaxHierarchy()), bindings, compilation); Action byName = () => { var tm = context.TypeManager; }; byName.Should().Throw <InvalidOperationException>().WithMessage(expectedMessage); Action byNode = () => { var b = context.Bindings; }; byNode.Should().Throw <InvalidOperationException>().WithMessage(expectedMessage); context.Unlock(); context.TypeManager.Should().NotBeNull(); context.Bindings.Should().NotBeNull(); }
public FunctionSymbol( SymbolContext context, Scope scope, Dictionary <string, ParserRuleContext> meta = null) : base(context, scope, meta) { Signatures = new(); }
// https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-value // Convert string to number public static FormulaValue Value(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, FormulaValue[] args) { var arg0 = args[0]; if (arg0 is NumberValue n) { return(n); } if (arg0 is DateValue dv) { return(DateToNumber(irContext, new DateValue[] { dv })); } if (arg0 is DateTimeValue dtv) { return(DateTimeToNumber(irContext, new DateTimeValue[] { dtv })); } var str = ((StringValue)arg0).Value.Trim(); var styles = NumberStyles.Any; if (string.IsNullOrEmpty(str)) { return(new BlankValue(irContext)); } double div = 1; if (str[str.Length - 1] == '%') { str = str.Substring(0, str.Length - 1); div = 100; styles = NumberStyles.Number; } else if (str[0] == '%') { str = str.Substring(1, str.Length - 1); div = 100; styles = NumberStyles.Number; } if (!double.TryParse(str, styles, runner.CultureInfo, out var val)) { return(CommonErrors.InvalidNumberFormatError(irContext)); } if (IsInvalidDouble(val)) { return(CommonErrors.ArgumentOutOfRange(irContext)); } val /= div; return(new NumberValue(irContext, val)); }
public BindingContext(DriverOptions options, ParserOptions parserOptions = null) { Options = options; ParserOptions = parserOptions; Symbols = new SymbolContext(); TranslationUnitPasses = new PassBuilder <TranslationUnitPass>(this); GeneratorOutputPasses = new PassBuilder <GeneratorOutputPass>(this); }
public Driver(DriverOptions options, IDiagnosticConsumer diagnostics) { Options = options; Diagnostics = diagnostics; Project = new Project(); ASTContext = new ASTContext(); Symbols = new SymbolContext(); TypeDatabase = new TypeMapDatabase(); TranslationUnitPasses = new PassBuilder <TranslationUnitPass>(this); GeneratorOutputPasses = new PassBuilder <GeneratorOutputPass>(this); }
// Average([1,2,3], Value * Value) public static FormulaValue AverageTable(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, FormulaValue[] args) { var arg0 = (TableValue)args[0]; if (arg0.Rows.Count() == 0) { return(CommonErrors.DivByZeroError(irContext)); } return(RunAggregator(new AverageAgg(), runner, symbolContext, irContext, args)); }
public override object Resolve(SymbolContext context) { if (ChildExpressions.Count == 0) { return(string.Empty); } else { return(ChildExpressions[0].Resolve(context)); } }
/// <summary> /// Construct new symbol from specified rule context in stated scope with metadata. /// </summary> /// <param name="context"> Parse rule context of symbol in parse tree. </param> /// <param name="scope"> Symbol scope according to parse tree lookup. </param> /// <param name="meta"> Metadata dictionary. </param> public SymbolBase(SymbolContext context, Scope scope, Dictionary <string, ParserRuleContext> meta = null) { _ = context ?? throw new ArgumentNullException(nameof(scope)); Guid = Guid.NewGuid(); Name = context.GetText(); Context = context; Scope = scope; Meta = meta ?? new(); }
// Create new table public static FormulaValue AddColumns(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, FormulaValue[] args) { var sourceArg = (TableValue)args[0]; var newColumns = NamedLambda.Parse(args); var tableType = (TableType)irContext.ResultType; var recordIRContext = new IRContext(irContext.SourceContext, tableType.ToRecord()); var rows = LazyAddColumns(runner, symbolContext, sourceArg.Rows, recordIRContext, newColumns); return(new InMemoryTableValue(irContext, rows)); }
public static FormulaValue SortTable(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, FormulaValue[] args) { var arg0 = (TableValue)args[0]; var arg1 = (LambdaFormulaValue)args[1]; var arg2 = (StringValue)args[2]; var pairs = arg0.Rows.Select(row => { if (row.IsValue) { var childContext = symbolContext.WithScopeValues(row.Value); return(new KeyValuePair <DValue <RecordValue>, FormulaValue>(row, arg1.Eval(runner, childContext))); } return(new KeyValuePair <DValue <RecordValue>, FormulaValue>(row, row.ToFormulaValue())); }).ToList(); var errors = new List <ErrorValue>(pairs.Select(pair => pair.Value).OfType <ErrorValue>()); var allNumbers = pairs.All(pair => IsValueTypeErrorOrBlank <NumberValue>(pair.Value)); var allStrings = pairs.All(pair => IsValueTypeErrorOrBlank <StringValue>(pair.Value)); var allBooleans = pairs.All(pair => IsValueTypeErrorOrBlank <BooleanValue>(pair.Value)); if (!(allNumbers || allStrings || allBooleans)) { errors.Add(CommonErrors.RuntimeTypeMismatch(irContext)); return(ErrorValue.Combine(irContext, errors)); } if (errors.Count != 0) { return(ErrorValue.Combine(irContext, errors)); } var compareToResultModifier = 1; if (arg2.Value.ToLower() == "descending") { compareToResultModifier = -1; } if (allNumbers) { return(SortValueType <NumberValue, double>(pairs, irContext, compareToResultModifier)); } else if (allStrings) { return(SortValueType <StringValue, string>(pairs, irContext, compareToResultModifier)); } else { return(SortValueType <BooleanValue, bool>(pairs, irContext, compareToResultModifier)); } }
public static FormulaValue Split(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, StringValue[] args) { var text = args[0].Value; var separator = args[1].Value; // The separator can be zero, one, or more characters that are matched as a whole in the text string. Using a zero length or blank // string results in each character being broken out individually. var substrings = string.IsNullOrEmpty(separator) ? text.Select(c => new string(c, 1)) : text.Split(new string[] { separator }, StringSplitOptions.None); var rows = substrings.Select(s => new StringValue(IRContext.NotInSource(FormulaType.String), s)); return(new InMemoryTableValue(irContext, StandardSingleColumnTableFromValues(irContext, rows.ToArray(), BuiltinFunction.OneColumnTableResultName))); }
public static FormulaValue TimeParse(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, StringValue[] args) { var str = args[0].Value; if (TimeSpan.TryParse(str, runner.CultureInfo, out TimeSpan result)) { return(new TimeValue(irContext, result)); } else { return(CommonErrors.InvalidDateTimeError(irContext)); } }
public static FormulaValue DateParse(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, StringValue[] args) { var str = args[0].Value; if (DateTime.TryParse(str, runner.CultureInfo, DateTimeStyles.None, out DateTime result)) { return(new DateValue(irContext, result.Date)); } else { return(CommonErrors.InvalidDateTimeError(irContext)); } }
public BindingContext(DriverOptions options, ParserOptions parserOptions = null) { Options = options; ParserOptions = parserOptions; Symbols = new SymbolContext(); Delegates = new Dictionary <Function, DelegatesPass.DelegateDefinition>(); TypeMaps = new TypeMapDatabase(); TranslationUnitPasses = new PassBuilder <TranslationUnitPass>(this); GeneratorOutputPasses = new PassBuilder <GeneratorOutputPass>(this); }
public BindingContext(IDiagnostics diagnostics, DriverOptions options, ParserOptions parserOptions = null) { Options = options; Diagnostics = diagnostics; ParserOptions = parserOptions; Symbols = new SymbolContext(); Delegates = new Dictionary <Function, DelegatesPass.DelegateDefinition>(); TypeDatabase = new TypeMapDatabase(); TypeDatabase.SetupTypeMaps(Options.GeneratorKind); TranslationUnitPasses = new PassBuilder <TranslationUnitPass>(this); GeneratorOutputPasses = new PassBuilder <GeneratorOutputPass>(this); }
public static FormulaValue CountIf(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, FormulaValue[] args) { // Streaming var sources = (TableValue)args[0]; var filter = (LambdaFormulaValue)args[1]; int count = 0; var errors = new List <ErrorValue>(); foreach (var row in sources.Rows) { if (row.IsValue) { var childContext = symbolContext.WithScopeValues(row.Value); var result = filter.Eval(runner, childContext); if (result is ErrorValue error) { errors.Add(error); continue; } bool include = ((BooleanValue)result).Value; if (include) { count++; } } if (row.IsError) { errors.Add(row.Error); } } if (errors.Count != 0) { return(ErrorValue.Combine(irContext, errors)); } return(new NumberValue(irContext, count)); }
// Filter ([1,2,3,4,5], Value > 5) public static FormulaValue FilterTable(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, FormulaValue[] args) { // Streaming var arg0 = (TableValue)args[0]; var arg1 = (LambdaFormulaValue)args[1]; if (args.Length > 2) { return(new ErrorValue(irContext, new ExpressionError() { Message = "Filter() only supports one predicate", Span = irContext.SourceContext, Kind = ErrorKind.Validation })); } var rows = LazyFilter(runner, symbolContext, arg0.Rows, arg1); return(new InMemoryTableValue(irContext, rows)); }
// Lazy evaluation public static FormulaValue Or(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, FormulaValue[] args) { foreach (var arg in args) { var res = runner.EvalArg <BooleanValue>(arg, symbolContext, arg.IRContext); if (res.IsValue) { var val = res.Value; if (val.Value) { return(new BooleanValue(irContext, true)); } } else if (res.IsError) { return(res.Error); } } return(new BooleanValue(irContext, false)); }
public string Process(string text, EntityReference entityReference, Dictionary <string, string> staticParameters) { List <Symbol> symbols = GetListOfSymbols(text); SymbolContext symbolContext = new SymbolContext() { OrganizationService = this.OrganizationService , RecordRefence = entityReference , StaticParameters = staticParameters }; StringBuilder processedTextBuilder = new StringBuilder(); foreach (Symbol symbol in symbols) { processedTextBuilder.Append(symbol.Resolve(symbolContext)); } return(processedTextBuilder.ToString()); }
public static FormulaValue Concat(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, FormulaValue[] args) {// Streaming var arg0 = (TableValue)args[0]; var arg1 = (LambdaFormulaValue)args[1]; StringBuilder sb = new StringBuilder(); foreach (var row in arg0.Rows) { if (row.IsValue) { var childContext = symbolContext.WithScopeValues(row.Value); // Filter evals to a boolean var result = arg1.Eval(runner, childContext); var str = (StringValue)result; sb.Append(str.Value); } } return(new StringValue(irContext, sb.ToString())); }
// https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-isblank-isempty // Take first non-blank value. // public static FormulaValue Coalesce(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, FormulaValue[] args) { var errors = new List <ErrorValue>(); foreach (var arg in args) { var res = runner.EvalArg <ValidFormulaValue>(arg, symbolContext, arg.IRContext); if (res.IsValue) { var val = res.Value; if (!(val is StringValue str && str.Value == "")) { if (errors.Count == 0) { return(res.ToFormulaValue()); } else { return(ErrorValue.Combine(irContext, errors)); } } } if (res.IsError) { errors.Add(res.Error); } } if (errors.Count == 0) { return(new BlankValue(irContext)); } else { return(ErrorValue.Combine(irContext, errors)); } }
public override void ExitSymbol([NotNull] SymbolContext context) { PopElementStack(); }
public override void EnterSymbol([NotNull] SymbolContext context) { var e = CreateElementOnStack <Symbol>().Initialize(context.GetText()); GiveChildToParent(e); }