# Dealing with the return stack.
#

import core


class OntoReturn(core.Word):
    """(n -- ) Move an item from the top of the parameter stack to the return stack."""
    def execute(self, interp):
        n = interp.stack.pop()
        interp.return_stack.push(n)
core.registerWord('>R', OntoReturn())


class FromReturn(core.Word):
    """( -- n) Move an item from the top of the return stack to the parameter stack."""
    def execute(self, interp):
        n = interp.return_stack.pop()
        interp.stack.push(n)
core.registerWord('R>', FromReturn())


class CopyFirst(core.Word):
    """( -- n) Copy the first item from the return stack onto the parameter stack."""
    def execute(self, interp):
        items = interp.return_stack.copyOfItems()
        n = interp.stack.push(items[-1])
core.registerWord('I', CopyFirst())


class CopySecond(core.Word):
# Simple (integer) arithmetic.
#

import core


class Plus(core.Word):
    "(n1 n2 -- n) Add the top two numbers on the stack and push the result"
    def execute(self, interp):
        n1 = interp.stack.pop()
        n2 = interp.stack.pop()
        interp.stack.push(n2 + n1)
core.registerWord('+', Plus())


class Minus(core.Word):
    "(n1 n2 -- n) Subtract n2 from n1 and push the result on the stack"
    def execute(self, interp):
        n1 = interp.stack.pop()
        n2 = interp.stack.pop()
        interp.stack.push(n2 - n1)
core.registerWord('-', Minus())


class Multiply(core.Word):
    "(n1 n2 -- n) Multiply the top two numbers and push the result"
    def execute(self, interp):
        n1 = interp.stack.pop()
        n2 = interp.stack.pop()
        interp.stack.push(n2 * n1)
core.registerWord('*', Multiply())
示例#3
0
        if token == 'LOOP' or token == '+LOOP':
            self.count -= 1
            if self.count == 0:
                claimNextToken = False
        elif token == 'DO':
            self.count += 1

        loopBodies.top().append(token)

        if claimNextToken == True:
            interp.giveNextTokenTo(self)
        else:
            # In Forth, the loop always runs once!
            core.Batch().start(loopBodies.top(), interp)
core.registerWord('DO', Do())


def isLoopFinished(limit, index, interp):
    if index >= limit:
        # We're done!
        loopBodies.pop()
        result = None
    else:
        # Add the items and batch.
        # Oh no, we're not done. Rebatch!
        result = loopBodies.top()
    return result
 
#
#
示例#4
0
# How to handle strings. Yeesh.
#

import core

# Literals
# I.e. be able to do stuff like: ." Hello, World "
#
class PrintLiteral(core.Word):
    def execute(self, interp):
        interp.giveNextTokenTo(self)

    def handleToken(self, token, interp):
        interp.output.write(token)
core.registerWord('."', PrintLiteral())


# Store the literal.
#
class StoreLiteral(core.Word):
    def execute(self, interp):
        interp.giveNextTokenTo(self)

    def handleToken(self, token, interp):
        store(token, interp)
core.registerWord('S"', StoreLiteral())


# Type out...
#
class Type(core.Word):
示例#5
0
# Simple stuff.
#
import core
from core import Word, Batch
from util import *


class CR(Word):
    """( -- ) Simply print out a carriage return."""
    def execute(self, interp):
        interp.output.write('\n')
core.registerWord('CR', CR())


class Spaces(Word):
    """(n -- ) Print out n spaces."""
    def execute(self, interp):
        n = interp.stack.pop()
        for i in range(n):
            interp.output.write(' ')
core.registerWord('SPACES', Spaces())


class Space(Word):
    """( -- ) Print out just one space."""
    def execute(self, interp):
        interp.output.write(' ')
core.registerWord('SPACE', Space())


class Emit(Word):
示例#6
0
import core


class TwoSwap(core.Word):
    """(d1 d2 -- d2 d1) Reverses the top pair of numbers."""

    def execute(self, interp):
        d2b = interp.stack.pop()
        d2a = interp.stack.pop()
        d1b = interp.stack.pop()
        d1a = interp.stack.pop()
        for d in [d2a, d2b, d1a, d1b]:
            interp.stack.push(d)


core.registerWord("2SWAP", TwoSwap())


class TwoDup(core.Word):
    """(d -- d d) Duplicate the top pair of numbers."""

    def execute(self, interp):
        db = interp.stack.pop()
        da = interp.stack.pop()
        for d in [da, db, da, db]:
            interp.stack.push(d)


core.registerWord("2DUP", TwoDup())