Search in sources :

Example 1 with SimpleNode

use of gov.sandia.n2a.language.parse.SimpleNode in project n2a by frothga.

the class Split method getOperandsFrom.

public void getOperandsFrom(SimpleNode node) throws ParseException {
    if (!(node instanceof ASTList))
        throw new Error("AST for type list has unexpected form");
    ASTList l = (ASTList) node;
    int count = l.jjtGetNumChildren();
    names = new String[count];
    for (int i = 0; i < count; i++) {
        SimpleNode n = (SimpleNode) l.jjtGetChild(i);
        if (!(n instanceof ASTIdentifier))
            throw new ParseException("Items in $type list must all be part names.");
        names[i] = n.jjtGetValue().toString();
    }
}
Also used : ASTIdentifier(gov.sandia.n2a.language.parse.ASTIdentifier) ASTList(gov.sandia.n2a.language.parse.ASTList) SimpleNode(gov.sandia.n2a.language.parse.SimpleNode)

Example 2 with SimpleNode

use of gov.sandia.n2a.language.parse.SimpleNode in project n2a by frothga.

the class Operator method getFrom.

public static Operator getFrom(SimpleNode node) throws Exception {
    Operator result;
    if (node instanceof ASTOperator) {
        Factory f = operators.get(node.jjtGetValue().toString());
        result = f.createInstance();
    } else if (node instanceof ASTIdentifier) {
        String value = node.jjtGetValue().toString();
        if (value.endsWith("()")) {
            Factory f = operators.get(value.substring(0, value.length() - 2));
            if (// It's either this or an undefined function. In the second case, variable access will fail.
            f == null)
                // It's either this or an undefined function. In the second case, variable access will fail.
                result = new AccessElement();
            else
                result = f.createInstance();
        } else {
            result = new AccessVariable();
        }
    } else if (node instanceof ASTConstant)
        result = new Constant();
    else if (node instanceof ASTMatrix)
        result = new BuildMatrix();
    else if (node instanceof ASTList) {
        if (node.jjtGetNumChildren() == 1)
            return getFrom((SimpleNode) node.jjtGetChild(0));
        // Lists can exist elsewhere besides a $type split, but they should be processed out by getOperandsFrom(SimpleNode).
        result = new Split();
    } else
        result = new Operator();
    result.getOperandsFrom(node);
    return result;
}
Also used : ASTOperator(gov.sandia.n2a.language.parse.ASTOperator) ASTConstant(gov.sandia.n2a.language.parse.ASTConstant) ASTIdentifier(gov.sandia.n2a.language.parse.ASTIdentifier) SimpleNode(gov.sandia.n2a.language.parse.SimpleNode) ASTConstant(gov.sandia.n2a.language.parse.ASTConstant) ASTList(gov.sandia.n2a.language.parse.ASTList) ASTOperator(gov.sandia.n2a.language.parse.ASTOperator) ASTMatrix(gov.sandia.n2a.language.parse.ASTMatrix)

Example 3 with SimpleNode

use of gov.sandia.n2a.language.parse.SimpleNode in project n2a by frothga.

the class AccessElement method getOperandsFrom.

public void getOperandsFrom(SimpleNode node) throws Exception {
    if (node.jjtGetNumChildren() == 0)
        throw new UnsupportedFunctionException(node.jjtGetValue().toString());
    if (node.jjtGetNumChildren() != 1)
        throw new Error("AST for function has unexpected form");
    Object o = node.jjtGetChild(0);
    if (!(o instanceof ASTList))
        throw new Error("AST for function has unexpected form");
    ASTList l = (ASTList) o;
    int count = l.jjtGetNumChildren();
    operands = new Operator[count + 1];
    String value = node.jjtGetValue().toString();
    // Remove "()" from the end of name.
    operands[0] = new AccessVariable(value.substring(0, value.length() - 2));
    operands[0].parent = this;
    for (int i = 0; i < count; i++) {
        operands[i + 1] = Operator.getFrom((SimpleNode) l.jjtGetChild(i));
        operands[i + 1].parent = this;
    }
}
Also used : ASTList(gov.sandia.n2a.language.parse.ASTList) SimpleNode(gov.sandia.n2a.language.parse.SimpleNode)

Example 4 with SimpleNode

use of gov.sandia.n2a.language.parse.SimpleNode in project n2a by frothga.

the class BuildMatrix method getOperandsFrom.

public void getOperandsFrom(SimpleNode node) throws Exception {
    int rows = node.jjtGetNumChildren();
    int cols = 0;
    for (int r = 0; r < rows; r++) {
        SimpleNode row = (SimpleNode) node.jjtGetChild(r);
        int c = 1;
        if (!(row instanceof ASTConstant))
            c = row.jjtGetNumChildren();
        cols = Math.max(cols, c);
    }
    operands = new Operator[cols][rows];
    for (int r = 0; r < rows; r++) {
        SimpleNode row = (SimpleNode) node.jjtGetChild(r);
        if (row instanceof ASTConstant) {
            operands[0][r] = Operator.getFrom(row);
            operands[0][r].parent = this;
        } else {
            int currentCols = row.jjtGetNumChildren();
            for (int c = 0; c < currentCols; c++) {
                operands[c][r] = Operator.getFrom((SimpleNode) row.jjtGetChild(c));
                operands[c][r].parent = this;
            }
        }
    }
}
Also used : ASTConstant(gov.sandia.n2a.language.parse.ASTConstant) SimpleNode(gov.sandia.n2a.language.parse.SimpleNode)

Example 5 with SimpleNode

use of gov.sandia.n2a.language.parse.SimpleNode in project n2a by frothga.

the class Function method getOperandsFrom.

public void getOperandsFrom(SimpleNode node) throws Exception {
    if (node.jjtGetNumChildren() == 0) {
        operands = new Operator[0];
        return;
    }
    if (node.jjtGetNumChildren() != 1)
        throw new Error("AST for function has unexpected form");
    Object o = node.jjtGetChild(0);
    if (!(o instanceof ASTList))
        throw new Error("AST for function has unexpected form");
    ASTList l = (ASTList) o;
    int count = l.jjtGetNumChildren();
    operands = new Operator[count];
    for (int i = 0; i < count; i++) {
        operands[i] = Operator.getFrom((SimpleNode) l.jjtGetChild(i));
        operands[i].parent = this;
    }
}
Also used : ASTList(gov.sandia.n2a.language.parse.ASTList) SimpleNode(gov.sandia.n2a.language.parse.SimpleNode)

Aggregations

SimpleNode (gov.sandia.n2a.language.parse.SimpleNode)5 ASTList (gov.sandia.n2a.language.parse.ASTList)4 ASTConstant (gov.sandia.n2a.language.parse.ASTConstant)2 ASTIdentifier (gov.sandia.n2a.language.parse.ASTIdentifier)2 ASTMatrix (gov.sandia.n2a.language.parse.ASTMatrix)1 ASTOperator (gov.sandia.n2a.language.parse.ASTOperator)1