What is the Logic Tree?

This is a continuation of the previous sections, if you haven’t already, you can read from the start


You can try out this logic tree here!


It looks very similar to the the number tree, but it uses logic gates instead.


They both do pretty much the same thing, just not in the same contexts. So, the code and general processes are all similar, with only a few minor differences here and there.

Differences

None of the major functions changed much. They just use different symbols and inputs. For the logic tree, I decided to skip the simplifier. It is important, but not completely necessary. Still, I plan to add it in some time in the future.


With the number tree, the parser just had to check for single symbols. But now, it has to check for multiple. For example, ~& (Nand) uses two characters: ~ and &. Which means, we can’t just go about it character by character, we need to first make sure that we combine the symbols together.


You might remember the joinSimilars function from the number tree. We’re just going to change that a bit to make sure it joins the operators together.

const gateNames = ["~", "&", "|", "~&", "~|", "!=", "true", "false"];

function prepareGateString(gate: string, names: string[]): string[] {
    const listed = gate.split("")
    
    // joining
    let waiting: string = "";
    let prepared: string[] = [];

    for (const idx in listed) {
        if (listed[idx] === " ") {
            
            if(waiting !== "") {
                prepared = [...prepared, waiting];
            }

            waiting = "";

        } else if (listed[idx] === "(" || listed[idx] === ")") {
            prepared = [...prepared, ...(waiting === "" ? [] : [waiting]), listed[idx]];
            waiting = "";

        } else if (JSON.parse(idx) === listed.length - 1) {
            prepared = [...prepared, ...(waiting === "" ? [] : [waiting]), listed[idx]];

        } else if (waiting === "~") {
            if(listed[idx] === "&" || listed[idx] === "|") {
                waiting = waiting + listed[idx];
            } else {
                prepared = [...prepared, waiting, listed[idx]];
                waiting = "";
            }
        } else {
            waiting = waiting + listed[idx];

        }
    }

    return prepared;
}

It looks a little complicated, so let’s go line by line.

First, the input string can be split to make a list. You can see that there is a waiting here as well.


This one’s a bit different, though. It stores just a string. For symbols like ~&, it will store ~, then add & to it after.


Then there’s also a prepared. This one has the same function as parsed in the parser. It just stores the result-in-progress.


This one uses a for loop. Let’s start with the first if.

if (listed[idx] === " ") {
            
            if(waiting !== "") {
                prepared = [...prepared, waiting];
            }

            waiting = "";

        }

For this one, we’re using " " (space) as a separator. Whenever theres a space, it adds the waiting to the prepared list. Then, it resets the waiting.


Next, open parentheses and closed parentheses.


else if (listed[idx] === "(" || listed[idx] === ")") {
    prepared = [...prepared, ...(waiting === "" ? [] : [waiting]), listed[idx]];
    waiting = "";

}

This one does pretty much the same thing.


It adds the waiting to the prepared, but also the open/closed parentheses. If the waiting is empty, it just puts the parentheses in there. It resets the waiting as well, just like before.


else if (JSON.parse(idx) === listed.length - 1) {
    prepared = [...prepared, ...(waiting === "" ? [] : [waiting]), listed[idx]];
}

This is to check if it’s on the last value of the list. If it is, puts the waiting and the value into the prepared list.


This step is needed because otherwise, the value would be added to the waiting, and the loop would end without the value.


else if (waiting === "~") {
    if(listed[idx] === "&" || listed[idx] === "|") {
        waiting = waiting + listed[idx];
    } else {
        prepared = [...prepared, waiting, listed[idx]];
        waiting = "";
    }
}

This is for the ~. It could either be a not, or it could be the start of a nand or nor.


This one actually checks if waiting is ~, not the current value. If the waiting is ~, it either adds the current value to ~ if the value is & or |.
Otherwise, it just adds the waiting and current value to the prepared and then resets the waiting.


else {
    waiting = waiting + listed[idx];
}

It comes here whenever the current value is a gate (except for when the waiting is ~), variable, boolean, or binary.


Finally, it just returns prepared.


That’s pretty much the only change. The other major functions (parser, orderer, evaluator) all do the same thing as with the number tree, but they use logic gates instead.

Go To:

< Simplifier Outro >