Exemple #1
0
        private CompilerContext IntLiteral(CompilerContext context)
        {
            context.AsFluent()
            .Mov.RU(Register.A, uint.Parse(context.Node.ToString()));

            return(context);
        }
Exemple #2
0
        private CompilerContext UnaryOperator(CompilerContext context)
        {
            switch (context.Node.Value)
            {
            case "!":
                context.AsFluent()
                .Not.RR(Register.A, Register.A);
                break;

            case "-":
                context.AsFluent()
                .Neg.RR(Register.A, Register.A);
                break;

            default:
                throw new InvalidOperationException("Cannot determine unary operator: " + context.Node.Value);
            }

            return(context);
        }
Exemple #3
0
        private CompilerContext Identifier(CompilerContext context)
        {
            var name   = context.Node.Value;
            var symbol = context.SymbolTable[name];

            context.AsFluent()
            .Mov.RU(Register.A, symbol.Address)
            .Add.RR(Register.A, Register.H)
            .Mov.RM(Register.A, MemoryAddress.A);

            return(context);
        }
Exemple #4
0
        private CompilerContext AssignmentExpression(CompilerContext context)
        {
            var identifier = context.Node[0];

            foreach (var right in context.Node[1])
            {
                context.Compile(right[1]);
            }

            var name   = identifier.Value;
            var symbol = context.SymbolTable[name];

            context.AsFluent()
            .Mov.RU(Register.B, symbol.Address, $"Load variable {name} into rB")
            .Add.RR(Register.B, Register.H)
            .Mov.MR(MemoryAddress.B, Register.A);

            return(context);
        }
Exemple #5
0
        public IEnumerable<Instruction> Compile()
        {
            var context = new CompilerContext
            {
                Code = new CodeStream(),
                Compiler = Compile,
                Node = _root,
                Parent = null,
                SymbolTable = new SymbolTable(),
            };

            foreach ( var node in context.Node )
                context.Compile(node);

            context.AsFluent()
                .Exit.R(Register.A);

            return context.Code;
        }
Exemple #6
0
        public IEnumerable <Instruction> Compile()
        {
            var context = new CompilerContext
            {
                Code        = new CodeStream(),
                Compiler    = Compile,
                Node        = _root,
                Parent      = null,
                SymbolTable = new SymbolTable(),
            };

            foreach (var node in context.Node)
            {
                context.Compile(node);
            }

            context.AsFluent()
            .Exit.R(Register.A);

            return(context.Code);
        }
Exemple #7
0
        private static CompilerContext CompileExpression(CompilerContext context, params Tuple <string, Action <FluentWriter>, OpCode>[] codeWriters)
        {
            context.Compile(context.Node[0]);
            var code        = context.AsFluent();
            var expressions = codeWriters.ToDictionary(k => k.Item1, v => new { writer = v.Item2, jumpCode = v.Item3 });

            foreach (var right in context.Node[1])
            {
                code.Push.R(Register.A);

                var @operator = right[0];
                context.Compile(right[1]);

                code.Pop.R(Register.B);

                var expr = expressions[@operator.Value];
                expr.writer(code);

                context.JumpExpression = expr.jumpCode;
            }

            return(context);
        }
Exemple #8
0
        private static CompilerContext CompileExpression(CompilerContext context, params Tuple<string, Action<FluentWriter>, OpCode>[] codeWriters)
        {
            context.Compile(context.Node[0]);
            var code = context.AsFluent();
            var expressions = codeWriters.ToDictionary(k => k.Item1, v => new { writer = v.Item2, jumpCode = v.Item3 });

            foreach (var right in context.Node[1])
            {
                code.Push.R(Register.A);

                var @operator = right[0];
                context.Compile(right[1]);

                code.Pop.R(Register.B);

                var expr = expressions[@operator.Value];
                expr.writer(code);

                context.JumpExpression = expr.jumpCode;
            }

            return context;
        }
Exemple #9
0
        private CompilerContext UnaryOperator(CompilerContext context)
        {
            switch ( context.Node.Value )
            {
                case "!":
                    context.AsFluent()
                        .Not.RR(Register.A, Register.A);
                    break;
                case "-":
                    context.AsFluent()
                        .Neg.RR(Register.A, Register.A);
                    break;
                default:
                    throw new InvalidOperationException("Cannot determine unary operator: " + context.Node.Value );
            }

            return context;
        }
Exemple #10
0
        private CompilerContext IntLiteral(CompilerContext context)
        {
            context.AsFluent()
                .Mov.RU(Register.A, uint.Parse(context.Node.ToString()));

            return context;
        }
Exemple #11
0
        private CompilerContext Identifier(CompilerContext context)
        {
            var name = context.Node.Value;
            var symbol = context.SymbolTable[name];

            context.AsFluent()
                .Mov.RU(Register.A, symbol.Address)
                .Add.RR(Register.A, Register.H)
                .Mov.RM(Register.A, MemoryAddress.A);

            return context;
        }
Exemple #12
0
        private CompilerContext AssignmentExpression(CompilerContext context)
        {
            var identifier = context.Node[0];
            foreach (var right in context.Node[1])
            {
                context.Compile(right[1]);
            }

            var name = identifier.Value;
            var symbol = context.SymbolTable[name];

            context.AsFluent()
                .Mov.RU(Register.B, symbol.Address, $"Load variable {name} into rB")
                .Add.RR(Register.B, Register.H)
                .Mov.MR(MemoryAddress.B, Register.A);

            return context;
        }