nand2/07_Operating_System/07_String_Test/Main.jack

84 lines
2.9 KiB
Plaintext
Raw Normal View History

2023-01-11 10:13:09 +00:00
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/12/StringTest/Main.jack
/** Test program for the OS String class. */
class Main {
/** Performs various string manipulations and displays their results. */
function void main() {
var String s;
var String i;
let s = String.new(0); // a zero-capacity string should be supported
do s.dispose();
let s = String.new(6); // capacity 6, make sure that length 5 is displayed
let s = s.appendChar(97);
let s = s.appendChar(98);
let s = s.appendChar(99);
let s = s.appendChar(100);
let s = s.appendChar(101);
do StdIO.printString("new,appendChar: ");
do StdIO.printString(s); // new, appendChar: abcde
do StdIO.println();
let i = String.new(6);
do i.setInt(12345);
do StdIO.printString("setInt: ");
do StdIO.printString(i); // setInt: 12345
do StdIO.println();
let i = String.new(6);
do i.setInt(-32767);
do StdIO.printString("setInt: ");
do StdIO.printString(i); // setInt: -32767
do StdIO.println();
let s="abcde";
do StdIO.printString("length: ");
do StdIO.printInt(s.length()); // length: 5
do StdIO.println();
do StdIO.printString("charAt[2]: ");
do StdIO.printInt(s.charAt(2)); // charAt[2]: 99
do StdIO.println();
do s.setCharAt(2, 45);
do StdIO.printString("setCharAt(2,'-'): ");
do StdIO.printString(s); // setCharAt(2,'-'): ab-de
do StdIO.println();
let s="abcde";
do s.eraseLastChar();
do StdIO.printString("eraseLastChar: ");
do StdIO.printString(s); // eraseLastChar: ab-d
do StdIO.println();
let s = "456";
do StdIO.printString("intValue: ");
do StdIO.printInt(s.intValue()); // intValue: 456
do StdIO.println();
let s = "-32123";
do StdIO.printString("intValue: ");
do StdIO.printInt(s.intValue()); // intValue: -32123
do StdIO.println();
do StdIO.printString("backSpace: ");
do StdIO.printInt(String.backSpace()); // backSpace: 129
do StdIO.println();
do StdIO.printString("doubleQuote: ");
do StdIO.printInt(String.doubleQuote());// doubleQuote: 34
do StdIO.println();
do StdIO.printString("newLine: ");
do StdIO.printInt(String.newLine()); // newLine: 128
do StdIO.println();
return;
}
}