Compiling multiple files

21 views
Skip to first unread message

Andreas Hartmann

unread,
Oct 10, 2013, 6:28:29 PM10/10/13
to roy...@googlegroups.com
Hi everyone,

at the moment it doesn't seem to be possible to compile multiple files which depend on each other without explicitly providing the ordering.

I created two files declare.roy and consume.roy.
consume.roy uses a type declared in declare.roy.

This works:
./roy declare.roy consume.roy

This fails:
./roy consume.roy declare.roy

This also fails since the files are enumerated alphabetically:
./roy *.roy

Am I missing something?
Would it be feasible to implement an algorithm which determines dependencies and compiles the files in the right order? Maybe something simple like a "require" statement at the beginning of the file? IIUC when using Roy modules I'd also have to compile the modules in the correct order.

TIA for any insights!

-- Andreas

Andreas Hartmann

unread,
Oct 10, 2013, 7:20:38 PM10/10/13
to roy...@googlegroups.com
I have added a little function to node_repl.js which allows to add dependencies. There are certainly more elegant solutions, but this does the trick for me for the moment. No circular dependency detection.

Declare dependencies using comments:

// require other_roy_file

Here's the code:

    function sortByDependencies(srcFiles) {
     
      function dependencies(src) {
        var source = getFileContents(src);
        var depsMatches = source.match(/^\/{2,}\s*require\s+([\w\.\/]+)\s*$/mg);
        return depsMatches === null ? [] : depsMatches.map(function(d) {
          return /^\/{2,}\s*require\s+([\w\.\/]+)\s*$/.exec(d)[1];
        });
      }
     
      function depends(src1, src2) {
        return _.contains(src1.deps, src2.name);
      }
     
      function cmp(src1, src2) {
        if (depends(src1, src2)) return 2;
        else if (depends(src2, src1)) return 1;
        else return 0;
      }

      var sources = _.map(srcFiles, function(src) {
        return {
          src: src,
          name: path.basename(src).replace(extensions, ''),
          deps: dependencies(src)
        };
      });
      var sorted = sources.sort(cmp);
      return _.pluck(sorted, 'src');
    }

-- Andreas

Andreas Hartmann

unread,
Oct 10, 2013, 7:23:56 PM10/10/13
to roy...@googlegroups.com
There was a typo in the cmp function:

      function cmp(src1, src2) {
        if (depends(src1, src2)) return 1;
        else if (depends(src2, src1)) return -1;
        else return 0;
      }

Andreas Hartmann

unread,
Oct 10, 2013, 8:56:11 PM10/10/13
to roy...@googlegroups.com
This only worked for very simple setups since transitive dependencies were not considered.
I have added a more sophisticated version with circular dependency detection to my fork:

https://github.com/devkat/roy/blob/master/src/node-repl.js#L255

Maybe it makes sense to use such an algorithm for import statements to allow compiling multiple modules and source files depending on these modules?

-- Andreas
Reply all
Reply to author
Forward
0 new messages