コード例 #1
0
ファイル: kasm.py プロジェクト: clker/kasm
def fn_ds(tokenizer, phaseNumber):
    global gLoc

    value = eval.Expression(tokenizer).eval()
    if value == None:
        raise Exception("Undefined expression")
    gLoc += value
コード例 #2
0
ファイル: kasm.py プロジェクト: clker/kasm
def fn_dw(tokenizer, phaseNumber):

    while not tokenizer.atEnd():

        if tokenizer.curTok() == tok.STRING:

            s = tokenizer.curValue()
            for c in s:
                depositWord(ord(c))

            tokenizer.advance()

        else:

            expr = eval.Expression(tokenizer)

            value = expr.eval()
            if phaseNumber > 0:
                depositWord(value)

            else:
                depositWord(0)

        if tokenizer.curTok() != ',':
            break
        else:
            tokenizer.advance()
コード例 #3
0
ファイル: kasm.py プロジェクト: clker/kasm
def fn_db(tokenizer, phaseNumber):
    while not tokenizer.atEnd():

        if tokenizer.curTok() == tok.STRING:

            s = tokenizer.curValue()
            for c in s:
                depositByte(ord(c))

            tokenizer.advance()

        else:

            expr = eval.Expression(tokenizer)

            value = expr.eval()
            if phaseNumber > 0:
                if value > 0xff or value < -128:
                    raise Exception("value too large for a byte")
                depositByte(value)

            else:
                depositByte(0)

        if tokenizer.curTok() != ',':
            break
        else:
            tokenizer.advance()
コード例 #4
0
ファイル: kasm.py プロジェクト: clker/kasm
def assembleLine(line, phaseNumber=0):
    global gLoc
    for k, v in gDefines.items():
        line = line.replace(k, v)

    clearLineBytes()
    tokenizer = tok.Tokenizer(line)

    #
    #   Set '*' psuedo-symbol at the start of each line
    #
    symbols.set('*', gLoc)

    #
    #   SYMBOL = VALUE
    #
    if tokenizer.curTok() == tok.SYMBOL and tokenizer.peek(1) == '=':
        sym = tokenizer.curValue()
        tokenizer.advance(2)
        expr = eval.Expression(tokenizer)
        if not tokenizer.atEnd():
            raise Exception("Bad expression (extra gunk)")

        value = expr.eval()

        if phaseNumber > 0 and value == None:
            raise Exception(str.format("Undefined expression"))

        symbols.set(sym, expr.eval())

        if gListingFile != None and phaseNumber > 0:
            generateListingLine(line)

        return

    #
    #   handle SYMBOL: at start of line
    #   NOTE: could enforce leadingWhitespace, but we have a ':'
    #   instead of that.
    #
    if tokenizer.curTok() == tok.SYMBOL and tokenizer.peek(1) == ':':
        sym = tokenizer.curValue()
        tokenizer.advance(2)

        if phaseNumber == 0:
            symbols.set(sym, gLoc)

        else:
            #
            #   check that the symbol has the same value in
            #   subsequent phases
            #
            symbols.setScope(sym)
            if symbols.get(sym) != gLoc:
                raise Exception(
                    str.format("Symbol phase error (expected {0}, have {1})",
                               symbols.get(sym), gLoc))

    #
    #   handle ops
    #
    if tokenizer.curTok() == tok.SYMBOL:

        op = tokenizer.curValue().lower()
        tokenizer.advance()

        if op in gPsuedoOps:
            gPsuedoOps[op](tokenizer, phaseNumber)
        elif op in preOps:
            if preOps[op](line):
                raise Exception("define syntax error")
        elif op in gOps:
            assembleInstruction(op, tokenizer, phaseNumber)
        else:
            raise Exception(str.format('Unknown op: {0}', op))

    if gListingFile != None and phaseNumber > 0:
        generateListingLine(line)
コード例 #5
0
ファイル: kasm.py プロジェクト: clker/kasm
def parseAddressingMode(tokenizer):

    if tokenizer.atEnd():
        return IMPLIED, None

    if tokenizer.curTok() == '#':
        tokenizer.advance()
        expr = eval.Expression(tokenizer)
        return IMMED, expr

    #
    #   (n)
    #   (nn)
    #   (n,x)
    #   (n),y
    #
    if tokenizer.curTok() == '(':
        tokenizer.advance()
        expr = eval.Expression(tokenizer)

        #   (expr,x)
        if tokenizer.curTok() == ',':
            if tokenizer.peek(1) == tok.SYMBOL and tokenizer.peekValue(
                    1).lower() == 'x' and tokenizer.peek(2) == ')':
                tokenizer.advance(3)
                return INDX, expr
            else:
                raise Exception(
                    "bad addressing mode (started out looking like indirect-x)"
                )

        elif tokenizer.curTok() == ')':

            tokenizer.advance()

            #
            #   (expr),y
            #   (expr)
            #
            if tokenizer.curTok() == ',' and tokenizer.peek(
                    1) == tok.SYMBOL and tokenizer.peekValue(1).lower() == 'y':
                tokenizer.advance(2)
                return INDY, expr
            else:
                return IND, expr

        else:
            raise Exception(
                "bad addressing mode (started out looking indirect, but fizzled)"
            )

    #
    #   nn
    #   n
    #   rel
    #
    #   n,x
    #   n,y
    #

    expr = eval.Expression(tokenizer)

    if tokenizer.curTok() == ',':
        tokenizer.advance()

        if tokenizer.curTok() == tok.SYMBOL:
            if tokenizer.curValue().lower() == 'x':
                return UNDECIDED_X, expr
            elif tokenizer.curValue().lower() == 'y':
                return UNDECIDED_Y, expr
            else:
                raise Exception(
                    str.format("Unxpected symbol {0} following expression",
                               tokenizer.curValue()))
        else:
            raise Exception("Unxpected gunk following expression")

    return UNDECIDED, expr
コード例 #6
0
ファイル: kasm.py プロジェクト: clker/kasm
def fn_org(tokenizer, phaseNumber):
    global gLoc
    org = eval.Expression(tokenizer).eval()
    if org == None:
        raise Exception("Undefined expression")
    gLoc = org