From 98a7bfff98384ad76f11c47e1dcc26e2dba21615 Mon Sep 17 00:00:00 2001 From: Adrian Thurston Date: Sat, 21 Mar 2015 16:56:24 -0400 Subject: added global versions of atoi, atoo, suffix and prefix These are easier to use as globals, rather than members on string. --- src/bytecode.c | 24 ++++++++++++++++++++++++ src/bytecode.h | 2 ++ src/declare.cc | 16 ++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/src/bytecode.c b/src/bytecode.c index 9a5cb2d9..03f57a19 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -3461,6 +3461,30 @@ again: treeDownref( prg, sp, (Tree*)str ); break; } + case IN_PREFIX: { + debug( prg, REALM_BYTECODE, "IN_PREFIX\n" ); + + Value len = vm_pop_value(); + Str *str = vm_pop_string(); + + Str *res = string_prefix( prg, str, (long) len ); + treeUpref( (Tree*) res ); + vm_push_string( res ); + treeDownref( prg, sp, (Tree*)str ); + break; + } + case IN_SUFFIX: { + debug( prg, REALM_BYTECODE, "IN_SUFFIX\n" ); + + Value pos = vm_pop_value(); + Str *str = vm_pop_string(); + + Str *res = string_suffix( prg, str, (long) pos ); + treeUpref( (Tree*) res ); + vm_push_string( res ); + treeDownref( prg, sp, (Tree*)str ); + break; + } case IN_LOAD_ARG0: { Half field; read_half( field ); diff --git a/src/bytecode.h b/src/bytecode.h index 7d8be05e..eacf2182 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -407,6 +407,8 @@ typedef unsigned long colm_value_t; #define IN_VLIST_POP_HEAD_BKT 0x35 #define IN_EXIT 0x39 #define IN_EXIT_HARD 0x3a +#define IN_PREFIX 0x3b +#define IN_SUFFIX 0x3c enum TYPE { diff --git a/src/declare.cc b/src/declare.cc index 90622192..dbe360cb 100644 --- a/src/declare.cc +++ b/src/declare.cc @@ -942,6 +942,22 @@ void Compiler::declareGlobalFields() IN_TO_UPPER, IN_TO_UPPER, uniqueTypeStr, true ); method->useCallObj = false; + method = initFunction( uniqueTypeInt, globalObjectDef, "atoi", + IN_STR_ATOI, IN_STR_ATOI, uniqueTypeStr, true, true ); + method->useCallObj = false; + + method = initFunction( uniqueTypeInt, globalObjectDef, "atoo", + IN_STR_ATOO, IN_STR_ATOO, uniqueTypeStr, true, true ); + method->useCallObj = false; + + method = initFunction( uniqueTypeStr, globalObjectDef, "prefix", + IN_PREFIX, IN_PREFIX, uniqueTypeStr, uniqueTypeInt, true, true ); + method->useCallObj = false; + + method = initFunction( uniqueTypeStr, globalObjectDef, "suffix", + IN_SUFFIX, IN_SUFFIX, uniqueTypeStr, uniqueTypeInt, true, true ); + method->useCallObj = false; + method = initFunction( uniqueTypeInt, globalObjectDef, "exit", IN_EXIT, IN_EXIT, uniqueTypeInt, true, true ); -- cgit v1.2.1