예제 #1
0
파일: RppField.cs 프로젝트: dubik/csharprpp
 protected bool Equals(RppField other)
 {
     return Equals(Name, other.Name) && Equals(Type, other.Type) && Equals(Modifiers, other.Modifiers);
 }
예제 #2
0
        // ClassParams ::= {Annotation} [{Modifier} (‘val’ | ‘var’)] id [‘:’ ParamType] [‘=’ Expr]
        public bool ParseClassParam(out RppField classParam)
        {
            classParam = null;

            MutabilityFlag mutability = MutabilityFlag.MfUnspecified;
            if (Require(RppLexer.KW_Var))
            {
                mutability = MutabilityFlag.MfVar;
            }
            else if (Require(RppLexer.KW_Val))
            {
                mutability = MutabilityFlag.MfVal;
            }

            if (!Require(RppLexer.Id))
            {
                return false;
            }

            string name = _lastToken.Text;
            IToken nameToken = _lastToken;
            Expect(RppLexer.OP_Colon);
            RTypeName paramType;
            if (!ParseType(out paramType))
            {
                RaiseSyntaxError("Type is expected");
            }

            classParam = new RppField(mutability, name, Collections.NoModifiers, new ResolvableType(paramType)) {Token = nameToken, IsClassParam = true};
            return true;
        }
예제 #3
0
파일: RppClass.cs 프로젝트: dubik/csharprpp
        public RppClass(ClassKind kind, HashSet<ObjectModifier> modifiers, [NotNull] string name, [NotNull] IList<RppField> classParams,
            [NotNull] IEnumerable<IRppNode> classBody, [NotNull] IList<RppVariantTypeParam> typeParams, RppBaseConstructorCall baseConstructorCall) : base(name)
        {
            Kind = kind;
            BaseConstructorCall = baseConstructorCall;
            _classParams = classParams;
            // TODO all of this can be simplified, we don't need to separate body of class. We can just walk rpp nodes with visitors
            IEnumerable<IRppNode> rppNodes = classBody as IList<IRppNode> ?? classBody.ToList();
            _funcs = rppNodes.OfType<RppFunc>().Where(f => !f.IsConstructor).ToList();
            _funcs.ForEach(DefineFunc);
            var constrExprs = rppNodes.OfType<IRppExpr>().Where(n => !(n is RppField)).ToList();
            // TODO for some reason RppField is also IRppExpr, I think it shouldn't be
            _typeParams = typeParams;
            Modifiers = modifiers;

            _constructors = rppNodes.OfType<RppFunc>().Where(f => f.IsConstructor).ToList();

            _fields = _classParams.Where(param => param.MutabilityFlag != MutabilityFlag.MfUnspecified || IsCase).ToList();

            rppNodes.OfType<RppField>().ForEach(_fields.Add);

            var primaryConstructor = CreatePrimaryConstructor(constrExprs);
            _constructors.Add(primaryConstructor);

            CreateProperties();

            if (kind == ClassKind.Object)
            {
                string objectName = SymbolTable.GetObjectName(Name);
                ResolvableType instanceFieldType = new ResolvableType(new RTypeName(objectName));
                InstanceField = new RppField(MutabilityFlag.MfVal, "_instance", Collections.NoModifiers, instanceFieldType,
                    new RppNew(instanceFieldType, Collections.NoExprs));
                _fields.Add(InstanceField);
            }

            _nested = rppNodes.OfType<RppClass>().ToList();
        }
예제 #4
0
파일: RppClass.cs 프로젝트: dubik/csharprpp
        private static IEnumerable<RppFunc> CreatePropertyAccessors(RppField field)
        {
            IRppParam valueParam = new RppParam("value", field.Type);

            if (field.MutabilityFlag != MutabilityFlag.MfVal)
            {
                RppFunc setter = new RppFunc(RppMethodInfo.GetSetterAccessorName(field.Name), new[] {valueParam}, ResolvableType.UnitTy,
                    new RppAssignOp(new RppId(field.MangledName), new RppId("value", valueParam)))
                {
                    IsSynthesized = true,
                    IsPropertyAccessor = true,
                    Modifiers = field.Modifiers
                };

                yield return setter;
            }

            RppFunc getter = new RppFunc(RppMethodInfo.GetGetterAccessorName(field.Name), Enumerable.Empty<IRppParam>(), field.Type,
                new RppId(field.MangledName, field))
            {
                IsSynthesized = true,
                IsPropertyAccessor = true,
                Modifiers = field.Modifiers
            };

            yield return getter;
        }
예제 #5
0
 public virtual void Visit(RppField node)
 {
 }
예제 #6
0
 public override void Visit(RppField node)
 {
     node.InitExpr.Accept(this);
 }