Skip to content

Fix Big integers, remove hex and octal support. Fix number parsing #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/reflection.ys
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ classes:
- java.lang.String
- java.lang.System
- java.lang.Thread
- java.math.BigInteger

--- !YS-v0:

Expand Down
9 changes: 1 addition & 8 deletions core/src/yamlscript/ast.clj
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,7 @@

(defn Chr [s] {:Chr (symbol s)})

(defn Int [s]
{:Int (cond
(re-find #"^0x" s)
(Long/parseLong (subs s 2) 16)
(re-find #"^0o" s)
(Long/parseLong (subs s 2) 8)
:else
(parse-long s))})
(defn Int [s] {:Int (bigint s)})

(defn Flt [s] {:Flt (parse-double s)})

Expand Down
11 changes: 2 additions & 9 deletions core/src/yamlscript/re.clj
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,8 @@
(def fnum (re #"(?:$inum\.\d+(?:e$inum)?)")) ; Floating point literal token
(def xnum (re #"(?:$fnum|$inum)")) ; Numeric literal token
; Maybe Number token
(def mnum (re #"(?x)
(?: $xnum
(?:[-+/*%_] \d* |
\.\d+ |
\.\w+ [\?\!]? (?=[^\(\w\?\!] | $)
)*
)
"))

(def mnum (re #"(?:$xnum[/0-9A-Za-z\.]*)")); *anything* that starts with a number, could be a number
; We check later with `yamlscript.ysreader/is-bad-number?`
(def xsym #"(?:[=!]~~?)") ; Special operator token

;; Operator symbol token
Expand Down
2 changes: 1 addition & 1 deletion core/src/yamlscript/runtime.clj
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
[sci/file file
ARGS (vec
(map #(cond
(re-matches re/inum %1) (parse-long %1)
(re-matches re/inum %1) (bigint %1)
(re-matches re/fnum %1) (parse-double %1)
(re-matches re/keyw %1) (keyword (subs %1 1))
:else %1)
Expand Down
4 changes: 2 additions & 2 deletions core/src/yamlscript/ysreader.clj
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@
(let [[[key val]] (seq %1)
val (str val)]
(if (and (= :Sym key) (re-matches #"_\d+" val))
(let [n (parse-long (subs val 1))]
(let [n (bigint (subs val 1))]
(swap! args assoc n true)
(swap! maxn max n))
%1))
Expand Down Expand Up @@ -398,7 +398,7 @@
(= "true" token) [(Bln token) tokens]
(= "false" token) [(Bln token) tokens]
(is-narg? token) (let [n (subs token 1)
n (parse-long n)
n (bigint n)
_ (when (or (<= n 0) (> n 20))
(die "Invalid numbered argument: " token))
n (str "_" n)]
Expand Down
2 changes: 1 addition & 1 deletion core/src/ys/std.clj
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@
number? x
string? (if (re-find #"\." x)
(parse-double x)
(parse-long x))
(bigint x))
nil? (util/die "Can't convert a nil value to a number")
seqable? (count x)
char? (int x)
Expand Down
12 changes: 9 additions & 3 deletions core/test/compiler-stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1553,10 +1553,16 @@
clojure: |


- name: Octal and hex literals not loading correctly
- name: Octal literals aren't supported
yamlscript: |
!YS-v0:
- 0o12
error: |
Invalid number: 0x12

- name: Hex literals aren't supported
yamlscript: |
!YS-v0:
- 0x12
build: |
{:Vec [{:Int 10} {:Int 18}]}
error: |
Invalid number: 0x12
6 changes: 2 additions & 4 deletions core/test/resolver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- >
folded
block scalar
- [41, -42, +43, +0, -0, 0o1337, 0xcafebabe]
- [41, -42, +43, +0, -0]
- [.1, -.2, +.3, 4.00, -5.99]
- [.1e-00, -.2e+3, +.3e11, 4.00E-4, -5.99E99]
- [true, True, TRUE]
Expand All @@ -35,9 +35,7 @@
{:int "-42"}
{:int "+43"}
{:int "+0"}
{:int "-0"}
{:int "0o1337"}
{:int "0xcafebabe"}]}
{:int "-0"}]}
{:seq
[{:flt ".1"}
{:flt "-.2"}
Expand Down
2 changes: 1 addition & 1 deletion yamltest/src/yamltest/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
(let [kw (keyword (subs kw 1))]
(when (contains? short-keys kw) kw))
(when (re-matches #"\d+" opt)
(parse-long opt))))
(bigint opt))))
opts))
(repeat true)))))

Expand Down
2 changes: 1 addition & 1 deletion ys/share/ys-0.bash
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ $program
(let [argv (vec
(map
#(if (re-matches #"\d+" %)
(parse-long %) %)
(bigint %) %)
args))]
(apply main argv)))
EOF
Expand Down