Search in sources :

Example 1 with Operator

use of dyvilx.tools.compiler.ast.expression.operator.Operator in project Dyvil by Dyvil.

the class OperatorParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case TYPE:
            this.mode = OPERATOR;
            switch(type) {
                case DyvilKeywords.PREFIX:
                    if (token.next().type() == DyvilKeywords.POSTFIX) {
                        pm.skip();
                        this.type = IOperator.CIRCUMFIX;
                        return;
                    }
                    this.type = IOperator.PREFIX;
                    return;
                case DyvilKeywords.POSTFIX:
                    this.type = IOperator.POSTFIX;
                    return;
                case DyvilKeywords.INFIX:
                    this.type = IOperator.INFIX;
                    return;
            }
            pm.report(token, "operator.type.invalid");
            return;
        case OPERATOR:
            this.mode = OPERATOR_SYMBOL;
            if (type != DyvilKeywords.OPERATOR) {
                pm.reparse();
                pm.report(token, "operator.operator");
            }
            return;
        case OPERATOR_SYMBOL:
            {
                Name name = getOperatorName(pm, token);
                if (name == null) {
                    pm.report(token, "operator.identifier");
                    return;
                }
                this.operator = new Operator(name, this.type);
                // TODO Add 'ternary' keyword?
                if (this.type == IOperator.INFIX) {
                    name = getOperatorName(pm, token.next());
                    if (name != null) {
                        this.operator.setType(IOperator.TERNARY);
                        this.operator.setName2(name);
                        pm.skip();
                    }
                } else if (this.type == IOperator.CIRCUMFIX || this.type == IOperator.TERNARY) {
                    name = getOperatorName(pm, token.next());
                    if (name != null) {
                        this.operator.setName2(name);
                        pm.skip();
                    } else {
                        pm.report(SourcePosition.between(token, token.next()), "operator.identifier2");
                    }
                }
                this.mode = SEPARATOR;
                return;
            }
        case SEPARATOR:
            switch(type) {
                case BaseSymbols.SEMICOLON:
                    pm.popParser(true);
                    this.map.addOperator(this.operator);
                    return;
                case Tokens.EOF:
                    pm.popParser();
                    this.map.addOperator(this.operator);
                    return;
                case BaseSymbols.OPEN_CURLY_BRACKET:
                    this.mode = PROPERTY;
                    return;
            }
            pm.popParser(true);
            this.map.addOperator(this.operator);
            pm.report(token, "operator.separator");
            return;
        case PROPERTY:
            switch(type) {
                case Tokens.LETTER_IDENTIFIER:
                    final Name name = token.nameValue();
                    if (name == precedence) {
                        this.mode = PRECEDENCE;
                        return;
                    }
                    if (name == associativity) {
                        this.mode = ASSOCIATIVITY;
                        return;
                    }
                    if (this.parseAssociativity(pm, token, name)) {
                        this.mode = COMMA;
                        return;
                    }
                    break;
                case BaseSymbols.CLOSE_CURLY_BRACKET:
                    pm.popParser();
                    this.map.addOperator(this.operator);
                    return;
            }
            if ((type & Tokens.INT) != 0) {
                this.setPrecedence(pm, token, token.intValue());
                this.mode = COMMA;
                return;
            }
            pm.report(Markers.syntaxError(token, "operator.property.invalid", token));
            return;
        case COMMA:
            if (type == BaseSymbols.CLOSE_CURLY_BRACKET) {
                pm.popParser();
                this.map.addOperator(this.operator);
                return;
            }
            this.mode = PROPERTY;
            if (type != BaseSymbols.COMMA) {
                pm.reparse();
                pm.report(token, "operator.property.comma");
                return;
            }
            return;
        case PRECEDENCE:
            this.mode = COMMA;
            if ((type & Tokens.INT) == 0) {
                pm.reparse();
                pm.report(token, "operator.property.precedence");
                return;
            }
            this.setPrecedence(pm, token, token.intValue());
            return;
        case ASSOCIATIVITY:
            this.mode = COMMA;
            if (type == Tokens.LETTER_IDENTIFIER) {
                final Name name = token.nameValue();
                if (this.parseAssociativity(pm, token, name)) {
                    return;
                }
            }
            pm.reparse();
            pm.report(token, "operator.property.associativity");
    }
}
Also used : IOperator(dyvilx.tools.compiler.ast.expression.operator.IOperator) Operator(dyvilx.tools.compiler.ast.expression.operator.Operator) Name(dyvil.lang.Name)

Example 2 with Operator

use of dyvilx.tools.compiler.ast.expression.operator.Operator in project Dyvil by Dyvil.

the class HeaderContext method read.

@Override
public void read(DataInput in) throws IOException {
    this.headerDeclaration = new HeaderDeclaration(this);
    this.headerDeclaration.read(in);
    this.name = this.headerDeclaration.getName();
    // Import Declarations
    int imports = in.readShort();
    for (int i = 0; i < imports; i++) {
        ImportDeclaration id = new ImportDeclaration(null);
        id.read(in);
        this.addImport(id);
    }
    int operators = in.readShort();
    for (int i = 0; i < operators; i++) {
        Operator op = Operator.read(in);
        this.addOperator(op);
    }
    int typeAliases = in.readShort();
    for (int i = 0; i < typeAliases; i++) {
        TypeAlias ta = new TypeAlias();
        ta.read(in);
        this.addTypeAlias(ta);
    }
}
Also used : IOperator(dyvilx.tools.compiler.ast.expression.operator.IOperator) Operator(dyvilx.tools.compiler.ast.expression.operator.Operator) ImportDeclaration(dyvilx.tools.compiler.ast.imports.ImportDeclaration) TypeAlias(dyvilx.tools.compiler.ast.type.alias.TypeAlias) ITypeAlias(dyvilx.tools.compiler.ast.type.alias.ITypeAlias)

Aggregations

IOperator (dyvilx.tools.compiler.ast.expression.operator.IOperator)2 Operator (dyvilx.tools.compiler.ast.expression.operator.Operator)2 Name (dyvil.lang.Name)1 ImportDeclaration (dyvilx.tools.compiler.ast.imports.ImportDeclaration)1 ITypeAlias (dyvilx.tools.compiler.ast.type.alias.ITypeAlias)1 TypeAlias (dyvilx.tools.compiler.ast.type.alias.TypeAlias)1