diff options
-rw-r--r-- | src/bytecode.c | 24 | ||||
-rw-r--r-- | src/bytecode.h | 1 | ||||
-rw-r--r-- | src/declare.cc | 18 | ||||
-rw-r--r-- | src/loadcolm.cc | 18 | ||||
-rw-r--r-- | src/loadinit.cc | 9 | ||||
-rw-r--r-- | src/parsedata.h | 1 | ||||
-rw-r--r-- | src/parsetree.h | 2 | ||||
-rw-r--r-- | src/synthesis.cc | 3 | ||||
-rw-r--r-- | test/accum3.lm | 4 | ||||
-rw-r--r-- | test/argv1.lm | 4 | ||||
-rw-r--r-- | test/argv2.lm | 2 | ||||
-rw-r--r-- | test/factor5.lm | 1 |
12 files changed, 68 insertions, 19 deletions
diff --git a/src/bytecode.c b/src/bytecode.c index 432e2179..f746f587 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -405,12 +405,23 @@ static void downrefLocals( Program *prg, Tree ***psp, Tree **frame, LocalInfo *l } } +Tree *constructArgv0( Program *prg, int argc, const char **argv ) +{ + Tree *arg = 0; + if ( argc > 0 ) { + Head *head = stringAllocPointer( prg, argv[0], strlen(argv[0]) ); + arg = constructString( prg, head ); + treeUpref( arg ); + } + return arg; +} + Tree *constructArgv( Program *prg, int argc, const char **argv ) { Tree *list = createGeneric( prg, prg->rtd->argvGenericId ); treeUpref( list ); int i; - for ( i = 0; i < argc; i++ ) { + for ( i = 1; i < argc; i++ ) { Head *head = stringAllocPointer( prg, argv[i], strlen(argv[i]) ); Tree *arg = constructString( prg, head ); treeUpref( arg ); @@ -3700,6 +3711,17 @@ again: break; } + case IN_LOAD_ARGV0: { + Half field; + read_half( field ); + debug( prg, REALM_BYTECODE, "IN_LOAD_ARGV0 %lu\n", field ); + + /* Tree comes back upreffed. */ + Tree *tree = constructArgv0( prg, prg->argc, prg->argv ); + setField( prg, prg->global, field, tree ); + break; + } + case IN_SYSTEM: { debug( prg, REALM_BYTECODE, "IN_SYSTEM\n" ); diff --git a/src/bytecode.h b/src/bytecode.h index 78654c9e..39233b1c 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -269,6 +269,7 @@ typedef unsigned char uchar; #define IN_GET_STDOUT 0xb6 #define IN_GET_STDERR 0xb7 #define IN_LOAD_ARGV 0xb8 +#define IN_LOAD_ARGV0 0xed #define IN_TO_UPPER 0xb9 #define IN_TO_LOWER 0xba #define IN_EXIT 0xbb diff --git a/src/declare.cc b/src/declare.cc index 34fd0280..923fb406 100644 --- a/src/declare.cc +++ b/src/declare.cc @@ -1023,6 +1023,11 @@ void Compiler::addArgv() el->isArgv = true; el->isConst = true; globalObjectDef->rootScope->insertField( el->name, el ); + + el = ObjectField::cons( internal, argvTypeRef, "argv0" ); + el->isArgv0 = true; + el->isConst = true; + globalObjectDef->rootScope->insertField( el->name, el ); } void Compiler::addError() @@ -1055,6 +1060,19 @@ int Compiler::argvOffset() assert(false); } +int Compiler::argv0_Offset() +{ + for ( ObjFieldList::Iter field = *globalObjectDef->objFieldList; + field.lte(); field++ ) + { + if ( field->value->isArgv0 ) { + globalObjectDef->referenceField( this, field->value ); + return field->value->offset; + } + } + assert(false); +} + void Compiler::initMapFunctions( GenericType *gen ) { addLengthField( gen->objDef, IN_MAP_LENGTH ); diff --git a/src/loadcolm.cc b/src/loadcolm.cc index 03700dde..39ea9944 100644 --- a/src/loadcolm.cc +++ b/src/loadcolm.cc @@ -751,12 +751,13 @@ struct LoadColm String file = unescape( lit ); - const char *argv[2]; - argv[0] = file.data; - argv[1] = 0; + const char *argv[3]; + argv[0] = "load-include"; + argv[1] = file.data; + argv[2] = 0; colm_program *program = colm_new_program( &colm_object ); - colm_run_program( program, 1, argv ); + colm_run_program( program, 2, argv ); /* Extract the parse tree. */ start Start = ColmTree( program ); @@ -2391,13 +2392,14 @@ void LoadColm::go( long activeRealm ) { LoadColm::init(); - const char *argv[2]; - argv[0] = inputFileName; - argv[1] = 0; + const char *argv[3]; + argv[0] = "load-colm"; + argv[1] = inputFileName; + argv[2] = 0; colm_program *program = colm_new_program( &colm_object ); colm_set_debug( program, activeRealm ); - colm_run_program( program, 1, argv ); + colm_run_program( program, 2, argv ); /* Extract the parse tree. */ start Start = ColmTree( program ); diff --git a/src/loadinit.cc b/src/loadinit.cc index 1bbdcad8..2884c4cc 100644 --- a/src/loadinit.cc +++ b/src/loadinit.cc @@ -348,12 +348,13 @@ void LoadInit::go( long activeRealm ) StmtList *stmtList = new StmtList; - const char *argv[2]; - argv[0] = inputFileName; - argv[1] = 0; + const char *argv[3]; + argv[0] = "load-init"; + argv[1] = inputFileName; + argv[2] = 0; colm_program *program = colm_new_program( &colm_object ); - colm_run_program( program, 1, argv ); + colm_run_program( program, 2, argv ); /* Extract the parse tree. */ start Start = ColmTree( program ); diff --git a/src/parsedata.h b/src/parsedata.h index 9077757c..b18dc0cb 100644 --- a/src/parsedata.h +++ b/src/parsedata.h @@ -769,6 +769,7 @@ struct Compiler void addArgv(); void addError(); int argvOffset(); + int argv0_Offset(); void initGlobalFunctions(); void makeDefaultIterators(); void addLengthField( ObjectDef *objDef, Code getLength ); diff --git a/src/parsetree.h b/src/parsetree.h index 3f7aa7f2..5a2362ff 100644 --- a/src/parsetree.h +++ b/src/parsetree.h @@ -2266,6 +2266,7 @@ struct ObjectField isConst(false), isLhsEl(false), isRhsEl(false), refActive(false), + isArgv0(false), isArgv(false), isCustom(false), isParam(false), @@ -2302,6 +2303,7 @@ struct ObjectField bool isLhsEl; bool isRhsEl; bool refActive; + bool isArgv0; bool isArgv; bool isCustom; bool isParam; diff --git a/src/synthesis.cc b/src/synthesis.cc index c426f8de..97014d62 100644 --- a/src/synthesis.cc +++ b/src/synthesis.cc @@ -2449,6 +2449,9 @@ void Compiler::compileRootBlock( ) code.append( IN_INIT_LOCALS ); code.appendHalf( 0 ); + code.append( IN_LOAD_ARGV0 ); + code.appendHalf( argv0_Offset() ); + code.append( IN_LOAD_ARGV ); code.appendHalf( argvOffset() ); diff --git a/test/accum3.lm b/test/accum3.lm index 956bb3f4..d4e68582 100644 --- a/test/accum3.lm +++ b/test/accum3.lm @@ -18,7 +18,7 @@ def item | [`- with_opt zero? word zero] def args - [word zero item*] + [item*] cons ArgParser: parser<args> [] @@ -33,4 +33,4 @@ print( '\n' ) ##### ARGS ##### -qv -h -o output sdf -i eth0 file ##### EXP ##### -<args><word>./working/accum3</word><zero>�</zero><_repeat_item><item><_literal_0001>-</_literal_0001><_repeat_single><single>q</single><single>v</single></_repeat_single><zero>�</zero></item><item><_literal_0001>-</_literal_0001><_repeat_single><single>h</single></_repeat_single><zero>�</zero></item><item><_literal_0001>-</_literal_0001><with_opt>o</with_opt><_opt_zero><zero>�</zero></_opt_zero><word>output</word><zero>�</zero></item><item><file>sdf</file><zero>�</zero></item><item><_literal_0001>-</_literal_0001><with_opt>i</with_opt><_opt_zero><zero>�</zero></_opt_zero><word>eth0</word><zero>�</zero></item><item><file>file</file><zero>�</zero></item></_repeat_item></args> +<args><_repeat_item><item><_literal_0001>-</_literal_0001><_repeat_single><single>q</single><single>v</single></_repeat_single><zero>�</zero></item><item><_literal_0001>-</_literal_0001><_repeat_single><single>h</single></_repeat_single><zero>�</zero></item><item><_literal_0001>-</_literal_0001><with_opt>o</with_opt><_opt_zero><zero>�</zero></_opt_zero><word>output</word><zero>�</zero></item><item><file>sdf</file><zero>�</zero></item><item><_literal_0001>-</_literal_0001><with_opt>i</with_opt><_opt_zero><zero>�</zero></_opt_zero><word>eth0</word><zero>�</zero></item><item><file>file</file><zero>�</zero></item></_repeat_item></args> diff --git a/test/argv1.lm b/test/argv1.lm index 74086e71..00fd57bd 100644 --- a/test/argv1.lm +++ b/test/argv1.lm @@ -1,8 +1,8 @@ ##### LM ##### -print_xml( argv ) +print_xml( argv0 argv ) print( '\n' ) ##### ARGS ##### a b c 1 2 3 ##### EXP ##### -<__list0><str>./working/argv1</str><str>a</str><str>b</str><str>c</str><str>1</str><str>2</str><str>3</str></__list0> +<str>./working/argv1</str><__list0><str>a</str><str>b</str><str>c</str><str>1</str><str>2</str><str>3</str></__list0> diff --git a/test/argv2.lm b/test/argv2.lm index 5c84564f..6f46e1bf 100644 --- a/test/argv2.lm +++ b/test/argv2.lm @@ -34,7 +34,7 @@ def item | [file zero] def args - [word zero item*] + [item*] # The argument parser. Using an accumulator so we can send nulls after each # arg. diff --git a/test/factor5.lm b/test/factor5.lm index bc2d8115..e2be8c0c 100644 --- a/test/factor5.lm +++ b/test/factor5.lm @@ -1,5 +1,4 @@ ##### LM ##### -argv.pop() print( argv.pop() '\n' ) ##### ARGS ##### a |