Search in sources :

Example 1 with Closure

use of dyvilx.tools.compiler.ast.statement.Closure in project Dyvil by Dyvil.

the class ApplyAccess method resolve.

@Override
public IValue resolve(MarkerList markers, IContext context) {
    // -> with(x..., { statements })
    if (this.arguments.size() == 1 && this.receiver instanceof ICall) {
        final ICall call = (ICall) this.receiver;
        IValue argument = this.arguments.getFirst();
        if (argument instanceof Closure) {
            argument = argument.resolve(markers, context);
            final ArgumentList oldArgs = call.getArguments();
            call.resolveReceiver(markers, context);
            call.resolveArguments(markers, context);
            call.setArguments(oldArgs.appended(null, argument));
            final IValue resolvedCall = call.resolveCall(markers, context, false);
            if (resolvedCall != null) {
                return resolvedCall;
            }
            // Revert
            call.setArguments(oldArgs);
            this.receiver = call.resolveCall(markers, context, true);
            return this.resolveCall(markers, context, true);
        }
    }
    return super.resolve(markers, context);
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) Closure(dyvilx.tools.compiler.ast.statement.Closure) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList)

Example 2 with Closure

use of dyvilx.tools.compiler.ast.statement.Closure in project Dyvil by Dyvil.

the class StatementListParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    if (type == BaseSymbols.CLOSE_CURLY_BRACKET) {
        this.end(pm);
        return;
    }
    if (type == Tokens.EOF) {
        this.end(pm);
        pm.report(token, "statement_list.close_brace");
        return;
    }
    switch(this.mode) {
        case OPEN_BRACKET:
            {
                final IToken next = token.next();
                final IToken lambdaArrow = this.findLambdaArrow(next);
                if (lambdaArrow != null) {
                    this.lambdaExpr = new LambdaExpr(lambdaArrow.raw());
                    this.lambdaExpr.setValue(this.statementList = new StatementList(token));
                    if (next == lambdaArrow) {
                        // { ->
                        // { =>
                        this.mode = LAMBDA_TYPE_ARROW;
                        return;
                    }
                    if (next.type() == BaseSymbols.OPEN_PARENTHESIS) {
                        // { ( ... ) =>
                        // { ( ... ) ->
                        pm.skip();
                        pm.pushParser(new ParameterListParser(this.lambdaExpr));
                        this.mode = LAMBDA_PARAMETERS_END;
                        return;
                    }
                    // { ... ->
                    // { ... =>
                    pm.pushParser(new ParameterListParser(this.lambdaExpr).withFlags(LAMBDA_ARROW_END));
                    this.mode = LAMBDA_TYPE_ARROW;
                    return;
                }
                // { ...
                this.statementList = this.closure ? new Closure(token) : new StatementList(token);
                this.mode = EXPRESSION;
                if (type != BaseSymbols.OPEN_CURLY_BRACKET) {
                    pm.report(token, "statement_list.open_brace");
                    pm.reparse();
                }
                return;
            }
        case LAMBDA_PARAMETERS_END:
            this.mode = LAMBDA_TYPE_ARROW;
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.report(token, "statement_list.lambda.close_paren");
            }
            return;
        case LAMBDA_TYPE_ARROW:
            if (type == DyvilSymbols.ARROW_RIGHT) {
                pm.pushParser(LambdaOrTupleParser.returnTypeParser(this.lambdaExpr));
                this.mode = LAMBDA_RETURN_ARROW;
                return;
            }
        // Fallthrough
        case LAMBDA_RETURN_ARROW:
            if (type != DyvilSymbols.DOUBLE_ARROW_RIGHT) {
                pm.report(token, "statement_list.lambda.arrow");
                return;
            }
            this.mode = EXPRESSION;
            return;
        case EXPRESSION:
            switch(type) {
                case BaseSymbols.SEMICOLON:
                case BaseSymbols.COMMA:
                    return;
                case DyvilKeywords.LABEL:
                    this.mode = LABEL_NAME;
                    return;
            }
            this.mode = SEPARATOR;
            final MemberParser<IVariable> parser = new MemberParser<>(this).withFlags(MemberParser.NO_FIELD_PROPERTIES);
            if (this.tryParserManager.tryParse(pm, parser, token, EXIT_ON_ROOT)) {
                return;
            }
            pm.pushParser(new ExpressionParser(this));
            return;
        case LABEL_NAME:
            if (Tokens.isIdentifier(type)) {
                this.label = token.nameValue();
                this.mode = LABEL_END;
                return;
            }
            this.mode = EXPRESSION;
            if (type != BaseSymbols.COLON) {
                pm.reparse();
            }
            pm.report(token, "statement_list.label.name");
            return;
        case LABEL_END:
            switch(type) {
                case BaseSymbols.COLON:
                case BaseSymbols.SEMICOLON:
                    this.mode = EXPRESSION;
                    return;
            }
            this.mode = EXPRESSION;
            pm.reparse();
            pm.report(SourcePosition.between(token, token.next()), "statement_list.label.separator");
            return;
        case SEPARATOR:
            this.mode = EXPRESSION;
            switch(type) {
                case BaseSymbols.SEMICOLON:
                case BaseSymbols.COMMA:
                    return;
            }
            pm.report(token, "statement_list.semicolon");
    }
}
Also used : ParameterListParser(dyvilx.tools.compiler.parser.method.ParameterListParser) Closure(dyvilx.tools.compiler.ast.statement.Closure) IToken(dyvilx.tools.parsing.token.IToken) LambdaExpr(dyvilx.tools.compiler.ast.expression.LambdaExpr) StatementList(dyvilx.tools.compiler.ast.statement.StatementList) IVariable(dyvilx.tools.compiler.ast.field.IVariable) ExpressionParser(dyvilx.tools.compiler.parser.expression.ExpressionParser)

Aggregations

Closure (dyvilx.tools.compiler.ast.statement.Closure)2 IValue (dyvilx.tools.compiler.ast.expression.IValue)1 LambdaExpr (dyvilx.tools.compiler.ast.expression.LambdaExpr)1 IVariable (dyvilx.tools.compiler.ast.field.IVariable)1 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)1 StatementList (dyvilx.tools.compiler.ast.statement.StatementList)1 ExpressionParser (dyvilx.tools.compiler.parser.expression.ExpressionParser)1 ParameterListParser (dyvilx.tools.compiler.parser.method.ParameterListParser)1 IToken (dyvilx.tools.parsing.token.IToken)1