Exemplo n.º 1
0
        public static void CheckArgIsRegisterOrAtom(Token token)
        {
            if (token == null)
            {
                throw new ArgumentNullException("token");
            }

            bool ok = false;

            do
            {
                if (TokenIsRegister(token))
                {
                    ok = true;
                    break;
                }

                if (TokenIsAtom(token))
                {
                    ok = true;
                    break;
                }

                // got here => got problem (ok is false)

            } while (false);

            if (!ok)
            {
                throw new ApplicationException(); // todo2[ak]
            }
        }
        public override Instruction Build(Token[] args)
        {
            AsmHelper.CheckArgsCount(args, 1);

            Instruction instr = (JmpInstructionBase)Activator.CreateInstance(this.JmpInstructionType);
            return instr;
        }
Exemplo n.º 3
0
        public override Instruction Build(Token[] args)
        {
            // todo1[ak] check args
            AsmHelper.CheckArgsCount(args, 0);

            Instruction instr = new RetInstruction();
            return instr;
        }
Exemplo n.º 4
0
        public static void CheckArgsCount(Token[] tokens, int expectedCount)
        {
            // todo1[ak] check args

            if (tokens.Length != expectedCount)
            {
                throw new ApplicationException(); // todo1[ak]
            }
        }
Exemplo n.º 5
0
        public override Instruction Build(Token[] args)
        {
            // todo1[ak] check args
            AsmHelper.CheckArgsCount(args, 1);

            RegisterName regName = AsmHelper.TokenToRegisterName(args[0]);
            TestInstruction instr = new TestInstruction(regName);

            return instr;
        }
Exemplo n.º 6
0
        public static void CheckArgIsRegister(Token token)
        {
            // todo1[ak] check args

            bool isReg = _registersIds.ContainsKey(TokenToSymbolName(token));

            if (!isReg)
            {
                throw new ApplicationException(); // todo2[ak]
            }
        }
Exemplo n.º 7
0
        public void AddInstruction(InstructionBuilder instructionBuilder, Token[] argTokens)
        {
            // todo1[ak] check args

            if (this.IsFinished)
            {
                throw new ApplicationException(); // todo2[ak]
            }

            InstructionData data = new InstructionData(instructionBuilder, argTokens);
            _instructionDatas.Add(data);
        }
Exemplo n.º 8
0
        public override Instruction Build(Token[] args)
        {
            // todo1[ak] check args
            AsmHelper.CheckArgsCount(args, 2);

            int index = AsmHelper.TokenToInt(args[0]);
            RegisterName regName = AsmHelper.TokenToRegisterName(args[1]);

            NthInstruction instr = new NthInstruction(index, regName);

            return instr;
        }
Exemplo n.º 9
0
        public override Instruction Build(Token[] args)
        {
            // todo1[ak] check args

            if (args.Length != 1)
            {
                throw new ApplicationException();
            }

            RegisterName regName = AsmHelper.TokenToRegisterName(args[0]);

            Instruction instr = new PopInstruction(regName);
            return instr;
        }
Exemplo n.º 10
0
        public override Instruction Build(Token[] args)
        {
            // todo1[ak] check args

            AsmHelper.CheckArgsCount(args, 2);
            AsmHelper.CheckArgIsRegister(args[0]);
            AsmHelper.CheckArgIsRegisterOrAtom(args[1]);

            RegisterName to = AsmHelper.TokenToRegisterName(args[0]);
            object from = AsmHelper.TokenToRegisterNameOrAtom(args[1]);

            MovInstruction instr = new MovInstruction(to, from);
            return instr;
        }
Exemplo n.º 11
0
        public override Instruction Build(Token[] args)
        {
            // todo1[ak] check args

            if (args.Length != 1)
            {
                throw new ApplicationException();
            }

            Token tok = args[0];
            if (!(tok is SymbolToken))
            {
                throw new ApplicationException();
            }

            SymbolToken sym = (SymbolToken)tok;
            RegisterName registerName = AsmHelper.GetRegisterName(sym.Name);

            Instruction instr = new CarInstruction(registerName);
            return instr;
        }
Exemplo n.º 12
0
        public override Instruction Build(Token[] args)
        {
            // todo1[ak] check args
            AsmHelper.CheckArgsCount(args, 1, 2);

            int from = AsmHelper.TokenToInt(args[0]);
            int to;

            if (args.Length == 2)
            {
                to = AsmHelper.TokenToInt(args[1]);
            }
            else
            {
                to = from;
            }

            // todo1[ak] check from..to

            Instruction instr = new CheckArgsCountInstruction(from, to);
            return instr;
        }
Exemplo n.º 13
0
        private Atom TokenToSimpleAtom(Token token)
        {
            Atom atom;

            if (token is SymbolToken)
            {
                atom = new Symbol(((SymbolToken)token).Name);
            }
            else if (token is NumberToken)
            {
                atom = new Number(((NumberToken)token).Value);
            }
            else
            {
                throw new NotImplementedException();
            }

            return atom;
        }
Exemplo n.º 14
0
        public static LispObject TokenToAtom(Token tok)
        {
            // todo1[ak] check args

            LispObject el;

            if (tok is SymbolToken)
            {
                el = new Symbol(((SymbolToken)tok).Name);
            }
            else if (tok is NumberToken)
            {
                el = new Number(((NumberToken)tok).Value);
            }
            else
            {
                throw new NotImplementedException();
            }

            return el;
        }
Exemplo n.º 15
0
        public static bool TokenIsAtom(Token token)
        {
            bool res =
                token is SymbolToken ||
                token is NumberToken;

            return res;
        }
Exemplo n.º 16
0
        public static bool TokenIsRegister(Token token)
        {
            bool res =
                token is SymbolToken &&
                _registersIds.ContainsKey(TokenToSymbolName(token));

            return res;
        }
Exemplo n.º 17
0
        public static bool IsAtomToken(Token tok)
        {
            // todo1[ak] check args

            bool res;

            if (
                tok is SymbolToken ||
                tok is NumberToken)
            {
                res = true;
            }
            else
            {
                res = false;
            }

            return res;
        }
Exemplo n.º 18
0
 public abstract Instruction Build(Token[] args);
Exemplo n.º 19
0
        public static int TokenToInt(Token token)
        {
            // todo1[ak]

            if (!(token is NumberToken))
            {
                throw new ApplicationException(); // todo2[ak]
            }

            NumberToken numTok = (NumberToken)token;

            int val = int.Parse(numTok.Value);
            return val;
        }
Exemplo n.º 20
0
        public static RegisterName TokenToRegisterName(Token token)
        {
            // todo1[ak] check args

            RegisterName res = _registersIds[TokenToSymbolName(token)];
            return res;
        }
Exemplo n.º 21
0
        public static object TokenToRegisterNameOrAtom(Token token)
        {
            // todo1[ak] check args

            object res;

            if (TokenIsRegister(token))
            {
                res = TokenToRegisterName(token);
            }
            else if (token is SymbolToken)
            {
                SymbolToken symTok = (SymbolToken)token;
                string name = symTok.Name;

                if (name == "NIL")
                {
                    res = Symbol.Nil;
                }
                else if (name == "T")
                {
                    res = Symbol.True;
                }
                else
                {
                    throw new ApplicationException(); // todo2[ak]
                }
            }
            else
            {
                throw new NotImplementedException();
            }

            return res;
        }
Exemplo n.º 22
0
 internal InstructionData(InstructionBuilder builder, Token[] argTokens)
 {
     this.Builder = builder;
     this.ArgTokens = argTokens;
 }
Exemplo n.º 23
0
        public static string TokenToSymbolName(Token token)
        {
            if (token == null)
            {
                throw new ArgumentNullException("token");
            }

            if (!(token is SymbolToken))
            {
                throw new ApplicationException(); // todo1[ak]
            }

            string name = ((SymbolToken)token).Name;
            return name;
        }
Exemplo n.º 24
0
        public static void CheckArgsCount(Token[] tokens, int from, int to)
        {
            // todo1[ak] check args

            if (!tokens.Length.Between(from, to))
            {
                throw new ApplicationException(); // todo1[ak]
            }
        }