musictheory / NilScript

Objective-C-style language superset of JavaScript with a tiny, simple runtime
Other
50 stars 5 forks source link

nsc compiler works, but how to run result? #164

Open plaurent opened 4 years ago

plaurent commented 4 years ago

The README in the 3.0 branch specifies how to compile code, but not how to run the result.

Given a file called Example1.ns:

@class TheClass
- (String) doSomethingWithString:(String)string andNumber:(Number)number
{
    return string + "-" + number;    
}
@end

let instance = [[TheClass alloc] init];
console.log([instance doSomethingWithString:"A" andNumber:3]);

using nsc Example1.ns -o a.js, when I run a.js I get:

root@9e1af5a6a5d9:~/Project1# node a.js 
/root/Project1/a.js:1
N$$_._registerClass("N$_c_TheClass",null,function(N$_s, N$_m) { function N$_c_TheClass() { this.constructor = N$_c_TheClass;this.N$_id = ++N$$_._id;}
^

ReferenceError: N$$_ is not defined
    at Object.<anonymous> (/root/Project1/a.js:1:1)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
    at internal/main/run_main_module.js:17:11
root@9e1af5a6a5d9:~/Project1# 

How can I run a.js? Do I need to import something?

iccir commented 4 years ago

You need evaluate runtime.js located in the lib folder prior to any NilScript compiled output.

cat lib/runtime.js ~/Project1/a.js | node -

Note: the 3.0 branch is still under development with breaking changes to syntax.

plaurent commented 4 years ago

Wow, thank you it worked! Is this the way of running things for the foreseeable future? Would you advise I file a pull request to include this in the readme? (after the Compiling section)

iccir commented 4 years ago

Usually, you wouldn't use cat, as this will invalidate source map data. I'm not sure what the modern way of doing this is. We used source-map-concat in the past, but that hasn't been updated for 4 years.

The Compiling section should probably mention that runtime.js needs to either be loaded first or concatenated in. I need to ponder the exact phrasing (also the readme is being overhauled since the whole language is rapidly changing in 3.0 ;) )