Changeset 3708


Ignore:
Timestamp:
Apr 16, 2008, 6:42:43 PM (13 years ago)
Author:
yura
Message:
  • Use System.intValue and etc functions.
Location:
to-imperative/trunk/java/java/refal/plus
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/java/java/refal/plus/Arithm.java

    r3423 r3708  
    1 /*
    2  * $Id$
    3  *
    4  * vim: set syntax=java expandtab shiftwidth=4 tabstop=4:
    5  */
     1// $Id$
    62
    73package refal.plus;
     
    139import org.refal.plus.RefalFormat;
    1410import org.refal.plus.Result;
     11import org.refal.plus.Symbol;
    1512
    1613public final class Arithm {
     
    1815    private Arithm () {}
    1916
    20     @RefalFormat("$func Add s.Int1 s.Int2 = s.Int;")
    21     public static void Add (Expr s1, Expr s2, Result res) throws RefalException {
    22         assert s1.length() == 1;
    23         assert s2.length() == 1;
     17    @RefalFormat("$func Add s.int1 s.int2 = s.int;")
     18    public static void Add (Expr int1, Expr int2, Result res) throws RefalException {
    2419        try {
    25             res.assign(((BigInteger) s1.at(0)).add((BigInteger) s2.at(0)));
     20            res.assign(Symbol.getBigInteger(int1).add(Symbol.getBigInteger(int2)));
    2621        } catch (ClassCastException e) {
    2722            throw new RefalException("Arithm", "Add", "Invalid argument");
     
    2924    }
    3025
    31     @RefalFormat("$func Sub s.Int1 s.Int2 = s.Int;")
    32     public static void Sub (Expr s1, Expr s2, Result res) throws RefalException {
    33         assert s1.length() == 1;
    34         assert s2.length() == 1;
     26    @RefalFormat("$func Sub s.int1 s.int2 = s.int;")
     27    public static void Sub (Expr int1, Expr int2, Result res) throws RefalException {
    3528        try {
    36             res.assign(((BigInteger) s1.at(0)).subtract((BigInteger) s2.at(0)));
     29            res.assign(Symbol.getBigInteger(int1).subtract(Symbol.getBigInteger(int2)));
    3730        } catch (ClassCastException e) {
    3831            throw new RefalException("Arithm", "Sub", "Invalid argument");
     
    4033    }
    4134
    42     @RefalFormat("$func Mult s.Int1 s.Int2 = s.Int;")
    43     public static void Mult (Expr s1, Expr s2, Result res) throws RefalException {
    44         assert s1.length() == 1;
    45         assert s2.length() == 1;
     35    @RefalFormat("$func Mult s.int1 s.int2 = s.int;")
     36    public static void Mult (Expr int1, Expr int2, Result res) throws RefalException {
    4637        try {
    47             res.assign(((BigInteger) s1.at(0)).multiply((BigInteger) s2.at(0)));
     38            res.assign(Symbol.getBigInteger(int1).multiply(Symbol.getBigInteger(int2)));
    4839        } catch (ClassCastException e) {
    4940            throw new RefalException("Arithm", "Mult", "Invalid argument");
     
    5142    }
    5243
    53     @RefalFormat("$func Div s.Int1 s.Int2 = s.Quo;")
    54     public static void Div (Expr s1, Expr s2, Result res) throws RefalException {
    55         assert s1.length() == 1;
    56         assert s2.length() == 1;
     44    @RefalFormat("$func Div s.int1 s.int2 = s.quo;")
     45    public static void Div (Expr int1, Expr int2, Result quo) throws RefalException {
    5746        try {
    58             res.assign(((BigInteger) s1.at(0)).divide((BigInteger) s2.at(0)));
     47            quo.assign(Symbol.getBigInteger(int1).divide(Symbol.getBigInteger(int2)));
     48        } catch (ArithmeticException e) {
     49            throw new RefalException("Arithm", "Div", "Divide by zero");
    5950        } catch (ClassCastException e) {
    6051            throw new RefalException("Arithm", "Div", "Invalid argument");
    61         } catch (ArithmeticException e) {
    62             throw new RefalException("Arithm", "Div", "Divide by zero");
    6352        }
    6453    }
    6554
    66     @RefalFormat("$func Rem s.Int1 s.Int2 = s.Rem;")
    67     public static void Rem (Expr s1, Expr s2, Result res) throws RefalException {
    68         assert s1.length() == 1;
    69         assert s2.length() == 1;
     55    @RefalFormat("$func Rem s.int1 s.int2 = s.rem;")
     56    public static void Rem (Expr int1, Expr int2, Result rem) throws RefalException {
    7057        try {
    71             res.assign(((BigInteger) s1.at(0)).remainder((BigInteger) s2.at(0)));
     58            rem.assign(Symbol.getBigInteger(int1).remainder(Symbol.getBigInteger(int2)));
     59        } catch (ArithmeticException e) {
     60            throw new RefalException("Arithm", "Div", "Divide by zero");
    7261        } catch (ClassCastException e) {
    7362            throw new RefalException("Arithm", "Rem", "Invalid argument");
    74         } catch (ArithmeticException e) {
    75             throw new RefalException("Arithm", "Rem", "Divide by zero");
    7663        }
    7764    }
    7865
    79     @RefalFormat("$func DivRem s.Int1 s.Int2 = s.Quo s.Rem;")
    80     public static void DivRem (Expr s1, Expr s2, Result res1, Result res2) throws RefalException {
    81         assert s1.length() == 1;
    82         assert s2.length() == 1;
     66    @RefalFormat("$func DivRem s.int1 s.int2 = s.quo s.rem;")
     67    public static void DivRem (Expr int1, Expr int2, Result quo, Result rem) throws RefalException {
    8368        try {
    84             BigInteger[] a = ((BigInteger) s1.at(0)).divideAndRemainder((BigInteger) s2.at(0));
    85             res1.assign(a[0]);
    86             res2.assign(a[1]);
     69            BigInteger[] a = Symbol.getBigInteger(int1).divideAndRemainder(Symbol.getBigInteger(int2));
     70            quo.assign(a[0]);
     71            rem.assign(a[1]);
     72        } catch (ArithmeticException e) {
     73            throw new RefalException("Arithm", "DivRem", "Divide by zero");
    8774        } catch (ClassCastException e) {
    8875            throw new RefalException("Arithm", "DivRem", "Invalid argument");
    89         } catch (ArithmeticException e) {
    90             throw new RefalException("Arithm", "DivRem", "Divide by zero");
    9176        }
    9277    }
    9378
    94     @RefalFormat("$func Gcd s.Int1 s.Int2 = s.Gcd;")
    95     public static void Gcd (Expr s1, Expr s2, Result res) throws RefalException {
    96         assert s1.length() == 1;
    97         assert s2.length() == 1;
     79    @RefalFormat("$func Gcd s.int1 s.int2 = s.gcd;")
     80    public static void Gcd (Expr int1, Expr int2, Result gcd) throws RefalException {
    9881        try {
    99             BigInteger d = ((BigInteger) s1.at(0)).gcd((BigInteger) s2.at(0));
     82            BigInteger d = Symbol.getBigInteger(int1).gcd(Symbol.getBigInteger(int2));
    10083            if (d.equals(BigInteger.ZERO))
    10184                throw new RefalException("Arithm", "Gcd", "Zero arguments");
    102             res.assign(d);
     85            gcd.assign(d);
    10386        } catch (ClassCastException e) {
    10487            throw new RefalException("Arithm", "Gcd", "Invalid argument");
     
    10689    }
    10790
    108     @RefalFormat("$func Abs s.Int = s.Int;")
    109     public static void Abs (Expr s, Result res) throws RefalException {
    110         assert s.length() == 1;
     91    @RefalFormat("$func Abs s.int = s.int;")
     92    public static void Abs (Expr int1, Result res) throws RefalException {
    11193        try {
    112             res.assign(((BigInteger) s.at(0)).abs());
     94            res.assign((Symbol.getBigInteger(int1)).abs());
    11395        } catch (ClassCastException e) {
    11496            throw new RefalException("Arithm", "Gcd", "Invalid argument");
     
    11698    }
    11799
    118     @RefalFormat("$func Max s.Int1 s.Int2 = s.Max;")
    119     public static void Max (Expr s1, Expr s2, Result res) throws RefalException {
    120         assert s1.length() == 1;
    121         assert s2.length() == 1;
     100    @RefalFormat("$func Max s.int1 s.int2 = s.max;")
     101    public static void Max (Expr int1, Expr int2, Result max) throws RefalException {
    122102        try {
    123             res.assign(((BigInteger) s1.at(0)).max((BigInteger) s2.at(0)));
     103            max.assign(Symbol.getBigInteger(int1).max(Symbol.getBigInteger(int2)));
    124104        } catch (ClassCastException e) {
    125105            throw new RefalException("Arithm", "Max", "Invalid argument");
     
    127107    }
    128108
    129     @RefalFormat("$func Min s.Int1 s.Int2 = s.Min;")
    130     public static void Min (Expr s1, Expr s2, Result res) throws RefalException {
    131         assert s1.length() == 1;
    132         assert s2.length() == 1;
     109    @RefalFormat("$func Min s.int1 s.int2 = s.min;")
     110    public static void Min (Expr int1, Expr int2, Result min) throws RefalException {
    133111        try {
    134             res.assign(((BigInteger) s1.at(0)).min((BigInteger) s2.at(0)));
     112            min.assign(Symbol.getBigInteger(int1).min(Symbol.getBigInteger(int2)));
    135113        } catch (ClassCastException e) {
    136114            throw new RefalException("Arithm", "Min", "Invalid argument");
  • to-imperative/trunk/java/java/refal/plus/Bit.java

    r3428 r3708  
    1 /*
    2  * $Id$
    3  *
    4  * vim: set syntax=java expandtab shiftwidth=4 tabstop=4:
    5  */
     1// $Id$
    62
    73package refal.plus;
    84
    9 import java.lang.System;
    105import java.math.BigInteger;
    116
     
    149import org.refal.plus.RefalFormat;
    1510import org.refal.plus.Result;
    16 import org.refal.plus.Util;
     11import org.refal.plus.Symbol;
     12import org.refal.plus.Util.ValueOutOfBoundsException;
    1713
    1814public final class Bit {
     
    2016    private Bit () {}
    2117
    22     @RefalFormat("$func BitAnd s.Int1 s.Int2 = s.Int;")
    23     public static void BitAnd (Expr s1, Expr s2, Result res) throws RefalException {
    24         assert s1.length() == 1;
    25         assert s2.length() == 1;
     18    @RefalFormat("$func BitAnd s.int1 s.int2 = s.int;")
     19    public static void BitAnd (Expr int1, Expr int2, Result res) throws RefalException {
    2620        try {
    27             res.assign(((BigInteger) s1.at(0)).and((BigInteger) s2.at(0)));
    28         } catch (ClassCastException e) {
     21            res.assign(Symbol.getBigInteger(int1).and(Symbol.getBigInteger(int2)));
     22        } catch (ClassCastException _) {
    2923            throw new RefalException("Bit", "BitAnd", "Invalid argument");
    3024        }
    3125    }
    3226
    33     @RefalFormat("$func BitOr s.Int1 s.Int2 = s.Int;")
    34     public static void BitOr (Expr s1, Expr s2, Result res) throws RefalException {
    35         assert s1.length() == 1;
    36         assert s2.length() == 1;
     27    @RefalFormat("$func BitOr s.int1 s.int2 = s.int;")
     28    public static void BitOr (Expr int1, Expr int2, Result res) throws RefalException {
    3729        try {
    38             res.assign(((BigInteger) s1.at(0)).or((BigInteger) s2.at(0)));
    39         } catch (ClassCastException e) {
     30            res.assign(Symbol.getBigInteger(int1).or(Symbol.getBigInteger(int2)));
     31        } catch (ClassCastException _) {
    4032            throw new RefalException("Bit", "BitOr", "Invalid argument");
    4133        }
    4234    }
    4335
    44     @RefalFormat("$func BitXor s.Int1 s.Int2 = s.Int;")
    45     public static void BitXor (Expr s1, Expr s2, Result res) throws RefalException {
    46         assert s1.length() == 1;
    47         assert s2.length() == 1;
     36    @RefalFormat("$func BitXor s.int1 s.int2 = s.int;")
     37    public static void BitXor (Expr int1, Expr int2, Result res) throws RefalException {
    4838        try {
    49             res.assign(((BigInteger) s1.at(0)).xor((BigInteger) s2.at(0)));
    50         } catch (ClassCastException e) {
     39            res.assign(Symbol.getBigInteger(int1).xor(Symbol.getBigInteger(int2)));
     40        } catch (ClassCastException _) {
    5141            throw new RefalException("Bit", "BitXor", "Invalid argument");
    5242        }
    5343    }
    5444
    55     @RefalFormat("$func BitNot s.Int = s.Int;")
    56     public static void BitNot (Expr s1, Result res) throws RefalException {
    57         assert s1.length() == 1;
     45    @RefalFormat("$func BitNot s.int = s.int;")
     46    public static void BitNot (Expr int1, Result res) throws RefalException {
    5847        try {
    59             res.assign(((BigInteger) s1.at(0)).not());
    60         } catch (ClassCastException e) {
     48            res.assign(Symbol.getBigInteger(int1).not());
     49        } catch (ClassCastException _) {
    6150            throw new RefalException("Bit", "BitNot", "Invalid argument");
    6251        }
    6352    }
    6453
    65     @RefalFormat("$func BitLeft s.Int s.Shift = s.Int;")
    66     public static void BitLeft (Expr s1, Expr s2, Result res) throws RefalException {
    67         assert s1.length() == 1;
    68         assert s2.length() == 1;
     54    @RefalFormat("$func BitLeft s.int s.shift = s.int;")
     55    public static void BitLeft (Expr int1, Expr shift, Result res) throws RefalException {
    6956        try {
    70             int n = Util.intValue((BigInteger) s2.at(0));
    71             res.assign(((BigInteger) s1.at(0)).shiftLeft(n));
    72         } catch (Exception e) {
     57            res.assign(Symbol.getBigInteger(int1).shiftLeft(Symbol.intValue(shift)));
     58        } catch (ValueOutOfBoundsException _) {
     59            throw new RefalException("Bit", "BitLeft", "Invalid argument");
     60        } catch (ClassCastException _) {
    7361            throw new RefalException("Bit", "BitLeft", "Invalid argument");
    7462        }
    7563    }
    7664
    77     @RefalFormat("$func BitRight s.Int s.Shift = s.Int;")
    78     public static void BitRight (Expr s1, Expr s2, Result res) throws RefalException {
    79         assert s1.length() == 1;
    80         assert s2.length() == 1;
     65    @RefalFormat("$func BitRight s.int s.shift = s.int;")
     66    public static void BitRight (Expr int1, Expr shift, Result res) throws RefalException {
    8167        try {
    82             int n = Util.intValue((BigInteger) s2.at(0));
    83             res.assign(((BigInteger) s1.at(0)).shiftRight(n));
    84         } catch (Exception e) {
     68            res.assign(Symbol.getBigInteger(int1).shiftRight(Symbol.intValue(shift)));
     69        } catch (ValueOutOfBoundsException _) {
     70            throw new RefalException("Bit", "BitRight", "Invalid argument");
     71        } catch (ClassCastException _) {
    8572            throw new RefalException("Bit", "BitRight", "Invalid argument");
    8673        }
    8774    }
    8875
    89     @RefalFormat("$func? BitTest s.Int s.Pos = ;")
    90     public static boolean BitTest (Expr s1, Expr s2) throws RefalException {
    91         assert s1.length() == 1;
    92         assert s2.length() == 1;
     76    @RefalFormat("$func? BitTest s.int s.pos = ;")
     77    public static boolean BitTest (Expr int1, Expr pos) throws RefalException {
    9378        try {
    94             int n = Util.intValue((BigInteger) s2.at(0));
    95             return ((BigInteger) s1.at(0)).testBit(n);
    96         } catch (Exception e) {
     79            return Symbol.getBigInteger(int1).testBit(Symbol.uintValue(pos));
     80        } catch (ValueOutOfBoundsException _) {
     81            throw new RefalException("Bit", "BitLeft", "Invalid argument");
     82        } catch (ClassCastException _) {
    9783            throw new RefalException("Bit", "BitTest", "Invalid argument");
    9884        }
    9985    }
    10086
    101     @RefalFormat("$func BitSet s.Int s.Pos = s.Int;")
    102     public static void BitSet (Expr s1, Expr s2, Result res) throws RefalException {
    103         assert s1.length() == 1;
    104         assert s2.length() == 1;
     87    @RefalFormat("$func BitSet s.int s.pos = s.int;")
     88    public static void BitSet (Expr int1, Expr pos, Result res) throws RefalException {
    10589        try {
    106             int n = Util.intValue((BigInteger) s2.at(0));
    107             res.assign(((BigInteger) s1.at(0)).setBit(n));
    108         } catch (Exception e) {
     90            res.assign(Symbol.getBigInteger(int1).setBit(Symbol.uintValue(pos)));
     91        } catch (ValueOutOfBoundsException _) {
     92            throw new RefalException("Bit", "BitSet", "Invalid argument");
     93        } catch (ClassCastException _) {
    10994            throw new RefalException("Bit", "BitSet", "Invalid argument");
    11095        }
    11196    }
    11297
    113     @RefalFormat("$func BitClear s.Int s.Pos = s.Int;")
    114     public static void BitClear (Expr s1, Expr s2, Result res) throws RefalException {
    115         assert s1.length() == 1;
    116         assert s2.length() == 1;
     98    @RefalFormat("$func BitClear s.int s.pos = s.int;")
     99    public static void BitClear (Expr int1, Expr pos, Result res) throws RefalException {
    117100        try {
    118             int n = Util.intValue((BigInteger) s2.at(0));
    119             res.assign(((BigInteger) s1.at(0)).clearBit(n));
    120         } catch (Exception e) {
     101            res.assign(Symbol.getBigInteger(int1).clearBit(Symbol.uintValue(pos)));
     102        } catch (ValueOutOfBoundsException _) {
     103            throw new RefalException("Bit", "BitClear", "Invalid argument");
     104        } catch (ClassCastException _) {
    121105            throw new RefalException("Bit", "BitClear", "Invalid argument");
    122106        }
    123107    }
    124108
    125     @RefalFormat("$func BitLength s.Int = s.Len;")
    126     public static void BitLength (Expr s1, Result res) throws RefalException {
    127         assert s1.length() == 1;
     109    @RefalFormat("$func BitLength s.int = s.len;")
     110    public static void BitLength (Expr int1, Result len) throws RefalException {
    128111        try {
    129             res.assign(BigInteger.valueOf(((BigInteger) s1.at(0)).bitLength()));
    130         } catch (ClassCastException e) {
     112            len.assign(BigInteger.valueOf(Symbol.getBigInteger(int1).bitLength()));
     113        } catch (ClassCastException _) {
    131114            throw new RefalException("Bit", "BitLength", "Invalid argument");
    132115        }
    133116    }
    134117
    135     @RefalFormat("$func BitPrint s.Int = ;")
    136     public static void BitPrint (Expr s1) throws RefalException {
    137         assert s1.length() == 1;
     118    @RefalFormat("$func BitToHex s.int = s.hex;")
     119    public static void BitToHex (Expr int1, Result hex) throws RefalException {
    138120        try {
    139             BigInteger i = (BigInteger) s1.at(0);
    140             if (i.compareTo(BigInteger.ZERO) >= 0)
    141                 System.out.print("0x" + i.toString(16));
    142             else
    143                 System.out.print("-0x" + i.abs().toString(16));
    144         } catch (ClassCastException e) {
     121            BigInteger i = Symbol.getBigInteger(int1);
     122            hex.assign((i.compareTo(BigInteger.ZERO) >= 0 ? "0x" + i.toString(16) : "-0x" + i.abs().toString(16)));
     123        } catch (ClassCastException _) {
    145124            throw new RefalException("Bit", "BitPrint", "Invalid argument");
    146125        }
Note: See TracChangeset for help on using the changeset viewer.