Commits

Roman Dawydkin  committed 31253a1

More common commands - lists operations.

  • Participants
  • Parent commits f3963ca

Comments (0)

Files changed (2)

File src/toy/CommonCommands.java

 package toy;
 
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 				m.push(list.value);
 			}
 		};
-		new Command(env, "cons") {
-			public void exec(Machine m) {
-				final Cons list = (Cons) m.pop();
-				final Object item = m.pop();
-				m.push(new Cons(item, list));
-			}
-		};
-		new Command(env, "swons") {
-			public void exec(Machine m) {
-				final Object item = m.pop();
-				final Cons list = (Cons) m.pop();
-				m.push(new Cons(item, list));
-			}
-		};
-		new Command(env, "uncons") {
-			public void exec(Machine m) {
-				final Cons list = (Cons) m.pop();
-				m.push(list.value);
-				m.push(list.next);
-			}
-		};
-		new Command(env, "unswons") {
-			public void exec(Machine m) {
-				final Cons list = (Cons) m.pop();
-				m.push(list.next);
-				m.push(list.value);
-			}
-		};
-		new Command(env, "reverse") {
-			public void exec(Machine m) {
-				final Cons list = (Cons) m.pop();
-				m.push(Cons.reverse(list));
-			}
-		};
 		new Command(env, ".") {
 			public void exec(Machine m) {
-				final Object obj = m.peek();
-				m.printer.printLine(obj.toString());
+				final Object obj = m.pop();
+				final String str = (obj == null) ? "[]" : obj.toString();
+				m.printer.printLine(str);
 			}
 		};
 		new Command(env, "s.") {
 				m.push(!obj1.equals(obj2));
 			}
 		};
+		// --------------------------------------------------------------- Lists
+		new LiteralCommand(env, "nil", null);
+		new Command(env, "first") {
+			public void exec(Machine m) {
+				final Cons list = (Cons) m.pop();
+				if (list == null) {
+					m.push(null);
+				} else {
+					m.push(list.value);
+				}
+			}
+		};
+		new Command(env, "second") {
+			public void exec(Machine m) {
+				Cons list = (Cons) m.pop();
+				if (list == null) {
+					m.push(null);
+					return;
+				}
+				list = list.next;
+				if (list == null) {
+					m.push(null);
+					return;
+				}
+				m.push(list.value);
+			}
+		};
+		new Command(env, "third") {
+			public void exec(Machine m) {
+				Cons list = (Cons) m.pop();
+				if (list == null) {
+					m.push(null);
+					return;
+				}
+				list = list.next;
+				if (list == null) {
+					m.push(null);
+					return;
+				}
+				list = list.next;
+				if (list == null) {
+					m.push(null);
+					return;
+				}
+				m.push(list.value);
+			}
+		};
+		new Command(env, "rest") {
+			public void exec(Machine m) {
+				final Cons cons = (Cons) m.pop();
+				m.push(cons.next);
+			}
+		};
+		new Command(env, "cons") {
+			public void exec(Machine m) {
+				final Cons list = (Cons) m.pop();
+				final Object item = m.pop();
+				m.push(new Cons(item, list));
+			}
+		};
+		new Command(env, "swons") {
+			public void exec(Machine m) {
+				final Object item = m.pop();
+				final Cons list = (Cons) m.pop();
+				m.push(new Cons(item, list));
+			}
+		};
+		new Command(env, "uncons") {
+			public void exec(Machine m) {
+				final Cons list = (Cons) m.pop();
+				m.push(list.value);
+				m.push(list.next);
+			}
+		};
+		new Command(env, "unswons") {
+			public void exec(Machine m) {
+				final Cons list = (Cons) m.pop();
+				m.push(list.next);
+				m.push(list.value);
+			}
+		};
+		new Command(env, "at") {
+			public void exec(Machine m) {
+				final int num = ((Number) m.pop()).intValue();
+				final Cons list = (Cons) m.pop();
+				m.push(Cons.at(list, num));
+			}
+		};
+		new Command(env, "of") {
+			public void exec(Machine m) {
+				final Cons list = (Cons) m.pop();
+				final int num = ((Number) m.pop()).intValue();
+				m.push(Cons.at(list, num));
+			}
+		};
+		new Command(env, "drop") {
+			public void exec(Machine m) {
+				int num = ((Number) m.pop()).intValue();
+				Cons list = (Cons) m.pop();
+				for (; (num > 0) && (list != null); num--) {
+					list = list.next;
+				}
+				m.push(list);
+			}
+		};
+		new Command(env, "take") {
+			public void exec(Machine m) {
+				int num = ((Number) m.pop()).intValue();
+				Cons list = (Cons) m.pop();
+				Cons result = null;
+				for (; (num > 0) && (list != null); num--) {
+					result = new Cons(list.value, result);
+					list = list.next;
+				}
+				m.push(Cons.reverse(result));
+			}
+		};
+		new Command(env, "size") {
+			public void exec(Machine m) {
+				Cons list = (Cons) m.pop();
+				int result = 0;
+				while (list != null) {
+					result++;
+					list = list.next;
+				}
+				m.push(BigDecimal.valueOf(result));
+			}
+		};
+		new Command(env, "reverse") {
+			public void exec(Machine m) {
+				final Cons list = (Cons) m.pop();
+				m.push(Cons.reverse(list));
+			}
+		};
 		// -------------------------------------------------- Control structures
 		new Command(env, "ifte") {
 			/* cond then else --> */

File src/toy/Cons.java

 		return result;
 	}
 
+	static Object at(Cons list, int pos) {
+		for (; pos > 0; pos--) {
+			if (list == null) {
+				return null;
+			}
+			list = list.next;
+		}
+		return list.value;
+	}
+
 	// ================================================================== Fields
 
 	final Object value;