상속: IVisitable
예제 #1
0
        public void CompilePharoCorePointForBrowser()
        {
            ChunkReader chunkCoreReader = new ChunkReader(@"PharoCoreKernelObjects.st");
            CodeReader coreReader = new CodeReader(chunkCoreReader);
            ChunkReader chunkReader = new ChunkReader(@"PharoCorePoint.st");
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            coreReader.Process(model);
            reader.Process(model);

            this.compiler.Visit(model);
            this.writer.Close();
            string output = this.writer.ToString();

            Assert.IsTrue(ContainsLine(output, "AjTalk = function() {"));
            Assert.IsTrue(ContainsLine(output, "var send = base.send;"));
            Assert.IsTrue(ContainsLine(output, "var sendSuper = base.sendSuper;"));

            Assert.IsTrue(ContainsLine(output, "function Point()"));
            Assert.IsTrue(ContainsLine(output, "PointClass.__super = ObjectClass;"));
            Assert.IsTrue(ContainsLine(output, "Point.__super = Object;"));
            Assert.IsTrue(ContainsLine(output, "PointClass.prototype.__proto__ = ObjectClass.prototype;"));
            Assert.IsTrue(ContainsLine(output, "Point.prototype.__proto__ = Object.prototype;"));
            Assert.IsTrue(ContainsLine(output, "Point.prototype.$x = null;"));
            Assert.IsTrue(ContainsLine(output, "Point.prototype.$y = null;"));

            Assert.IsTrue(ContainsLine(output, "Point : Point"));
        }
예제 #2
0
        public void CompilePharoCoreKernelObjectsForNodeJs()
        {
            ChunkReader chunkReader = new ChunkReader(@"PharoCoreKernelObjects.st");
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            reader.Process(model);

            this.compiler.Visit(model);
            this.writer.Close();
            string output = this.writer.ToString();

            // TODO more tests
            Assert.IsTrue(ContainsLine(output, "function Object()"));
            Assert.IsTrue(ContainsLine(output, "ObjectClass.__super = ProtoObjectClass;"));
            Assert.IsTrue(ContainsLine(output, "Object.__super = ProtoObject;"));
            Assert.IsTrue(ContainsLine(output, "ObjectClass.prototype.__proto__ = ProtoObjectClass.prototype;"));
            Assert.IsTrue(ContainsLine(output, "Object.prototype.__proto__ = ProtoObject.prototype;"));
            Assert.IsTrue(ContainsLine(output, "BooleanClass.prototype.__proto__ = ObjectClass.prototype;"));
            Assert.IsTrue(ContainsLine(output, "Boolean.prototype.__proto__ = Object.prototype;"));
            Assert.IsTrue(ContainsLine(output, "function Boolean()"));
            Assert.IsTrue(ContainsLine(output, "exports.Object = Object;"));
            Assert.IsTrue(ContainsLine(output, "exports.Boolean = Boolean;"));
            Assert.IsTrue(ContainsLine(output, "exports.ProtoObject = ProtoObject;"));
        }
예제 #3
0
        public override void Visit(CodeModel model)
        {
            // TODO Review browser dependent preface code
            this.WriteLine("AjTalk = function() {");
            this.WriteLine("var send = base.send;");
            this.WriteLine("var sendSuper = base.sendSuper;");

            base.Visit(model);

            this.WriteLine();

            int n = 0;

            this.WriteLineStart("return {");

            foreach (var element in model.Elements)
            {
                if (!(element is ClassModel))
                    continue;

                if (n > 0)
                    this.WriteLine(",");

                this.Write(string.Format("{0} : {0}", ((ClassModel)element).Name));
                n++;
            }

            this.WriteLine(string.Empty);
            this.WriteLineEnd("}");

            this.WriteLine("}();");
        }
예제 #4
0
        public void CompilePharoCorePointForNodeJs()
        {
            ChunkReader chunkCoreReader = new ChunkReader(@"PharoCoreKernelObjects.st");
            CodeReader coreReader = new CodeReader(chunkCoreReader);
            ChunkReader chunkReader = new ChunkReader(@"PharoCorePoint.st");
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            coreReader.Process(model);
            reader.Process(model);

            this.compiler.Visit(model);
            this.writer.Close();
            string output = this.writer.ToString();

            Assert.IsTrue(ContainsLine(output, "var base = require('./js/ajtalk-base.js');"));
            Assert.IsTrue(ContainsLine(output, "var send = base.send;"));
            Assert.IsTrue(ContainsLine(output, "var sendSuper = base.sendSuper;"));
            Assert.IsTrue(ContainsLine(output, "var primitives = require('./js/ajtalk-primitives.js');"));

            Assert.IsTrue(ContainsLine(output, "function Point()"));
            Assert.IsTrue(ContainsLine(output, "PointClass.__super = ObjectClass;"));
            Assert.IsTrue(ContainsLine(output, "Point.__super = Object;"));
            Assert.IsTrue(ContainsLine(output, "PointClass.prototype.__proto__ = ObjectClass.prototype;"));
            Assert.IsTrue(ContainsLine(output, "Point.prototype.__proto__ = Object.prototype;"));
            Assert.IsTrue(ContainsLine(output, "exports.Point = Point;"));
            Assert.IsTrue(ContainsLine(output, "Point.prototype.$x = null;"));
            Assert.IsTrue(ContainsLine(output, "Point.prototype.$y = null;"));
        }
예제 #5
0
        public void Process(CodeModel model)
        {
            for (string chunk = this.reader.GetChunk(); chunk != null; chunk = this.reader.GetChunk())
            {
                bool isreader = false;

                if (chunk.StartsWith("!"))
                {
                    chunk = chunk.Substring(1);
                    isreader = true;
                }

                ModelParser parser = new ModelParser(chunk);
                IExpression expression = parser.ParseExpression();

                if (isreader)
                {
                    if (expression is MessageExpression && ((MessageExpression)expression).Selector.Contains("methodsFor:"))
                        this.ProcessMethods(model, (MessageExpression)expression);
                    else if (expression is MessageExpression && ((MessageExpression)expression).Selector.Contains("commentStamp:"))
                        this.ProcessComment(model, (MessageExpression)expression);
                    else
                        this.ProcessReader();
                }
                else
                {
                    if (expression is MessageExpression)
                        this.ProcessMessageExpression(model, (MessageExpression)expression);
                }
            }
        }
예제 #6
0
파일: Program.cs 프로젝트: ajlopez/AjTalk
        public static void Main(string[] args)
        {
            StreamWriter swriter = new StreamWriter(GetOutputFileName(args));
            SourceWriter writer = new SourceWriter(swriter);
            CodeModel model = new CodeModel();
            Compilers.Javascript.Compiler compiler = null;

            string target = GetTarget(args);

            if (target == "node")
                compiler = new NodeCompiler(writer);
            else if (target == "browser")
                compiler = new BrowserCompiler(writer);
            else if (target == "vm")
                compiler = new VirtualMachineCompiler(writer);
            else
                throw new ArgumentException("Invalid target");

            foreach (string filename in GetFileNames(args))
            {
                ChunkReader chunkReader = new ChunkReader(filename);
                CodeReader reader = new CodeReader(chunkReader);

                reader.Process(model);
            }

            compiler.Visit(model);

            swriter.Close();
        }
예제 #7
0
        public void CompileEmptyModel()
        {
            CodeModel model = new CodeModel();
            this.compiler.Visit(model);
            this.writer.Close();
            string output = this.writer.ToString();

            Assert.IsTrue(ContainsLine(output, "var ajtalk;"));
            Assert.IsTrue(ContainsLine(output, "var Smalltalk;"));
            Assert.IsTrue(ContainsLine(output, "if (typeof(ajtalk) === 'undefined')"));
            Assert.IsTrue(ContainsLine(output, "ajtalk = require('ajtalk.js');"));
            Assert.IsTrue(ContainsLine(output, "if (typeof(Smalltalk) === 'undefined')"));
            Assert.IsTrue(ContainsLine(output, "Smalltalk = ajtalk.Smalltalk;"));
        }
예제 #8
0
        public void ProcessFileOut01()
        {
            ChunkReader chunkReader = new ChunkReader(@"FileOut01.st");
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            reader.Process(model);

            Assert.IsNotNull(model.GetClass("Object"));
            Assert.AreEqual(3, model.Elements.Count());

            Assert.IsInstanceOfType(model.Elements.First(), typeof(ClassModel));
            Assert.IsInstanceOfType(model.Elements.Skip(1).First(), typeof(MethodModel));
            Assert.IsInstanceOfType(model.Elements.Skip(2).First(), typeof(MethodModel));
        }
예제 #9
0
        public override void Visit(CodeModel model)
        {
            this.WriteLine("var ajtalk;");
            this.WriteLine("var Smalltalk;");
            this.WriteLine();

            this.WriteLineStart("if (typeof(ajtalk) === 'undefined')");
            this.WriteLine("ajtalk = require('ajtalk.js');");
            this.WriteLineEnd(string.Empty);

            this.WriteLineStart("if (typeof(Smalltalk) === 'undefined')");
            this.WriteLine("Smalltalk = ajtalk.Smalltalk;");
            this.WriteLineEnd(string.Empty);

            base.Visit(model);
        }
예제 #10
0
        public override void Visit(CodeModel model)
        {
            // TODO Review Node.js dependent preface code
            this.WriteLine("var base = require('./js/ajtalk-base.js');");
            this.WriteLine("var send = base.send;");
            this.WriteLine("var sendSuper = base.sendSuper;");
            this.WriteLine("var primitives = require('./js/ajtalk-primitives.js');");

            base.Visit(model);

            this.WriteLine();

            foreach (var element in model.Elements)
            {
                if (!(element is ClassModel))
                    continue;

                // TODO Node.js module dependent
                this.WriteLine(string.Format("exports.{0} = {0};", ((ClassModel)element).Name));
            }
        }
예제 #11
0
        public void CompileSqueakKernelObjects()
        {
            ChunkReader chunkReader = new ChunkReader(@"SqueakKernelObjects.st");
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            reader.Process(model);

            this.compiler.Visit(model);
            this.writer.Close();
            string output = this.writer.ToString();

            // TODO more tests
            Assert.IsTrue(ContainsLine(output, "Smalltalk.Object.subclass_instanceVariableNames_classVariableNames_('MessageSend', 'receiver selector arguments', '');"));
        }
예제 #12
0
        public void CompileSqueakKernelObjectsForNodeJs()
        {
            ChunkReader chunkReader = new ChunkReader(@"SqueakKernelObjects.st");
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            reader.Process(model);

            this.compiler.Visit(model);
            this.writer.Close();
            string output = this.writer.ToString();

            // TODO more tests
            Assert.IsTrue(ContainsLine(output, "function Object()"));
            Assert.IsTrue(ContainsLine(output, "function Boolean()"));
            Assert.IsTrue(ContainsLine(output, "exports.Object = Object;"));
            Assert.IsTrue(ContainsLine(output, "exports.Boolean = Boolean;"));

            // Class variables in Object
            Assert.IsTrue(ContainsLine(output, "ObjectClass.$DependentsFields = null;"));
        }
예제 #13
0
        public void CompileSqueakObjectForNodeJs()
        {
            ChunkReader chunkReader = new ChunkReader(@"SqueakObject.st");
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            reader.Process(model);

            this.compiler.Visit(model);
            this.writer.Close();
            string output = this.writer.ToString();

            // TODO more tests
            Assert.IsTrue(ContainsLine(output, "var base = require('./js/ajtalk-base.js');"));
            Assert.IsTrue(ContainsLine(output, "var send = base.send;"));
            Assert.IsTrue(ContainsLine(output, "var sendSuper = base.sendSuper;"));
            Assert.IsTrue(ContainsLine(output, "var primitives = require('./js/ajtalk-primitives.js');"));

            Assert.IsTrue(ContainsLine(output, "function Object()"));

            Assert.IsTrue(ContainsLine(output, "exports.Object = Object;"));
        }
예제 #14
0
        public void CompileSqueakObject()
        {
            ChunkReader chunkReader = new ChunkReader(@"SqueakObject.st");
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            reader.Process(model);

            this.compiler.Visit(model);
            this.writer.Close();
            string output = this.writer.ToString();

            // TODO more tests
            Assert.IsTrue(ContainsLine(output, "var ajtalk;"));
            Assert.IsTrue(ContainsLine(output, "var Smalltalk;"));
        }
예제 #15
0
        public void CompilePharoCoreRectangle()
        {
            ChunkReader chunkCoreReader = new ChunkReader(@"PharoCoreKernelObjects.st");
            CodeReader coreReader = new CodeReader(chunkCoreReader);
            ChunkReader chunkReader = new ChunkReader(@"PharoCoreRectangle.st");
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            coreReader.Process(model);
            reader.Process(model);

            foreach (var element in model.Elements)
            {
                Block block = new Block();
                BytecodeCompiler compiler = new BytecodeCompiler(block);
                element.Visit(compiler);

                if (element is MethodModel && ((MethodModel)element).Body == null)
                    continue;

                Assert.IsNotNull(block.ByteCodes);
            }
        }
예제 #16
0
        internal static void CompileFile(string filename)
        {
            ChunkReader chunkReader = new ChunkReader(filename);
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            reader.Process(model);

            foreach (var element in model.Elements)
            {
                Block block = new Block();
                BytecodeCompiler compiler = new BytecodeCompiler(block);
                element.Visit(compiler);
                if (element is MethodModel && ((MethodModel)element).Body == null)
                    continue;
                Assert.IsNotNull(block.ByteCodes);
            }
        }
예제 #17
0
 public override void Visit(CodeModel model)
 {
     foreach (var element in model.Elements)
         element.Visit(this);
 }
예제 #18
0
 public abstract void Visit(CodeModel expression);
예제 #19
0
        public void CompilePharoCoreRectangle()
        {
            ChunkReader chunkCoreReader = new ChunkReader(@"PharoCoreKernelObjects.st");
            CodeReader coreReader = new CodeReader(chunkCoreReader);
            ChunkReader chunkReader = new ChunkReader(@"PharoCoreRectangle.st");
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            coreReader.Process(model);
            reader.Process(model);

            this.compiler.Visit(model);
            this.writer.Close();
            string output = this.writer.ToString();

            // TODO more tests
            Assert.IsTrue(ContainsLine(output, "function Rectangle()"));
            Assert.IsTrue(ContainsLine(output, "RectangleClass.__super = ObjectClass;"));
            Assert.IsTrue(ContainsLine(output, "Rectangle.__super = Object;"));
            Assert.IsTrue(ContainsLine(output, "RectangleClass.prototype.__proto__ = ObjectClass.prototype;"));
            Assert.IsTrue(ContainsLine(output, "Rectangle.prototype.__proto__ = Object.prototype;"));
            Assert.IsTrue(ContainsLine(output, "Rectangle.prototype.$origin = null;"));
            Assert.IsTrue(ContainsLine(output, "Rectangle.prototype.$corner = null;"));
        }
예제 #20
0
        public void CompileFileOut01()
        {
            ChunkReader chunkReader = new ChunkReader(@"FileOut01.st");
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            reader.Process(model);

            this.compiler.Visit(model);
            this.writer.Close();
            string output = this.writer.ToString();

            // TODO more tests
            Assert.IsTrue(ContainsLine(output, "function Object()"));
        }
예제 #21
0
        public void CompileSqueakObjectForBrowser()
        {
            ChunkReader chunkReader = new ChunkReader(@"SqueakObject.st");
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            reader.Process(model);

            this.compiler.Visit(model);
            this.writer.Close();
            string output = this.writer.ToString();

            // TODO more tests
            Assert.IsTrue(ContainsLine(output, "AjTalk = function() {"));
            Assert.IsTrue(ContainsLine(output, "var send = base.send;"));
            Assert.IsTrue(ContainsLine(output, "var sendSuper = base.sendSuper;"));

            Assert.IsTrue(ContainsLine(output, "function Object()"));

            Assert.IsTrue(ContainsLine(output, "Object : Object"));
        }
예제 #22
0
        public void CompilePharoCorePoint()
        {
            ChunkReader chunkReader = new ChunkReader(@"PharoCorePoint.st");
            CodeReader reader = new CodeReader(chunkReader);
            CodeModel model = new CodeModel();

            reader.Process(model);

            this.compiler.Visit(model);
            this.writer.Close();
            string output = this.writer.ToString();

            // TODO more tests
            Assert.IsTrue(ContainsLine(output, "Smalltalk.Object.subclass_instanceVariableNames_classVariableNames_('Point', 'x y', '');"));
            Assert.IsTrue(ContainsLine(output, "factor = 3['reciprocal']();"));
        }
예제 #23
0
 private void ProcessComment(CodeModel model, MessageExpression expression)
 {
     this.reader.GetChunk();
 }
예제 #24
0
        private void ProcessMethods(CodeModel model, MessageExpression expression)
        {
            string className = expression.Target.AsString();
            ClassModel @class = model.GetClass(className);

            for (string chunk = this.reader.GetChunk(); chunk != null; chunk = this.reader.GetChunk())
            {
                if (string.IsNullOrEmpty(chunk.Trim()))
                    break;
                ModelParser parser = new ModelParser(chunk);
                MethodModel method = parser.ParseMethod(@class, false);
                model.AddElement(method);
            }
        }
예제 #25
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);
            }
        }