Beispiel #1
0
        public ConditionGenerator(GeneratorFactory factory, BlockGenerator block,
                                  ExpressionSyntaxNode expr, LabelStatementSyntaxNode falseLabel, bool reverseCondition)
        {
            while (expr is UnaryExpressionSyntaxNode unary && unary.Operator == UnaryOperator.Not)
            {
                expr             = unary.Operand;
                reverseCondition = !reverseCondition;
            }

            if (expr is BinaryExpressionSyntaxNode bin && IsComparisonExpr(bin, out _opcode, out _exchangeComparison))
            {
                _isComparison = true;
                //Normally, reverse the cmp (jump to false label if condition is not met).
                if (!reverseCondition)
                {
                    ReverseComparisonResult(ref _opcode);
                }
                _expr1 = factory.CreateExpression(block, bin.Left);
                _expr2 = factory.CreateExpression(block, bin.Right);
                if (!_expr1.TryGetFromStack(out _))
                {
                    _expr1Stack = block.TempAllocator.Allocate(_expr1.GetSingleType());
                }
                if (!_expr2.TryGetFromStack(out _))
                {
                    _expr2Stack = block.TempAllocator.Allocate(_expr2.GetSingleType());
                }
            }
 protected BinaryExpression(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     left  = (ExpressionSyntaxNode)info.GetValue("left", typeof(ExpressionSyntaxNode));
     right = (ExpressionSyntaxNode)info.GetValue("right", typeof(ExpressionSyntaxNode));
     optr  = (Operator)info.GetValue("optr", typeof(Operator));
 }
Beispiel #3
0
        public AllocatedLocal Allocate(ExpressionSyntaxNode expr)
        {
            //Allocate a new slot for each temp var.
            var type = expr.SpecializationType.GetVMSpecializationType();

            return(Allocate(type));
        }
 public IfElseStatement(ISyntaxNode parent, ExpressionSyntaxNode expression, IReadOnlyList <StatementSyntaxNode> statements,
                        IReadOnlyList <StatementSyntaxNode> elseStatements)
     : base(parent, statements)
 {
     Expression     = expression;
     ElseStatements = elseStatements;
 }
Beispiel #5
0
 internal override void Deserialize(BinaryReader br)
 {
     base.Deserialize(br);
     Operator = DeserializeV <BinaryOperator>(br);
     Left     = DeserializeO <ExpressionSyntaxNode>(br);
     Right    = DeserializeO <ExpressionSyntaxNode>(br);
 }
Beispiel #6
0
 public IndirectMemberAccessExpressionSyntaxNode IndirectMemberAccessExpressionSyntax(SyntaxToken openingBracket, ExpressionSyntaxNode expression, SyntaxToken closingBracket)
 {
     return(new IndirectMemberAccessExpressionSyntaxNode(openingBracket, expression, closingBracket));
 }
Beispiel #7
0
 public UnaryPostfixOperationExpressionSyntaxNode UnaryPostfixOperationExpressionSyntax(ExpressionSyntaxNode operand, SyntaxToken operation)
 {
     return(new UnaryPostfixOperationExpressionSyntaxNode(operand, operation));
 }
Beispiel #8
0
 public ExpressionStatementSyntaxNode ExpressionStatementSyntax(ExpressionSyntaxNode expression)
 {
     return(new ExpressionStatementSyntaxNode(expression));
 }
Beispiel #9
0
 public IfStatementSyntaxNode IfStatementSyntax(SyntaxToken ifKeyword, ExpressionSyntaxNode condition, SyntaxList <SyntaxToken> optionalCommas, StatementSyntaxNode body, SyntaxList <ElseifClause> elseifClauses, ElseClause?elseClause, SyntaxToken endKeyword)
 {
     return(new IfStatementSyntaxNode(ifKeyword, condition, optionalCommas, body, elseifClauses, elseClause, endKeyword));
 }
Beispiel #10
0
 public WhileStatementSyntaxNode WhileStatementSyntax(SyntaxToken whileKeyword, ExpressionSyntaxNode condition, SyntaxList <SyntaxToken> optionalCommas, StatementSyntaxNode body, SyntaxToken endKeyword)
 {
     return(new WhileStatementSyntaxNode(whileKeyword, condition, optionalCommas, body, endKeyword));
 }
Beispiel #11
0
 public SwitchStatementSyntaxNode SwitchStatementSyntax(SyntaxToken switchKeyword, ExpressionSyntaxNode switchExpression, SyntaxList <SyntaxToken> optionalCommas, SyntaxList <SwitchCaseSyntaxNode> cases, SyntaxToken endKeyword)
 {
     return(new SwitchStatementSyntaxNode(switchKeyword, switchExpression, optionalCommas, cases, endKeyword));
 }
 public WhileStatement(ISyntaxNode parent, ExpressionSyntaxNode expression, IReadOnlyList <StatementSyntaxNode> statements)
     : base(parent, statements)
 {
     Expression = expression;
 }
Beispiel #13
0
        // x = a + b

        private Action WriteLValueExpression(ExpressionSyntaxNode expression, out IType?expressionResultType)
        {
            switch (expression)
            {
            case ArrayIndexExpression arrIdx:
                IType?arrayType  = null;
                IType?lengthType = null;

                WriteExpression(arrIdx.Expression, true, false, ref arrayType);
                WriteExpression(arrIdx.LengthExpression, true, false, ref lengthType);

                typeChecker.TypeCheck(store.Types["System.Int32"], lengthType);

                if (arrayType == null)
                {
                    throw new MissingTypeException("Array type must have been found");
                }

                // Use the runtime provided Get() method to determine the inner type of the array
                var arrayRootType = arrayType.GetMethod("Get").ReturnType;

                // The expression needs the inner array type, then will call a Stelem
                expressionResultType = arrayRootType;
                return(() => generator.EmitStelem(arrayRootType));

            case VariableSyntaxNode varNode:
                if (currentMethodInfo.Locals.TryGetValue(varNode.Name, out var localVar))
                {
                    expressionResultType = localVar.LocalType;
                    return(() => generator.EmitStloc(localVar));
                }
                else if (currentMethodInfo.Parameters.TryGetValue(varNode.Name, out var parameterVar))
                {
                    expressionResultType = parameterVar.type;
                    return(() => generator.EmitStarg(parameterVar.idx));
                }
                else if (currentMethodInfo.Fields.TryGetValue(varNode.Name, out var fieldVar))
                {
                    generator.EmitLdthis();
                    expressionResultType = fieldVar.FieldType;
                    return(() => generator.EmitStfld(fieldVar));
                }
                else
                {
                    throw new MissingVariableDefinitionException("Not supported");
                }

            case VariableAccessExpression varAccess:
            {
                IType?callTarget = null;
                WriteExpression(varAccess.Expression, true, false, ref callTarget);

                if (callTarget == null)
                {
                    throw new MissingTypeException("No target for field access");
                }

                IFieldInfo?fieldToCall = null;

                if (store.Fields.TryGetValue(callTarget, out var typeFieldList))
                {
                    foreach (var localField in typeFieldList)
                    {
                        if (localField.Name == varAccess.Name)
                        {
                            fieldToCall = localField;
                            break;
                        }
                    }
                }

                if (fieldToCall == null)
                {
                    throw new MissingTypeException("Field not found");
                }

                expressionResultType = fieldToCall.FieldType;
                return(() => generator.EmitStfld(fieldToCall));
            }

            default:
                throw new LValueOperationException("No other type of operations supported as lvalue");
            }
        }
Beispiel #14
0
        public ExpressionGenerator CreateExpression(BlockGenerator parentBlock, ExpressionSyntaxNode expr)
        {
            switch (expr)
            {
            case BinaryExpressionSyntaxNode binary:
            {
                switch (binary.Operator.V)
                {
                case BinaryOperator.Raw.Add:
                case BinaryOperator.Raw.Sub:
                case BinaryOperator.Raw.Mul:
                case BinaryOperator.Raw.Div:
                case BinaryOperator.Raw.Pow:
                case BinaryOperator.Raw.Mod:
                    return(new BinaryExpressionGenerator(this, parentBlock, binary));

                case BinaryOperator.Raw.Conc:
                    return(new ConcatBinaryExpressionGenerator(this, parentBlock, binary));

                case BinaryOperator.Raw.L:
                case BinaryOperator.Raw.LE:
                case BinaryOperator.Raw.G:
                case BinaryOperator.Raw.GE:
                case BinaryOperator.Raw.E:
                case BinaryOperator.Raw.NE:
                    return(new ComparisonBinaryExpressionGenerator(this, parentBlock, binary));

                case BinaryOperator.Raw.And:
                case BinaryOperator.Raw.Or:
                    return(new AndOrExpressionGenerator(this, parentBlock, binary));

                default:
                    break;
                }
                break;
            }

            case FunctionExpressionSyntaxNode function:
                return(new FunctionExpressionGenerator(this, function));

            case IndexVariableSyntaxNode indexVariable:
                return(new IndexExpressionGenerator(this, parentBlock, indexVariable));

            case InvocationExpressionSyntaxNode invocation:
                return(new InvocationExpressionGenerator(this, parentBlock, invocation));

            case LiteralExpressionSyntaxNode literal:
                return(new LiteralExpressionGenerator(Function, literal));

            case NamedVariableSyntaxNode nameVariable:
            {
                //We don't create from the expr syntax node, so confirm the type is correct.
                var ret = Function.Locals[nameVariable.Variable.Target];
                Debug.Assert(nameVariable.SpecializationType.GetVMSpecializationType() == ret.GetSingleType());
                return(ret);
            }

            case TableExpressionSyntaxNode table:
                return(new TableExpressionGenerator(this, parentBlock, table));

            case UnaryExpressionSyntaxNode unary:
                return(new UnaryExpressionGenerator(this, parentBlock, unary));

            case VarargExpressionSyntaxNode vararg:
                return(new VarargExpressionGenerator(Function.FunctionDefinition, vararg));

            default:
                break;
            }
            throw new Exception();
        }
 public BinaryExpression(ExpressionSyntaxNode left, ExpressionSyntaxNode right, Operator optr)
 {
     this.left  = left;
     this.right = right;
     this.optr  = optr;
 }
Beispiel #16
0
 public ClassInvokationExpressionSyntaxNode ClassInvokationExpressionSyntax(ExpressionSyntaxNode methodName, SyntaxToken atSign, ExpressionSyntaxNode baseClassNameAndArguments)
 {
     return(new ClassInvokationExpressionSyntaxNode(methodName, atSign, baseClassNameAndArguments));
 }
Beispiel #17
0
 public AttributeAssignmentSyntaxNode AttributeAssignmentSyntax(SyntaxToken assignmentSign, ExpressionSyntaxNode value)
 {
     return(new AttributeAssignmentSyntaxNode(assignmentSign, value));
 }
Beispiel #18
0
 public LambdaExpressionSyntaxNode LambdaExpressionSyntax(SyntaxToken atSign, FunctionInputDescriptionSyntaxNode input, ExpressionSyntaxNode body)
 {
     return(new LambdaExpressionSyntaxNode(atSign, input, body));
 }
Beispiel #19
0
 public SwitchCaseSyntaxNode SwitchCaseSyntax(SyntaxToken caseKeyword, ExpressionSyntaxNode caseIdentifier, SyntaxList <SyntaxToken> optionalCommas, StatementSyntaxNode body)
 {
     return(new SwitchCaseSyntaxNode(caseKeyword, caseIdentifier, optionalCommas, body));
 }
Beispiel #20
0
 public BinaryOperationExpressionSyntaxNode BinaryOperationExpressionSyntax(ExpressionSyntaxNode lhs, SyntaxToken operation, ExpressionSyntaxNode rhs)
 {
     return(new BinaryOperationExpressionSyntaxNode(lhs, operation, rhs));
 }
Beispiel #21
0
 public ElseifClause ElseifClause(SyntaxToken elseifKeyword, ExpressionSyntaxNode condition, SyntaxList <SyntaxToken> optionalCommas, StatementSyntaxNode body)
 {
     return(new ElseifClause(elseifKeyword, condition, optionalCommas, body));
 }
Beispiel #22
0
 public ParenthesizedExpressionSyntaxNode ParenthesizedExpressionSyntax(SyntaxToken openingBracket, ExpressionSyntaxNode expression, SyntaxToken closingBracket)
 {
     return(new ParenthesizedExpressionSyntaxNode(openingBracket, expression, closingBracket));
 }
Beispiel #23
0
 public AssignmentExpressionSyntaxNode AssignmentExpressionSyntax(ExpressionSyntaxNode lhs, SyntaxToken assignmentSign, ExpressionSyntaxNode rhs)
 {
     return(new AssignmentExpressionSyntaxNode(lhs, assignmentSign, rhs));
 }
Beispiel #24
0
 public CellArrayElementAccessExpressionSyntaxNode CellArrayElementAccessExpressionSyntax(ExpressionSyntaxNode expression, SyntaxToken openingBrace, SyntaxList nodes, SyntaxToken closingBrace)
 {
     return(new CellArrayElementAccessExpressionSyntaxNode(expression, openingBrace, nodes, closingBrace));
 }
Beispiel #25
0
 public UnaryPrefixOperationExpressionSyntaxNode UnaryPrefixOperationExpressionSyntax(SyntaxToken operation, ExpressionSyntaxNode operand)
 {
     return(new UnaryPrefixOperationExpressionSyntaxNode(operation, operand));
 }
Beispiel #26
0
 public FunctionCallExpressionSyntaxNode FunctionCallExpressionSyntax(ExpressionSyntaxNode functionName, SyntaxToken openingBracket, SyntaxList nodes, SyntaxToken closingBracket)
 {
     return(new FunctionCallExpressionSyntaxNode(functionName, openingBracket, nodes, closingBracket));
 }