Exemplo n.º 1
0
        // 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));
        }
Exemplo n.º 2
0
        // 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;
            }
        }
Exemplo n.º 3
0
        // 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);
        }
Exemplo n.º 4
0
        // 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));
        }
Exemplo n.º 5
0
        // 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;
            }
        }
Exemplo n.º 6
0
        // 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));
        }
Exemplo n.º 7
0
        // public int Line;
        // public int Column;



        public abstract void Compile(GeneratorState state, Trac42Program program);
Exemplo n.º 8
0
        // public bool Value;

        public override void Compile(GeneratorState state, Trac42Program program)
        {
            program.Emit(new Instruction(Instruction.OPCODE.PUSHBOOL, Value ? 1 : 0));
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
        // public string Name;
        // public Expression Argument;

        public override void Compile(GeneratorState state, Trac42Program program)
        {
            // not present after lift
            throw new NotImplementedException();
        }