From c94edbe007910755087e71cbb1a6a349d75e9b85 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Tue, 3 Jan 2023 02:03:54 -0500 Subject: Basic addition and subtraction --- tnslc/util.tnsl | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'tnslc/util.tnsl') diff --git a/tnslc/util.tnsl b/tnslc/util.tnsl index 156cbb4..d4a7b4b 100644 --- a/tnslc/util.tnsl +++ b/tnslc/util.tnsl @@ -69,9 +69,10 @@ /; string_from_int(int i) [{}uint8] ;{}uint8 c = "" + ;bool n = false /; if (i < 0) - ;c = "-" + ;n = true ;i = -i ;/ @@ -82,17 +83,28 @@ ;c.append(digit_to_char(i)) ;/ + /;if (n) + ;c.append('-') + ;/ + ;return reverse_string(c) ;/ /; int_from_string ({}uint8 str) [int] ;bool inv = str{0} == '-' ;int out = 0 + ;int fac = 10 + ;int i = 0 - /; loop () - + /; if (inv) + ;i++ + ;/ + + /; loop (i < len str) [i++] + ;out = out * fac + ;out = out + str{i} - '0' ;/ - + /; if (inv) ;out = -out ;/ -- cgit v1.2.3