Skip to content

Commit

Permalink
feat: 🚀 Switch instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseLorenzana272 committed Oct 5, 2024
1 parent a40601a commit 0325c67
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions JS_Analyzer_parts/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,68 @@ visitVariableAssign(node) {
this.code.comment('Fin Continue');
}

/**
* @type {BaseVisitor['visitSwitchNode']}
*/
visitSwitchNode(node) {
this.code.comment('Inicio de Switch');

// Evaluar la expresión del switch
node.exp.accept(this);
this.code.popObject(r.T0);

const endSwitchLabel = this.code.getLabel();
this.break_labels.push(endSwitchLabel);

const defaultLabel = this.code.getLabel();
let hasDefault = false;

// Se generan las etiquetas para cada caso en un array
const caseLabels = node.cases.map(caseNode => ({
value: caseNode.value.value,
label: this.code.getLabel()
}));
console.log(caseLabels);

// Comparar la expresión del switch con cada caso
for (const {value, label} of caseLabels) {
this.code.comment(`Comparación caso ${value}`);
// Aqui es mejor cargar el valor de manera inmediata
this.code.li(r.T1, value);
this.code.beq(r.T0, r.T1, label);
}

// Se va a default si no cuumple los cases
if (node.def) {
hasDefault = true;
this.code.j(defaultLabel);
} else {
this.code.j(endSwitchLabel);
}

// Instrucciones de cada case
for (let i = 0; i < node.cases.length; i++) {
const caseNode = node.cases[i];
this.code.addLabel(caseLabels[i].label);
this.code.comment(`Ejecutando caso ${caseLabels[i].value}`);
caseNode.inst.forEach(stmt => stmt.accept(this));
// Fall through to next case if there's no explicit break
}

// Default case
if (hasDefault) {
this.code.addLabel(defaultLabel);
this.code.comment('Caso por defecto');
node.def.stmts.forEach(stmt => stmt.accept(this));
}

this.code.addLabel(endSwitchLabel);
this.break_labels.pop();
this.code.comment('Fin del Switch');
}



}
/*
visitPrint(node) {
Expand Down

0 comments on commit 0325c67

Please sign in to comment.