Skip to content

Commit f4c8a09

Browse files
committed
Make: refactor number type. Support large numbers.
- Can support numbers up to 100 decimal digits in length. - Still no support for negative numbers or floating point. - Change time-secs to time-ms and remove conditional in perf.mal
1 parent 0f352c3 commit f4c8a09

9 files changed

+478
-99
lines changed

make/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
TESTS = tests/types.mk tests/reader.mk tests/stepA_interop.mk
33

4-
SOURCES_BASE = util.mk readline.mk gmsl.mk types.mk reader.mk printer.mk
4+
SOURCES_BASE = util.mk numbers.mk readline.mk gmsl.mk types.mk \
5+
reader.mk printer.mk
56
SOURCES_LISP = env.mk core.mk stepA_interop.mk
67
SOURCES = $(SOURCES_BASE) $(SOURCES_LISP)
78

make/core.mk

+13-14
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,17 @@ keyword? = $(if $(call _keyword?,$(1)),$(__true),$(__false))
4343
# Number functions
4444
number? = $(if $(call _number?,$(1)),$(__true),$(__false))
4545

46-
number_lt = $(if $(call int_lt,$($(word 1,$(1))_value),$($(word 2,$(1))_value)),$(__true),$(__false))
47-
number_lte = $(if $(call int_lte,$($(word 1,$(1))_value),$($(word 2,$(1))_value)),$(__true),$(__false))
48-
number_gt = $(if $(call int_gt,$($(word 1,$(1))_value),$($(word 2,$(1))_value)),$(__true),$(__false))
49-
number_gte = $(if $(call int_gte,$($(word 1,$(1))_value),$($(word 2,$(1))_value)),$(__true),$(__false))
46+
number_lt = $(if $(call int_lt_encoded,$($(word 1,$(1))_value),$($(word 2,$(1))_value)),$(__true),$(__false))
47+
number_lte = $(if $(call int_lte_encoded,$($(word 1,$(1))_value),$($(word 2,$(1))_value)),$(__true),$(__false))
48+
number_gt = $(if $(call int_gt_encoded,$($(word 1,$(1))_value),$($(word 2,$(1))_value)),$(__true),$(__false))
49+
number_gte = $(if $(call int_gte_encoded,$($(word 1,$(1))_value),$($(word 2,$(1))_value)),$(__true),$(__false))
5050

51-
number_plus = $(call _pnumber,$(call int_plus,$($(word 1,$(1))_value),$($(word 2,$(1))_value)))
52-
number_subtract = $(call _pnumber,$(call int_subtract,$($(word 1,$(1))_value),$($(word 2,$(1))_value)))
53-
number_multiply = $(call _pnumber,$(call int_multiply,$($(word 1,$(1))_value),$($(word 2,$(1))_value)))
54-
number_divide = $(call _pnumber,$(call int_divide,$($(word 1,$(1))_value),$($(word 2,$(1))_value)))
55-
56-
time_secs = $(call _number,$(shell echo $$(( $$(date +%s) % 65536 ))))
51+
number_plus = $(call _pnumber,$(call int_add_encoded,$($(word 1,$(1))_value),$($(word 2,$(1))_value)))
52+
number_subtract = $(call _pnumber,$(call int_sub_encoded,$($(word 1,$(1))_value),$($(word 2,$(1))_value)))
53+
number_multiply = $(call _pnumber,$(call int_mult_encoded,$($(word 1,$(1))_value),$($(word 2,$(1))_value)))
54+
number_divide = $(call _pnumber,$(call int_div_encoded,$($(word 1,$(1))_value),$($(word 2,$(1))_value)))
5755

56+
time_ms = $(call _number,$(shell echo $$(date +%s%3N)))
5857

5958
# String functions
6059

@@ -70,7 +69,7 @@ read_str= $(call READ_STR,$(1))
7069
slurp = $(call _string,$(call _read_file,$(call str_decode,$($(1)_value))))
7170

7271
subs = $(strip \
73-
$(foreach start,$(call gmsl_plus,1,$(call int_decode,$($(word 2,$(1))_value))),\
72+
$(foreach start,$(call int_add,1,$(call int_decode,$($(word 2,$(1))_value))),\
7473
$(foreach end,$(if $(3),$(call int_decode,$($(3)_value)),$(words $($(word 1,$(1))_value))),\
7574
$(call _string,$(wordlist $(start),$(end),$($(word 1,$(1))_value))))))
7675

@@ -133,7 +132,7 @@ concat = $(word 1,$(foreach new_list,$(call _list),$(new_list) $(eval $(new_list
133132

134133
nth = $(strip \
135134
$(if $(call int_lt,$($(word 2,$(1))_value),$(call int_encode,$(call _count,$(word 1,$(1))))),\
136-
$(word $(call gmsl_plus,1,$(call int_decode,$($(word 2,$(1))_value))),$($(word 1,$(1))_value)),\
135+
$(word $(call int_add,1,$(call int_decode,$($(word 2,$(1))_value))),$($(word 1,$(1))_value)),\
137136
$(call _error,nth: index out of range)))
138137

139138
sfirst = $(word 1,$($(1)_value))
@@ -162,7 +161,7 @@ srest = $(word 1,$(foreach new_list,$(call _list),\
162161
# (function object) using the remaining arguments.
163162
sapply = $(call $(word 1,$(1))_value,\
164163
$(strip \
165-
$(wordlist 2,$(call gmsl_subtract,$(words $(1)),1),$(1)) \
164+
$(wordlist 2,$(call int_sub,$(words $(1)),1),$(1)) \
166165
$($(word $(words $(1)),$(1))_value)))
167166

168167
# Map a function object over a list object
@@ -240,7 +239,7 @@ core_ns = type obj_type \
240239
- number_subtract \
241240
* number_multiply \
242241
/ number_divide \
243-
time-secs time_secs \
242+
time-ms time_ms \
244243
\
245244
list _list \
246245
list? list? \

make/gmsl.mk

+5-58
Original file line numberDiff line numberDiff line change
@@ -47,66 +47,13 @@ __mal_gmsl_included := true
4747
#
4848
# ----------------------------------------------------------------------------
4949

50-
51-
# Numbers
52-
__gmsl_sixteen := x x x x x x x x x x x x x x x x
53-
__gmsl_input_int := $(foreach a,$(__gmsl_sixteen), \
54-
$(foreach b,$(__gmsl_sixteen), \
55-
$(foreach c,$(__gmsl_sixteen), \
56-
$(__gmsl_sixteen)))))
57-
58-
int_decode = $(words $1)
59-
int_encode = $(wordlist 1,$1,$(__gmsl_input_int))
60-
61-
__gmsl_int_wrap = $(call int_decode,$(call $1,$(call int_encode,$2),$(call int_encode,$3)))
62-
63-
int_plus = $(strip $1 $2)
64-
int_subtract = $(strip $(if $(call int_gte,$1,$2), \
65-
$(filter-out xx,$(join $1,$2)), \
66-
$(warning Subtraction underflow)))
67-
int_multiply = $(strip $(foreach a,$1,$2))
68-
# _error function must be provided to report/catch division by zero
69-
int_divide = $(strip $(if $2, \
70-
$(if $(call int_gte,$1,$2), \
71-
x $(call int_divide,$(call int_subtract,$1,$2),$2),), \
72-
$(call _error,Division by zero)))
73-
74-
int_max = $(subst xx,x,$(join $1,$2))
75-
int_min = $(subst xx,x,$(filter xx,$(join $1,$2)))
76-
int_gt = $(strip $(filter-out $(words $2),$(words $(call int_max,$1,$2))))
77-
int_gte = $(strip $(call int_gt,$1,$2)$(call int_eq,$1,$2))
78-
int_lt = $(strip $(filter-out $(words $1),$(words $(call int_max,$1,$2))))
79-
int_lte = $(strip $(call int_lt,$1,$2)$(call int_eq,$1,$2))
80-
int_eq = $(strip $(filter $(words $1),$(words $2)))
81-
int_ne = $(strip $(filter-out $(words $1),$(words $2)))
82-
83-
gmsl_plus = $(call __gmsl_int_wrap,int_plus,$1,$2)
84-
gmsl_subtract = $(call __gmsl_int_wrap,int_subtract,$1,$2)
85-
gmsl_multiply = $(call __gmsl_int_wrap,int_multiply,$1,$2)
86-
gmsl_divide = $(call __gmsl_int_wrap,int_divide,$1,$2)
87-
88-
8950
# Strings
9051

91-
__gmsl_characters := A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
92-
__gmsl_characters += a b c d e f g h i j k l m n o p q r s t u v w x y z
93-
__gmsl_characters += 0 1 2 3 4 5 6 7 8 9
94-
__gmsl_characters += ` ~ ! @ \# $$ % ^ & * ( ) - _ = +
95-
__gmsl_characters += { } [ ] \ : ; ' " < > , . / ? |
96-
__syntax_highlight_protect = #"'`
97-
98-
99-
__gmsl_space :=
100-
__gmsl_space +=
101-
102-
gmsl_strlen = $(strip $(eval __temp := $(subst $(__gmsl_space),x,$1)) \
103-
$(foreach a,$(__gmsl_characters),$(eval __temp := $$(subst $$a,x,$(__temp)))) \
104-
$(eval __temp := $(subst x,x ,$(__temp))) \
105-
$(words $(__temp)))
106-
107-
gmsl_merge = $(strip $(if $2, \
108-
$(if $(call _EQ,1,$(words $2)), \
109-
$2,$(firstword $2)$1$(call gmsl_merge,$1,$(wordlist 2,$(words $2),$2)))))
52+
gmsl_characters := A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
53+
gmsl_characters += a b c d e f g h i j k l m n o p q r s t u v w x y z
54+
gmsl_characters += 0 1 2 3 4 5 6 7 8 9
55+
gmsl_characters += ` ~ ! @ \# $$ % ^ & * ( ) - _ = +
56+
gmsl_characters += { } [ ] \ : ; ' " < > , . / ? |
11057
11158
gmsl_pairmap = $(strip \
11259
$(if $2$3,$(call $1,$(word 1,$2),$(word 1,$3)) \

0 commit comments

Comments
 (0)