static object ComputationValue(Racr.AstNode n) { var op = n.GetOperator(); var operands = n.GetOperands().Children(); var args = operands.Select(p => ((Racr.AstNode) p).Value()).ToArray(); var func = opTable[op]; try { return func(args); } catch { return null; } }
static string ComputationSExpr(Racr.AstNode n) { return "(~~ " + n.GetOperator() + " " + String.Join(" ", n.GetOperands().Children().Select(x => ((Racr.AstNode)x).SExpr())) + ")"; }
static ValueTypes ComputationType(Racr.AstNode n) { var op = n.GetOperator(); var inType = ValueTypes.ErrorType; var outType = ValueTypes.ErrorType; var operands = n.GetOperands().Children(); if (op == "&&" || op == "//" || op == "not") inType = outType = ValueTypes.Boolean; else if (op == "=" || op == "<" || op == ">" || op == "<=" || op == ">=" || op == "!=") { inType = ValueTypes.Number; outType = ValueTypes.Boolean; } else if (op == "string=?" || op == "string<?" || op == "string>?" || op == "string<=?" || op == "string>=?") { inType = ValueTypes.String; outType = ValueTypes.Boolean; } else if (op == "+" || op == "-" || op == "*" || op == "/") inType = outType = ValueTypes.Number; else if (op == "string-append") inType = outType = ValueTypes.String; if (operands.Any(x => ((Racr.AstNode) x).Type() != inType)) return ValueTypes.ErrorType; return outType; }