A+B: Difference between revisions

2,279 bytes added ,  3 years ago
Added Order implementation of a program to add signed numbers A and B
(Added Order implementation of a program to add signed numbers A and B)
Line 3,406:
a = 5 + 4;
echo (a);
</lang>
 
=={{header|Order}}==
Since Order is implemented in the C pre-processor, which cannot express arithmetic operations, this program has been built around Order's standard library simulated natural number arithmetic implementation, extended to handle a sign bit.
 
To run this Order program, you must define the macros '''A''' and '''B''' to values of the form '''8int(SIGN, 8nat(VALUE))''', where SIGN is 1/0 to represent signed/unsigned numbers, and VALUES is any comma-separated list of decimal digits. For example, to evaluate the sum of A=-150, B=275, define A to be '''8int(1, 8nat(1,5,0))''' and B to be '''8int(0, 8nat(2,7,5))'''.
<lang order>
#define ORDER_PP_DEF_1int_is_positive \
ORDER_PP_FN(8fn(8X, 8is_0(8tuple_at_0(8X))))
 
#define ORDER_PP_DEF_1int_get_unsigned \
ORDER_PP_FN(8fn(8X, 8tuple_at_1(8X)))
 
#define ORDER_PP_DEF_1int_add_impl \
ORDER_PP_FN(8fn(8A, 8B, 8S, 8int(8S, 8add(8A, 8B))))
 
#define ORDER_PP_DEF_1int_sub_impl \
ORDER_PP_FN(8fn(8A, 8B, \
8if(8greater(8A, 8B), \
8int(0, 8sub(8A, 8B)), \
8int(1, 8sub(8B, 8A)))))
 
#define ORDER_PP_DEF_8int_add \
ORDER_PP_FN(8fn(8A, 8B, \
8cond((8and(1int_is_positive(8A), 1int_is_positive(8B)), \
1int_add_impl(1int_get_unsigned(8A), 1int_get_unsigned(8B), 0)) \
(8and(1int_is_positive(8A), 8not(1int_is_positive(8B))), \
1int_sub_impl(1int_get_unsigned(8A), 1int_get_unsigned(8B))) \
(8and(8not(1int_is_positive(8A)), 1int_is_positive(8B)), \
1int_sub_impl(1int_get_unsigned(8B), 1int_get_unsigned(8A))) \
(8and(8not(1int_is_positive(8A)), 8not(1int_is_positive(8B))), \
1int_add_impl(1int_get_unsigned(8A), 1int_get_unsigned(8B), 1)))))
 
#define ORDER_PP_DEF_8int_to_lit \
ORDER_PP_FN(8fn(8X, \
8if(1int_is_positive(8X), \
8to_lit(1int_get_unsigned(8X)), \
8adjacent(8(-), 8to_lit(1int_get_unsigned(8X))))))
 
#define ORDER_PP_DEF_8int \
ORDER_PP_FN(8fn(8S, 8N, 8pair(8S, 8N)))
 
ORDER_PP(8int_to_lit(8int_add(A, B)))
</lang>
 
Anonymous user