Permalink
Browse files

update cm0 to encode cli arguments as M0 strings

The code emits a warning and needs a bit of cleanup, but the new
args_basic test successfully prints out its first arg.  Issues with the
m0_args.t are probably due a bug in that test.
  • Loading branch information...
1 parent 50bf1d7 commit 2638f9f96feb99f7292ed38867a86c884f43448c @cotto cotto committed Apr 30, 2012
Showing with 72 additions and 3 deletions.
  1. +17 −1 src/m0/c/m0_interp.c
  2. +3 −2 t/m0/integration/m0_args.m0
  3. +52 −0 t/m0/integration/m0_args_basic.m0
View
@@ -35,12 +35,28 @@ m0_chunk_free_bytecode( M0_Bytecode_Segment *bytecode );
int
main( int argc, const char *argv[]) {
M0_Interp *interp = new_interp();
+ int i;
+ char** interp_argv;
if (!interp)
exit(1);
(*interp)[ARGC] = argc - 1;
- (*interp)[ARGV] = (uint64_t)&(argv[1]);
+ interp_argv = (char**) malloc((argc-1) * sizeof(char*));
+
+ // encode cli arguments as M0 strings, skipping the first (name of the interp)
+ for (i = 1; i < argc; i++) {
+ const char *arg = argv[i];
+ uint64_t m0_arg_const_len = strlen(arg) + 4 + 4 + 1; // byte count + encoding + string bytes + terminal null
+ uint64_t m0_arg_len = strlen(arg) + 1;
+ uint64_t m0_arg_encoding = 0;
+ char *m0_arg_const = (char*) malloc(m0_arg_const_len * sizeof(char));
+ memcpy(&(m0_arg_const[0]), &m0_arg_len, sizeof(uint64_t));
+ memcpy(&(m0_arg_const[4]), &m0_arg_encoding, sizeof(uint64_t));
+ memcpy(&(m0_arg_const[8]), arg, m0_arg_len * sizeof(char));
+ interp_argv[i-1] = m0_arg_const;
+ }
+ (*interp)[ARGV] = interp_argv;
if (argc < 2) {
fprintf( stderr, "Usage: m0 <filename.mob>\n" );
@@ -5,8 +5,8 @@
1 "\n"
2 "ok 1 - arg count: got 6 arguments!\n"
3 "not ok 1 - arg count: didn't get 6 arguments (got "
-4 "ok 2 - arg contents: first argument was 'kittens'\n"
-5 "not ok 2 - arg contents: first argument was 'kittens' # TODO M0 doesn't support this yet \n"
+4 "ok 2 - arg contents: second argument was 'kittens'\n"
+5 "not ok 2 - arg contents: second argument was 'kittens' # TODO M0 doesn't support this yet \n"
6 " arguments)\n"
7 "kittens"
.metadata
@@ -62,6 +62,7 @@ argv_test:
set_imm I1, 0, 1
set_imm S0, 0, ARGV
deref S0, INTERP, S0
+ deref S0, S0, I1
set_imm I2, 0, 1
get_word S0, S0, I2
@@ -0,0 +1,52 @@
+.version 0
+.chunk "args_test"
+.constants
+0 "arg count: "
+1 "\n"
+2 "printing arg 1...\n"
+3 "done\n"
+.metadata
+.bytecode
+
+ # I1 is the arg count
+
+ # print "arg count "
+ set_imm S0, 0, 0
+ set_imm I0, 0, 1
+ deref S0, CONSTS, S0
+ print_s I0, S0, x
+
+ # print arg count value
+ set_imm I1, 0, ARGC
+ print_i I0, I1, x
+
+ # load/print newline
+ set_imm S0, 0, 1
+ deref S0, CONSTS, S0
+ print_s I0, S0, x
+
+ # load/print "printing arg 1..."
+ set_imm S0, 0, 2
+ deref S0, CONSTS, S0
+ print_s I0, S0, x
+
+ # put argv[1] into S0 (argv[0] is the name of this file)
+ set_imm S0, 0, ARGV
+ deref S0, INTERP, S0
+ set_imm I1, 0, 1
+ deref S0, S0, I1
+ print_s I0, S0, x
+
+ # load/print newline
+ set_imm S0, 0, 1
+ deref S0, CONSTS, S0
+ print_s I0, S0, x
+
+ # load/print "done"
+ set_imm S0, 0, 3
+ deref S0, CONSTS, S0
+ print_s I0, S0, x
+ exit I0, x, x
+
+# This code isn't really PASM, but the highlighting works well.
+# vim: expandtab shiftwidth=4 ft=pasm:

0 comments on commit 2638f9f

Please sign in to comment.