home *** CD-ROM | disk | FTP | other *** search
-
- using System;
- using System.IO;
- using System.Collections;
-
- class CLisp {
- String file;
-
- Lexer l;
- Parser p;
- CodeGen cg;
-
- public bool Lex()
- {
- l = new Lexer(file);
- return l.Lex();
- }
-
- public bool Parse()
- {
- p = new Parser(l.tokens);
- return p.Parse();
- }
-
- public void CodeGen(String filename)
- {
- cg = new CodeGen(p.e, p.Functions, p.GlobalVars);
- #if (PERF_BENCHMARK)
- PerfTimer Pf = new PerfTimer("Reflection Emit");
- int totaltime = Pf.StartTimer("Code Generation Time using Reflection Emit");
- #endif
- cg.Generate(filename);
- #if (PERF_BENCHMARK)
- Pf.StopTimer(totaltime);
- Pf.OutputStoppedTime();
- #endif
- }
-
- public void ReadFile(String FileName, int FileSize)
- {
- char[] buf = new char[FileSize];
- StreamReader sr = new StreamReader(new FileStream(FileName, FileMode.Open, FileAccess.Read));
- int retval = sr.ReadBlock(buf, 0, FileSize);
- file = new String(buf);
- // Console.WriteLine(file);
- }
-
- public static void Main(String[] args){
- String[] cmdline = Environment.GetCommandLineArgs();
- Environment.ExitCode = (100);
-
- Console.WriteLine("Microsoft (R) CLisp - Demo Lisp Compiler \n" +
- "Copyright (C) Microsoft Corp 2000. All rights reserved.\n");
-
-
- if (cmdline.Length < 2) {
- Console.WriteLine("Usage: " + cmdline[0] + " <input file> ");
- return;
- }
-
- File[] fe = (new Directory(".")).GetFiles(cmdline[1]);
- if (fe.Length == 0){
- Console.WriteLine(cmdline[1] + ": file not found");
- return;
- }
-
- CLisp mine = new CLisp();
-
- try{
- mine.ReadFile(cmdline[1], (int)fe[0].Length);
- }
- catch(IOException e){
- Console.WriteLine("I/O error occured" + e);
- Environment.ExitCode = (-1);
- }
-
- try{
- if (!mine.Lex()){
- Queue tokens = new Queue(mine.l.tokens);
- while(tokens.Count > 0)
- Console.WriteLine(tokens.Dequeue());
- Console.WriteLine("Lex Error. Stopping");
- Environment.ExitCode = (-1);
- return;
-
- }
- }
- catch(Exception e){
- Console.WriteLine("Unexpected LEX error: " + e);
- Environment.ExitCode = (-1);
- return;
- }
-
- try {
- if (!mine.Parse()){
- Console.WriteLine(mine.p.e);
- Console.WriteLine("Parse Error. Stopping");
- Environment.ExitCode = (-1);
- return;
- }
- }
- catch(Exception e){
- Console.WriteLine("Unexpected Parse error:" + e);
- Environment.ExitCode = (-1);
- return;
- }
-
- try {
- String name = cmdline[1].Substring(0, cmdline[1].LastIndexOf('.'));
- mine.CodeGen(name);
- }
- catch(Exception e){
- Console.WriteLine("Unexpected CodeGen error:" + e);
- Environment.ExitCode = (-1);
- return;
- }
-
- }
- }
-
-