Example usage for org.antlr.v4.runtime ParserRuleContext accept

List of usage examples for org.antlr.v4.runtime ParserRuleContext accept

Introduction

In this page you can find the example usage for org.antlr.v4.runtime ParserRuleContext accept.

Prototype

@Override
    public <T> T accept(ParseTreeVisitor<? extends T> visitor) 

Source Link

Usage

From source file:net.udidb.expr.lang.c.CExpressionCompiler.java

License:Open Source License

@VisibleForTesting
static void simplifyExpression(ParserRuleContext parseTree, ParseTreeProperty<NodeState> states,
        ExecutionContext executionContext) {
    ExpressionSimplificationVisitor visitor = new ExpressionSimplificationVisitor(states, executionContext);
    parseTree.accept(visitor);
}

From source file:net.udidb.expr.lang.c.CExpressionCompiler.java

License:Open Source License

@VisibleForTesting
static Expression generateCode(ParserRuleContext parseTree, ParseTreeProperty<NodeState> states,
        ExecutionContext executionContext) {
    CodeGenVisitor visitor = new CodeGenVisitor(states, executionContext);
    return parseTree.accept(visitor);
}

From source file:net.udidb.expr.lang.c.ExpressionSimplificationVisitor.java

License:Open Source License

@Override
public Void visitAdditiveExpression(@NotNull CParser.AdditiveExpressionContext ctx) {
    NodeState nodeState = getNodeState(ctx);

    ParserRuleContext multExpr = ctx.multiplicativeExpression();
    multExpr.accept(this);

    if (ctx.additiveExpression() == null) {
        nodeState.setExpressionValue(getNodeState(multExpr).getExpressionValue());
        return null;
    }//from  ww  w .j  a  va  2  s.c om

    ParserRuleContext addExpr = ctx.additiveExpression();
    addExpr.accept(this);
    if (getNodeState(addExpr).getExpressionValue() == null
            || getNodeState(multExpr).getExpressionValue() == null) {
        // The computation cannot be performed yet
        return null;
    }

    String operation = ctx.getChild(1).getText();

    Number left = getNodeState(addExpr).getExpressionValue().getNumberValue();
    Number right = getNodeState(multExpr).getExpressionValue().getNumberValue();

    DebugType effectiveType = nodeState.getEffectiveType();
    Number computedValue;
    switch (operation) {
    case "+":
        if (effectiveType == Types.getType(Types.FLOAT_NAME)) {
            computedValue = left.floatValue() + right.floatValue();
        } else if (effectiveType == Types.getType(Types.DOUBLE_NAME)) {
            computedValue = left.doubleValue() + right.doubleValue();
        } else {
            // TODO handle other cases
            computedValue = 0;
        }
        break;
    case "-":
        // TODO implement
        computedValue = 0;
        break;
    default:
        throw new ParseCancellationException(INVALID_TREE_MSG);
    }

    nodeState.setExpressionValue(new NumberValue(computedValue));
    return null;
}