home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples1.exe / CLisp / CLisp.cs next >
Encoding:
Text File  |  2000-06-23  |  2.7 KB  |  121 lines

  1.  
  2. using System;
  3. using System.IO;
  4. using System.Collections;
  5.  
  6. class CLisp {
  7.     String file;
  8.  
  9.     Lexer l;
  10.     Parser p;
  11.     CodeGen cg;
  12.  
  13.     public bool Lex()
  14.     {
  15.     l = new Lexer(file);
  16.     return l.Lex();
  17.     }    
  18.  
  19.     public bool Parse()
  20.     {
  21.     p = new Parser(l.tokens);
  22.     return p.Parse();
  23.     }    
  24.  
  25.     public void CodeGen(String filename)
  26.     {
  27.     cg = new CodeGen(p.e, p.Functions, p.GlobalVars);
  28.     #if (PERF_BENCHMARK)
  29.     PerfTimer Pf = new PerfTimer("Reflection Emit");
  30.     int totaltime = Pf.StartTimer("Code Generation Time using Reflection Emit");
  31.     #endif
  32.     cg.Generate(filename);
  33.     #if (PERF_BENCHMARK)
  34.     Pf.StopTimer(totaltime);
  35.     Pf.OutputStoppedTime();
  36.     #endif
  37.     }
  38.      
  39.     public void ReadFile(String FileName, int FileSize)
  40.     {
  41.     char[] buf = new char[FileSize];
  42.     StreamReader sr = new StreamReader(new FileStream(FileName, FileMode.Open, FileAccess.Read));
  43.     int retval = sr.ReadBlock(buf, 0, FileSize);
  44.     file = new String(buf);
  45.     //    Console.WriteLine(file);
  46.     }
  47.  
  48.     public static void Main(String[] args){
  49.     String[] cmdline = Environment.GetCommandLineArgs();
  50.     Environment.ExitCode = (100);
  51.  
  52.     Console.WriteLine("Microsoft (R) CLisp - Demo Lisp Compiler \n" +
  53.                   "Copyright (C) Microsoft Corp 2000.  All rights reserved.\n");
  54.  
  55.     
  56.     if (cmdline.Length < 2) {
  57.         Console.WriteLine("Usage: " + cmdline[0] + " <input file> ");
  58.         return;
  59.     }
  60.  
  61.     File[] fe = (new Directory(".")).GetFiles(cmdline[1]);
  62.     if (fe.Length == 0){
  63.         Console.WriteLine(cmdline[1] + ": file not found");
  64.         return;        
  65.     }
  66.     
  67.     CLisp mine = new CLisp();
  68.  
  69.     try{
  70.         mine.ReadFile(cmdline[1], (int)fe[0].Length);
  71.     }
  72.     catch(IOException e){
  73.         Console.WriteLine("I/O error occured" + e);
  74.         Environment.ExitCode = (-1);
  75.     }
  76.  
  77.     try{
  78.         if (!mine.Lex()){
  79.         Queue tokens = new Queue(mine.l.tokens);
  80.         while(tokens.Count > 0)
  81.             Console.WriteLine(tokens.Dequeue());
  82.         Console.WriteLine("Lex Error. Stopping");
  83.         Environment.ExitCode = (-1);
  84.         return;
  85.         
  86.         }
  87.     }
  88.     catch(Exception e){
  89.         Console.WriteLine("Unexpected LEX error: " + e);
  90.         Environment.ExitCode = (-1);
  91.         return;
  92.     }
  93.  
  94.     try {
  95.         if (!mine.Parse()){
  96.                 Console.WriteLine(mine.p.e);
  97.             Console.WriteLine("Parse Error. Stopping");
  98.             Environment.ExitCode = (-1);
  99.             return;
  100.         }        
  101.     }
  102.     catch(Exception e){
  103.         Console.WriteLine("Unexpected Parse error:" + e);
  104.         Environment.ExitCode = (-1);
  105.         return;
  106.     }
  107.     
  108.     try {
  109.         String name = cmdline[1].Substring(0, cmdline[1].LastIndexOf('.'));
  110.         mine.CodeGen(name);
  111.     }
  112.     catch(Exception e){
  113.         Console.WriteLine("Unexpected CodeGen error:" + e);
  114.         Environment.ExitCode = (-1);
  115.         return;
  116.     }
  117.  
  118.     }
  119. }
  120.     
  121.