Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.43.0

0.43.0 / src / back / effects.lucb

#==============================================================================================
#
#   effects - Evaluation-order facts shared by both backends
#
#   DESCRIPTION:
#       Whether one operand of an expression may have an effect another operand could
#       observe, so the emitters know when the order of evaluation matters (§7.1).
#
#==============================================================================================

import front.ast

## Whether evaluating `e` may have an effect another operand could observe (§7.1): a call
## (not `sizeof` and its kin), a `try`, a `catch`, an allocation, or a formatted string
## with such a field. A lambda's
## body runs later, not here.
pub func may_have_effect(e: ast.Node*) -> bool:
    match e.kind:
        .call:
            # `memory.size_of`, `align_of` and `offset_of` are constants, not calls that run
            if let callee = e.left:
                if callee.kind == ast.Kind.name and callee.has(ast.flag_builtin) and (callee.text == "size_of" or callee.text == "align_of" or callee.text == "offset_of"):
                    return false
            return true
        .catch_, .new_, .new_unwritten, .match_expr, .propagate:
            return true
        .unary:
            if e.text == "try":
                return true
        .lambda:
            return false
        _:
            _ = e
    if let l = e.left:
        if may_have_effect(l):
            return true
    if let r = e.right:
        if may_have_effect(r):
            return true
    if e.kind == ast.Kind.conditional:
        if let condition = e.ty:
            if may_have_effect(condition):
                return true
    var cursor = e.body
    while let b = cursor:
        if may_have_effect(b):
            return true
        cursor = b.next
    return false

## Whether any argument of a call may have an effect.
pub func arguments_have_effect(args: ast.Node*?) -> bool:
    var cursor = args
    while let a = cursor:
        if let v = a.left:
            if may_have_effect(v):
                return true
        cursor = a.next
    return false