private void ParseParameter(ParseTreeNode Node) { if (Node.Term.Name == "identifier") { LocalVariables.Add(Node.FindTokenAndGetValue().ToLower()); numofParams++; } else { foreach (ParseTreeNode child in Node.ChildNodes) { ParseParameter(child); } } }
private static ExpTreeNode GetAsExpressionTree(this ParseTreeNode parsedNode, ExpTreeNode parentExpNode) { //The current node is a variable / value token. Create a value node and return it back if (!parsedNode.ChildNodes.Any()) { switch (parsedNode.Term.Name) { case "variable": { var varName = parsedNode.FindTokenAndGetValue <string>(); return(new VariableValue(varName, parentExpNode)); } case "boolean": return(new LiteralValue(parsedNode.FindTokenAndGetValue <bool>(), parentExpNode)); case "integer": case "decimal": return(new LiteralValue(parsedNode.FindTokenAndGetValue <double>(), parentExpNode)); case "SingleQuoteString": case "DoubleQuoteString": return(new LiteralValue(parsedNode.FindTokenAndGetValue <string>(), parentExpNode)); default: throw parsedNode.GetException($"Invalid token type '{parsedNode.Term.Name}' in expression"); } } // Look on the next node down else if (parsedNode.ChildNodes.Count == 1) { return(GetAsExpressionTree(parsedNode.ChildNodes[0], parentExpNode)); } //Ignore parenthesis, the middle non-terminal is what we want // Look on the next node down else if (parsedNode.ChildNodes.Count == 3 && parsedNode.ChildNodes[0]?.Token?.Text == "(") { return(GetAsExpressionTree(parsedNode.ChildNodes[1], parentExpNode)); } //Binary operator else if (parsedNode.ChildNodes.Count == 3) { BinaryOperatorBase binaryOp; var opStr = parsedNode.ChildNodes[1].FindToken().ValueString; switch (opStr) { // Math case "+": binaryOp = new AdditionOperator(parentExpNode); break; case "-": binaryOp = new SubtractionOperator(parentExpNode); break; case "*": binaryOp = new MultiplacationOperator(parentExpNode); break; case "/": binaryOp = new DivisionOperator(parentExpNode); break; case "%": binaryOp = new ModulusOperator(parentExpNode); break; case "^": binaryOp = new PowerOperator(parentExpNode); break; // Bool case "~==": binaryOp = new LooseEqualsOperator(parentExpNode); break; case "~!=": binaryOp = new LooseNotEqualsOperator(parentExpNode); break; case "==": binaryOp = new EqualsOperator(parentExpNode); break; case "!=": binaryOp = new NotEqualsOperator(parentExpNode); break; case "like": binaryOp = new LikeOperator(parentExpNode); break; case "match": binaryOp = new MatchOperator(parentExpNode); break; case ">": binaryOp = new GreaterThanOperator(parentExpNode); break; case ">=": binaryOp = new GreaterThanOrEqualOperator(parentExpNode); break; case "<": binaryOp = new LessThanOperator(parentExpNode); break; case "<=": binaryOp = new LessThanOrEqualOperator(parentExpNode); break; case "&&": binaryOp = new AndOperator(parentExpNode); break; case "||": binaryOp = new OrOperator(parentExpNode); break; default: throw parsedNode.ChildNodes[1].GetException($"Unrecognized operator '{opStr}'"); } binaryOp.LeftOperand = GetAsExpressionTree(parsedNode.ChildNodes[0], binaryOp); binaryOp.RightOperand = GetAsExpressionTree(parsedNode.ChildNodes[2], binaryOp); //Optimize if (binaryOp.LeftOperand is LiteralValue && binaryOp.RightOperand is LiteralValue) { return(new LiteralValue(binaryOp.Eval(), parentExpNode)); } return(binaryOp); } // Unary operator else if (parsedNode.ChildNodes.Count == 2) { var opVal = parsedNode.ChildNodes[0].FindToken().Value; UnaryOperatorBase unaryOp; if (parsedNode.ChildNodes[0].FindToken().Value is TypeCode convertType) { unaryOp = new TypeConversionOperator(convertType, parentExpNode); } else { var opStr = opVal.ToString(); switch (opStr) { case "!": unaryOp = new NotOperator(parentExpNode); break; //Property Checking case "lengthof": unaryOp = new LengthOperator(parentExpNode); break; case "typeof": unaryOp = new TypeOperator(parentExpNode); break; default: unaryOp = new GenericUnaryMathFunctionOperator(opStr, parentExpNode); break; } } unaryOp.Operand = GetAsExpressionTree(parsedNode.ChildNodes[1], unaryOp); //Optimize if (unaryOp.Operand is LiteralValue) { return(new LiteralValue(unaryOp.Eval(), parentExpNode)); } return(unaryOp); } else { throw parsedNode.GetException($"Invalid number of tokens ({parsedNode.ChildNodes.Count})"); } }
private static T FindTokenAndGetValue <T>(this ParseTreeNode node, T defaultValue) { try { return(node.FindTokenAndGetValue <T>()); } catch { return(defaultValue); } }
private void EmitGetFunction(ParseTreeNode node) { ushort ptrToInclude = 0x3e; int nodeIndex = 0; if (node.ChildNodes[0].Term.Name == "gscForFunction") { ptrToInclude = GetStringPosByName(node.FindTokenAndGetValue()); nodeIndex = 1; } EmitOpcode(OP_GetFunction); AddRefToCall(node.ChildNodes[nodeIndex].FindTokenAndGetValue(), 0, 1, ptrToInclude); SetAlignedDword(); AddInt(GetStringPosByName(node.ChildNodes[nodeIndex].FindTokenAndGetValue())); }
private void ParseParameter(ParseTreeNode Node) { if (Node.Term.Name == "identifier") { LocalVariables.Add(Node.FindTokenAndGetValue().ToLower()); numofParams++; } else { foreach (ParseTreeNode child in Node.ChildNodes) { ParseParameter(child); } } }