Пример #1
0
        /// <summary>
        /// Initialize
        /// </summary>
        public Interpreter()
        {
            _settings = new LangSettings();

            // Initialzie the context.
            _context = new Context();
            _context.Settings = _settings;
            _context.Limits.Init();

            _memory = _context.Memory;
            _parser = new Parser(_context);
            _parser.Settings = _settings;
            InitSystemFunctions();
        }
Пример #2
0
 /// <summary>
 /// Initialize the combinator.
 /// </summary>
 /// <param name="parser">The core parser</param>
 /// <param name="tokenIt">The token iterator</param>
 public virtual void Init(Parser parser, TokenIterator tokenIt)
 {
     _parser = parser;
     _tokenIt = tokenIt;
 }
Пример #3
0
        /// <summary>
        /// Parses parameters.
        /// </summary>
        /// <param name="args">The list of arguments to store.</param>
        /// <param name="tokenIt">The token iterator</param>
        /// <param name="parser">The parser</param>
        /// <param name="meta">The function meta for checking parameters</param>
        /// <param name="expectParenthesis">Whether or not to expect parenthis to designate the start of the parameters.</param>
        /// <param name="enableNewLineAsEnd">Whether or not to treat a newline as end</param>
        public static void ParseFuncParameters(List<Expr> args, TokenIterator tokenIt, Parser parser, bool expectParenthesis, bool enableNewLineAsEnd, FunctionMetaData meta)
        {
            int totalParameters = 0;
            if (tokenIt.NextToken.Token == Tokens.LeftParenthesis)
                expectParenthesis = true;

            // START with check for "("
            if (expectParenthesis) tokenIt.Expect(Tokens.LeftParenthesis);

            bool passNewLine = !enableNewLineAsEnd;
            var endTokens = BuildEndTokens(enableNewLineAsEnd, meta);

            int totalNamedParams = 0;
            var hasMetaArguments = meta != null && meta.ArgumentNames != null && meta.ArgumentNames.Count > 0;
            while (true)
            {
                Expr exp = null;

                // Check for end of statment or invalid end of script.
                if (parser.IsEndOfParameterList(Tokens.RightParenthesis, enableNewLineAsEnd))
                    break;

                if (tokenIt.NextToken.Token == Tokens.Comma)
                    tokenIt.Advance();

                var token = tokenIt.NextToken.Token;
                var peek = tokenIt.Peek().Token;

                var isVar = parser.Context.Symbols.Contains(token.Text);
                var isParamNameMatch = hasMetaArguments && meta.ArgumentsLookup.ContainsKey(token.Text);
                var isKeywordParamName = token.Kind == TokenKind.Keyword && isParamNameMatch;

                // CASE 1: Named params for external c# object method calls
                // CASE 2: Named params for internal script functions ( where we have access to its param metadata )
                if (   (meta == null && token.Kind == TokenKind.Ident && peek == Tokens.Colon )
                    || (token.Kind == TokenKind.Ident && isParamNameMatch && !isVar)
                    || (token.Kind == TokenKind.Ident && !isParamNameMatch && !isVar && peek == Tokens.Colon)
                    || (isKeywordParamName && !isVar ) )
                {
                    var paramName = token.Text;
                    var namedParamToken = tokenIt.NextToken;
                    tokenIt.Advance();

                    // Advance and check if ":"
                    if (tokenIt.NextToken.Token == Tokens.Colon)
                        tokenIt.Advance();

                    exp = parser.ParseExpression(endTokens, true, false, true, passNewLine, true);
                    exp = Exprs.NamedParam(paramName, exp, namedParamToken);

                    args.Add(exp);
                    totalNamedParams++;
                }
                // CASE 2: Name of variable being passed to function is same as one of the parameter names.
                else if (isVar && hasMetaArguments && meta.ArgumentsLookup.ContainsKey(token.Text))
                {
                    // Can not have normal parameters after named parameters.
                    if (totalNamedParams > 0)
                        throw tokenIt.BuildSyntaxException("Un-named parameters must come before named parameters");

                    var next = tokenIt.Peek();
                    if (next.Token.Kind == TokenKind.Symbol)
                        exp = parser.ParseExpression(endTokens, true, false, true, passNewLine, false);
                    else
                        exp = parser.ParseIdExpression();
                    args.Add(exp);
                }
                // CASE 3: Normal param
                else
                {
                    // Can not have normal parameters after named parameters.
                    if (totalNamedParams > 0)
                        throw tokenIt.BuildSyntaxException("Un-named parameters must come before named parameters");

                    exp = parser.ParseExpression(endTokens, true, false, true, passNewLine, true);
                    args.Add(exp);
                }
                totalParameters++;
                parser.Context.Limits.CheckParserFunctionParams(exp, totalParameters);

                // Check for end of statment or invalid end of script.
                if (parser.IsEndOfParameterList(Tokens.RightParenthesis, enableNewLineAsEnd))
                    break;

                // Advance if not using fluent-parameters
                if(meta == null)
                    tokenIt.Expect(Tokens.Comma);
            }

            // END with check for ")"
            if (expectParenthesis) tokenIt.Expect(Tokens.RightParenthesis);
        }
Пример #4
0
 /// <summary>
 /// initializes this phase.
 /// </summary>
 public ParsePhase(Parser parser)
 {
     _parser = parser;
     this.Name = "ast-parsing";
 }
Пример #5
0
        /// <summary>
        /// Executes the using.
        /// </summary>
        public override object DoEvaluate(IAstVisitor visitor)
        {
            var source = Source;

            // Case 1: System namespace
            if (source.StartsWith("sys"))
            {
                // TODO: Register the types found in the sys.namespace.
            }
            // Case 2: Relative file path
            else if (source.StartsWith(".."))
            {
                // TODO: Get absolute path of file
            }
            // Case 3: Absolution file path from root
            else if (source.StartsWith("\\"))
            {
                // TODO: Get absolute path of file
            }
            // Case 4: Envrionment variable path
            else if (source.StartsWith("env"))
            {
                // TODO: Get absolute path of file
            }
            // Case 5: File path
            else
            {
                if (!File.Exists(source))
                    throw new FileNotFoundException("File : " + source + " does not exist");
            }

            var script = System.IO.File.ReadAllText(source);
            Context ctx = null;
            var scriptParser = new Parser(ctx);
            scriptParser.Parse(script);
            Parser parser = null;
            parser.Statements.AddRange(scriptParser.Statements);
            return LObjects.Null;
        }