// 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) //{ // return new StringLit("Error: BuiltIn.apply not yet implemented"); //} public override Node apply(Node args) { int num = Util.expLength(args); bool flag = num > 2; if (flag) { Console.Error.WriteLine("Error: wrong number of arguments"); } bool flag2 = num == 0; // length 0 arguments Node result; if (flag2) { result = this.apply0(); } else //1 or 2 argument lengths { bool flag3 = num == 1; // length 1 arguments if (flag3) { result = this.apply1(args.getCar()); } else { result = this.apply2(args.getCar(), args.getCdr().getCar()); } } return result; }
public override Node eval(Node t, Environment env) { Node clauseList = t.getCdr(); Node clause = clauseList.getCar(); while(clause != Nil.getInstance()) { Node predicate = clause.getCar().eval(env); if(predicate == BoolLit.getInstance(true) || predicate.getName() == "else") { Node expressionList = clause.getCdr(); //evaluate all expressions and return the result of the last evaluation Node lastEval = expressionList.getCar().eval(env); //If there are more expressions to evaluate while(expressionList.getCdr() != Nil.getInstance()) { expressionList = expressionList.getCdr(); lastEval = expressionList.getCar().eval(env); } return lastEval; } clauseList = clauseList.getCdr(); clause = clauseList.getCar(); } //Does not yet handle else's return Nil.getInstance(); }
public override void print(Node t, int n, bool p) { for (int i = Console.CursorLeft; i < n; i++) Console.Write(" "); if (!p) Console.Write("("); //Print "lambda" t.getCar().print(n); Console.Write(" "); Node second = t.getCdr().getCar(); if (second.isPair()) { second.print(n, false); } Console.WriteLine(); Node term = t.getCdr().getCdr(); while(!term.isNull()) { term.getCar().print(n + 4); term = term.getCdr(); Console.WriteLine(); } Nil.getNil().print(n,true); }
public override void print(Node t, int n, bool p) { if (!p) { var spaces = ""; if (n >= 0) spaces = new string(' ', n); Console.Write(spaces + "(if "); if (t.getCdr().getCar().isPair()) { if((t.getCdr().getCar() as Cons).form is If || (t.getCdr().getCar() as Cons).form is Lambda || (t.getCdr().getCar() as Cons).form is Define) t.getCdr().getCar().print(n+4, true); else { t.getCdr().getCar().print(0, false); } } else t.getCdr().getCar().print(0, false); t = t.getCdr().getCdr(); do { Console.WriteLine(""); t.getCar().print(n + 4, false); t = t.getCdr(); } while (t.getCdr() != null); Console.WriteLine(""); t.print(n, true); } else { var spaces = ""; if (n >= 0) spaces = new string(' ', n); Console.Write("(if "); if (t.getCdr().getCar().isPair()) { if ((t.getCdr().getCar() as Cons).form is If || (t.getCdr().getCar() as Cons).form is Lambda || (t.getCdr().getCar() as Cons).form is Define) t.getCdr().getCar().print(n+4, true); else { t.getCdr().getCar().print(0, false); } } t.getCdr().getCar().print(0, false); t = t.getCdr().getCdr(); do { Console.WriteLine(""); t.getCar().print(n + 4, false); t = t.getCdr(); } while (t.getCdr() != null); Console.WriteLine(""); t.print(n, true); } }
public override void print(Node t, int n, bool p) { for (int i = Console.CursorLeft; i < n; i++) Console.Write(" "); if (!p) { Console.Write("("); } //print "let" t.getCar().print(n); Console.WriteLine(); Node rest = t.getCdr(); if (rest.isPair()) { rest.getCar().print(n + 4, false); Console.WriteLine(); while ((rest = rest.getCdr()) != Nil.getNil()) { rest.getCar().print(n + 4, false); Console.WriteLine(); } Console.Write(")"); } else t.getCdr().print(n, true); }
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 override void print(Node t, int n, bool p) { Console.Write("".PadLeft(n)); if ( p != true) { Console.Write("("); } if(t.getCar().isPair()) { t.getCar().print(0); } else { t.getCar().print(0, true); } Console.Write(" "); if(t.getCdr() is Nil) { Console.WriteLine(")"); } else { t.getCdr().print(0, true); } // Console.WriteLine(); }
public override void print(Node t, int n, bool p) { // TODO: Implement this function. Console.Write("".PadLeft(n)); Console.Write("(define "); Node name = t.getCdr().getCar(); name.print(n); Console.Write(" "); Node definition = t.getCdr().getCdr().getCar(); if (definition.isNull()) { Console.Error.Write("Syntax error with DEFINE-definition"); } else if (definition.isPair()) { Console.WriteLine(); // Console.Write("".PadLeft(n+4)); definition.print(n+4); } else { definition.print(n); } // made n to 0 // Console.WriteLine(); Console.WriteLine("".PadLeft(n) + ")"); }
// Key Deletion public bool Delete(double key) { bool value = KeyDeletion(null, root, key); if (root.key.Count == 0 && root.children[0] != null) root = root.children[0]; return value; }
/* A O(n) iterative program for construction of BST from preorder traversal */ public Node ConstuctTreeFromPreOrder(int[] preOrder) { Node root = new Node(); root.data = preOrder[0]; Stack<Node> _stack = new Stack<Node>(); Node temp; for (int i = 1; i < preOrder.Length; i++) { temp = null; while (_stack.Count > 0 && preOrder[i] > _stack.Peek().data) { temp = _stack.Pop(); } if (temp != null) { temp.right.data = preOrder[i]; _stack.Push(temp.right); } else { temp.left.data = preOrder[i]; _stack.Push(temp.left); } } return root; }
static Tree<int> ParseTree() { var nodes = new Dictionary<int, Node<int>>(); Console.Write("Number of nodes:"); int n = int.Parse(Console.ReadLine()); Console.WriteLine("Start typing the node pairs separated by a single space"); for (int i = 0; i < n-1; i++) { var pair = Console.ReadLine().Split(); int first = int.Parse(pair[0]); int second = int.Parse(pair[1]); if (!nodes.ContainsKey(first)) { Node<int> parent = new Node<int>(first); nodes.Add(first, parent); } if (!nodes.ContainsKey(second)) { Node<int> newNode = new Node<int>(second); newNode.HasParent = true; nodes.Add(second,newNode); nodes[first].Children.Add(newNode); } nodes[first].Children.Add(nodes[second]); nodes[second].HasParent=true; } var root = nodes.First(x => x.Value.HasParent == false); var tree = new Tree<int>(root.Value); return tree; }
// 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); }
public override void print(Node t, int n, bool p) { // TODO: Implement this function. // Console.Write("".PadLeft(n)); if (!p) { Console.Write("".PadLeft(n) + '('); } t.getCar().print(0, !p); // if(!t.getCdr().isNull()) // { // Console.Write(" "); // } if(t.getCdr().isNull()) { t.getCdr().print(0, true); // Console.WriteLine(); } else { Console.Write(" "); t.getCdr().print(n, true); } // if(t.getCdr().isNull()) // { // Console.WriteLine(); // } }
public override Node eval(Node t, Environment env) { if (t.getCdr() == Nil.getInstance()) { Console.Error.WriteLine("Error: invalid argument"); throw new InvalidOperationException(); } if (t.getCdr().getCar().eval(env) != BoolLit.getInstance(false)) { if (t.getCdr().getCdr() == Nil.getInstance()) { Console.Error.WriteLine("Error: invalid argument"); throw new InvalidOperationException(); } return t.getCdr().getCdr().getCar().eval(env); } if (t.getCdr().getCdr() == Nil.getInstance()) { Console.Error.WriteLine("Error: invalid argument"); throw new InvalidOperationException(); } if (t.getCdr().getCdr().getCdr() == Nil.getInstance()) { return Nil.getInstance(); } return t.getCdr().getCdr().getCdr().getCar().eval(env); }
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 t, Environment e) { if(t.getCdr() is Closure) return e.lookup(t.getCar()).apply(t.getCdr()); else { Node args = t.getCdr(); Node param; if (args.getCar().isSymbol()) param = new Cons(e.lookup((Node)args.GetCar()), Nil.getInstance()); else param = new Cons((Node)args.GetCar(), Nil.getInstance()); Node currentNode = param; args = args.getCdr(); while (args != Nil.getInstance()) { if (args.getCar().isSymbol()) currentNode.setCdr(new Cons(e.lookup(args.getCar()), Nil.getInstance())); else currentNode.setCdr(new Cons(args.getCar(), Nil.getInstance())); currentNode = currentNode.getCdr(); args = args.getCdr(); } return e.lookup(t.getCar()).apply(param); } }
public override void print(Node t, int n, bool p) { // print leading spaces and ' if (n < 0) { n = -(n + 1); } Console.Write(new string(' ', n) + "'"); // if quote a list if (t.getCdr().getCar().isPair()) { // this while is to print following quote as ' to get the sample output while (t.getCdr().getCar().getCar().isSymbol() && ((Ident) t.getCdr().getCar().getCar()).getName().Equals("quote")) { Console.Write("'"); t = t.getCdr().getCar(); } // using the Regular form to print quoted list Special v = new Regular(); v.print(t.getCdr().getCar(), 0, false); // if the caller asks for change line if (n > 0) { Console.WriteLine(); } } // if quote a single element else { t.getCdr().getCar().print(0); } }
//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 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; }
public override void print(Node t, int n, bool p) { // TODO: Implement this function. Console.Write("".PadLeft(n)); Console.WriteLine("(let"); Node bindings = t.getCdr().getCar(); if (bindings.isPair()) { bindings.print(0,false); } else { Console.Error.WriteLine("Syntax error for LET-BINDINGS"); } Console.Write("".PadLeft(n)); Node body = t.getCdr().getCdr(); if (body.isPair()) { body.print(n+4, true); } else { Console.Error.WriteLine("Syntax error for LET-BODY"); } Console.Write("".PadLeft(n)+ ")"); }
public override Node eval(Node t, Environment e) { while (t.getCdr() != Nil.getInstance()) { t = t.getCdr(); if (t.getCar().GetType() != typeof(Cons)) { Console.Error.WriteLine("Error: invalid argument"); throw new InvalidOperationException(); } if (t.getCar().getCar().eval(e) == BoolLit.getInstance(true)) { t = t.getCar(); while(t.getCdr() != Nil.getInstance()) { t = t.getCdr(); if (t.getCdr() == Nil.getInstance()) break; t.getCar().eval(e); } return (Node)t.getCar().eval(e); } else if (t.getCar().getCar().eval(e) != BoolLit.getInstance(false)) { Console.Error.WriteLine("Error: invalid argument"); throw new InvalidOperationException(); } } return Nil.getInstance(); }
public void assign(Node id, Node val) { // TODO: implement this function // create name // if (search for name in either frame or environment) // name = newvalue }
// REMEMBER: If not explictly false, then true. public Node eval(Node t, Environment e) { Node cond = t.getCdr().getCar(); // condition -> cadr(exp) if (cond == null) { Console.Error.WriteLine("Error: Null in Condition"); } Node ifTrue = t.getCdr().getCdr().getCar(); // true -> caddr(exp) if (ifTrue == null) { Console.Error.WriteLine("Error: Null in True/If"); } Node ifFalse = t.getCdr().getCdr().getCdr().getCar(); // false -> cadddr(exp) if (ifFalse == null) { Console.Error.WriteLine("Error: Null in False/Else"); } bool checkTrueResult = checkTrue(t, e); if (checkTrueResult == true) { return ifTrue.eval(ifTrue, e); } else { return ifFalse.eval(ifFalse, e); } }
public override void print(Node t, int n, bool p) { Console.Write("".PadLeft(n)); Console.Write("(lambda"); Node Num2Node = t.getCdr().getCar(); if (Num2Node.isPair()) { Num2Node.print(0,false); } else { Console.Error.Write("Syntax Error with Lambda (Num2Node)"); } Console.Write("".PadLeft(n)); Node Num3Node = t.getCdr().getCdr().getCar(); if( Num3Node.isPair()) { Num3Node.print(n+4, false); } else { Console.Error.Write("Syntax Error with Lambda (Num3Node)"); } Console.Write("".PadLeft(n)); }
private bool CompareTrees(Node Root1, Node Root2) { if (Root1 == null || Root2 == null) return true; if (Root1.Data != Root2.Data) return false; return CompareTrees(Root1.Left, Root2.Left) || CompareTrees(Root1.Right, Root2.Right); }
public void TestSerialize() { var node = new Node(); node.Key = "test"; var payload = this.serializer.Marshal(node); var result = this.serializer.Unmarshal(payload); Assert.AreEqual(node, result); }
public override Node eval(Node t, Environment env) { //arg1: set! //arg2: variable //arg3: value env.assign(t.getCdr().getCar(), t.getCdr().getCdr().getCar().eval(env)); return new StringLit("set done"); }
private static void GetDepthRecurively(Node node, ref int depth) { if (node.Children.Count > 0) { depth += 1; GetDepthRecurively(node.Children.FirstOrDefault(), ref depth); } }
// 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 car = this.fun.getCdr().getCar(); Node cdr = this.fun.getCdr().getCdr(); Environment environment = new Environment(this.env); Closure.pairup(car, args, environment); return Util.begin(cdr, environment); }
public override Node eval(Node t, Environment env) { Node bodystart = t.getCdr().getCar(); Node expression = t.getCdr().getCdr().getCar(); Environment e = new Environment(env); bodystart = evalLetBody(bodystart, e); //Should we be returning bodystart? return expression.eval(e); //Why return e? }
public Node(int key) { this.leftNode = null; this.rightNode = null; this.key = key; }
public override void print(Node t, int n, bool p) { Printer.printRegular(t, n, p); }
public BuiltIn(Node s) { symbol = s; }
public Node eval(Node t, Environment env) { Console.Error.WriteLine("Error in eval BuiltIN"); return(Nil.getInstance()); }
public override Node apply(Node args) { if (args == null) { return(null); } String symbolType = symbol.getName(); Node arg1 = args.getCar(); Node arg2 = args.getCdr(); if (arg1.isNull()) { arg1 = Nil.getInstance(); } if (arg2.isNull()) { arg2 = Nil.getInstance(); } else { arg2 = args.getCdr().getCar(); } if (symbolType.Equals("b+")) { if (arg1.isNumber() & arg2.isNumber()) { return(new IntLit(arg1.getValue() + arg2.getValue())); } else { return(new StringLit("Error: Both arguments must be numbers for b+")); } } if (symbolType.Equals("b-")) { if (arg1.isNumber() & arg2.isNumber()) { return(new IntLit(arg1.getValue() - arg2.getValue())); } else { return(new StringLit("Error: Both arguments must be numbers for b-")); } } if (symbolType.Equals("b*")) { if (arg1.isNumber() & arg2.isNumber()) { return(new IntLit(arg1.getValue() * arg2.getValue())); } else { return(new StringLit("Error: Both arguments must be numbers for b*")); } } if (symbolType.Equals("b/")) { if (arg1.isNumber() & arg2.isNumber()) { return(new IntLit(arg1.getValue() / arg2.getValue())); } else { return(new StringLit("Error: Both arguments must be numbers for b/")); } } if (symbolType.Equals("b=")) { if (arg1.isNumber() & arg2.isNumber()) { return(new BoolLit(arg1.getValue() == arg2.getValue())); } else { return(new StringLit("Error: Both arguments must be numbers for b=")); } } if (symbolType.Equals("b<")) { if (arg1.isNumber() & arg2.isNumber()) { return(new BoolLit(arg1.getValue() < arg2.getValue())); } else { return(new StringLit("Error: Both arguments must be numbers for b<")); } } if (symbolType.Equals("null?")) { return(new BoolLit(arg1.isNull())); } if (symbolType.Equals("number?")) { return(new BoolLit(arg1.isNumber())); } if (symbolType.Equals("pair?")) { return(new BoolLit(arg1.isPair())); } if (symbolType.Equals("symbol?")) { return(new BoolLit(arg1.isSymbol())); } if (symbolType.Equals("procedure?")) { return(new BoolLit(arg1.isProcedure())); } if (symbolType.Equals("car")) { if (arg1.isNull()) { return(arg1); } return(arg1.getCar()); } if (symbolType.Equals("cdr")) { if (arg1.isNull()) { return(arg1); } return(arg1.getCdr()); } if (symbolType.Equals("set-car!")) { arg1.setCar(arg2); return(arg1); } if (symbolType.Equals("set-cdr!")) { arg1.setCdr(arg2); return(arg1); } if (symbolType.Equals("cons")) { return(new Cons(arg1, arg2)); } if (symbolType.Equals("eq?")) { return(new BoolLit(arg1 == arg2)); } if (symbolType.Equals("display")) { return(arg1); } if (symbolType.Equals("write")) { //write calls your pretting printer arg1.print(0); return(new StringLit("")); } if (symbolType.Equals("eval")) { //eval calls your C# eval() function return(eval(arg1, env)); } if (symbolType.Equals("apply")) { //apply calls your C# apply function return(arg1.apply(arg2)); } if (symbolType.Equals("newline")) { return(new StringLit("")); } if (symbolType.Equals("read")) { Scanner io = new Scanner(Console.In); TreeBuilder tree = new TreeBuilder(); Parser parse = new Parser(io, tree); Node theNode = (Node)parse.parseExp(); return(theNode); } if (symbolType.Equals("interaction-environment")) { env.print(0); } else { arg1.print(0); return(Nil.getInstance()); } return(Nil.getInstance()); }
public override Node eval(Environment env, Node args = null) { return(new StringLit(stringVal)); }