Exemple #1
0
 public bool Initialize()
 {
     Pointer = 0;
     OpCode.Clear();
     OpCode.AddRange(Code.Split(',').Select(x => int.Parse(x)));
     return(true);
 }
 public bool RestoreMemory()
 {
     OpCode.Clear();
     OpCode.AddRange(Code.Split(',').Select(x => int.Parse(x)).ToList());
     Pointer = 0;
     Noun    = 0;
     Verb    = 0;
     return(true);
 }
Exemple #3
0
    public void ParsingText(string s)
    {
        Token tk;
        int   instwidth = 0;

        if (s != null)
        {
            //is pre inst?
            opcode.Clear();
            Inst inst = OpCode.IsPreOpCode(s);
            if (inst != Inst.none)
            {
                opcode.preinst = inst;
                tk             = lex.NextToken();
                if (tk != Token.Ident)
                {
                    Error("inst expect but " + s + " found");
                    lex.SkipLine();
                    return;
                }
                s = lex.GetTokenString();
                lex.Match(Token.Ident);
            }
            ;
            // is inst?
            inst = opcode.IsOpCode(s);
            if (inst == Inst.none)
            {
                Error("unknow inst: " + s);
                lex.SkipLine();
                return;
            }
            else
            {
                opcode.inst = inst;
                instwidth   = opcode.GetInstWidth(opcode.inst);
            }
        }
        int count = opcode.ParamCount;
        int index = 0;

        while (count > 0)
        {
            tk = lex.NextToken();
            // a indexed addressing mode like -1(,,)
            if (tk == Token.plus || tk == Token.minus || tk == Token.Number)
            {
                if (!ParsingImmValue(tk))
                {
                    return;
                }
                tk = lex.NextToken();
                if (tk == Token.leftbracket)
                {
                    ParsingIndexedAddressingMode(index);
                }
                else
                {
                    Error("'(' expect but" + lex.GetTokenString() + " found");
                    lex.SkipLine();
                    return;
                }
            }
            // (,,)
            else if (tk == Token.leftbracket)
            {
                ParsingIndexedAddressingMode(index);
            }
            //immVariable or indexed addressing value(,,)
            else if (tk == Token.Ident)
            {
                if (!ParsingImmVariable(tk, index))
                {
                    return;
                }
                tk = lex.NextToken();
                if (tk == Token.leftbracket)
                {
                    ParsingIndexedAddressingMode(index);
                }
            }
            //register addressing like %eax , %cs or indexed addressing %cs(,,) %cs:-10(,,) %cs:value(,,)
            else if (tk == Token.percent)
            {
                Addressing addressing = ParsingRegister();
                if (addressing == Addressing.none)
                {
                    return;
                }

                if (opcode.IsSegRegister(addressing))
                {
                    opcode.SegReg = addressing;
                    tk            = lex.NextToken();
                    if (tk == Token.leftbracket)
                    {
                        ParsingIndexedAddressingMode(index);
                    }
                    else if (tk == Token.Ident)
                    {
                        if (!ParsingImmVariable(tk, index))
                        {
                            return;
                        }
                        tk = lex.NextToken();
                        if (tk == Token.leftbracket)
                        {
                            ParsingIndexedAddressingMode(index);
                        }
                    }
                    else if (tk == Token.plus || tk == Token.minus || tk == Token.Number)
                    {
                        if (!ParsingImmValue(tk))
                        {
                            return;
                        }
                        tk         = lex.NextToken();
                        opcode.Imm = opcode.TempImm;
                        if (tk == Token.leftbracket)
                        {
                            ParsingIndexedAddressingMode(index);
                        }
                    }
                    else
                    {
                        opcode.Register[index] = addressing;
                    }
                }
                else
                {
                    if (CheckRegisterWidth(addressing, instwidth))
                    {
                        opcode.Register[index] = addressing;
                    }
                    else
                    {
                        return;
                    }
                }
            }
            //imm addr $10 or $addr
            else if (tk == Token.dollar)
            {
                lex.Match(Token.dollar);
                tk = lex.NextToken();
                if (tk == Token.Ident)
                {
                    s = lex.GetTokenString();
                    int addr = GetLabelAddr(s);
                    if (addr == -1)
                    {
                        Error("unknown identifier: " + s);
                        lex.SkipLine();
                        return;
                    }
                    lex.Match(Token.Ident);
                    opcode.Imm             = addr;
                    opcode.Register[index] = Addressing.imm;
                }
                else if (tk == Token.plus || tk == Token.minus || tk == Token.Number)
                {
                    if (!ParsingImmValue(tk))
                    {
                        return;
                    }
                    opcode.Imm             = opcode.TempImm;
                    opcode.Register[index] = Addressing.imm;
                }
            }

            count--;
            if (count > 0)
            {
                if (!lex.Match(Token.comma))
                {
                    Error("',' expect but " + lex.GetTokenString() + " found");
                    lex.SkipLine();
                }
            }

            index++;
        }
        EmitCode();
    }