Inheritance: IVisitable
Exemple #1
0
        public override void Visit(MethodModel method)
        {
            if (method.Class != null)
            {
                if (method.Class.Name.EndsWith(" class"))
                    this.writer.Write(string.Format("{0}Class.prototype['{1}'] = function(", method.Class.Name.Substring(0, method.Class.Name.Length - " class".Length), ToMethodName(method.Selector)));
                else
                    this.writer.Write(string.Format("{0}.prototype['{1}'] = function(", method.Class.Name, ToMethodName(method.Selector)));
            }
            else
            {
                this.writer.Write(string.Format("function {0}(", ToMethodName(method.Selector)));
            }

            int nparameters = 0;

            foreach (string name in method.ParameterNames)
            {
                if (nparameters > 0)
                    this.writer.Write(", ");
                this.writer.Write(name);
                nparameters++;
            }

            this.writer.WriteLine(")");
            this.writer.WriteLineStart("{");
            this.writer.WriteLine("var self = this;");
            
            // TODO Review local variable for block returns
            if (method.HasBlock())
                this.writer.WriteLine("var __context = {};");

            this.writer.WriteLine(string.Format("console.log('{0}');", ToMethodName(method.Selector)));

            foreach (string locname in method.LocalVariables)
                this.writer.WriteLine(string.Format("var {0} = null", ToVariableName(locname)));

            MethodModel previousMethod = this.CurrentMethod;

            try
            {
                this.CurrentMethod = method;
                this.Visit(method.Body);
            }
            finally
            {
                this.CurrentMethod = previousMethod;
            }

            if (method.Class != null)
                this.writer.WriteLineEnd("};");
            else
                this.writer.WriteLineEnd("}");
        }
Exemple #2
0
        public override void Visit(MethodModel method)
        {
            if (method.ParameterNames != null)
                foreach (var parname in method.ParameterNames)
                    this.block.CompileArgument(parname);

            if (method.LocalVariables != null)
                foreach (var locname in method.LocalVariables)
                    this.block.CompileLocal(locname);

            this.Visit(method.Body);
        }
Exemple #3
0
 public abstract void Visit(MethodModel method);
Exemple #4
0
 public void CompileMethod(MethodModel method)
 {
     method.Visit(this);
 }
Exemple #5
0
        public MethodModel ParseMethod()
        {
            string name = this.ParseNameOrOperator();
            string selector;
            IList<string> parameterNames = new List<string>();
            IList<string> localVariables = new List<string>();

            if (IsUnarySelector(name))
                selector = name;
            else if (IsBinarySelector(name))
            {
                selector = name;
                parameterNames.Add(this.ParseSimpleName());
            }
            else
            {
                selector = name;

                parameterNames.Add(this.ParseSimpleName());

                name = this.TryParseMultipleKeywordSelector();

                while (name != null)
                {
                    selector += name;
                    parameterNames.Add(this.ParseSimpleName());
                    name = this.TryParseMultipleKeywordSelector();
                }
            }

            if (this.TryParseBar())
            {
                string varname;

                while ((varname = this.TryParseSimpleName()) != null)
                {
                    localVariables.Add(varname);
                }

                this.ParseBar();
            }

            IEnumerable<IExpression> body = this.ParseExpressions();

            MethodModel model = new MethodModel(selector, parameterNames, localVariables, body, this.@class, this.isClassMethod);

            Token token = this.NextToken();

            if (token != null)
                throw new ParserException(string.Format("Unexpected '{0}'", token.Value));

            return model;
        }