static public Variable ExecuteMethodDeclaration(node methodNode) { //string type = methodNode.nodes[0].body; string Identifier = methodNode.nodes[0].body; string[] args = new string[methodNode.nodes[1].nodes.Count]; int i = 0; foreach (node argNode in methodNode.nodes[1].nodes) { args[i] = argNode.body; i++; } node body = methodNode.nodes[2]; if (!MethodHandler.Exists(Identifier)) { MethodHandler.Add(Identifier, args, body); } else { SkryptMethod found = MethodHandler.GetSk(Identifier); int foundIndex = MethodContainer.SKmethods.IndexOf(found); MethodContainer.SKmethods[foundIndex].methodNode = body; } Variable returnVariable = new Variable(Identifier, "method", body); return(returnVariable); }
static public void Initialise() { MDelegate f = delegate(object[] i) { Console.WriteLine(i[0]); return(null); }; MethodHandler.Add("print", "void", new string[] { "input" }, f); f = delegate(object[] i) { return(Math.Sqrt(Convert.ToDouble(i[0]))); }; MethodHandler.Add("sqrt", "numeric", new string[] { "input" }, f); f = delegate(object[] i) { return(Math.E); }; MethodHandler.Add("e", "numeric", new string[0], f); }
static public node ParseGlobal(node branch, List <Token> tokensList) { node newNode = branch; List <Token> tokenBuffer = new List <Token>(); // Branching bool isBranch = false; int branchDepth = 0; string branchType = ""; bool prevWasIf = false; bool prevWasLiteralIf = false; node ifNode = null; // Method declaration bool isMethod = false; bool isMethodArgs = false; bool isMethodBody = false; bool hasMethodArgs = false; int bracketDepth = 0; node argsNode = null; string methodType = ""; List <Token> methodArgs = new List <Token>(); List <Token> methodBody = new List <Token>(); List <Token> returnExpr = new List <Token>(); for (int i = 0; i < tokensList.Count; i++) { Token token = tokensList[i]; if (token.value == "lbracket") { bracketDepth++; } if (token.value == "rbracket") { bracketDepth--; } if (token.value == "def") { isMethod = true; continue; } else if (isMethod) { methodType = token.value; if (!hasMethodArgs && token.value == "lpar") { isMethodArgs = true; continue; } if (!hasMethodArgs && isMethodArgs && token.value == "rpar") { isMethodArgs = false; hasMethodArgs = true; MethodHandler.Add(tokenBuffer[0].value, new string[0], (node)null); SkryptMethod found = MethodHandler.GetSk(tokenBuffer[0].value); int foundIndex = MethodContainer.SKmethods.IndexOf(found); //MethodContainer.SKmethods[foundIndex].returnType = tokenBuffer[0].value; argsNode = new node() { body = "arguments" }; int j = 0; string[] argNames = new string[methodArgs.Count]; MethodContainer.SKmethods[foundIndex].arguments = new string[methodArgs.Count]; foreach (Token argTkn in methodArgs) { MethodContainer.SKmethods[foundIndex].arguments[j] = argTkn.value; argNames[j] = argTkn.value; j++; if (argTkn.value == "seperate") { continue; } argsNode.nodes.Add(new node() { body = argTkn.value, depth = argsNode.depth + 2 }); } continue; } if (isMethodArgs) { methodArgs.Add(token); } if (!isMethodBody && hasMethodArgs && token.value == "lbracket") { isMethodBody = true; continue; } if (isMethodBody && token.value == "rbracket" && bracketDepth == 0) { node MethodNode = ParseMethodDeclaration(newNode, tokenBuffer, argsNode, methodBody); Executor.ExecuteMethodDeclaration(MethodNode); isMethodBody = false; //hasMethodBody = true; tokenBuffer.Clear(); isMethod = false; continue; } if (isMethodBody) { methodBody.Add(token); } tokenBuffer.Add(token); continue; } else if (token.value == "if" || token.value == "while" || token.value == "for" || token.value == "else" || token.value == "elseif") { branchDepth++; branchType = token.value; isBranch = true; } else if (isBranch) { if (token.value == "rbracket") { branchDepth--; if (branchDepth == 0) { node baseNode = new node() { body = "branch", depth = newNode.depth + 1 }; tokenBuffer.Add(token); if ((branchType == "else" || branchType == "elseif")) { if ((branchType == "else" || branchType == "elseif") && !prevWasIf) { throw new SkryptException(branchType + " has to be preceeded by if/elseif statement", tokenBuffer[0]); } if ((branchType == "elseif") && !prevWasLiteralIf) { throw new SkryptException(branchType + " has to be preceeded by if statement", tokenBuffer[0]); } if (branchType == "elseif") { prevWasIf = true; } node addNode = new node() { body = "secondary", depth = ifNode.depth + 1 }; ifNode.nodes.Add(addNode); node branchNode = ParseBranch(addNode, tokenBuffer, branchType); ifNode = branchNode; prevWasLiteralIf = false; tokenBuffer.Clear(); continue; } else if (branchType == "if") { prevWasLiteralIf = true; prevWasIf = true; ifNode = ParseBranch(baseNode, tokenBuffer, branchType); newNode.nodes.Add(baseNode); tokenBuffer.Clear(); continue; } tokenBuffer.Clear(); continue; } } } else if (token.value == "eol") { node baseNode = new node() { body = "expression", depth = newNode.depth + 1 }; newNode.nodes.Add(baseNode); ParseExpression(baseNode, tokenBuffer); tokenBuffer.Clear(); continue; } tokenBuffer.Add(token); } return(newNode); }