// DONE :: The method apply() should be defined in class Node // to report an error. It should be overridden only in classes // BuiltIn and Closure. // The method apply() takes the environment out of the closure, // adds a new frame for the function call, defines bindings for the // parameters with the argument values in the new frame, and evaluates // the function body. public override Node apply(Node args) { Environment env = new Environment(this.env); Node car = fun.getCdr().getCar(); Node cdr = fun.getCdr().getCdr(); while (!car.isNull() && !args.isNull()) { if (car.isSymbol()) { env.define(car, args); break; } else if (car.isPair() && args.isPair()) { env.define(car.getCar(), args.getCar()); car = car.getCdr(); args = args.getCdr(); } else { Console.Error.WriteLine("Error: invalid input"); } } Node acar = cdr.getCar().eval(env); Node acdr = cdr.getCdr(); while (!acdr.isNull()) { acar = acdr.getCar().eval(env); acdr = acdr.getCdr(); } return(acar); }
public override Node eval(Node exp, Environment env) { int length = Util.expLength(exp); if (length < 3) { Console.Error.WriteLine("Error: invalid expression"); return Nil.getInstance(); } Node second = exp.getCdr().getCar(); Node name, value; if (length == 3 && second.isSymbol()) { name = second; value = exp.getCdr().getCdr().getCar().eval(env); env.define(name, value); return null; } if (!second.isPair()) { Console.Error.WriteLine("Error: invalid expression"); return Nil.getInstance(); } name = second.getCar(); Node lambda = exp.getCdr(); lambda.setCar(second.getCdr()); lambda = new Cons(new Ident("lambda"), lambda); value = lambda.eval(env); env.define(name, value); return null; }
// 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) { Environment closureEnv = new Environment(env); Node parameters = fun.getCdr().getCar(), body = fun.getCdr().getCdr().getCar(); // lambda expression of the form 'lambda identifier exp+' if (parameters.isPair() == false) { if (args.getCdr().isNull() == false) { return(new StringLit("Error: Too many arguments supplied for lambda expression.")); } closureEnv.define(parameters, args.getCar()); } // lambda expression of the form 'lambda ( [ parm ] ) exp+' else { while (parameters.isPair() == true && args.isPair() == true) { closureEnv.define(parameters.getCar(), args.getCar()); parameters = parameters.getCdr(); args = args.getCdr(); } // If both aren't null, then the # of arguments and parameters don't match if (!(parameters.isNull() == true && args.isNull() == true)) { return(new StringLit("Error: # of arguments does not match # of parameters in lambda expression.")); } } // Evaluate the body return(body.eval(closureEnv)); }
// 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) { Node param = fun.getCdr().getCar(); int numArgs = Util.expLength(args); int numParam = Util.expLength(param); if (numArgs != numParam) { Console.Error.WriteLine("Error: wrong number of arguments"); } Environment env1 = new Environment(env); while (param.isPair() && args.isPair()) { Node id = param.getCar(); Node val = args.getCar(); env1.define(id, val); param = param.getCdr(); args = args.getCdr(); } while (param.isPair()) { Node id = param.getCar(); Node val = Nil.getInstance(); env1.define(id, val); param = param.getCdr(); } Node exp1 = fun.getCdr().getCdr(); return Begin.evalBody(exp1, env1); }
private static int define(Node bindings, Environment env, Environment letEnv) { //no bindings left, exit with 0 if (bindings.isNull()) { return(0); } Node binding = bindings.getCar(); int numArgs = 1; Node expCdr = binding.getCdr(); while (!expCdr.isNull()) { numArgs++; expCdr = expCdr.getCdr(); } //binding has wrong num args, exit with -1 if (numArgs != 2) { return(-1); } Node var = binding.getCar(); Node val = binding.getCdr().getCar().eval(env); letEnv.define(var, val); return(define(bindings.getCdr(), env, letEnv)); }
public override Node eval(Node exp, Environment env) { int length = Util.expLength(exp); if (length < 3) { Console.Error.WriteLine("Error: invalid expression"); return Nil.getInstance(); } Node second = exp.getCdr().getCar(); while (second.isPair()) { if (Util.expLength(second.getCar()) != 2) { Console.Error.WriteLine("Error: invalid expression"); return Nil.getInstance(); } second = second.getCdr(); } second = exp.getCdr().getCar(); Environment env1 = new Environment(env); while (second.isPair()) { Node id = second.getCar().getCar(); Node val = second.getCar().getCdr().getCar(); env1.define(id, val); second = second.getCdr(); } Node exp1 = exp.getCdr().getCdr(); return Begin.evalBody(exp1, env1); }
public override Node eval(Node a, Environment e) { Node key = a.getCdr().getCar(); Node val = a.getCdr().getCdr().getCar(); if (key.isSymbol()) { e.define(key, val); } else { Closure function = new Closure(new Cons(a.getCdr().getCar().getCdr(), a.getCdr().getCdr()), e); e.define(key.getCar(), function); } return(new StringLit("; no values returned")); }
public Node evalLetBody(Node t, Environment env) { if(t == Nil.getInstance()) //This isn't correct. What do? { Node l = new Cons(new Nil(), new Nil()); //tree with pair of () return l; } else { Node restBody = t.getCdr(); Node variable = t.getCar().getCar(); Node value = t.getCar().getCdr().getCar(); if(variable.isSymbol()) //was value earlier, should be variable? { env.define(variable,value.eval(env)); return evalLetBody(restBody, env); } else if(!variable.isPair()) //if it is a cons node. ??? { Console.Write("Error in Let->evalLetBody, variable is not valid"); return new Nil(); } else if(variable.isNull()) { return new Nil(); } else { return variable.eval(env); } } }
public Node evalBody(Node t, Environment env) { if (t.isNull()) { return(Nil.getInstance()); } else { Node arg = t.getCar().getCar(); Node func = t.getCar().getCdr().getCar(); Node rest = t.getCdr(); if (arg.isSymbol()) { env.define(arg, func.eval(env)); return(evalBody(rest, env)); } else if (arg.isPair()) { return(arg.eval(env)); } else if (arg.isNull()) { return(Nil.getInstance()); } return(null); } }
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 var env = new Tree.Environment(); // create built in environment for scheme functions var id = new Ident("car"); // TODO: create lines for definitions of built in functions from first page of docs id = new Ident("b+"); env.define(id, new BuiltIn(id)); // populates environment--puts car into built in environment--define manages tree for you env = new Tree.Environment(env); // root = (Node) parser.parseExp(); while (root != null) { root.print(0); root = (Node) parser.parseExp(); } return 0; }
public override Node eval(Node t, Environment env) { Node var = t.getCdr().getCar(); Node val = t.getCdr().getCdr().getCar(); env.define(var, val); return(Void.getInstance()); }
//need to check to see if binding for x (in this case 'identifier') already exists public override Node eval(Node t, Environment env) { //works for (x 3) but does it work for twice (lambda (f x) (f.............))? Node identifier = t.getCdr().getCar(); Node value = t.getCdr().getCdr().getCar(); if ((identifier.isSymbol())) //this is for if it is in the form of (define x 3), here x is not a function { env.define(identifier,value); } else { Closure idFunction = new Closure(new Cons(t.getCdr().getCar().getCdr(), t.getCdr().getCdr()), env); env.define(identifier.getCar(), idFunction); } return null; }
public override Node eval(Node a, Environment e) { Node key = a.getCdr().getCar(); Node expression = t.getCdr().getCdr().getCar(); e.define(key, expression.eval(e)); return(new StrLit("")); }
public override Node eval(Node t, Environment env) { Node iden = t.getCdr().getCar(); Node exp = t.getCdr().getCdr().getCar(); env.define(iden, exp.eval(env)); return(new StringLit("")); }
public override Node eval(Node t, Environment env) { Node variable = t.getCdr().getCar(); if(variable.isPair()) { //variable is of the form (decl params...), ex: (foo x y) Node declaration = variable.getCar(); Node parameters = variable.getCdr(); Cons lambda = new Cons(new Ident("lambda"),new Cons(parameters,t.getCdr().getCdr().getCar())); env.define(declaration,new Closure(lambda, env)); } else { env.define(variable, t.getCdr().getCdr().getCar().eval(env)); } return new StringLit("definition recorded"); }
public Node eval(Node t, Environment env) { Node cadr = t.getCdr().getCar(); Node caddr = t.getCdr().getCdr().getCar(); env.define(cadr, caddr.eval(env)); return(Console.WriteLine("")); }
public override Node eval(Node t, Environment env) { Node identifier; Node exp; identifier = t.getCdr().getCar(); exp = t.getCdr().getCdr().getCar(); env.define(identifier, exp.eval(env)); return new StringLit(""); //Why? Probably will return void instead of an empty node. }
public override Node eval(Node t, Environment env) { Node car1 = t.getCdr().getCar(); Node car2 = t.getCdr().getCdr().getCar(); if (car1.isSymbol()) { // if first arg is not a list, define variable env.define(car1, car2); } else { // if first arg is list, define function and function body Cons funcbody = new Cons(car1.getCdr(), t.getCdr().getCdr()); Node closureFun = new Cons(new Ident("lambda"), funcbody).eval(env); env.define(car1.getCar(), closureFun); } return(Void.getInstance()); }
public override Node eval(Node exp, Environment env) { int numArgs = 0; Node expCdr = exp.getCdr(); while (!expCdr.isNull()) { numArgs++; expCdr = expCdr.getCdr(); } if (numArgs < 2) { Console.Error.WriteLine("Error: invalid expression"); return(Nil.getInstance()); } Node arg1 = exp.getCdr().getCar(); if (numArgs == 2 && arg1.isSymbol()) { Node val = exp.getCdr().getCdr().getCar(); env.define(arg1, val.eval(env)); return(new StringLit("; no values returned", false)); } if (arg1.isPair()) { Node fName = arg1.getCar(); Node fArgs = arg1.getCdr(); Node fBody = exp.getCdr().getCdr(); bool validArgs = verifyArgs(fArgs); if (fName.isSymbol() && validArgs) { Node node = new Cons(new Ident("lambda"), new Cons(fArgs, fBody)); env.define(fName, node.eval(env)); return(new StringLit("; no values returned", false)); } Console.Error.WriteLine("Error: ill-formed definition"); return(Nil.getInstance()); } Console.Error.WriteLine("Error: invalid expression"); return(Nil.getInstance()); }
public Node eval(Node t, Environment e) { Environment letE = new Environment(e); Node children = t.getCdr().getCar(); if (children != null) { Node current = t.getCar(); letE.define(current, null); return null; } }
public override Node eval(Node t, Environment env) { //get tmp environment arguements Node tmp1 = t.getCdr().getCar(); Node tmp2 = t.getCdr().getCdr().getCar(); //redefine environment env.define(tmp1, tmp2); //return empty string object return(new StringLit("")); }
public override Node eval(Environment env, Node args) { Node expr1 = args.getCar(); if (args.getCdr().isNull() == true) { return(new StringLit("Error: Only 1 argument passed to 'define.'")); } else if (args.getCdr().getCdr().isNull() == false) { return(new StringLit("Error: Too many arguments passed to 'define.'")); } // Is this defining a function? else if (expr1.isPair() == true) { if (expr1.getCar().isSymbol() == true) { // Construct the lambda expression Node formalsAndBody = new Cons(expr1.getCdr(), args.getCdr()); Node lambdaExpr = new Cons(new Ident("lambda"), formalsAndBody); // define.apply() return(env.define(expr1.getCar(), lambdaExpr.eval(env))); } else { return(new StringLit("Error: First argument to 'define' must be a <variable>")); } } else { if (expr1.isSymbol() == true) { // define.apply() return(env.define(expr1, args.getCdr().getCar().eval(env))); } else { return(new StringLit("Error: First argument to 'define' must be a <variable>")); } } }
public Node eval(Node t, Environment e) { Environment letE = new Environment(e); Node children = t.getCdr().getCar(); if (children != null) { Node current = t.getCar(); letE.define(current, null); return(null); } }
// 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) { Environment funcEnvironment = new Environment(env); Node param = fun.getCdr().getCar(); while (param != Nil.getInstance() && args != Nil.getInstance()) { funcEnvironment.define(param.getCar(), args.getCar()); param = param.getCdr(); args = args.getCdr(); } Node body = new Cons(new Ident("begin"), fun.getCdr().getCdr()); return body.eval(funcEnvironment); }
public override Node eval(Node t, Environment env) { Node n = t.getCdr().getCar(); Node temp = t.getCdr().getCdr(); Environment e = new Environment(env); while (!(n is Nil)) { e.define(n.getCar().getCar(), n.getCar().getCdr().getCar()); n = n.getCdr(); } return(temp.getCar().eval(e)); }
public override Node eval(Node t, Environment env) { // if cdr is null, define call has no arguments (error state) if (t.getCdr() == Nil.getInstance()) { Console.Error.WriteLine("Special form 'Define' evaluated with no arguments"); return(Nil.getInstance()); } // to evaluate an expression of the form '(eval x e)', // evaluate e, store its value as e1, then look up x in the current scope. // if a binding for x exists, set the value to e1 // otherwise, add (x e1) as the first element of the first association // lists into env. t = t.getCdr(); // determine if the form is '(eval x e)' if (t.getCar().isSymbol()) { // evaluate e, then store into e1 Node e1 = t.getCdr().eval(env); env.define(t.getCar(), e1); } // if the expression is of the form '(define (x p1 ... pn) b1 ... bm)', // construct the lambda expression // (lambda(p1...pn) b1...bm) // then proceed as for the definition // (define x(lambda(p1...pn) b1...bm)) if (t.getCar().isPair()) { Node arg1 = t.getCar().getCar(); Cons arg2 = new Cons(new Ident("lambda"), new Cons(t.getCdr().getCar(), t.getCdr().getCar().getCdr())); env.define(arg1, arg2); } return(null); }
public override Node eval(Node t, Environment e) { //Node begin = new Cons(new Ident("begin"), t.getCdr()); //return begin.eval(e); Node currentNode = t.getCdr(); while(currentNode.getCdr() != Nil.getInstance()) { e.define(currentNode.getCar().getCar(), currentNode.getCar().getCdr().getCar()); currentNode = currentNode.getCdr(); } if (currentNode.getCar().isSymbol()) return e.lookup(currentNode.getCar()); return currentNode.getCar().eval(e); }
public override Node eval(Node t, Environment env) { Node bindings = t.getCdr().getCar(); Environment sub = new Environment(env); //bind new stuff while(bindings != Nil.getInstance()) { Node pair = bindings.getCar(); sub.define(pair.getCar(), pair.getCdr().getCar().eval(sub)); bindings = bindings.getCdr(); } Node function = t.getCdr().getCdr().getCar(); return function.eval(sub); }
// TODO: Implement method apply() // It should be overridden only in classes BuiltIn and Closure public override Node apply(Node args, Environment env) { Node arglist = fun.getCar(); Node fun2run = fun.getCdr().getCar(); while ((!args.isNull())) { if (args.getCar() is Cons) { env.define(arglist.getCar().getName(), (Node)args.getCar().eval(env)); } else { env.define(arglist.getCar().getName(), args.getCar()); } args = args.getCdr(); arglist = arglist.getCdr(); } //&&(!(args).getCdr().isNull())); Node p = (Node)fun2run.eval(env); return(p); }
public override Node eval(Node t, Environment env) { Node iden = t.getCdr().getCar(); Node val = t.getCdr().getCdr().getCar(); if (iden.isSymbol()) { env.define(iden, val); } else { Closure close = new Closure(new Cons(t.getCdr().getCar().getCdr(), t.getCdr().getCdr()), env); env.define(iden.getCar(), close); } if (iden.isPair()) { Node func = iden.getCar(); Cons list = new Cons(new Ident("lambda"), new Cons(val, t.getCdr().getCar().getCdr())); env.define(func, list); } return(new StringLit("Values are null (Define)")); }
public override Node apply(Node args) { Environment theEnv = new Environment(env); Node lambda = fun.getCdr().getCar(); while (lambda != Nil.getInstance()) { theEnv.define(lambda.getCar(), args.getCar()); lambda = lambda.getCdr(); args = args.getCdr(); } Node function = fun.getCdr().getCdr().getCar(); return(function.eval(theEnv)); }
// TODO: implement public override Node apply(Node args) { Environment e = new Environment(env); Node n = fun.getCar().getCdr(); while (n != Nil.getInstance() && args != Nil.getInstance()) { e.define(n.getCar(), args.getCar()); n = n.getCdr(); args = args.getCdr(); } return(Nil.getInstance()); //Node funBody = ; }
public override Node apply(Node args) { Environment funcEnv = new Environment(env); Node temp = fun.getCdr().getCar(); while (temp != Nil.getInstance()) { funcEnv.define(temp.getCar(), args.getCar()); temp = temp.getCdr(); args = args.getCdr(); } Node body = new Cons(new Ident("begin"), fun.getCdr().getCdr()); return(body.eval(funcEnv)); }
private static void assignParams(Node parameters, Node args, Environment env) { if (parameters.isNull() && args.isNull()) { return; } if (parameters.isNull() || args.isNull()) { Console.Error.WriteLine("Error: number of arguments do not match number of parameters"); } else if (parameters.isSymbol()) { env.define(parameters, args); } else if (parameters.isPair() && args.isPair()) { env.define(parameters.getCar(), args.getCar()); assignParams(parameters.getCdr(), args.getCdr(), env); } else { Console.Error.WriteLine("Error: invalid closure"); } }
// 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) { //Environment in which to map the args Environment closureEnv = new Environment(env); Node lambdaArgsList = fun.getCdr().getCar(); //Bind each argument to its parameter in the environment while(lambdaArgsList!=Nil.getInstance()) { closureEnv.define(lambdaArgsList.getCar(), args.getCar()); lambdaArgsList = lambdaArgsList.getCdr(); args = args.getCdr(); } Node functionBody = fun.getCdr().getCdr().getCar(); return functionBody.eval(closureEnv); }
public override Node eval(Node exp, Environment env) { int num = Node.length(exp); if (num <= 2) { Console.Error.WriteLine("Error: invalid define expression"); return(Nil.getInstance()); } Node car1 = exp.getCdr().getCar(); if (car1.isSymbol() && num == 3) { Node car2 = exp.getCdr().getCdr().getCar(); env.define(car1, car2.eval(env)); return(Void.getInstance()); } if (!car1.isPair()) { Console.Error.WriteLine("Error: invalid expression"); return((Node)Nil.getInstance()); } Node car3 = car1.getCar(); Node cdr1 = car1.getCdr(); Node cdr2 = exp.getCdr().getCdr(); if (!car3.isSymbol() || !isValid(cdr1)) { Console.Error.WriteLine("Error: ill-formed definition"); return((Node)Nil.getInstance()); } Node node = (Node) new Cons((Node) new Ident("lambda"), (Node) new Cons(cdr1, cdr2)); env.define(car3, node.eval(env)); return((Node)Void.getInstance()); }
// 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) { Environment e = this.getEnv(); Node notFun = getFun(); Node car = notFun.getCar(); notFun = notFun.getCdr().getCar(); while (args != null && !args.getCar().isNull()) { e.define(car.getCar(), args.getCar()); car = car.getCdr(); args = args.getCdr(); } return(notFun.eval(e)); }
//added from here and down, possible errors private static int define(Node bind, Environment env, Environment lenv) { if (bind.isNull()) { return 0; } Node car = bind.getCar(); if (Util.expLength(car) != 2) { return -1; } Node car2 = car.getCar(); // (car x) Node value = car.getCdr().getCar().eval(env); //(eval (cadr x) env) lenv.define(car2, value); // (list (car x) (eval (cadr x) env))) return Let.define(bind.getCdr(), env, lenv); }
private int initialize(Node bindings, Environment env, Environment env2) { if (bindings.isNull()) { return(0); } var binding = bindings.getCar(); if (Node.length(binding) == 2) { var var1 = binding.getCar(); var init1 = binding.getCdr().getCar(); var node = init1.eval(env); env2.define(var1, node); return(initialize(bindings.getCdr(), env, env2)); } return(-1); }
public override Node eval(Environment env, Node args) { // Evaluate the inits in the current env and bind the variables to them in the new env Node binding = args.getCdr().getCar(); Environment letEnv = new Environment(env); while (binding.isPair()) { Node init = binding.getCar().getCdr().getCar().eval(env); Node variable = binding.getCar().getCar(); letEnv.define(variable, init); binding = binding.getCdr(); } Node bodyEval = args.getCdr().getCdr().eval(letEnv); return(args.getCar().eval(env).apply(bodyEval)); }
// TODO: The method apply() should be defined in class Node // to report an error. It should be overridden only in classes // BuiltIn and Closure. /* * Before calling the function apply, its arguments are evaluated * the first argument must be a closure, then apply needs todo * extract the environment out of the closure * add a new frame to the environment that binds the parameters * to the corresponding argument values * recursively call eval for the function body and the new enviornment * */ public override Node apply(Node args) { //need to get both args Node arg1 = args.getCar(), arg2 = args.getCdr(); //need to see if arg1 is a Closure if (arg1.isProcedure()) { //getting the environment out of the Closure arg1 Environment newEnv = ((Closure)arg1).getEnv(); //adding a new frame to the environment newEnv.define(((Closure)arg1).getFun(), arg2); //trying to recursively call eval newEnv.eval(newEnv); //return environment? return(newEnv); } else { return(new StringLit("Error: apply on non closure")); } }
// to evaluate an expression of the form '(let ((x1 e1) ... (xn en)) b1 ... bm)', // recursively evaluate e1 ... en, and call the results e1' ... en' // construct an association list ((x1 e1') ... (xn en')) // create a new environment, env1, by 'cons-ing' the assoc. list in front of 'env' // recursively evaluate b1 ... bm in the new environment env1, and return the // result of evaluating bm. public override Node eval(Node t, Environment env) { // error state if (t.getCdr() == Nil.getInstance()) { Console.Error.WriteLine("Special form 'Begin' evaluated with no arguments"); return(Nil.getInstance()); } // get cdr of t to eval Node temp = t.getCdr(); // construct a new environment for the Let expression Environment e = new Environment(env); while (temp.getCar() != Nil.getInstance()) { e.define(temp.getCar().getCar(), temp.getCar().getCdr().getCar().eval(e)); temp = temp.getCdr(); } return(t.getCdr().getCdr().getCar().eval(e)); }
//Function for pairing up arguments with corresponding symbols private static void pairup(Node parms, Node args, Environment env) { //if null return nothing if (parms.isNull() && args.isNull()) { return; } //check the symbol type if (parms.isSymbol()) { env.define(parms, args); } //depending on the parameter, check the # of required valid arguments else if (parms.isNull() || args.isNull()) { Console.get_Error().WriteLine("Error: wrong number of arguments"); } //look for values in different env w/ closure pointer? idk how to explain this else if (parms.isPair() && args.isPair()) { env.define(parms.getCar(), args.getCar()); Closure.pairup(parms.getCdr(), args.getCdr(), env); } else { Console.get_Error().WriteLine("Error: invalid expression"); } }
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 = new Node(); // TODO: Create and populate the built-in environment and // create the top-level environment Tree.Environment env = new Tree.Environment(); Ident id = new Ident("b+"); env.define(id, new BuiltIn(id)); env = new Tree.Environment(env); Ident test = new Ident("xxxx"); IntLit test2 = new IntLit(3); env.define(test, test2); root = (Node) parser.parseExp(); while (root != null) { root.eval(env).print(0); root = (Node) parser.parseExp(); } // Read-eval-print loop // TODO: print prompt and evaluate the expression /* root = (Node) parser.parseExp(); while (root != null) { root.print(0); root = (Node) parser.parseExp(); } */ return 0; }
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; }
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; // Read-eval-print loop Tree.Environment builtin = new Tree.Environment(); Ident id = new Ident("symbol?"); builtin.define(id, new BuiltIn(id)); id = new Ident("number?"); builtin.define(id, new BuiltIn(id)); id = new Ident("b+"); builtin.define(id, new BuiltIn(id)); id = new Ident("b-"); builtin.define(id, new BuiltIn(id)); id = new Ident("b*"); builtin.define(id, new BuiltIn(id)); id = new Ident("b/"); builtin.define(id, new BuiltIn(id)); id = new Ident("b="); builtin.define(id, new BuiltIn(id)); id = new Ident("b<"); builtin.define(id, new BuiltIn(id)); id = new Ident("car"); builtin.define(id, new BuiltIn(id)); id = new Ident("cdr"); builtin.define(id, new BuiltIn(id)); id = new Ident("set-car!"); builtin.define(id, new BuiltIn(id)); id = new Ident("set-cdr!"); builtin.define(id, new BuiltIn(id)); id = new Ident("null?"); builtin.define(id, new BuiltIn(id)); id = new Ident("pair?"); builtin.define(id, new BuiltIn(id)); id = new Ident("eq?"); builtin.define(id, new BuiltIn(id)); id = new Ident("procedure?"); builtin.define(id, new BuiltIn(id)); id = new Ident("read"); builtin.define(id, new BuiltIn(id)); id = new Ident("write"); builtin.define(id, new BuiltIn(id)); id = new Ident("display"); builtin.define(id, new BuiltIn(id)); id = new Ident("newline"); builtin.define(id, new BuiltIn(id)); id = new Ident("eval"); builtin.define(id, new BuiltIn(id)); id = new Ident("apply"); builtin.define(id, new BuiltIn(id)); id = new Ident("interaction-environment"); builtin.define(id, new BuiltIn(id)); Tree.Environment env = new Tree.Environment(builtin); root = (Node)parser.parseExp(); while (root != null) { root.eval(env).print(0); root = (Node)parser.parseExp(); } return(0); }
public override Node eval(Node t, Environment e) { e.define(t.getCdr().getCar(), t.getCdr().getCdr().getCar()); return Void.getInstance(); }
public override object eval(Environment e) { if (form is If) { if ((Boolean)((Node)cdr.getCar().eval(e)).eval(e)) { if (cdr.getCdr().getCar() is Cons) { return(cdr.getCdr().getCar().eval(e)); } return(cdr.getCdr().getCar()); } else { if (cdr.getCdr().getCdr().getCar() is Cons) { return(cdr.getCdr().getCdr().getCar().eval(e)); } return(cdr.getCdr().getCdr().getCar()); } } else if (form is Cond) { Node nextClause = cdr; Node o = (Node)nextClause.getCar().getCar().eval(e); while ((nextClause.getCar().getCar().getName() != "else") && (!(Boolean)o.eval(e))) { nextClause = nextClause.getCdr(); } Node p = (nextClause.getCar().getCdr().getCar()); while (p is Cons) { p = (Node)p.eval(e); } return(p); } //TODO: Closures else if (form is Define) { if (cdr.GetCar() is Cons) { e.define(cdr.getCar().getCar().getName(), new Closure(new Cons(cdr.getCar().getCdr(), cdr.getCdr()), e)); } else { e.define(cdr.getCar().getName(), cdr.getCdr().getCar()); } } else if (form is Lambda) { return(new Closure(cdr, e)); } else if (form is Let) { Environment env = new Environment(e); Node assign = (Node)cdr.GetCar(); while (!assign.isNull()) { if (assign.getCar().getCdr().getCar() is Cons) { env.define(assign.getCar().getCar().getName(), (Node)assign.getCar().getCdr().getCar().eval(env)); } else { env.define(assign.getCar().getCar().getName(), (Node)assign.getCar().getCdr().getCar()); } assign = assign.getCdr(); } return(cdr.getCdr().getCar().eval(env)); } else if (form is Quote)//buggy { return(cdr); }