// parseList() `parses' special forms, constructs an appropriate // object of a subclass of Special, and stores a pointer to that // object in variable form. It would be possible to fully parse // special forms at this point. Since this causes complications // when using (incorrect) programs as data, it is easiest to let // parseList only look at the car for selecting the appropriate // object from the Special hierarchy and to leave the rest of // parsing up to the interpreter. void parseList() { if (car.isSymbol()) { Ident a = (Ident)car; string type = a.getSymbol(); if (type.Equals("quote")) { form = new Quote(); } //else if (type.Equals("if")) // form = new If(); //else if (type.Equals("begin")) // form = new Begin(); //else if (type.Equals("cond")) // form = new Cond(); //else if (type.Equals("define")) // form = new Define(); //else if (type.Equals("lambda")) // form = new Lambda(); //else if (type.Equals("let")) // form = new Let(); //else if (type.Equals("set!")) // form = new Set(); else { form = new Regular(); } } else { form = new Regular(); } }
} // BuiltIn public virtual Node eval(Environment e) { if (this.isSymbol()) { Ident id = new Ident(this.getName()); return(id.eval(this, e)); } return(this); }
public Node eval(Environment env) { if(this.isSymbol()) { id = new Ident(this.getName()); return id.eval(this, env); } return this; }
public virtual Node eval(Environment env) { if (this is Ident) { iden = new Ident(this.getName()); return(iden.eval(this, env)); } return(this); }
} // Cons // parseList() `parses' special forms, constructs an appropriate // object of a subclass of Special, and stores a pointer to that // object in variable form. It would be possible to fully parse // special forms at this point. Since this causes complications // when using (incorrect) programs as data, it is easiest to let // parseList only look at the car for selecting the appropriate // object from the Special hierarchy and to leave the rest of // parsing up to the interpreter. private void parseList() { if (car.isSymbol()) { Ident special = (Ident)car; switch (special.getName()) { case "quote": form = new Quote(); break; case "lambda": form = new Lambda(); break; case "begin": form = new Begin(); break; case "if": form = new If(); break; case "let": form = new Let(); break; case "cond": form = new Cond(); break; case "define": form = new Define(); break; case "set!": form = new Set(); break; default: form = new Regular(); break; } } else { form = new Regular(); } }
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; }
//if arg length = 0 private Node apply0() { string name = this.symbol.getName(); bool flag = name.Equals("read"); Node result; if (flag) { Parse.Scanner s = new Parse.Scanner(Console.In); Parse.Parser parser = new Parse.Parser(s, new TreeBuilder()); Node node = (Node)parser.parseExp(); bool flag2 = node != null; if (flag2) { result = node; } else { result = new Ident("end-of-file"); } }//end outer if else { bool flag3 = name.Equals("newline"); if (flag3) { Console.WriteLine(); result = Unspecific.getInstance(); } else { bool flag4 = name.Equals("interaction-environment"); if (flag4) { result = Scheme4101.env; } else { Console.Error.WriteLine("Error: wrong number of arguments"); result = Nil.getInstance(); } } }//end outer else return result; }