diff options
author | Adrian Thurston <thurston@complang.org> | 2015-03-14 15:07:54 -0400 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2015-03-14 15:07:54 -0400 |
commit | 71548b81cd65687f53ecb2abcf14807d488d7af5 (patch) | |
tree | 7cfade7a4cd01a201fbd6875e273ffb8b232a10a | |
parent | b2a6b17f584b4bfcedbb405a65d020506cd82458 (diff) | |
download | colm-71548b81cd65687f53ecb2abcf14807d488d7af5.tar.gz |
added string.atoo (octal version of atoi)
Need to clean up these function names and make them global.
-rw-r--r-- | src/bytecode.c | 10 | ||||
-rw-r--r-- | src/bytecode.h | 2 | ||||
-rw-r--r-- | src/declare.cc | 3 | ||||
-rw-r--r-- | src/string.c | 11 |
4 files changed, 26 insertions, 0 deletions
diff --git a/src/bytecode.c b/src/bytecode.c index 2014de8a..a382c994 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -3443,6 +3443,16 @@ again: treeDownref( prg, sp, (Tree*)str ); break; } + case IN_STR_ATOO: { + debug( prg, REALM_BYTECODE, "IN_STR_ATOO\n" ); + + Str *str = vm_pop_string(); + Word res = strAtoo( str->value ); + Value integer = res; + vm_push_value( integer ); + treeDownref( prg, sp, (Tree*)str ); + break; + } case IN_STR_UORD8: { debug( prg, REALM_BYTECODE, "IN_STR_UORD8\n" ); diff --git a/src/bytecode.h b/src/bytecode.h index f332bfda..a8325767 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -353,6 +353,7 @@ typedef unsigned long colm_value_t; */ #define IN_FN 0xff #define IN_STR_ATOI 0x1d +#define IN_STR_ATOO 0x38 #define IN_STR_UORD8 0x01 #define IN_STR_SORD8 0x02 #define IN_STR_UORD16 0x03 @@ -592,6 +593,7 @@ void stringFree( struct colm_program *prg, Head *head ); void stringShorten( Head *tokdata, long newlen ); Head *concatStr( Head *s1, Head *s2 ); Word strAtoi( Head *str ); +Word strAtoo( Head *str ); Word strUord16( Head *head ); Word strUord8( Head *head ); Word cmpString( Head *s1, Head *s2 ); diff --git a/src/declare.cc b/src/declare.cc index 3e672b28..01cd8a0d 100644 --- a/src/declare.cc +++ b/src/declare.cc @@ -797,6 +797,9 @@ void Compiler::declareStrFields( ) initFunction( uniqueTypeInt, strObj, "atoi", IN_STR_ATOI, IN_STR_ATOI, true, true ); + initFunction( uniqueTypeInt, strObj, "atoo", + IN_STR_ATOO, IN_STR_ATOO, true, true ); + initFunction( uniqueTypeInt, strObj, "uord8", IN_STR_UORD8, IN_STR_UORD8, true, true ); diff --git a/src/string.c b/src/string.c index ba55923c..0c69d1b2 100644 --- a/src/string.c +++ b/src/string.c @@ -228,6 +228,17 @@ Word strAtoi( Head *str ) return res; } +Word strAtoo( Head *str ) +{ + /* FIXME: need to implement this by hand. There is no null terminator. */ + char *nulled = (char*)malloc( str->length + 1 ); + memcpy( nulled, str->data, str->length ); + nulled[str->length] = 0; + int res = strtol( nulled, 0, 8 ); + free( nulled ); + return res; +} + Head *intToStr( Program *prg, Word i ) { char data[20]; |