コード例 #1
0
        public Method Walk(Class c)
        {
            method = new Method();

            // modifiers
            method.Modifiers = new ModifierListTranslator(node.GetChild(0)).Walk();
            method.DeclaringType = c;

            int i = 1;
            ITree child = node.GetChild(i++);

            // generic
            if (child.Type == (int) JavaNodeType.GENERIC_TYPE_PARAM_LIST)
            {
                child = node.GetChild(i++);
            }

            // type
            if (child.Type == (int) JavaNodeType.TYPE)
            {
                method.ReturnType = new TypeTranslator(child).Walk();

                child = node.GetChild(i++);
            }
            else
            {
                method.ReturnType = PrimativeTypes.Void;
            }

            // name
            method.Name = new IdentifierTranslator(child).Walk();
            child = node.GetChild(i++);

            // parameters
            method.Parameters.AddRange(new MethodParameterTranslator(child).Walk());
            child = node.GetChild(i++);

            // arrayDeclaratorList
            if (child.Type == (int) JavaNodeType.ARRAY_DECLARATOR_LIST)
            {
                //TODO; method.ArrayLevels = TypeTranslator.ProcessArray(child);

                child = node.GetChild(i++);
            }

            // throws
            if (child.Type == (int) JavaNodeType.THROWS_CLAUSE)
            {
                child = node.GetChild(i);
            }

            // body?
            if (child.Type == (int) JavaNodeType.BLOCK_SCOPE)
            {
                method.Body = new BlockTranslator(child).Walk();
            }

            return method;
        }
コード例 #2
0
ファイル: Class.cs プロジェクト: will14smith/JavaCompiler
        public override void Resolve(List<Package> imports)
        {
            base.Resolve(imports);

            Super = ClassLocator.Find(Super, imports) as Class;

            if (Super != null) Super.Resolve(imports);
        }
コード例 #3
0
 public static Field GetField(Class c, CompileFieldInfo field, List<CompileConstant> constants)
 {
     return new Field
                {
                    DeclaringType = c,
                    Name = GetUtf8(field.Name, constants),
                    Modifiers = field.Modifiers,
                    ReturnType = GetType(field.Descriptor, constants),
                };
 }
コード例 #4
0
        public static void MakeStringBuffer(ByteCodeGenerator generator, Class sb)
        {
            var sbInit = (Method)sb.Constructors.First(x => x.Parameters.Count == 0);

            var sbIndex = generator.Manager.AddConstantClass(sb);

            generator.EmitNew(sbIndex, sb);
            generator.Emit(OpCodeValue.dup);

            new MemberItem(generator, sbInit, true).Invoke();
        }
コード例 #5
0
        public Constructor Walk(Class c)
        {
            var constructor = new Constructor
                                  {
                                      Modifiers = new ModifierListTranslator(node.GetChild(0)).Walk(),
                                      DeclaringType = c
                                  };

            constructor.Parameters.AddRange(new MethodParameterTranslator(node.GetChild(1)).Walk());

            constructor.Body = new ConstructorBlockTranslator(node.GetChild(2)).Walk();

            return constructor;
        }
コード例 #6
0
        public List<Field> Walk(Class c)
        {
            List<VarDeclarationNode> varDeclarations = new VarDeclarationTranslator(node).Walk();

            // TODO: Field initialization

            return varDeclarations.Select(declaration =>
                                          new Field
                                              {
                                                  Name = declaration.Name,
                                                  DeclaringType = c,
                                                  ReturnType = declaration.Type,
                                                  Modifiers = declaration.Modifiers
                                              }).ToList();
        }
コード例 #7
0
        public static Method GetMethod(Class c, CompileMethodInfo method, List<CompileConstant> constants)
        {
            var m = new Method
                        {
                            DeclaringType = c,
                            Name = GetUtf8(method.Name, constants),
                            Modifiers = method.Modifiers,
                        };

            string methodDescriptor = GetUtf8(method.Descriptor, constants);
            Tuple<List<Type>, Type> methodTypes = GetMethodTypeFromDescriptor(methodDescriptor);

            m.Parameters.AddRange(methodTypes.Item1.Select(x => new Method.Parameter { Type = x }));
            m.ReturnType = methodTypes.Item2;

            return m;
        }
コード例 #8
0
        public Class Walk()
        {
            @class = new Class
                         {
                             Modifiers = new ClassModifierListTranslator(node.GetChild(0)).Walk(),
                             Name = new IdentifierTranslator(node.GetChild(1)).Walk()
                         };

            int i = 2;
            ITree child = node.GetChild(i++);

            if ((JavaNodeType)child.Type == JavaNodeType.GENERIC_TYPE_PARAM_LIST)
            {
                child = node.GetChild(i++);
            }

            if ((JavaNodeType)child.Type == JavaNodeType.EXTENDS_CLAUSE)
            {
                @class.Super = new TypeTranslator(child.GetChild(0)).Walk() as Class;

                child = node.GetChild(i++);
            }
            else
            {
                @class.Super = BuiltinTypes.Object;
            }

            if ((JavaNodeType)child.Type == JavaNodeType.IMPLEMENTS_CLAUSE)
            {
                child = node.GetChild(i);
            }

            ITree body = child;

            WalkBody(body);

            if (@class.Constructors.Count == 0)
            {
                @class.Constructors.Add(new Constructor { DeclaringType = @class, Body = new MethodTree(), Modifiers = Modifier.Public });
            }

            return @class;
        }
コード例 #9
0
        public static Type Load(Stream stream)
        {
            var reader = new EndianBinaryReader(EndianBitConverter.Big, stream);

            int magic = reader.ReadInt32();

            short minorVersion = reader.ReadInt16();
            short majorVersion = reader.ReadInt16();

            List<CompileConstant> constants = new List<CompileConstant>(ReadConstants(reader));

            var modifiers = (ClassModifier)reader.ReadInt16();

            short thisClass = reader.ReadInt16();
            short superClass = reader.ReadInt16();

            IEnumerable<short> interfaces = ReadInterfaces(reader);
            IEnumerable<CompileFieldInfo> fields = ReadFields(reader, constants);
            IEnumerable<CompileMethodInfo> methods = ReadMethods(reader, constants);
            CompileAttribute[] attributes = ReadAttributes(reader, constants);

            var c = new Class
                        {
                            Name = GetClass(thisClass, constants).Name,
                            Modifiers = modifiers,
                            Super = (superClass == 0 ? null : GetClass(superClass, constants)) as Class
                        };

            c.Interfaces.AddRange(interfaces.Select(x => GetClass(x, constants)).OfType<Interface>());
            c.Fields.AddRange(fields.Select(x => GetField(c, x, constants)));

            List<Method> javaMethods = methods.Select(x => GetMethod(c, x, constants)).ToList();

            c.Methods.AddRange(javaMethods.Where(x => x.Name != "<init>"));
            c.Constructors.AddRange(javaMethods.Where(x => x.Name == "<init>").Select(x => (Constructor)x));

            return c;
        }
コード例 #10
0
 public ClassCompiler(Class @class)
 {
     this.@class = @class;
 }