interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / determine-if-a-string-is-numeric / determine-if-a-string-is-numeric.om

112 lines · 1964 bytes Raw Blame History
1note Rosetta Code task "Determine if a string is numeric": https://rosettacode.org/wiki/Determine_if_a_string_is_numeric
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text tq "\""
6text tyes "\" numeric\n"
7text tno "\" not numeric\n"
8text s0 "0"
9text s1 "-12"
10text s2 "3.14"
11text s3 "+1e5"
12text s4 "1e-3"
13text s5 ".5"
14text s6 "5."
15text s7 "abc"
16text s9 "12a"
17text s10 "-"
18text s11 "1.2.3"
19text s12 "e5"
20text s13 "1e"
21text s14 "--1"
22text s15 "6.02E+23"
23note emit writes every byte of a to stdout; a short write is retried, an error exits 1
24fn emit a view uchar * to void
25o = ulong 0
26while lt o len a bound 4096
27k = write 1 a o sub len a o
28if le k 0
29exit 1
30end
31o = add o ulong k
32end
33end
34note digits returns the offset after a run of decimal digits starting at i
35fn digits a view uchar * i ulong to ulong
36j = i
37while lt j len a bound 4096
38c = get a j
39if or lt c 48 gt c 57
40ret j
41end
42j = add j 1
43end
44ret j
45end
46note sign returns the offset after an optional + or - at i
47fn sign a view uchar * i ulong to ulong
48if lt i len a
49c = get a i
50if or eq c 43 eq c 45
51ret add i 1
52end
53end
54ret i
55end
56note numeric accepts [+-] then digits with an optional fraction (at least one digit in all), then an
57note optional exponent e or E, [+-], at least one digit; nothing else, no spaces
58fn numeric a view uchar * to bool
59i = sign a 0
60j = digits a i
61m = sub j i
62if lt j len a
63if eq get a j 46
64i = add j 1
65j = digits a i
66m = add m sub j i
67end
68end
69if eq m 0
70ret false
71end
72if lt j len a
73c = get a j
74if or eq c 101 eq c 69
75i = sign a add j 1
76j = digits a i
77if eq j i
78ret false
79end
80end
81end
82ret eq j len a
83end
84fn show a view uchar * to void
85emit tq
86emit a
87if numeric a
88emit tyes
89else
90emit tno
91end
92end
93fn main to int
94show s0
95show s1
96show s2
97show s3
98show s4
99show s5
100show s6
101show s7
102note the empty string: an empty text literal would read as unsized at this pin, so slice one
103show part s7 0 0
104show s9
105show s10
106show s11
107show s12
108show s13
109show s14
110show s15
111ret 0
112end