Ejemplo n.º 1
0
    void HandleMessage(IQueryMsg e)
    {
        var expr = VerifiedExpression.Parse(e.Message, CalcEnv);
        // Only automatically calculate if the expression is legitimate and if it's reasonable to assume it's meant
        // to be a calculation (not just a single number). A minimal calculation will involve at least 3 tokens:
        // `number operator number`.
        const int minTokenCount = 3;

        if (expr.Success && expr.Expression.Count >= minTokenCount)
        {
            double result = ShuntingYard.Calculate(expr);
            e.Reply(result.ToString());
        }
    }
Ejemplo n.º 2
0
    void HandleTrigger(ITriggerMsg e)
    {
        var exprStr = e.MessageWithoutTrigger();

        if (exprStr.HasValue())
        {
            var expr = VerifiedExpression.Parse(exprStr, CalcEnv);

            if (expr.Success)
            {
                double result = ShuntingYard.Calculate(expr);
                e.Reply(result.ToString());
            }
            else
            {
                OutputError(e, expr);
            }
        }
    }
Ejemplo n.º 3
0
    void DefVar(ITriggerMsg e)
    {
        var expression = e.GetArg(out string symbol).ToJoined();

        if (ParseArgs.Success(symbol, expression))
        {
            var expr = VerifiedExpression.Parse(expression, CalcEnv);
            if (CheckPreconditions(e, expr, symbol))
            {
                double result = ShuntingYard.Calculate(expr);
                if (CalcEnv.Variable(symbol, out double previous))
                {
                    e.Reply("{0} = {1} (Previous value: {2})", symbol, result, previous);
                }
                else
                {
                    e.Reply("{0} = {1}", symbol, result);
                }

                CalcEnv.AssignVariable(symbol, result);
            }
        }
    }