Ejemplo n.º 1
0
        public void Compile(Generator gen, String outputFile)
        {
            // Generate compiletime environment
            var labelMap = new Dictionary <String, String>();

            foreach (String funName in fenv.getFunctionNames())
            {
                labelMap.Add(funName, Label.Fresh());
            }
            CEnv cenv = new CEnv(labelMap);

            // Compile expression
            e.Compile(cenv, gen);
            gen.Emit(Instruction.PRINTI);
            gen.Emit(Instruction.STOP);

            // Compile functions
            foreach (FuncDef f in fenv.getFunctions())
            {
                cenv = new CEnv(labelMap);
                f.Compile(gen, cenv);
            }

            //  Generate bytecode at and print to file
            gen.PrintCode();
            int[] bytecode = gen.ToBytecode();
            using (TextWriter wr = new StreamWriter(outputFile)) {
                foreach (int b in bytecode)
                {
                    wr.Write(b);
                    wr.Write(" ");
                }
            }
        }
Ejemplo n.º 2
0
 public override void Compile(CEnv env, Generator gen)
 {
     //arg.Compile (env, gen);
     //String fLabel = env.getFunctionLabel(fName);
     //gen.Emit(new CALL(1, fLabel));
     throw new NotSupportedException("This functionality will be provided at a later moment.");
 }
Ejemplo n.º 3
0
 public void Compile(Generator gen, CEnv env)
 {
     //env.DeclareLocal(formArg.Fst); // Formal argument name points to top of stack
     //gen.Label(env.getFunctionLabel(fName));
     //body.Compile(env, gen);
     //gen.Emit(new RET(1));
     throw new NotSupportedException("This functionality will be provided at a later moment.");
 }
 public override void Compile(CEnv env, Generator gen)
 {
     e1.Compile(env, gen);
     env.DeclareLocal(this.name);
     e2.Compile(env, gen);
     env.PopEnv();
     gen.Emit(Instruction.SWAP);
     gen.Emit(new INCSP(-1));
 }
 public void Compile(Generator gen, CEnv env)
 {
     foreach (Pair <String, Type> formArg in this.formArgs)
     {
         env.DeclareLocal(formArg.Fst);
     }
     gen.Label(env.getFunctionLabel(fName));
     body.Compile(env, gen);
     gen.Emit(new RET(formArgs.Count));
 }
        public override void Compile(CEnv env, Generator gen)
        {
            String L1 = Label.Fresh();
            String L2 = Label.Fresh();

            e1.Compile(env, gen);
            gen.Emit(new IFZERO(L1));
            e2.Compile(env, gen);
            gen.Emit(new GOTO(L2));
            gen.Label(L1);
            e3.Compile(env, gen);
            gen.Label(L2);
        }
Ejemplo n.º 7
0
        public override void Compile(CEnv env, Generator gen)
        {
            int argCount = 0;

            foreach (var expression in expressions)
            {
                expression.Compile(env, gen);
                argCount++;
            }
            String fLabel = env.getFunctionLabel(fName);

            gen.Emit(new CALL(argCount, fLabel));
        }
Ejemplo n.º 8
0
        public override void Compile(CEnv env, Generator gen)
        {
            string l1 = Label.Fresh();
            string l2 = Label.Fresh();

            cond.Compile(env, gen);
            gen.Emit(new IFZERO(l1));
            e2.Compile(env, gen);
            gen.Emit(new GOTO(l2));
            gen.Label(l1);
            e3.Compile(env, gen);
            gen.Label(l2);
        }
Ejemplo n.º 9
0
        public void Compile(Generator gen, CEnv env)
        {
            int argCount = 0;

            foreach (var formArg in formArgs)
            {
                env.DeclareLocal(formArg.Fst);
                argCount++;
            }

            gen.Label(env.getFunctionLabel(fName));
            body.Compile(env, gen);
            gen.Emit(new RET(argCount));
            //throw new NotSupportedException("This functionality will be provided at a later moment.");
        }
        public override void Compile(CEnv env, Generator gen)
        {
            foreach (Expression arg in args)
            {
                arg.Compile(env, gen);
                env.PushTemporary();
            }

            String fLabel = env.getFunctionLabel(fName);

            gen.Emit(new CALL(args.Count, fLabel));

            foreach (Expression arg in args)
            {
                env.PopTemporary();
            }
        }
Ejemplo n.º 11
0
        public override void Compile(CEnv env, Generator gen)
        {
            e1.Compile(env, gen);
            switch (op)
            {
            case Operator.Not:
                gen.Emit(Instruction.NOT);
                break;

            case Operator.Neg:
                gen.Emit(new CSTI(0));
                gen.Emit(Instruction.SWAP);
                gen.Emit(Instruction.SUB);
                break;

            default:
                throw new Exception("Unknown unary operator: " + op);
            }
        }
Ejemplo n.º 12
0
 public override void Compile(CEnv env, Generator gen)
 {
     throw new NotSupportedException("This functionality will be provided at a later moment.");
 }
Ejemplo n.º 13
0
        public override void Compile(CEnv env, Generator gen)
        {
            e1.Compile(env, gen);
            env.PushTemporary();
            e2.Compile(env, gen);
            switch (op)
            {
            case Operator.Add:
                gen.Emit(Instruction.ADD);
                break;

            case Operator.Div:
                gen.Emit(Instruction.DIV);
                break;

            case Operator.Mul:
                gen.Emit(Instruction.MUL);
                break;

            case Operator.Sub:
                gen.Emit(Instruction.SUB);
                break;

            case Operator.Eq:
                gen.Emit(Instruction.EQ);
                break;

            case Operator.Ne:
                gen.Emit(Instruction.EQ);
                gen.Emit(Instruction.NOT);
                break;

            case Operator.Ge:
                gen.Emit(Instruction.LT);
                gen.Emit(Instruction.NOT);
                break;

            case Operator.Gt:
                gen.Emit(Instruction.SWAP);
                gen.Emit(Instruction.LT);
                break;

            case Operator.Le:
                gen.Emit(Instruction.SWAP);
                gen.Emit(Instruction.LT);
                gen.Emit(Instruction.NOT);
                break;

            case Operator.Lt:
                gen.Emit(Instruction.LT);
                break;

            case Operator.And:
                gen.Emit(Instruction.MUL);
                break;

            case Operator.Or:
                gen.Emit(Instruction.ADD);
                gen.Emit(new CSTI(0));
                gen.Emit(Instruction.EQ);
                gen.Emit(Instruction.NOT);
                break;

            default:
                throw new Exception("Unknown binary operator: " + op);
            }
            env.PopTemporary();
        }
Ejemplo n.º 14
0
 public override void Compile(CEnv env, Generator gen)
 {
     env.CompileVariable(gen, name);
     gen.Emit(Instruction.LDI);
 }
Ejemplo n.º 15
0
 public override void Compile(CEnv env, Generator gen)
 {
     gen.Emit(new CSTI(value));
 }
Ejemplo n.º 16
0
 abstract public void Compile(CEnv env, Generator gen);