/******************************************************************

    $Header$

    Module: frontend.c

    Author: Jeff Lait

    Copyright 1997 Ytinasin.

    Description: Front end for scheme interpreter

 ******************************************************************/

/******************************************************************

  Revision Record

    Rev Date        Auth    Changes
    === ====        ====    =======

    0.0 12/4/97     jml     start
    0.1 13/08/97    jml     Hacked for uC++

 ******************************************************************/

/******************************************************************
 * INCLUDES:
 ******************************************************************/
#include "defines.h"
#include <stdio.h>
#include <string.h>
#include "atom.h"
#include "evaluate.h"

/******************************************************************
 * DEFINES:
 ******************************************************************/

/******************************************************************
 * STRUCTURES:
 ******************************************************************/

/******************************************************************
 * GLOBAL VARIABLES:
 ******************************************************************/

/******************************************************************
 * LOCAL FUNCTION PROTOTYPES
 ******************************************************************/
void GetLine(char *in);

/******************************************************************
 * LOCAL VARIABLES:
 ******************************************************************/


/******************************************************************
 * Main
 ******************************************************************/
void uMain::main()
{
    char *in;
    int pos;
    ATOM *atom, *head, *result;
    EVALUATE *evaluator;

    uCout << "uScheme version " << VERSION << endl << "Copyright 1996-7, Jeff Lait, All rights reserved" << endl;

    evaluator = CreateEvaluator();

    in = GetMem(20000);
    resetiosflags(uCin, ios::skipws);
#if 1
    strcpy(in, "(load \"scheme.scm\")");
    for (pos = 1; pos < argc; pos++) {
        strcat(in, " (load \"");
        strcat(in, argv[pos]);
        strcat(in, "\")");
        }
#else
    uCout << "-> ";
    GetLine(in);
#endif
    while (in[0] != '~') {
        pos = 0;
        atom = ParseLine(in, &pos);
        uCout << "I read:\n";
        PrintAtom(atom, uCout);
        uCout << "\n";
        head = atom;
        while (atom) {
            result = evaluator->evaluateAtomGlobal(atom);
            uCout << "Result:\n";
            PrintAtom(result, uCout);
            uCout << "\n";
            glbAtomMgr.Destroy(result);
            atom = atom->next;
            }
        glbAtomMgr.Destroy(head);
// Does not handle functions retaining eval'd lambdas
//      GarbageCollection();
        uCout << "-> ";
        GetLine(in);
        }

    FreeMem(in);
uCout << "Deleting evaluator" << endl;
    delete evaluator;
    uCout << "Done!\n";
    }


/******************************************************************
 * GetLine
 ******************************************************************/
void GetLine(char *s)
{
    //uCin >> s;
#if 1
    char c = ' ';
    int pos = 0;
    int done = 0;
   
    fflush(stdout);
    while (!done) {
        //c = getc(stdin);
        uCin >> c;
        switch (c) {
            case '\b':
                if (pos) {
                    pos--;
                    //printf("\b \b");
                    //fflush(stdout);
                    }
                break;
            case '\n':
                s[pos++] = c;
                //fflush(stdout);
                done = 1;
                break;
            case '\t':
                //printf("   ");
                s[pos++] = ' ';
                s[pos++] = ' ';
                s[pos++] = ' ';
                //fflush(stdout);
                break;
            default:
                s[pos++] = c;
                //printf("%c", c);
                //fflush(stdout);
                break;
            }
        }
    //printf("\n");
    s[pos] = 0;
#endif
    }
