# =========================================
# Forth -> Assembly
# Written by Charles Childers
#
# For changelog, see the end of file. Notes
# are provided by each function
# =========================================


# =========================================
# Excise ^M from source code before conversion
# =========================================
s/\n//g


# =========================================
# Take care of special cases
#   * Word names
#   * Variables
#   * Constants
# Numbers are a special case, but we 
# handle them later on.
# =========================================
# Word Names
s/: //g


# =========================================
# Here we begin to handle the definitions
# Since we are doing subroutine threading,
# it'll look like:
# word_name
#    call x
#    call y
#    call z
# ret
# =========================================
s/ /\n  call /g


# =========================================
# Here we take care of the number handling
# Hexadecimal values are prefixed by
# 0x or $
# =========================================
# Decimal
s/call 0/upsh 0/g
s/call 1/upsh 1/g
s/call 2/upsh 2/g
s/call 3/upsh 3/g
s/call 4/upsh 4/g
s/call 5/upsh 5/g
s/call 6/upsh 6/g
s/call 7/upsh 7/g
s/call 8/upsh 8/g
s/call 9/upsh 9/g
# Hexadecimal
s/call 0x/upsh 0x/g
s/call $/upsh 0x/g


# =========================================
# Now that the number conversions are done,
# we change the 'call ;' lines into RET
# =========================================
s/  call ;/ret/g


# =========================================
# Since assemblers don't like us having
# some symbols (e.g. + - * / .) in word
# names, we change them into text
# =========================================
s/-/minus/g
s/+/plus/g
s/*/multiply/g
s/\//divide/g
s/\./print/g

# =========================================
# That's it! Your Forth code is now valid
# assembly using subroutine threading.
# =========================================



# =============================================================================
# This is part of the Retro Project, for details on the project and for
# updates, visit http://retro.tunes.org
# =============================================================================
# This is placed into the public domain
# There is no warranty
# =============================================================================

# =========================================
# This script is generally called from
# the 'forthc' shell script.
# =========================================


# =========================================
# There are a few known flaws in this
# tool, and a lot remains unfinished.
#
# What remains to be done?
#   * Support for constants and variables
#   * More handling of special symbols
#   * Actually get it to compile a runnable
#     application!
#   * Cleanup, testing, documentation
#   * Add support for EXTERN and GLOBAL
#     symbols
#   * Support for interfacing with other
#     languages
#   * Support for strings
#   * Support for IF/THEN and loops
#   * Return stack
#   * Call tables
#   * Anything else you want!
# =========================================

# =============================================================================
# Changelog |   If you modify this file, give details here
# -----------
#
#   DATE      WHO                       DETAILS
# --------    ---    --------------------------------------------
# 06/25/03    CRC    Began coding
# 06/26/03    CRC    Fixed several bugs
# 06/30/03    CRC    Split conversion into multiple files, many simplifications
#                    and bug fixes
#                    Started cleaning up the Retro6 source for use as the
#                    primitives
#                    Added changelog, in-file documentation, started work to 
#                    add new features.
# 07/05/03    CRC    Cleanup of outputted code
#                    Comments are stripped by 'forthc' script
#                    Script now strips ^M before running
#                    Removed number prefixing from decimal numbers
# 07/12/03    CRC    Fixed bug in number handling.
#=============================================================================
