Пример #1
0
        private void FUNC_LET(Tokens tokens, FuncArgs funcArgs)
        {
            Token funcNameToken = tokens.GetNextToken();

            tokens.ConsumeSpaces();

            Token varNameToken = tokens.GetNextToken();

            tokens.ConsumeSpaces();

            Token equalToken = tokens.GetNextToken();

            tokens.ConsumeSpaces();

            string val = string.Empty;

            if (tokens.InpectNextToken().Value == "(")
            {
                val = this.Expression(tokens, funcArgs);
            }
            else
            {
                val = this.GetFuncArg(tokens.GetNextToken().Value, funcArgs);
            }

            string varName = varNameToken.Value;

            if (funcArgs.ContainsKey(varName))
            {
                funcArgs[varName] = val;
            }
            else
            {
                funcArgs.Add(varName, val);
            }

            this.Next(tokens, funcArgs);
        }
Пример #2
0
        private void FUNC_FUNCTION(Tokens tokens, FuncArgs funcArgs)
        {
            Token funcToken = tokens.GetNextToken();

            tokens.ConsumeSpaces();

            Token funcNameToken = tokens.GetNextToken();

            if (funcNameToken == null)
            {
                throw new SyntaxError(string.Format("Function name required near line {0}.", funcToken.Line));
            }

            tokens.ConsumeSpaces();

            Dictionary <string, Token> funcArgList = new Dictionary <string, Token>();

            if (tokens.InpectNextToken().Value != "[")
            {
                while (tokens.Next())
                {
                    Token token = tokens.GetToken();

                    if (token.Value.Substring(0, 1) != ":")
                    {
                        throw new SyntaxError(string.Format("Unexpected token {0} on line {1}.", token.Value, token.Line));
                    }

                    if (funcArgList.ContainsKey(token.Value))
                    {
                        Token varToken = funcArgList[token.Value];
                        throw new SyntaxError(string.Format("Variable {0} has already been defined on line {1}.", token.Value, varToken.Line));
                    }

                    funcArgList.Add(token.Value, token);

                    tokens.ConsumeSpaces();

                    if (tokens.InpectNextToken().Value == "[")
                    {
                        break;
                    }
                }
            }

            tokens.ConsumeSpaces();

            Tokens funcStmTokens = new Tokens();

            int openBracketCount = 0;

            while (tokens.Next())
            {
                Token token = tokens.GetToken();

                funcStmTokens.Add(token);

                if (token.Value == "]")
                {
                    openBracketCount--;
                    if (openBracketCount == 0)
                    {
                        break;
                    }
                }
                if (token.Value == "[")
                {
                    openBracketCount++;
                }

                if (token.Value.ToLower() == "func")
                {
                    throw new SyntaxError(string.Format("Func not allowed ", token.Value, token.Line));
                }
            }

            if (tokens.Index() == tokens.Count())
            {
                Token lastToken = tokens.GetLastToken();
                throw new SyntaxError(string.Format("Unexpected end of input near line {0}.", lastToken.Line));
            }

            tokens.ConsumeSpaces();

            funcStmTokens.Remove(0).Remove(funcStmTokens.Count() - 1);

            tokens.ConsumeSpaces();

            this.functions.Add(funcNameToken.Value.ToLower(), (scopedTokens, scopedFuncArgs) => {
                Token localFuncNameToken = scopedTokens.GetNextToken();

                FuncArgs localFuncArgs = new FuncArgs();

                foreach (KeyValuePair <string, Token> pair in funcArgList)
                {
                    scopedTokens.ConsumeSpaces();
                    string argName      = pair.Value.Value;
                    Token argValueToken = scopedTokens.GetNextToken();

                    if (argValueToken != null)
                    {
                        string value = argValueToken.Value;

                        if (value.Substring(0, 1) == ":")
                        {
                            if (scopedFuncArgs.ContainsKey(value))
                            {
                                value = scopedFuncArgs[value];
                            }
                        }
                        localFuncArgs.Add(argName, value);
                    }
                    else
                    {
                        throw new SyntaxError(string.Format("Function {0} expects {1} argument(s) near line {2}", localFuncNameToken.Value, funcArgList.Count, localFuncNameToken.Line));
                    }
                }

                this.Compile(funcStmTokens, localFuncArgs);
                funcStmTokens.Reset();
                this.Next(scopedTokens, scopedFuncArgs);
            });

            this.Next(tokens, funcArgs);
        }
Пример #3
0
        private void FUNC_REPEAT(Tokens tokens, FuncArgs funcArgs)
        {
            FuncArgs scopedFuncArgs = new FuncArgs(funcArgs);
            Token    funcNameToken  = tokens.GetNextToken();

            tokens.ConsumeSpaces();

            Token funcArgToken = tokens.GetNextToken();

            if (funcArgToken == null)
            {
                throw new SyntaxError(string.Format("Error near line {0}. {1} function expects 1 argument.", funcNameToken.Line, funcNameToken.Value.ToUpper()));
            }

            int counter = 0;

            try{
                counter = Convert.ToInt32(this.GetFuncArg(funcArgToken.Value, funcArgs));
            }
            catch (FormatException e) {
                throw new SyntaxError(string.Format("Error near line {0}. {1} function expects a number. {2}", funcNameToken.Line, funcNameToken.Value.ToUpper(), e.Message));
            }

            tokens.ConsumeSpaces();

            Token leftBracket = tokens.GetNextToken();

            if (leftBracket == null)
            {
                throw new SyntaxError(string.Format("Unexpected identifier near line {0}.", funcArgToken.Line));
            }

            if (leftBracket.Value != "[")
            {
                throw new SyntaxError(string.Format("Unexpected identifier on line {0}", leftBracket.Line));
            }

            Tokens repeatStmTokens = new Tokens();

            int openBracketCount = 1;

            while (tokens.Next())
            {
                Token token = tokens.GetToken();

                if (token.Value == "]")
                {
                    if (openBracketCount == 1)
                    {
                        break;
                    }
                    else
                    {
                        openBracketCount--;
                    }
                }

                repeatStmTokens.Add(token);

                if (token.Value == "[")
                {
                    openBracketCount++;
                }
            }

            if (tokens.Index() == tokens.Count())
            {
                Token lastToken = tokens.GetLastToken();
                throw new SyntaxError(string.Format("Unexpected end of input near line {0}.", lastToken.Line));
            }

            tokens.ConsumeSpaces();

            string indexName = "";

            if (tokens.InpectNextToken() != null)
            {
                if (tokens.InpectNextToken().Value.Substring(0, 1) == ":")
                {
                    indexName = tokens.GetNextToken().Value;
                }
            }

            for (int i = 0; i < counter; i++)
            {
                if (!string.IsNullOrEmpty(indexName))
                {
                    scopedFuncArgs.Add(indexName, i.ToString());
                }
                repeatStmTokens.Reset();
                this.Compile(repeatStmTokens, scopedFuncArgs);
                scopedFuncArgs.Remove(indexName);
            }

            this.Next(tokens, funcArgs);
        }
Пример #4
0
 private void FUNC_FORWARD(Tokens tokens, FuncArgs funcArgs)
 {
     this.FUNC_DIRECTION(tokens, 0, funcArgs);
 }