From b454b6f32f95ec145d4aa2960371e138720c971a Mon Sep 17 00:00:00 2001 From: zeroflag Date: Sat, 10 Jul 2021 20:38:36 +0200 Subject: [PATCH] binary literals --- src/main/java/com/vectron/fcl/types/Num.java | 2 ++ src/test/java/com/vectron/fcl/FclTest.java | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/src/main/java/com/vectron/fcl/types/Num.java b/src/main/java/com/vectron/fcl/types/Num.java index 930bf6e..20a70c8 100644 --- a/src/main/java/com/vectron/fcl/types/Num.java +++ b/src/main/java/com/vectron/fcl/types/Num.java @@ -32,6 +32,8 @@ public class Num implements Obj, LogicOperand, ArithmeticOperand { try { if (str.startsWith("0x")) return new Num(Long.parseLong(str.substring(2), 16)); + else if (str.startsWith("0b")) + return new Num(Long.parseLong(str.substring(2), 2)); return new Num(Long.parseLong(str)); } catch (NumberFormatException e1) { try { diff --git a/src/test/java/com/vectron/fcl/FclTest.java b/src/test/java/com/vectron/fcl/FclTest.java index 4642459..db7151b 100644 --- a/src/test/java/com/vectron/fcl/FclTest.java +++ b/src/test/java/com/vectron/fcl/FclTest.java @@ -91,7 +91,14 @@ public class FclTest { assertEquals(asList(1l, 85l), evalGetStack("2 8 pow 3 /mod")); assertEquals(3, evalPop("10 3 /mod nip").intValue()); assertEquals(1, evalPop("10 3 /mod drop").intValue()); + } + @Test + public void testNumberLiterals() { + assertEquals(12345, evalPop("12345").longValue()); + assertEquals(255, evalPop("0xFF").longValue()); + assertEquals(131, evalPop("0b10000011").longValue()); + assertEquals(-1.44, evalPop("-1.44").doubleValue(), 0.01); } @Test