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