Programming-Language-Design / src / visitor / codegeneration / attribute_grammars / code_templates / ValueCG.txt
ValueCG.txt
Raw
Code function for Expression

    value[[Arithmetic: exp1 -> exp2 (+|-|*|/) exp3]] =
        value[[exp2]]
        exp2.type.convertTo(exp1.type)
        value[[exp3]]
        exp3.type.convertTo(exp1.type)
        switch(exp1.operator){
            case "+": <add> exp1.type.suffix(); break;
            case "-": <sub> exp1.type.suffix(); break;
            case "*": <div> exp1.type.suffix(); break;
            case "/": <mul> exp1.type.suffix(); break;
        }

    value[[ArrayIndexing: exp1 -> exp2 exp3]] =
        address[[exp1]]
        <load> exp1.type.suffix()

    value[[Cast: exp1 -> type exp2]]:
        value[[exp2]]
        exp2.type.convert(type)

    value[[CharLiteral: exp -> CHAR_CONSTANT]] =
        <pushb> exp.value
    value[[IntLiteral: exp -> INT_CONSTANT]] =
        <pushi> exp.value
    value[[DoubleLiteral: exp -> REAL_CONSTANT]] =
        <pushf> exp.value

    value[[Variable: exp -> ID]] =
        address[[exp]]
        <load> exp.type.suffix()

    value[[Comparison: exp1 -> exp2 (>|>=|<|<=|!=|==) exp3]] =
        value[[exp2]]
        value[[exp3]]
        switch(exp1.operator){
            case ">": <gt> exp2.type.suffix(); break;
            case "<": <lt> exp2.type.suffix(); break;
            case ">=": <ge> exp2.type.suffix(); break;
            case "<=": <le> exp2.type.suffix(); break;
            case "==": <eq> exp2.type.suffix(); break;
            case "!=": <ne> exp2.type.suffix(); break;
        }

    value[[Logical: exp1 -> exp2 (&&|/||) exp3]] =
        value[[exp2]]
        value[[exp3]]
        switch(exp1.operator){
            case "&&": <and>; break;
            case "||": <or>; break;
        }

    value[[FieldAccess: exp1 -> exp2 ID]]=
        address[[exp1]]
        <load> exp1.type.suffix();

    value[[FunctionInvocation: exp1 -> exp2 exp3*]]=
        exp3*.forEach(arg -> value[[arg]]);
        <call> exp2;