Пример #1
0
        Literal EvaluateFunctionCall(FunctionCall expr)
        {
            //Evaluate all arguments
            var args = new List<Literal>();
            foreach (var a in expr.Arguments)
                args.Add(Evaluate(a));

            switch (expr.Name.Key)
            {
                case "max":
                    var first = args[0];

                    dynamic max = first.Value;
                    LaxType type = first.ValueType;

                    foreach (var a in args)
                    {
                        if (a.ValueType.Unit != type.Unit)
                            throw new SemanticError(a, "All argument types must have the same unit");
                        dynamic val = a.Value;
                        if (val > max)
                        {
                            max = val;
                            type = a.ValueType;
                        }
                    }
                    return new Literal(expr.CodeRange, type, max);

                default:
                    throw new SemanticError(expr, "Unknown function");
            }
            throw new NotImplementedException();
        }
Пример #2
0
 static LaxExpression EvaluateFunctionCall(FunctionCall s)
 {
     bool optimized = false;
     var args = new List<LaxExpression>();
     foreach (var a in s.Arguments)
     {
         var e = Evaluate(a);
         if (e != a)
             optimized = true;
         args.Add(e);
     }
     if (optimized)
     {
         var f = new FunctionCall(s.CodeRange, s.Name);
         f.Arguments.AddRange(args);
         f.ValueType = s.ValueType;
         return f;
     }
     else
         return s;
 }
Пример #3
0
 string FormatFunctionCall(FunctionCall expr)
 {
     string s = expr.Name + " ";
     for (int n = 0; n < expr.Arguments.Count; n++)
     {
         var a = expr.Arguments[n];
         if (n > 0)
             s += ", ";
         s += Format(a);
     }
     return s + ";";
 }
Пример #4
0
        private FunctionCall ParseFunctionCall(TokenStream reader)
        {
            var name = reader.Current.ToIdentifier();
            var func = new FunctionCall(reader.Current.CodeRange, name);

            //Read arguments
            while (true)
            {
                var a = Parse(reader);
                func.Arguments.Add(a);
                if (reader.Current.Type == TokenType.ArgumentEnd ||
                    reader.Current.Type == TokenType.EndOfStream)
                {
                    break;
                }
                Debug.Assert(reader.Current.Type == TokenType.ArgumentSeparator);
            }

            func.CodeRange = func.CodeRange.Expand2(reader.Current);
            return func;
        }