From: bdemsky Date: Tue, 31 Oct 2006 06:20:38 +0000 (+0000) Subject: Integer class. This may come in handle for parsing integers (for operations on an... X-Git-Tag: preEdgeChange~796 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=ccdc4b3f3dfca402d65ca5ce9164050db415bd7a;p=IRC.git Integer class. This may come in handle for parsing integers (for operations on an object). --- diff --git a/Robust/src/ClassLibrary/Integer.java b/Robust/src/ClassLibrary/Integer.java new file mode 100644 index 00000000..650474b8 --- /dev/null +++ b/Robust/src/ClassLibrary/Integer.java @@ -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='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; + } +} diff --git a/Robust/src/ClassLibrary/String.java b/Robust/src/ClassLibrary/String.java index 211318c0..1410b82f 100644 --- a/Robust/src/ClassLibrary/String.java +++ b/Robust/src/ClassLibrary/String.java @@ -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(); } diff --git a/Robust/src/ClassLibrary/System.java b/Robust/src/ClassLibrary/System.java index 49b00937..700f2b09 100644 --- a/Robust/src/ClassLibrary/System.java +++ b/Robust/src/ClassLibrary/System.java @@ -5,4 +5,8 @@ public class System { } public static native void printString(String s); + + public static void error() { + System.printString("Error"); + } } diff --git a/Robust/src/IR/Tree/SemanticCheck.java b/Robust/src/IR/Tree/SemanticCheck.java index 0ced5626..00c89724 100644 --- a/Robust/src/IR/Tree/SemanticCheck.java +++ b/Robust/src/IR/Tree/SemanticCheck.java @@ -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()); diff --git a/Robust/src/Main/Main.java b/Robust/src/Main/Main.java index fbf54b89..75487a3f 100644 --- a/Robust/src/Main/Main.java +++ b/Robust/src/Main/Main.java @@ -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"); diff --git a/Robust/src/Tests/DoTests b/Robust/src/Tests/DoTests index 33233e51..052cac2f 100755 --- a/Robust/src/Tests/DoTests +++ b/Robust/src/Tests/DoTests @@ -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 index 00000000..2f3dda56 --- /dev/null +++ b/Robust/src/Tests/IntegerTest.java @@ -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 index 00000000..101e0aec --- /dev/null +++ b/Robust/src/Tests/output/IntegerTest.output.goal @@ -0,0 +1,2 @@ +312 +-34