// public BinaryOperator Operator; // public Expression Expression1; // public Expression Expression2; public override void Compile(GeneratorState state, Trac42Program program) { Expression1.Compile(state, program); Expression2.Compile(state, program); switch (Operator) { case BinaryOperator.Add: program.Emit(new Instruction(Instruction.OPCODE.ADD)); break; case BinaryOperator.Sub: program.Emit(new Instruction(Instruction.OPCODE.SUB)); break; case BinaryOperator.Eq: switch (OperandType) { case Type.BoolType: program.Emit(new Instruction(Instruction.OPCODE.EQBOOL)); break; case Type.NumType: program.Emit(new Instruction(Instruction.OPCODE.EQINT)); break; } break; case BinaryOperator.Lt: program.Emit(new Instruction(Instruction.OPCODE.LTINT)); break; } }
// List<LiftedFunctionExpression> Functions; // Expression MainExpression; public Trac42Program Compile() { var state = new GeneratorState(); var program = new Trac42Program(); Functions.ForEach(function => function.Compile(state, program)); program.Emit(new Instruction(Instruction.OPCODE.LABEL, "main")); MainExpression.Compile(state, program); program.Emit(new Instruction(Instruction.OPCODE.END)); return(program); }
// public string Name; // public List<Expression> Arguments; public override void Compile(GeneratorState state, Trac42Program program) { program.Emit(new Instruction(Instruction.OPCODE.DECL, 1)); for (var i = Arguments.Count - 1; i >= 0; i--) { Arguments[i].Compile(state, program); } program.Emit(new Instruction(Instruction.OPCODE.BSR, Name)); program.Emit(new Instruction(Instruction.OPCODE.POP, Arguments.Count)); }
// public string Name; public override void Compile(GeneratorState state, Trac42Program program) { var offset = state.Lookup(Name); switch (Type) { case Type.BoolType: program.Emit(new Instruction(Instruction.OPCODE.RVALBOOL, offset)); break; case Type.NumType: program.Emit(new Instruction(Instruction.OPCODE.RVALINT, offset)); break; } }
// public string Name; // public List<string> ArgumentNames; // public Type ReturnType; // public Expression Body; public override void Compile(GeneratorState state, Trac42Program program) { program.Emit(new Instruction(Instruction.OPCODE.LABEL, Name)); var argumentOffset = 2; foreach (var argumentName in ArgumentNames) { state.OffsetMap[argumentName] = argumentOffset; argumentOffset++; } var returnOffset = argumentOffset; program.Emit(new Instruction(Instruction.OPCODE.LINK)); program.Emit(new Instruction(Instruction.OPCODE.LVAL, returnOffset)); Body.Compile(state, program); switch (ReturnType) { case Type.BoolType: program.Emit(new Instruction(Instruction.OPCODE.ASSBOOL)); break; case Type.NumType: program.Emit(new Instruction(Instruction.OPCODE.ASSINT)); break; } program.Emit(new Instruction(Instruction.OPCODE.UNLINK)); program.Emit(new Instruction(Instruction.OPCODE.RTS)); }
// public string Name; // public Expression Expression; // public Expression Recipient; // public Type Type; public override void Compile(GeneratorState state, Trac42Program program) { program.Emit(new Instruction(Instruction.OPCODE.DECL, 1)); state.Bind(Name); Expression.Compile(state, program); switch (Type) { case Type.BoolType: program.Emit(new Instruction(Instruction.OPCODE.ASSBOOL)); break; case Type.NumType: program.Emit(new Instruction(Instruction.OPCODE.ASSINT)); break; } Recipient.Compile(state, program); state.Unbind(Name); program.Emit(new Instruction(Instruction.OPCODE.POP, 1)); }
// public bool Value; public override void Compile(GeneratorState state, Trac42Program program) { program.Emit(new Instruction(Instruction.OPCODE.PUSHBOOL, Value ? 1 : 0)); }
// public Expression Expression1; // public Expression Expression2; public override void Compile(GeneratorState state, Trac42Program program) { Expression1.Compile(state, program); program.Emit(new Instruction(Instruction.OPCODE.POP, 1)); Expression2.Compile(state, program); }