Question: SCALA PLEASE THANK YOU MACHINE INSTRUCTIONS ARE AS FOLLOWS: CODE: Your goal is to implement the compilation routine compileToStackMachineCode (e: Expr): List[StackMachine Instruction) in the
SCALA PLEASE THANK YOU

MACHINE INSTRUCTIONS ARE AS FOLLOWS:

CODE:

Your goal is to implement the compilation routine compileToStackMachineCode (e: Expr): List[StackMachine Instruction) in the file StackMachineCompilation.scala . The function takes in an expression e and outputs a list of stack machine instructions. Constant Rule The rule for constants is simple. An expression Const(f) compiles to the instruction Pushi(f). (const-rule) compileExpr(Const(f)) = [Pushi] Note again that compileExpr maps expressions to list of instructions. Add Rule compileExpr(e1) = L1, compileExpr(e2) = L2 compileExpr(Plus(e1, e2) = ListContatenation(L1, L2, [Addr]) (add-rule) The instructions concatenate the lists L1, L2 along with the list consisting of a single instruction [ AddI ] . Note that the ++ operator in scala implements the list concatenation. Subtract Rule compileExpr(e1) = Li, compileExpr(e2) = L2 compileExpr(Minus(e1, e2) = List Contatenation(L1, L2, [Subl]) (minus-rule) The instructions concatenate the lists L1, L2 along with the list consisting of a single instruction [ SubI ] . Rules for Other expressions We hope that you will be able to fill in rules for other cases Mult, Div, Exp , Log, Sine and Cosine. object stackMachineCompiler { /* Function compileToStackMachineCode Given expression e as input, return a corresponding list of stack machine instructions. The type of stackmachine instructions are in the file StackMachineEmulator.scala in this same directory The type of Expr is in the file Expr.scala in this directory. */ def compileToStackMachineCode(e: Expr): List[StackMachine Instruction] = ??? e = } Your goal is to implement the compilation routine compileToStackMachineCode (e: Expr): List[StackMachine Instruction) in the file StackMachineCompilation.scala . The function takes in an expression e and outputs a list of stack machine instructions. Constant Rule The rule for constants is simple. An expression Const(f) compiles to the instruction Pushi(f). (const-rule) compileExpr(Const(f)) = [Pushi] Note again that compileExpr maps expressions to list of instructions. Add Rule compileExpr(e1) = L1, compileExpr(e2) = L2 compileExpr(Plus(e1, e2) = ListContatenation(L1, L2, [Addr]) (add-rule) The instructions concatenate the lists L1, L2 along with the list consisting of a single instruction [ AddI ] . Note that the ++ operator in scala implements the list concatenation. Subtract Rule compileExpr(e1) = Li, compileExpr(e2) = L2 compileExpr(Minus(e1, e2) = List Contatenation(L1, L2, [Subl]) (minus-rule) The instructions concatenate the lists L1, L2 along with the list consisting of a single instruction [ SubI ] . Rules for Other expressions We hope that you will be able to fill in rules for other cases Mult, Div, Exp , Log, Sine and Cosine. object stackMachineCompiler { /* Function compileToStackMachineCode Given expression e as input, return a corresponding list of stack machine instructions. The type of stackmachine instructions are in the file StackMachineEmulator.scala in this same directory The type of Expr is in the file Expr.scala in this directory. */ def compileToStackMachineCode(e: Expr): List[StackMachine Instruction] = ??? e = }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
