// TODO: The method apply() should be defined in class Node // to report an error. It should be overridden only in classes // BuiltIn and Closure. public override Node apply(Node args) { if (symbol.getName().Equals("car")) { if (args.getCdr() == Nil.getInstance()) { return args.getCar().getCar(); } else { throw new InvalidOperationException(); } } else if (symbol.getName().Equals("cdr")) { if (args.getCdr() == Nil.getInstance()) { return args.getCar().getCdr(); } else { throw new InvalidOperationException(); } } else if (symbol.getName().Equals("cons")) { return new Cons(args.getCar(), args.getCdr()); } else if (symbol.getName().Equals("set-car!")) { if (args.getCar().GetType() != typeof(Cons)) { Console.Error.WriteLine("Error: invalid arguments"); throw new InvalidOperationException(); } args.getCar().setCar(args.getCdr().getCar()); return args.getCar(); } else if (symbol.getName().Equals("set-cdr!")) { if (args.getCar().GetType() != typeof(Cons)) { Console.Error.WriteLine("Error: invalid arguments"); throw new InvalidOperationException(); } args.getCar().setCdr(args.getCdr().getCar()); return args.getCar(); } else if (symbol.getName().Equals("null?")) { if (args.getCar() == Nil.getInstance()) return BoolLit.getInstance(true); else return BoolLit.getInstance(false); } else if (symbol.getName().Equals("pair?")) { if (args.getCar().isPair()) return BoolLit.getInstance(true); else return BoolLit.getInstance(false); } else if (symbol.getName().Equals("eq?")) { if (args.getCar() is Ident && args.getCdr().getCar() is Ident) { if (((Ident)args.getCar()).getName().Equals(((Ident)args.getCdr().getCar()).getName())) { return BoolLit.getInstance(true); } else { return BoolLit.getInstance(false); } } else { if (args.getCar() == args.getCdr().getCar()) { return BoolLit.getInstance(true); } else { return BoolLit.getInstance(false); } } } else if (symbol.getName().Equals("symbol?")) { if (args.getCdr() == Nil.getInstance()) { if (args.getCar().isSymbol()) { return BoolLit.getInstance(true); } else { return BoolLit.getInstance(false); } } else { throw new InvalidOperationException(); } } else if (symbol.getName().Equals("number?")) { if (args.getCdr() == Nil.getInstance()) { if (args.getCar().GetType() == typeof(IntLit)) { return BoolLit.getInstance(true); } else { return BoolLit.getInstance(false); } } else { throw new InvalidOperationException(); } } else if (symbol.getName().Equals("b+")) { if (args.getCdr().getCdr() == Nil.getInstance()) { if (args.getCar() is IntLit && args.getCdr().getCar() is IntLit) { return new IntLit(((IntLit)args.getCar()).intVal + ((IntLit)args.getCdr().GetCar()).intVal); } else throw new InvalidOperationException(); } else { throw new InvalidOperationException(); } } else if (symbol.getName().Equals("b-")) { if (args.getCdr().getCdr() == Nil.getInstance()) { if (args.getCar() is IntLit && args.getCdr().getCar() is IntLit) { return new IntLit(((IntLit)args.getCar()).intVal - ((IntLit)args.getCdr().GetCar()).intVal); } else throw new InvalidOperationException(); } else throw new InvalidOperationException(); } else if (symbol.getName().Equals("b*")) { if (args.getCdr().getCdr() == Nil.getInstance()) { if (args.getCar() is IntLit && args.getCdr().getCar() is IntLit) { return new IntLit(((IntLit)args.getCar()).intVal * ((IntLit)args.getCdr().GetCar()).intVal); } else throw new InvalidOperationException(); } else throw new InvalidOperationException(); } else if (symbol.getName().Equals("b/")) { if (args.getCdr().getCdr() == Nil.getInstance()) { if (args.getCar() is IntLit && args.getCdr().getCar() is IntLit) { return new IntLit(((IntLit)args.getCar()).intVal / ((IntLit)args.getCdr().GetCar()).intVal); } else throw new InvalidOperationException(); } else throw new InvalidOperationException(); } else if (symbol.getName().Equals("b=")) { if (args.getCdr().getCdr() == Nil.getInstance()) { if (args.getCar() is IntLit && args.getCdr().getCar() is IntLit) { return BoolLit.getInstance(((IntLit)args.getCar()).intVal == ((IntLit)args.getCdr().GetCar()).intVal); } else throw new InvalidOperationException(); } else throw new InvalidOperationException(); } else if (symbol.getName().Equals("b<")) { if (args.getCdr().getCdr() == Nil.getInstance()) { if (args.getCar() is IntLit && args.getCdr().getCar() is IntLit) { return BoolLit.getInstance(((IntLit)args.getCar()).intVal < ((IntLit)args.getCdr().GetCar()).intVal); } else throw new InvalidOperationException(); } else throw new InvalidOperationException(); } else if (symbol.getName().Equals("procedure?")) { if (args.getCdr() == Nil.getInstance()) { if (args.getCar().isProcedure()) { return BoolLit.getInstance(true); } else { return BoolLit.getInstance(false); } } else { throw new InvalidOperationException(); } } else if (symbol.getName().Equals("read")) { //not sure if works Scanner scanner = new Scanner(Console.In); TreeBuilder builder = new TreeBuilder(); Parser parser = new Parser(scanner, builder); return (Node)parser.parseExp(); } else if (symbol.getName().Equals("write")) { ((StringLit)args.getCar()).printQuote(true); return Nil.getInstance(); } else if (symbol.getName().Equals("display")) { ((StringLit)args.getCar()).printQuote(false); return Nil.getInstance(); } else if (symbol.getName().Equals("newline")) { Console.WriteLine(""); return Nil.getInstance(); } else if (symbol.getName().Equals("eval")) { //not sure if this works return args.getCar().eval((Tree.Environment)args.getCdr().getCar()); } else if (symbol.getName().Equals("apply")) { //not sure if this works return (Closure)args.getCar().apply(args.getCdr().getCar()); } else if (symbol.getName().Equals("interaction-environment")) { //Not yet implemented return Nil.getInstance(); } else { return Nil.getInstance(); } }
public static int Main(string[] args) { // Create scanner that reads from standard input Scanner scanner = new Scanner(Console.In); if (args.Length > 1 || (args.Length == 1 && ! args[0].Equals("-d"))) { Console.Error.WriteLine("Usage: mono SPP [-d]"); return 1; } // If command line option -d is provided, debug the scanner. if (args.Length == 1 && args[0].Equals("-d")) { // Console.Write("Scheme 4101> "); Token tok = scanner.getNextToken(); while (tok != null) { TokenType tt = tok.getType(); Console.Write(tt); if (tt == TokenType.INT) Console.WriteLine(", intVal = " + tok.getIntVal()); else if (tt == TokenType.STRING) Console.WriteLine(", stringVal = " + tok.getStringVal()); else if (tt == TokenType.IDENT) Console.WriteLine(", name = " + tok.getName()); else Console.WriteLine(); // Console.Write("Scheme 4101> "); tok = scanner.getNextToken(); } return 0; } // Create parser TreeBuilder builder = new TreeBuilder(); Parser parser = new Parser(scanner, builder); Node root; // TODO: Create and populate the built-in environment and // create the top-level environment // Read-eval-print loop Tree.Environment env = new Tree.Environment(); Node id = new Ident("car"); env.define(id, new BuiltIn(id)); id = new Ident("cdr"); env.define(id, new BuiltIn(id)); id = new Ident("cons"); env.define(id, new BuiltIn(id)); id = new Ident("set-car!"); env.define(id, new BuiltIn(id)); id = new Ident("set-cdr!"); env.define(id, new BuiltIn(id)); id = new Ident("null?"); env.define(id, new BuiltIn(id)); id = new Ident("pair?"); env.define(id, new BuiltIn(id)); id = new Ident("eq?"); env.define(id, new BuiltIn(id)); id = new Ident("symbol?"); env.define(id, new BuiltIn(id)); id = new Ident("number?"); env.define(id, new BuiltIn(id)); id = new Ident("b+"); env.define(id, new BuiltIn(id)); id = new Ident("b-"); env.define(id, new BuiltIn(id)); id = new Ident("b*"); env.define(id, new BuiltIn(id)); id = new Ident("b/"); env.define(id, new BuiltIn(id)); id = new Ident("b="); env.define(id, new BuiltIn(id)); id = new Ident("b<"); env.define(id, new BuiltIn(id)); id = new Ident("procedure?"); env.define(id, new BuiltIn(id)); id = new Ident("read"); env.define(id, new BuiltIn(id)); id = new Ident("write"); env.define(id, new BuiltIn(id)); id = new Ident("display"); env.define(id, new BuiltIn(id)); id = new Ident("newline"); env.define(id, new BuiltIn(id)); id = new Ident("eval"); env.define(id, new BuiltIn(id)); id = new Ident("apply"); env.define(id, new BuiltIn(id)); id = new Ident("interaction-environment"); env.define(id, new BuiltIn(id)); env = new Tree.Environment(env); // TODO: print prompt and evaluate the expression root = (Node) parser.parseExp(); while (root != null) { root.eval(env).print(0); root = (Node) parser.parseExp(); } return 0; }
static void Main(string[] args) { // TODO(kai): parse out the args and do things if (args.Length == 0) { Console.WriteLine("No file passed to compile."); Wait(); return; } var filePath = args[0]; var dir = Path.GetDirectoryName(filePath); var fileName = Path.GetFileNameWithoutExtension(filePath); var log = new DetailLogger(); var lexer = new Lexer(); var tokens = lexer.GetTokens(log, filePath); if (log.HasErrors) { Fail(log); return; } var parser = new Parser(); var ast = parser.Parse(log, tokens, filePath); if (log.HasErrors) { Fail(log); return; } var writer = new AstWriter(Console.Out); writer.Visit(ast); var semantics = new SemanticAnalyzer(log); var symbols = semantics.Analyze(ast); if (log.HasErrors) { Fail(log); return; } Console.WriteLine(symbols); var tyChecker = new TypeChecker(log); tyChecker.Check(ast, symbols); if (log.HasErrors) { Fail(log); return; } var compiler = new ScoreCompiler(log); var module = compiler.Compile(fileName, ast, symbols); if (log.HasErrors) { Fail(log); return; } DumpModule(module); var bcFilePath = Path.Combine(dir, fileName + ".bc"); WriteBitcodeToFile(module, bcFilePath); /* // Run the interpreter! Console.WriteLine(); Console.WriteLine("Interpreting:"); var cmd = @"/c ..\..\..\..\TEST_FILES\lli.exe " + bcFilePath; //Console.WriteLine(cmd); var processInfo = new ProcessStartInfo("cmd.exe", cmd); processInfo.CreateNoWindow = false; processInfo.UseShellExecute = false; processInfo.RedirectStandardOutput = true; processInfo.RedirectStandardError = true; var process = Process.Start(processInfo); process.OutputDataReceived += (object sender, DataReceivedEventArgs e) => { Console.WriteLine(e.Data); }; process.BeginOutputReadLine(); process.WaitForExit(); //*/ Wait(); }