[project @ 2000-03-14 14:34:47 by sewardj]
[ghc-hetmet.git] / ghc / interpreter / hugs.c
index 4292db8..155a391 100644 (file)
@@ -9,8 +9,8 @@
  * included in the distribution.
  *
  * $RCSfile: hugs.c,v $
- * $Revision: 1.34 $
- * $Date: 2000/01/10 16:27:03 $
+ * $Revision: 1.43 $
+ * $Date: 2000/03/14 14:34:47 $
  * ------------------------------------------------------------------------*/
 
 #include <setjmp.h>
 
 #include "prelude.h"
 #include "storage.h"
-#include "command.h"
-#include "backend.h"
 #include "connect.h"
 #include "errors.h"
 #include "version.h"
-#include "link.h"
 
 #include "Rts.h"
 #include "RtsAPI.h"
@@ -44,60 +41,59 @@ Bool multiInstRes = FALSE;
  * Local function prototypes:
  * ------------------------------------------------------------------------*/
 
-static Void   local initialize        Args((Int,String []));
-static Void   local promptForInput    Args((String));
-static Void   local interpreter       Args((Int,String []));
-static Void   local menu              Args((Void));
-static Void   local guidance          Args((Void));
-static Void   local forHelp           Args((Void));
-static Void   local set               Args((Void));
-static Void   local changeDir         Args((Void));
-static Void   local load              Args((Void));
-static Void   local project           Args((Void));
-static Void   local readScripts       Args((Int));
-static Void   local whatScripts       Args((Void));
-static Void   local editor            Args((Void));
-static Void   local find              Args((Void));
-static Bool   local startEdit         Args((Int,String));
-static Void   local runEditor         Args((Void));
-static Void   local setModule         Args((Void));
-static Module local findEvalModule    Args((Void));
-static Void   local evaluator         Args((Void));
-static Void   local stopAnyPrinting   Args((Void));
-static Void   local showtype          Args((Void));
-static String local objToStr          Args((Module, Cell));
-static Void   local info              Args((Void));
-static Void   local printSyntax       Args((Name));
-static Void   local showInst          Args((Inst));
-static Void   local describe          Args((Text));
-static Void   local listNames         Args((Void));
-
-static Void   local toggleSet         Args((Char,Bool));
-static Void   local togglesIn         Args((Bool));
-static Void   local optionInfo        Args((Void));
+static Void   local initialize        ( Int,String [] );
+static Void   local promptForInput    ( String );
+static Void   local interpreter       ( Int,String [] );
+static Void   local menu              ( Void );
+static Void   local guidance          ( Void );
+static Void   local forHelp           ( Void );
+static Void   local set               ( Void );
+static Void   local changeDir         ( Void );
+static Void   local load              ( Void );
+static Void   local project           ( Void );
+static Void   local readScripts       ( Int );
+static Void   local whatScripts       ( Void );
+static Void   local editor            ( Void );
+static Void   local find              ( Void );
+static Bool   local startEdit         ( Int,String );
+static Void   local runEditor         ( Void );
+static Void   local setModule         ( Void );
+static Module local findEvalModule    ( Void );
+static Void   local evaluator         ( Void );
+static Void   local stopAnyPrinting   ( Void );
+static Void   local showtype          ( Void );
+static String local objToStr          ( Module, Cell );
+static Void   local info              ( Void );
+static Void   local printSyntax       ( Name );
+static Void   local showInst          ( Inst );
+static Void   local describe          ( Text );
+static Void   local listNames         ( Void );
+
+static Void   local toggleSet         ( Char,Bool );
+static Void   local togglesIn         ( Bool );
+static Void   local optionInfo        ( Void );
 #if USE_REGISTRY || HUGS_FOR_WINDOWS
-static String local optionsToStr      Args((Void));
+static String local optionsToStr      ( Void );
 #endif
-static Void   local readOptions       Args((String));
-static Bool   local processOption     Args((String));
-static Void   local setHeapSize       Args((String));
-static Int    local argToInt          Args((String));
-
-static Void   local loadProject       Args((String));
-static Void   local clearProject      Args((Void));
-static Bool   local addScript         Args((Int));
-static Void   local forgetScriptsFrom Args((Script));
-static Void   local setLastEdit       Args((String,Int));
-static Void   local failed            Args((Void));
-static String local strCopy           Args((String));
-static Void   local browseit         Args((Module,String,Bool));
-static Void   local browse           Args((Void));
+static Void   local readOptions       ( String );
+static Bool   local processOption     ( String );
+static Void   local setHeapSize       ( String );
+static Int    local argToInt          ( String );
+
+static Void   local loadProject       ( String );
+static Void   local clearProject      ( Void );
+static Bool   local addScript         ( Int );
+static Void   local forgetScriptsFrom ( Script );
+static Void   local setLastEdit       ( String,Int );
+static Void   local failed            ( Void );
+static String local strCopy           ( String );
+static Void   local browseit         ( Module,String,Bool );
+static Void   local browse           ( Void );
 
 /* --------------------------------------------------------------------------
  * Machine dependent code for Hugs interpreter:
  * ------------------------------------------------------------------------*/
 
-       Bool   combined      = TRUE;
 
 #include "machdep.c"
 #ifdef WANT_TIMER
@@ -115,8 +111,10 @@ static Bool   addType       = FALSE;    /* TRUE => print type with value   */
 static Bool   useDots       = RISCOS;   /* TRUE => use dots in progress    */
 static Bool   quiet         = FALSE;    /* TRUE => don't show progress     */
 static Bool   lastWasObject = FALSE;
+
        Bool   preludeLoaded = FALSE;
        Bool   debugSC       = FALSE;
+       Bool   combined      = TRUE;
 
 typedef 
    struct { 
@@ -133,8 +131,8 @@ typedef
    }
    ScriptInfo;
 
-static Void   local makeStackEntry    Args((ScriptInfo*,String));
-static Void   local addStackEntry     Args((String));
+static Void   local makeStackEntry    ( ScriptInfo*,String );
+static Void   local addStackEntry     ( String );
 
 static ScriptInfo scriptInfo[NUM_SCRIPTS];
 
@@ -205,7 +203,7 @@ return;
 
 #ifndef NO_MAIN /* we omit main when building the "Hugs server" */
  
-Main main Args((Int, String []));       /* now every func has a prototype  */
+Main main ( Int, String [] );       /* now every func has a prototype  */
 
 Main main(argc,argv)
 int  argc;
@@ -320,6 +318,7 @@ String argv[]; {
    }
 
    addStackEntry("Prelude");
+   if (combined) addStackEntry("PrelHugs");
 
    for (i=1; i < argc; ++i) {            /* process command line arguments  */
         if (strcmp(argv[i], "--")==0) break;
@@ -485,7 +484,7 @@ ToDo
 #define PUTStr(c,s)                     \
     next=PUTStr_aux(next,c,s)
 
-static String local PUTStr_aux Args((String,Char, String));
+static String local PUTStr_aux ( String,Char, String));
 
 static String local PUTStr_aux(next,c,s)
 String next;
@@ -788,21 +787,13 @@ struct options toggle[] = {             /* List of command line toggles    */
     {'w', 1, "Always show which modules are loaded",  &listScripts},
     {'k', 1, "Show kind errors in full",              &kindExpert},
     {'o', 0, "Allow overlapping instances",           &allowOverlap},
-
-
-#if DEBUG_CODE
-    {'D', 1, "Debug: show generated code",            &debugCode},
-#endif
+    {'S', 1, "Debug: show generated SC code",         &debugSC},
 #if EXPLAIN_INSTANCE_RESOLUTION
     {'x', 1, "Explain instance resolution",           &showInstRes},
 #endif
 #if MULTI_INST
     {'m', 0, "Use multi instance resolution",         &multiInstRes},
 #endif
-#if DEBUG_CODE
-    {'D', 1, "Debug: show generated G code",          &debugCode},
-#endif
-    {'S', 1, "Debug: show generated SC code",         &debugSC},
     {0,   0, 0,                                       0}
 };
 
@@ -846,7 +837,7 @@ String s; {
     currProject = s;
     projInput(currProject);
     scriptFile = currProject;
-    forgetScriptsFrom(1);
+    forgetScriptsFrom(N_PRELUDE_SCRIPTS);
     while ((s=readFilename())!=0)
         addStackEntry(s);
     if (namesUpto<=1) {
@@ -1123,7 +1114,7 @@ static Void local load() {           /* read filenames from command line   */
                                      /* to be read                         */
     while ((s=readFilename())!=0)
         addStackEntry(s);
-    readScripts(1);
+    readScripts(N_PRELUDE_SCRIPTS);
 }
 
 static Void local project() {          /* read list of script names from   */
@@ -1144,7 +1135,7 @@ static Void local project() {          /* read list of script names from   */
         EEND;
     }
     loadProject(s);
-    readScripts(1);
+    readScripts(N_PRELUDE_SCRIPTS);
 }
 
 static Void local readScripts(n)        /* Reread current list of scripts, */
@@ -1259,6 +1250,11 @@ Int n; {                                /* loading everything after and    */
 
     { Int  m     = namesUpto-1;
       Text mtext = findText(scriptInfo[m].modName);
+
+      /* Hack to avoid starting up in PrelHugs */
+      if (mtext == findText("PrelHugs")) mtext = findText("Prelude");
+
+
       /* Commented out till we understand what
        * this is trying to do.
        * Problem, you cant find a module till later.
@@ -1324,11 +1320,11 @@ ToDo: Fix!
         startNewScript(0);
         if (nonNull(c=findTycon(t=findText(nm)))) {
             if (startEdit(tycon(c).line,scriptName[scriptThisTycon(c)])) {
-                readScripts(1);
+                readScripts(N_PRELUDE_SCRIPTS);
             }
         } else if (nonNull(c=findName(t))) {
             if (startEdit(name(c).line,scriptName[scriptThisName(c)])) {
-                readScripts(1);
+                readScripts(N_PRELUDE_SCRIPTS);
             }
         } else {
             ERRMSG(0) "No current definition for name \"%s\"", nm
@@ -1340,7 +1336,7 @@ ToDo: Fix!
 
 static Void local runEditor() {         /* run editor on script lastEdit   */
     if (startEdit(lastEdLine,lastEdit)) /* at line lastEdLine              */
-        readScripts(1);
+        readScripts(N_PRELUDE_SCRIPTS);
 }
 
 static Void local setLastEdit(fname,line)/* keep name of last file to edit */
@@ -1383,8 +1379,9 @@ static Void local evaluator() {        /* evaluate expr and print value    */
                                        /* allocated during evaluation      */
     parseExp();
     checkExp();
-    defaultDefns = evalDefaults;
+    defaultDefns = combined ? stdDefaults : evalDefaults;
     type         = typeCheckExp(TRUE);
+
     if (isPolyType(type)) {
         ks = polySigOf(type);
         bd = monotypeOf(type);
@@ -1418,9 +1415,9 @@ static Void local evaluator() {        /* evaluate expr and print value    */
             ERRTEXT   "\n"
             EEND;
         }
-        inputExpr = ap2(findName(findText("show")),d,inputExpr);
-        inputExpr = ap(findName(findText("putStr")), inputExpr);
-        inputExpr = ap(nameRunIO_toplevel, inputExpr);
+        inputExpr = ap2(nameShow,           d,inputExpr);
+        inputExpr = ap (namePutStr,         inputExpr);
+        inputExpr = ap (nameRunIO_toplevel, inputExpr);
 
         evalExp(); printf("\n");
         if (addType) {
@@ -1617,6 +1614,48 @@ Cell   c; {
 
 extern Name nameHw;
 
+static Void dumpStg ( void )
+{
+   String s;
+   Int i;
+   setCurrModule(findEvalModule());
+   startNewScript(0);
+   s = readFilename();
+
+   /* request to locate a symbol by name */
+   if (s && (*s == '?')) {
+      Text t = findText(s+1);
+      locateSymbolByName(t);
+      return;
+   }
+
+   /* request to dump a bit of the heap */
+   if (s && (*s == '-' || isdigit(*s))) {
+      int i = atoi(s);
+      print(i,100);
+      printf("\n");
+      return;
+   }
+
+   /* request to dump a symbol table entry */
+   if (!s 
+       || !(*s == 't' || *s == 'n' || *s == 'c' || *s == 'i')
+       || !isdigit(s[1])) {
+      fprintf(stderr, ":d -- bad request `%s'\n", s );
+      return;
+   }
+   i = atoi(s+1);
+   switch (*s) {
+      case 't': dumpTycon(i); break;
+      case 'n': dumpName(i); break;
+      case 'c': dumpClass(i); break;
+      case 'i': dumpInst(i); break;
+      default: fprintf(stderr, ":d -- `%c' not implemented\n", *s );
+   }
+}
+
+
+#if 0
 static Void local dumpStg( void ) {       /* print STG stuff                 */
     String s;
     Text   t;
@@ -1664,6 +1703,7 @@ static Void local dumpStg( void ) {       /* print STG stuff                 */
         }
     }
 }
+#endif
 
 static Void local info() {              /* describe objects                */
     Int    count = 0;                   /* or give menu of commands        */
@@ -1824,7 +1864,6 @@ Text t; {
         } else {
             Printf("<unknown type>");
         }
-printf("\n");print(name(nm).type,10);printf("\n");
         if (isCfun(nm)) {
             Printf("  -- data constructor");
         } else if (isMfun(nm)) {
@@ -1985,14 +2024,14 @@ String argv[]; {
             case FIND   : find();
                           break;
             case LOAD   : clearProject();
-                          forgetScriptsFrom(1);
+                          forgetScriptsFrom(N_PRELUDE_SCRIPTS);
                           load();
                           break;
             case ALSO   : clearProject();
                           forgetScriptsFrom(numScripts);
                           load();
                           break;
-            case RELOAD : readScripts(1);
+            case RELOAD : readScripts(N_PRELUDE_SCRIPTS);
                           break;
             case PROJECT: project();
                           break;
@@ -2269,9 +2308,9 @@ typedef struct _HugsStream {
     Int  next;                        /* next space in buffer              */
 } HugsStream;
 
-static Void   local vBufferedPrintf  Args((HugsStream*, const char*, va_list));
-static Void   local bufferedPutchar  Args((HugsStream*, Char));
-static String local bufferClear      Args((HugsStream *stream));
+static Void   local vBufferedPrintf  ( HugsStream*, const char*, va_list );
+static Void   local bufferedPutchar  ( HugsStream*, Char );
+static String local bufferClear      ( HugsStream *stream );
 
 static Void local vBufferedPrintf(stream, fmt, ap)
 HugsStream* stream;