Inheritance: IVisitable
Exemplo n.º 1
0
        public void GetEmptyNamesFromNull()
        {
            ClassModel @class = new ClassModel("AClass", string.Empty, null, null, false, null, null);

            Assert.AreEqual(string.Empty, @class.ClassVariableNamesAsString);
            Assert.AreEqual(string.Empty, @class.InstanceVariableNamesAsString);
        }
Exemplo n.º 2
0
        public override void Visit(ClassModel @class)
        {
            // TODO review
            if (@class.SuperClassName == null)
                return;

            string instanceVariableNames = string.Empty;
            string classVariableNames = string.Empty;

            if (@class.InstanceVariableNames != null && @class.InstanceVariableNames.Count > 0)
            {
                instanceVariableNames = @class.InstanceVariableNames.First();

                foreach (var name in @class.InstanceVariableNames.Skip(1))
                    instanceVariableNames += " " + name;
            }

            if (@class.ClassVariableNames != null && @class.ClassVariableNames.Count > 0)
            {
                classVariableNames = @class.ClassVariableNames.First();

                foreach (var name in @class.ClassVariableNames.Skip(1))
                    classVariableNames += " " + name;
            }

            this.WriteLine(string.Format(
                "Smalltalk.{0}.subclass_instanceVariableNames_classVariableNames_('{1}', '{2}', '{3}');",
                @class.SuperClassName,
                @class.Name,
                instanceVariableNames,
                classVariableNames));

            foreach (MethodModel method in @class.InstanceMethods)
                method.Visit(this);
        }
Exemplo n.º 3
0
 public MethodModel(string selector, IList<string> parameterNames, IList<string> localVariables, IEnumerable<IExpression> body, ClassModel @class, bool isClassMethod)
 {
     this.selector = selector;
     this.parameterNames = parameterNames;
     this.localVariables = localVariables;
     this.body = body;
     this.@class = @class;
     this.isClassMethod = isClassMethod;
 }
Exemplo n.º 4
0
        public void GetNames()
        {
            ClassModel @class = new ClassModel("AClass", string.Empty, new List<string>() { "x", "y" }, new List<string>() { "count" }, false, null, null);

            Assert.AreEqual("count", @class.ClassVariableNamesAsString);
            Assert.AreEqual("x y", @class.InstanceVariableNamesAsString);
            Assert.AreEqual(string.Empty, @class.Category);
            Assert.AreEqual(string.Empty, @class.PoolDictionariesAsString);
        }
Exemplo n.º 5
0
 public ClassModel(string name, IList<string> instanceVariableNames, IList<string> classVariableNames, bool isvariable, IList<string> poolDictionaries, string category)
 {
     this.name = name;
     this.instanceVariableNames = instanceVariableNames ?? new List<string>();
     this.classVariableNames = classVariableNames ?? new List<string>();
     this.classMethods = new List<MethodModel>();
     this.instanceMethods = new List<MethodModel>();
     if (!name.EndsWith(" class"))
         this.metaclass = new ClassModel(name + " class", this.superclassname == null ? null : this.superclassname + " class", null, null, isvariable, null, null);
     this.isvariable = isvariable;
     this.poolDictionaries = poolDictionaries ?? new List<string>();
     this.category = category ?? string.Empty;
 }
Exemplo n.º 6
0
        public override void Visit(ClassModel @class)
        {
            string selector = @class.IsVariable ? "variableSubclass:" : "subclass:";
            selector += "instanceVariableNames:classVariableNames:poolDictionaries:category:";

            this.block.CompileGet(@class.SuperClassName);
            this.block.CompileConstant(@class.Name);
            this.block.CompileGetConstant(@class.InstanceVariableNamesAsString);
            this.block.CompileGetConstant(@class.ClassVariableNamesAsString);
            this.block.CompileGetConstant(@class.PoolDictionariesAsString);
            this.block.CompileGetConstant(@class.Category);
            this.block.CompileSend(selector);
        }
Exemplo n.º 7
0
        public override void Visit(ClassModel @class)
        {
            this.writer.WriteLine(string.Format("function {0}Class()", @class.Name));
            this.writer.WriteLineStart("{");
            this.writer.WriteLineEnd("}");

            this.writer.WriteLine(string.Format("function {0}()", @class.Name));
            this.writer.WriteLineStart("{");
            this.writer.WriteLineEnd("}");

            this.writer.WriteLine(string.Format("{0}.prototype.__class = {0}Class.prototype;", @class.Name));
            this.writer.WriteLine(string.Format("{0}.classPrototype = {0}Class.prototype;", @class.Name));

            this.writer.WriteLine(string.Format("{0}Class.prototype['_basicNew'] = function() {{ return new {0}(); }};", @class.Name));

            if (@class.SuperClassName != null && @class.SuperClassName != @class.Name)
            {
                this.writer.WriteLine(string.Format("{0}Class.prototype.__proto__ = {1}Class.prototype;", @class.Name, @class.SuperClassName));
                this.writer.WriteLine(string.Format("{0}.prototype.__proto__ = {1}.prototype;", @class.Name, @class.SuperClassName));
            }

            foreach (string name in @class.InstanceVariableNames)
                this.writer.WriteLine(string.Format("{0}.prototype.${1} = null;", @class.Name, name));

            if (@class.SuperClassName != null && @class.SuperClassName != @class.Name)
            {
                this.writer.WriteLine(string.Format("{0}Class.__super = {1}Class;", @class.Name, @class.SuperClassName));
                this.writer.WriteLine(string.Format("{0}.__super = {1};", @class.Name, @class.SuperClassName));
            }

            // TODO Review class variables. Where? at Class? at Class.prototype?
            foreach (string name in @class.ClassVariableNames)
                this.writer.WriteLine(string.Format("{0}Class.${1} = null;", @class.Name, name));

            foreach (MethodModel method in @class.InstanceMethods)
                method.Visit(this);
        }
Exemplo n.º 8
0
 public ClassModel(string name, ClassModel superclass, IList<string> instanceVariableNames, IList<string> classVariableNames, bool isvariable, IList<string> poolDictionaries, string category)
     : this(name, instanceVariableNames, classVariableNames, isvariable, poolDictionaries, category)
 {
     this.superclass = superclass;
 }
Exemplo n.º 9
0
 private static MethodModel ParseMethod(string text)
 {
     ModelParser parser = new ModelParser(text);
     ClassModel classModel = new ClassModel("MyClass", (ClassModel)null, null, null, false, null, null);
     return parser.ParseMethod(classModel, false);
 }
Exemplo n.º 10
0
 public void CompileSimpleClassWithSimpleSetMethod()
 {
     ClassModel @class = new ClassModel("AClass", (ClassModel)null, new List<string>() { "x", "y" }, new List<string>(), false, null, null);
     ModelParser parser = new ModelParser("x: newX x := newX");
     MethodModel method = parser.ParseMethod(@class, false);
     @class.InstanceMethods.Add(method);
     this.compiler.CompileClass(@class);
     this.writer.Close();
     string output = this.writer.ToString();
     Assert.IsTrue(ContainsLine(output, "function AClass()"));
     Assert.IsTrue(ContainsLine(output, "{"));
     Assert.IsTrue(ContainsLine(output, "}"));
     Assert.IsTrue(ContainsLine(output, "AClass.prototype.$x = null;"));
     Assert.IsTrue(ContainsLine(output, "AClass.prototype.$y = null;"));
     Assert.IsTrue(ContainsLine(output, "AClass.prototype['x_'] = function(newX)"));
     Assert.IsTrue(ContainsLine(output, "{"));
     Assert.IsTrue(ContainsLine(output, "var self = this;"));
     Assert.IsTrue(ContainsLine(output, "self.$x = newX;"));
     Assert.IsTrue(ContainsLine(output, "};"));
 }
Exemplo n.º 11
0
 public void CompileSimpleClassWithInstanceVariables()
 {
     ClassModel @class = new ClassModel("AClass", (ClassModel)null, new List<string>() { "x", "y" }, new List<string>(), false, null, null);
     this.compiler.CompileClass(@class);
     this.writer.Close();
     string output = this.writer.ToString();
     Assert.IsTrue(ContainsLine(output, "function AClass()"));
     Assert.IsTrue(ContainsLine(output, "{"));
     Assert.IsTrue(ContainsLine(output, "}"));
     Assert.IsTrue(ContainsLine(output, "AClass.prototype.$x = null;"));
     Assert.IsTrue(ContainsLine(output, "AClass.prototype.$y = null;"));
 }
Exemplo n.º 12
0
        public void ParseInstanceMethodReturningInstanceVariable()
        {
            ClassModel @class = new ClassModel("AClass", (ClassModel)null, new List<string>() { "x", "y" }, new List<string>(), false, null, null);
            ModelParser parser = new ModelParser("x ^x");
            MethodModel method = parser.ParseMethod(@class, false);

            Assert.IsNotNull(method);
            Assert.AreEqual("x", method.Selector);
            Assert.AreEqual(0, method.ParameterNames.Count);
            Assert.AreEqual(0, method.LocalVariables.Count);
            Assert.IsInstanceOfType(method.Body.First(), typeof(ReturnExpression));

            ReturnExpression rexpression = (ReturnExpression)method.Body.First();
            Assert.IsInstanceOfType(rexpression.Expression, typeof(InstanceVariableExpression));
        }
Exemplo n.º 13
0
        private void ProcessMessageExpression(CodeModel model, MessageExpression expression)
        {
            // TODO implements weakSubclass
            if (expression.Selector.StartsWith("subclass:") || expression.Selector.StartsWith("weakSubclass:") || expression.Selector.StartsWith("variableSubclass:"))
            {
                bool isvariable = expression.Selector.StartsWith("variableSubclass:");
                SymbolExpression symbol = (SymbolExpression)expression.Arguments.First();
                VariableExpression variable = (VariableExpression)expression.Target;

                ClassModel super = null;

                if (variable.Name != null && variable.Name != symbol.Symbol)
                    //// TODO review quick hack if class is not defined yet
                    if (model.HasClass(variable.Name))
                        super = model.GetClass(variable.Name);

                ClassModel @class;

                if (super != null || variable.Name == null)
                    @class = new ClassModel(symbol.Symbol, super, GetInstanceVariableNames(expression), GetClassVariableNames(expression), isvariable, GetPoolDictionariesNames(expression), GetCategory(expression));
                else
                    @class = new ClassModel(symbol.Symbol, variable.Name, GetInstanceVariableNames(expression), GetClassVariableNames(expression), isvariable, GetPoolDictionariesNames(expression), GetCategory(expression));

                model.AddElement(@class);
            }
        }
Exemplo n.º 14
0
 public ClassVariableExpression(string name, ClassModel @class)
 {
     this.name = name;
     this.@class = @class;
 }
Exemplo n.º 15
0
 public void CompileClass(ClassModel @class)
 {
     @class.Visit(this);
 }
Exemplo n.º 16
0
 public void CompileSimpleClass()
 {
     ClassModel @class = new ClassModel("AClass", (ClassModel)null, null, null, false, null, null);
     this.compiler.CompileClass(@class);
     this.writer.Close();
     string output = this.writer.ToString();
     Assert.IsTrue(ContainsLine(output, "function AClassClass()"));
     Assert.IsTrue(ContainsLine(output, "function AClass()"));
     Assert.IsTrue(ContainsLine(output, "{"));
     Assert.IsTrue(ContainsLine(output, "}"));
     Assert.IsTrue(ContainsLine(output, "AClass.prototype.__class = AClassClass.prototype;"));
     Assert.IsTrue(ContainsLine(output, "AClass.classPrototype = AClassClass.prototype;"));
     Assert.IsTrue(ContainsLine(output, "AClassClass.prototype['_basicNew'] = function() { return new AClass(); };"));
 }
Exemplo n.º 17
0
 public abstract void Visit(ClassModel @class);
Exemplo n.º 18
0
 public MethodModel ParseMethod(ClassModel @class, bool isClassMethod)
 {
     this.@class = @class;
     this.isClassMethod = isClassMethod;
     return this.ParseMethod();
 }