|
| 1 | +using MScript.Evaluation.Types; |
| 2 | +using MScript.Parsing; |
| 3 | +using MScript.Parsing.AST; |
| 4 | +using System; |
| 5 | +using System.Linq; |
| 6 | + |
| 7 | +namespace MScript.Evaluation |
| 8 | +{ |
| 9 | + static class Evaluator |
| 10 | + { |
| 11 | + public static object Evaluate(Expression expression, EvaluationContext context) |
| 12 | + { |
| 13 | + switch (expression) |
| 14 | + { |
| 15 | + case NoneLiteral noneLiteral: return null; |
| 16 | + case NumberLiteral numberLiteral: return numberLiteral.Value; |
| 17 | + case StringLiteral stringLiteral: return stringLiteral.Value; |
| 18 | + case VectorLiteral vectorLiteral: return EvaluateVectorLiteral(vectorLiteral, context); |
| 19 | + case Variable variable: return context.Resolve(variable.Name); |
| 20 | + case FunctionCall functionCall: return EvaluateFunctionCall(functionCall, context); |
| 21 | + case Indexing indexing: return EvaluateIndexing(indexing, context); |
| 22 | + case MemberAccess memberAccess: return EvaluateMemberAccess(memberAccess, context); |
| 23 | + case BinaryOperation binaryOperation: return EvaluateBinaryOperation(binaryOperation, context); |
| 24 | + case UnaryOperation unaryOperation: return EvaluateUnaryOperation(unaryOperation, context); |
| 25 | + case ConditionalOperation conditionalOperation: return EvaluateConditionalOperation(conditionalOperation, context); |
| 26 | + |
| 27 | + default: throw new InvalidOperationException($"Unknown expression type: {expression}."); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + private static object EvaluateVectorLiteral(VectorLiteral vectorLiteral, EvaluationContext context) |
| 33 | + { |
| 34 | + var vector = new double[vectorLiteral.Elements.Count]; |
| 35 | + for (int i = 0; i < vectorLiteral.Elements.Count; i++) |
| 36 | + { |
| 37 | + if (!((Evaluate(vectorLiteral.Elements[i], context) ?? 0.0) is double number)) |
| 38 | + throw new InvalidOperationException("Vectors can only contain numbers."); |
| 39 | + |
| 40 | + vector[i] = number; |
| 41 | + } |
| 42 | + return vector; |
| 43 | + } |
| 44 | + |
| 45 | + private static object EvaluateFunctionCall(FunctionCall functionCall, EvaluationContext context) |
| 46 | + { |
| 47 | + if (!(Evaluate(functionCall.Function, context) is IFunction function)) |
| 48 | + throw new InvalidOperationException($"Function call requires a function."); |
| 49 | + |
| 50 | + var arguments = functionCall.Arguments.Select(argument => Evaluate(argument, context)).ToArray(); |
| 51 | + return function.Apply(arguments, context); |
| 52 | + } |
| 53 | + |
| 54 | + private static object EvaluateIndexing(Indexing indexing, EvaluationContext context) |
| 55 | + { |
| 56 | + var indexable = Evaluate(indexing.Indexable, context); |
| 57 | + if (indexable is double[] || indexable is string) |
| 58 | + { |
| 59 | + if (!((Evaluate(indexing.Index, context) ?? 0.0) is double index)) |
| 60 | + throw new InvalidOperationException($"Indexing requires a numeric index."); |
| 61 | + |
| 62 | + if (indexable is double[] vector) |
| 63 | + return Operations.Index(vector, (int)index); |
| 64 | + else if (indexable is string @string) |
| 65 | + return Operations.Index(@string, (int)index); |
| 66 | + } |
| 67 | + |
| 68 | + throw new InvalidOperationException($"{indexable} cannot be indexed."); |
| 69 | + } |
| 70 | + |
| 71 | + private static object EvaluateMemberAccess(MemberAccess memberAccess, EvaluationContext context) |
| 72 | + { |
| 73 | + var @object = Evaluate(memberAccess.Object, context); |
| 74 | + var type = TypeDescriptor.GetType(@object); |
| 75 | + var member = type.GetMember(memberAccess.MemberName); |
| 76 | + if (member is null) |
| 77 | + throw new InvalidOperationException($"{@object} does not have a member named '{memberAccess.MemberName}'."); |
| 78 | + |
| 79 | + return member.GetValue(@object); |
| 80 | + } |
| 81 | + |
| 82 | + private static object EvaluateBinaryOperation(BinaryOperation binaryOperation, EvaluationContext context) |
| 83 | + { |
| 84 | + switch (binaryOperation.Operator) |
| 85 | + { |
| 86 | + case BinaryOperator.And: return Operations.And(binaryOperation.LeftOperand, binaryOperation.RightOperand, context); |
| 87 | + case BinaryOperator.Or: return Operations.Or(binaryOperation.LeftOperand, binaryOperation.RightOperand, context); |
| 88 | + } |
| 89 | + |
| 90 | + var leftOperand = Evaluate(binaryOperation.LeftOperand, context); |
| 91 | + var rightOperand = Evaluate(binaryOperation.RightOperand, context); |
| 92 | + switch (binaryOperation.Operator) |
| 93 | + { |
| 94 | + case BinaryOperator.Add: return Operations.Add(leftOperand, rightOperand); |
| 95 | + case BinaryOperator.Subtract: return Operations.Subtract(leftOperand, rightOperand); |
| 96 | + case BinaryOperator.Multiply: return Operations.Multiply(leftOperand, rightOperand); |
| 97 | + case BinaryOperator.Divide: return Operations.Divide(leftOperand, rightOperand); |
| 98 | + case BinaryOperator.Remainder: return Operations.Remainder(leftOperand, rightOperand); |
| 99 | + case BinaryOperator.Equals: return Operations.Equals(leftOperand, rightOperand); |
| 100 | + case BinaryOperator.NotEquals: return Operations.NotEquals(leftOperand, rightOperand); |
| 101 | + case BinaryOperator.GreaterThan: return Operations.GreaterThan(leftOperand, rightOperand); |
| 102 | + case BinaryOperator.GreaterThanOrEqual: return Operations.GreaterThanOrEqual(leftOperand, rightOperand); |
| 103 | + case BinaryOperator.LessThan: return Operations.LessThan(leftOperand, rightOperand); |
| 104 | + case BinaryOperator.LessThanOrEqual: return Operations.LessThanOrEqual(leftOperand, rightOperand); |
| 105 | + default: throw new InvalidOperationException($"Unknown operator: {binaryOperation.Operator}."); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private static object EvaluateUnaryOperation(UnaryOperation unaryOperation, EvaluationContext context) |
| 110 | + { |
| 111 | + var operand = Evaluate(unaryOperation.Operand, context); |
| 112 | + switch (unaryOperation.Operator) |
| 113 | + { |
| 114 | + case UnaryOperator.Negate: return Operations.Negate(operand); |
| 115 | + case UnaryOperator.LogicalNegate: return Operations.LogicalNegate(operand); |
| 116 | + default: throw new InvalidOperationException($"Unknown operator: {unaryOperation.Operator}."); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static object EvaluateConditionalOperation(ConditionalOperation conditionalOperation, EvaluationContext context) |
| 121 | + { |
| 122 | + return Operations.Conditional( |
| 123 | + conditionalOperation.Condition, |
| 124 | + conditionalOperation.TrueExpression, |
| 125 | + conditionalOperation.FalseExpression, |
| 126 | + context); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments