Integer class. This may come in handle for parsing integers (for operations on an...
authorbdemsky <bdemsky>
Tue, 31 Oct 2006 06:20:38 +0000 (06:20 +0000)
committerbdemsky <bdemsky>
Tue, 31 Oct 2006 06:20:38 +0000 (06:20 +0000)
Robust/src/ClassLibrary/Integer.java [new file with mode: 0644]
Robust/src/ClassLibrary/String.java
Robust/src/ClassLibrary/System.java
Robust/src/IR/Tree/SemanticCheck.java
Robust/src/Main/Main.java
Robust/src/Tests/DoTests
Robust/src/Tests/IntegerTest.java [new file with mode: 0644]
Robust/src/Tests/output/IntegerTest.output.goal [new file with mode: 0644]

diff --git a/Robust/src/ClassLibrary/Integer.java b/Robust/src/ClassLibrary/Integer.java
new file mode 100644 (file)
index 0000000..650474b
--- /dev/null
@@ -0,0 +1,54 @@
+public class Integer {
+    private int value;
+
+    public Integer(int value) {
+       this.value=value;
+    }
+
+    public Integer(String str) {
+       value=Integer.parseInt(str, 10);
+    }
+
+    public int intValue() {
+       return value;
+    }
+
+    public static int parseInt(String str) {
+       return Integer.parseInt(str, 10);
+    }
+
+    public static int parseInt(String str, int radix) {
+       int value=0;
+       boolean isNeg=false;
+       int start=0;
+       byte[] chars=str.getBytes();
+       if (chars[0]=='-') {
+           isNeg=true;
+           start=1;
+       }
+       for(int i=start;i<str.length();i++) {
+           byte b=chars[i];
+           int val;
+           if (b>='0'&&b<='9')
+               val=b-'0';
+           else if (b>='a'&&b<='z')
+               val=10+b-'a';
+           else if (b>='A'&&b<='Z')
+               val=10+b-'A';
+           if (val>=radix)
+               System.error();
+           value=value*radix+val;
+       }
+       if (isNeg)
+           value=-value;
+       return value;
+    }
+
+    public String toString() {
+       return String.valueOf(value);
+    }
+
+    public int hashCode() {
+       return value;
+    }
+}
index 211318c0e8d80be2abd0a6694ed634b97c8c06df..1410b82fd91053504ef3777a0b5f33cad4f51807 100644 (file)
@@ -109,6 +109,10 @@ public class String {
        return value[i+offset];
     }
 
+    public String toString() {
+       return this;
+    }
+
     public static String valueOf(Object o) {
        return o.toString();
     }
index 49b00937c5e2603a25d883fddea09487d7cbd84d..700f2b090933be33ea26f55bd392aab9be9804db 100644 (file)
@@ -5,4 +5,8 @@ public class System {
     }
 
     public static native void printString(String s);
+
+    public static void error() {
+       System.printString("Error");
+    }
 }
index 0ced5626f4f5458054e9d54bd4b6f67b728f45ef..00c89724d2e96c0e6f23c8ea4dbc90a6c0db32fe 100644 (file)
@@ -262,7 +262,9 @@ public class SemanticCheck {
            throw new Error("Illegal return appears in Task: "+d.getSymbol());
        MethodDescriptor md=(MethodDescriptor)d;
        if (rn.getReturnExpression()!=null)
-           if (md.getReturnType().isVoid())
+           if (md.getReturnType()==null) 
+               throw new Error("Constructor can't return something.");
+           else if (md.getReturnType().isVoid())
                throw new Error(md+" is void");
            else
                checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
index fbf54b89a8e9f9449bad258b739809b8500adb9b..75487a3f315006a66b1bdb6df308c07343eebae5 100644 (file)
@@ -51,6 +51,7 @@ public class Main {
       readSourceFile(state, ClassLibraryPrefix+"Object.java");
       readSourceFile(state, ClassLibraryPrefix+"System.java");
       readSourceFile(state, ClassLibraryPrefix+"String.java");
+      readSourceFile(state, ClassLibraryPrefix+"Integer.java");
       readSourceFile(state, ClassLibraryPrefix+"StringBuffer.java");
       readSourceFile(state, ClassLibraryPrefix+"FileInputStream.java");
       readSourceFile(state, ClassLibraryPrefix+"FileOutputStream.java");
index 33233e51cf030342b9e89a6c1fc650a6400f4f31..052cac2f4f6aad908e1df6c37aa6d9d8c9c8a84f 100755 (executable)
@@ -15,3 +15,4 @@ dotest CommandLineTest CommandLineTest.java hello hi
 dotest WriteFile WriteFile.java
 dotest ReadFile ReadFile.java
 dotest FileLength FileLength.java
+dotest IntegerTest IntegerTest.java
\ No newline at end of file
diff --git a/Robust/src/Tests/IntegerTest.java b/Robust/src/Tests/IntegerTest.java
new file mode 100644 (file)
index 0000000..2f3dda5
--- /dev/null
@@ -0,0 +1,11 @@
+public class IntegerTest {
+    public static void main(String[] str) {
+       Integer i=new Integer("312");
+       System.printString(i.toString());
+       System.printString("\n");
+       System.printInt(Integer.parseInt("-34"));
+       System.printString("\n");
+    }
+
+
+}
diff --git a/Robust/src/Tests/output/IntegerTest.output.goal b/Robust/src/Tests/output/IntegerTest.output.goal
new file mode 100644 (file)
index 0000000..101e0ae
--- /dev/null
@@ -0,0 +1,2 @@
+312
+-34