Ejemplo n.º 1
0
        public override void SurroundWith(Environment environment)
        {
            base.SurroundWith(environment);
            var jmp = new JumpLabelDeclaration(MetaData, "");

            jmp.SurroundWith(Env);
            EndLabel.SurroundWith(Env);
            // FEATURE #16
            var conditionType = Condition.GetExpressionType().ToString();

            if (!string.Equals(conditionType, PrimaryType.BoolType, StringComparison.Ordinal))
            {
                Errors.Add(
                    $"{MetaData.GetErrorHeader()}expected a bool as the \"while\" statement\'s condition, " +
                    $"found {conditionType}");
            }
            OkStatementList.Statements.Add(jmp);
            var bodyEnv = new Environment(Env);

            bodyEnv.Declarations.Add(EndLabel);
            OkStatementList.SurroundWith(bodyEnv);
            // FEATURE #17
            if (Pragma.KeepAll || !(Condition is BoolLiteralExpression boolean) || boolean.Value)
            {
                return;
            }
            OptimizedStatementList = new StatementList(MetaData);
            Optimized = 1;
        }
Ejemplo n.º 2
0
        public override void SurroundWith(Environment environment)
        {
            base.SurroundWith(environment);
            // FEATURE #1
            var conditionType = Condition.GetExpressionType().ToString();

            if (!string.Equals(conditionType, PrimaryType.BoolType, StringComparison.Ordinal))
            {
                Errors.Add(
                    $"{MetaData.GetErrorHeader()}expected a bool as the \"if\" statement\'s condition, found {conditionType}");
            }
            IfStatementList.SurroundWith(new Environment(Env));
            ElseStatementList.SurroundWith(new Environment(Env));
            // FEATURE #17
            if (Pragma.KeepAll || !(Condition is BoolLiteralExpression boolean))
            {
                return;
            }
            if (boolean.Value)
            {
                ElseStatementList.Statements = new List <Statement>(0);
                Optimized = 1;
            }
            else
            {
                IfStatementList.Statements = new List <Statement>(0);
                Optimized = 2;
            }
        }
Ejemplo n.º 3
0
        public override void SurroundWith(Environment environment)
        {
            base.SurroundWith(environment);
            // https://github.com/Cm-lang/Cm-Document/issues/12
            if (string.Equals(Name, ReservedWords.Recur, Ordinal))
            {
                Debug.Assert(null != Expression.Env);
                Type = Expression.GetExpressionType();
                return;
            }
            Expression.SurroundWith(Env);
            var exprType = Expression.GetExpressionType();

            // FEATURE #8
            Type = Type ?? exprType;
            // FEATURE #30
            Type.SurroundWith(Env);
            if (Type is UnknownType unknownType)
            {
                Type = unknownType.Resolve();
            }
            if (Type is PrimaryType primaryType)
            {
                Align = primaryType.Align;
            }
            // FEATURE #11
            if (!string.Equals(exprType.ToString(), PrimaryType.NullType, Ordinal) &&
                !Equals(Type, exprType))
            {
                // FEATURE #9
                Errors.Add($"{MetaData.GetErrorHeader()}type mismatch, expected: {Type}, actual: {exprType}");
            }
        }
Ejemplo n.º 4
0
        public override void SurroundWith(Environment environment)
        {
            base.SurroundWith(environment);
            var internalEnv = new Environment(Env);

            foreach (var variableDeclaration in FieldList)
            {
                variableDeclaration.SurroundWith(internalEnv);
            }
        }
Ejemplo n.º 5
0
 public override void SurroundWith(Environment environment)
 {
     base.SurroundWith(environment);
     if (null == Struct)
     {
         Struct = Env.FindDeclarationByName(Name) as StructDeclaration;
     }
     if (null != Struct)
     {
         Struct.Used = true;
         return;
     }
     Errors.Add($"{MetaData.GetErrorHeader()}cannot resolve type {Name}");
     throw new CompilerException();
 }
Ejemplo n.º 6
0
 public override void SurroundWith(Environment environment)
 {
     base.SurroundWith(environment);
     Owner.SurroundWith(Env);
     Member.SurroundWith(Env);
     // FEATURE #29
     if (!(Owner.GetExpressionType() is SecondaryType type) ||
         !type.Struct.FieldList.Contains(Member.Declaration))
     {
         Errors.Add(MetaData.GetErrorHeader() + "invalid member access expression");
     }
     if (null != Owner.ConvertedResult)
     {
     }
 }
Ejemplo n.º 7
0
        public override void SurroundWith(Environment environment)
        {
            base.SurroundWith(environment);
            var jumpLabel = Env.FindJumpLabelByName(_labelName ?? "");

            if (null == jumpLabel)
            {
                Errors.AddAndThrow($"{MetaData.GetErrorHeader()}cannot return outside a lambda");
            }
            else
            {
                JumpLabel = jumpLabel;
                JumpLabel.StatementsUsingThis.Add(this);
            }
        }
Ejemplo n.º 8
0
        public override void SurroundWith(Environment environment)
        {
            base.SurroundWith(environment);
            Expression.SurroundWith(Env);
            var res = Expression.ConvertedResult;

            if (null != res)
            {
                ConvertedStatementList = new StatementList(MetaData,
                                                           res.ConvertedStatements.Concat(new[]
                {
                    new ExpressionStatement(MetaData, res.ConvertedExpression)
                }).ToArray());
            }
        }
Ejemplo n.º 9
0
 public override void SurroundWith(Environment environment)
 {
     base.SurroundWith(environment);
     if (null == Struct)
     {
         Struct = Env.FindDeclarationByName(Name) as StructDeclaration;
     }
     if (null == Struct)
     {
         Errors.AddAndThrow($"{MetaData.GetErrorHeader()}cannot resolve type {Name}");
     }
     else
     {
         Struct.UsageCount++;
     }
 }
Ejemplo n.º 10
0
 public override void SurroundWith(Environment environment)
 {
     base.SurroundWith(environment);
     for (var i = 0; i < ArgsList.Count; i++)
     {
         var type = ArgsList[i];
         type.SurroundWith(Env);
         if (type is UnknownType unknownType)
         {
             ArgsList[i] = unknownType.Resolve();
         }
     }
     RetType.SurroundWith(Env);
     if (RetType is UnknownType wtf)
     {
         RetType = wtf.Resolve();
     }
 }
Ejemplo n.º 11
0
        public override void SurroundWith(Environment environment)
        {
            base.SurroundWith(environment);
            LhsExpression.SurroundWith(Env);
            RhsExpression.SurroundWith(Env);
            var lhs = LhsExpression.GetExpressionType();
            var rhs = RhsExpression.GetExpressionType();

            // FEATURE #14
            // FEATURE #11
            if (!string.Equals(rhs.ToString(), PrimaryType.NullType, StringComparison.Ordinal) &&
                !string.Equals(lhs.ToString(), rhs.ToString(), StringComparison.Ordinal))
            {
                Errors.Add($"{MetaData.GetErrorHeader()}assigning a {rhs} to a {lhs} is invalid.");
            }
            // FEATURE #20
            var validLhs = LhsExpression.GetLhsExpression();

            if (null == validLhs)
            {
                Errors.Add($"{MetaData.GetErrorHeader()}a {lhs} cannot be assigned.");
            }
            else if (null == validLhs.Declaration)
            {
                Errors.Add($"{MetaData.GetErrorHeader()}can't find declaration of {validLhs.Name}");
            }
            // FEATURE #21
            else if (!validLhs.DeclarationMutability)
            {
                Errors.Add($"{MetaData.GetErrorHeader()}cannot assign to an immutable variable.");
            }
            else
            {
                validLhs.Declaration.UsageCount++;
            }
            if (!(RhsExpression is AtomicExpression))
            {
                // TODO
//				ConvertedStatementList = new StatementList(MetaData,
//					new VariableDeclaration(MetaData, ""));
            }
        }
Ejemplo n.º 12
0
 public override void SurroundWith(Environment environment)
 {
     base.SurroundWith(environment);
     Condition.SurroundWith(Env);
 }
Ejemplo n.º 13
0
 public void Init()
 {
     Environment.Initialize();
     Errors.ErrList.Clear();
 }
Ejemplo n.º 14
0
 public void Init()
 {
     Errors.ErrList.Clear();
     Pragma.KeepAll = false;
     Environment.Initialize();
 }