hi,

most languages that can run in interactive mode have some kind of welcome message and prompt that is printed before the user can give any input.

For example, Python prints:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>>

and another well-known language being implemented for Parrot, Lua:

Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
>

Attached is a patch that extends HLLCompiler with 2 attributes: commandline_message, and commandline_prompt

The first can be set to set the message to be shown when the interactive mode is entered. The second one is the prompt to be printed before the user can give input. (a possible improvement would be to have an array of prompts; many languages have 2 prompts, when a "\" or whatever is given. In Python, if one gives "\" at the end of the line, the next prompt is "...". In lua it is ">>" instead of the normal prompt ">".)

As an example, I used this in Pynie.pir, which prints nicely:

C:\parrot\languages\pynie>..\..\parrot pynie.pbc
Python 2.5 for Parrot
Type "help", "copyright", "credits" or "license" for more information.
>>>

(although the "help" etc. commands are not implemented).

I don't know if this feature is desired, but it was not too much work, and it makes languages look more like the real thing.

regards,
klaas-jan
Index: languages/pynie/pynie.pir
===================================================================
--- languages/pynie/pynie.pir	(revision 17174)
+++ languages/pynie/pynie.pir	(working copy)
@@ -35,7 +35,13 @@
     $P0.'language'('Pynie')
     $P0.'parsegrammar'('Pynie::Grammar')
     $P0.'astgrammar'('Pynie::PAST::Grammar')
-
+		
+		.local string welcomestring		
+		welcomestring = "Python 2.5 for Parrot\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n"
+		
+		$P0.'commandline_message'(welcomestring)
+		$P0.'commandline_prompt'('>>> ')
+		
     $P0 = get_hll_global ['PGE::Util'], 'die'
     set_hll_global ['Pynie::Grammar'], 'die', $P0
 .end
Index: runtime/parrot/library/Parrot/HLLCompiler.pir
===================================================================
--- runtime/parrot/library/Parrot/HLLCompiler.pir	(revision 17174)
+++ runtime/parrot/library/Parrot/HLLCompiler.pir	(working copy)
@@ -20,6 +20,11 @@
     addattribute $P0, '$ostgrammar'
     addattribute $P0, '@stages'
     addattribute $P0, '$!compsub'
+    
+    # the commandline_message is the welcome message for interactive mode
+    addattribute $P0, '$commandline_message'
+    # the commandline_prompt is the prompt to show for input    
+    addattribute $P0, '$commandline_prompt'
 .end
 
 =head2 Methods
@@ -105,6 +110,18 @@
     .return self.'attr'('$ostgrammar', value, has_value)
 .end
 
+.sub 'commandline_message' :method
+    .param string value        :optional
+    .param int has_value       :opt_flag
+    .return self.'attr'('$commandline_message', value, has_value)
+.end
+
+.sub 'commandline_prompt' :method
+    .param string value        :optional
+    .param int has_value       :opt_flag
+    .return self.'attr'('$commandline_prompt', value, has_value)
+.end
+
 =item removestage(string stagename)
 
 Delete a stage from the compilation process queue.
@@ -346,7 +363,11 @@
     .local string target, encoding
     target = adverbs['target']
     target = downcase target
-
+    
+    # on startup show the welcome message
+		$P0 = self.'commandline_message'()
+    printerr $P0
+    
     .local pmc stdin
     .local int has_readline
     stdin = getstdin
@@ -359,6 +380,11 @@
     unless stdin goto interactive_end
     ##  FIXME:  we have to avoid stdin.'readline'() when readline
     ##  libraries aren't present (RT #41103)
+    
+    # for each input line, print the prompt
+    $P0 = self.'commandline_prompt'()
+    printerr $P0
+    
     if has_readline < 0 goto no_readline
     code = stdin.'readline'('> ')
     if null code goto interactive_end

Reply via email to