Welcome to MSDN Blogs Sign in | Join | Help
簡単な言語の作り方3

せっかくコンソールができたので、「> 」というプロンプトではなく、もうちょっと自分用のプロンプトにしたいと思います。自分の言語用のプロンプトを作成するには、HostingフォルダにMyCalcCommnadLineクラスを追加します。このクラスの定義を以下のようにします。

using Microsoft.Scripting.Shell;
namespace MyCalc.Hosting
{
    public class MyCalcCommandLine : CommandLine
    {
        private string _log = "MyCalcだす ==> 試してね";
        private string _prompt = "\nMyCalc >";
        protected override string Logo
        {
            get {  return _log; }
        }

        protected override string Prompt
        {
            get {  return _prompt; }
        }
    }
}

MyCalcCommandLineクラスの特徴は、Microsoft.Scripting.Shell.CommandLineを継承することです。オーバーライドしている、LogoとPromptプロパティから何をしたかは理解できることでしょう。続いて、MyCalcLanguageContextクラスのGetServiceメソッドでMyCalcCommandLineのインスタンスを返せるような記述を行います。
public override ServiceType GetService<ServiceType>
                                   (params object[] args)
{
   if (typeof(ServiceType) == typeof(OptionsParser))
   {   return (ServiceType)
             (object)new Hosting.MyCalcOptionsParser();
   }
   else if (typeof(ServiceType) == typeof(CommandLine))
   {   return (ServiceType)
             (object)new Hosting.MyCalcCommandLine();
   }

   return base.GetService<servicetype>(args);
}
これで作成したMyCalcCommandLineクラスのインスタンスが、コマンドラインのインスタンスとして使用されるようになりました。この時点でビルドして実行すれば、以下のようなプロンプトが表示されます。
MyCalcだす ==> 試してね
MyCalc >
新しい言語のプロンプトらしくなってきました。それらしい見え方がすれば、俄然、もう少し進めようという意欲も湧いてくるというものです。それでは、次回から独自の言語であるMyCalcの振る舞いを実装していこうと思います。
Posted: Sunday, February 03, 2008 3:48 PM by shozoa
Filed under: , ,

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker