diff options
| author | Li Jin <dragon-fly@qq.com> | 2020-02-07 17:29:34 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2020-02-07 17:29:34 +0800 |
| commit | c241ea241e8e9c152f6eb14f163b2ae39749f7bf (patch) | |
| tree | 2fd05ca6866ea60ca778fb6ff31c7ec429e138c4 /src/MoonP | |
| parent | 2e50c15bfe67d4709880a0377d37fca191be2f3e (diff) | |
| download | yuescript-c241ea241e8e9c152f6eb14f163b2ae39749f7bf.tar.gz yuescript-c241ea241e8e9c152f6eb14f163b2ae39749f7bf.tar.bz2 yuescript-c241ea241e8e9c152f6eb14f163b2ae39749f7bf.zip | |
releasing moonplus as a lib.
Diffstat (limited to '')
| -rw-r--r-- | src/MoonP/moon_compiler.cpp | 33 | ||||
| -rw-r--r-- | src/MoonPlus.h | 125 |
2 files changed, 144 insertions, 14 deletions
diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index d3427d3..30adbae 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp | |||
| @@ -690,7 +690,7 @@ private: | |||
| 690 | break; | 690 | break; |
| 691 | } | 691 | } |
| 692 | } | 692 | } |
| 693 | throw std::logic_error(_info.errorMessage("Expression list must appear at the end of body block."sv, expList)); | 693 | throw std::logic_error(_info.errorMessage("Expression list must appear at the end of body or block."sv, expList)); |
| 694 | } | 694 | } |
| 695 | break; | 695 | break; |
| 696 | } | 696 | } |
| @@ -1252,26 +1252,31 @@ private: | |||
| 1252 | bool oneLined = defs.size() == expList->exprs.objects().size() && | 1252 | bool oneLined = defs.size() == expList->exprs.objects().size() && |
| 1253 | traversal::Stop != action->traverse([&](ast_node* n) { | 1253 | traversal::Stop != action->traverse([&](ast_node* n) { |
| 1254 | switch (n->getId()) { | 1254 | switch (n->getId()) { |
| 1255 | case "Callable"_id: { | 1255 | case "ChainValue"_id: { |
| 1256 | auto callable = static_cast<Callable_t*>(n); | 1256 | auto chainValue = static_cast<ChainValue_t*>(n); |
| 1257 | switch (callable->item->getId()) { | 1257 | const auto& items = chainValue->items.objects(); |
| 1258 | case "Variable"_id: | 1258 | BLOCK_START |
| 1259 | for (const auto& def : defs) { | 1259 | auto callable = ast_cast<Callable_t>(*items.begin()); |
| 1260 | BREAK_IF(!callable); | ||
| 1261 | auto next = items.begin(); ++next; | ||
| 1262 | BREAK_IF(next == items.end()); | ||
| 1263 | BREAK_IF((!ast_is<Invoke_t,InvokeArgs_t>(*next))); | ||
| 1264 | for (const auto& def : defs) { | ||
| 1265 | switch (callable->item->getId()) { | ||
| 1266 | case "Variable"_id: | ||
| 1260 | if (def == _parser.toString(callable->item)) { | 1267 | if (def == _parser.toString(callable->item)) { |
| 1261 | return traversal::Stop; | 1268 | return traversal::Stop; |
| 1262 | } | 1269 | } |
| 1263 | } | 1270 | return traversal::Return; |
| 1264 | return traversal::Return; | 1271 | case "SelfName"_id: |
| 1265 | case "SelfName"_id: | ||
| 1266 | for (const auto& def : defs) { | ||
| 1267 | if (def == "self"sv) { | 1272 | if (def == "self"sv) { |
| 1268 | return traversal::Stop; | 1273 | return traversal::Stop; |
| 1269 | } | 1274 | } |
| 1270 | } | 1275 | return traversal::Return; |
| 1271 | return traversal::Return; | 1276 | } |
| 1272 | default: | ||
| 1273 | return traversal::Continue; | ||
| 1274 | } | 1277 | } |
| 1278 | BLOCK_END | ||
| 1279 | return traversal::Continue; | ||
| 1275 | } | 1280 | } |
| 1276 | default: | 1281 | default: |
| 1277 | return traversal::Continue; | 1282 | return traversal::Continue; |
diff --git a/src/MoonPlus.h b/src/MoonPlus.h new file mode 100644 index 0000000..da5f3ac --- /dev/null +++ b/src/MoonPlus.h | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | R"moonscript_codes( | ||
| 2 | import "moonp" | ||
| 3 | import concat, insert, remove from table | ||
| 4 | unpack = unpack or table.unpack | ||
| 5 | lua = :loadstring, :load | ||
| 6 | |||
| 7 | local * | ||
| 8 | |||
| 9 | dirsep = "/" | ||
| 10 | |||
| 11 | moonp.moon_compiled = {} | ||
| 12 | |||
| 13 | split = (str, delim) -> | ||
| 14 | return {} if str == "" | ||
| 15 | str ..= delim | ||
| 16 | [m for m in str\gmatch("(.-)"..delim)] | ||
| 17 | |||
| 18 | get_options = (...) -> | ||
| 19 | count = select "#", ... | ||
| 20 | opts = select count, ... | ||
| 21 | if type(opts) == "table" | ||
| 22 | opts, unpack {...}, nil, count - 1 | ||
| 23 | else | ||
| 24 | {}, ... | ||
| 25 | |||
| 26 | -- create moon path package from lua package path | ||
| 27 | create_moonpath = (package_path) -> | ||
| 28 | moonpaths = for path in *split package_path, ";" | ||
| 29 | prefix = path\match "^(.-)%.lua$" | ||
| 30 | continue unless prefix | ||
| 31 | prefix .. ".moon" | ||
| 32 | concat moonpaths, ";" | ||
| 33 | |||
| 34 | moon_loader = (name) -> | ||
| 35 | name_path = name\gsub "%.", dirsep | ||
| 36 | |||
| 37 | local file, file_path | ||
| 38 | for path in package.moonpath\gmatch "[^;]+" | ||
| 39 | file_path = path\gsub "?", name_path | ||
| 40 | file = io.open file_path | ||
| 41 | break if file | ||
| 42 | |||
| 43 | if file | ||
| 44 | text = file\read "*a" | ||
| 45 | file\close! | ||
| 46 | res, err = loadstring text, "@#{file_path}" | ||
| 47 | if not res | ||
| 48 | error file_path .. ": " .. err | ||
| 49 | |||
| 50 | return res | ||
| 51 | |||
| 52 | return nil, "Could not find moon file" | ||
| 53 | |||
| 54 | |||
| 55 | loadstring = (...) -> | ||
| 56 | options, str, chunk_name, mode, env = get_options ... | ||
| 57 | chunk_name or= "=(moonscript.loadstring)" | ||
| 58 | |||
| 59 | code, err = moonp.to_lua str, options | ||
| 60 | unless code | ||
| 61 | return nil, err | ||
| 62 | |||
| 63 | moonp.moon_compiled[chunk_name] = code if chunk_name | ||
| 64 | -- the unpack prevents us from passing nil | ||
| 65 | (lua.loadstring or lua.load) code, chunk_name, unpack { mode, env } | ||
| 66 | |||
| 67 | loadfile = (fname, ...) -> | ||
| 68 | file, err = io.open fname | ||
| 69 | return nil, err unless file | ||
| 70 | text = assert file\read "*a" | ||
| 71 | file\close! | ||
| 72 | loadstring text, "@#{fname}", ... | ||
| 73 | |||
| 74 | -- throws errros | ||
| 75 | dofile = (...) -> | ||
| 76 | f = assert loadfile ... | ||
| 77 | f! | ||
| 78 | |||
| 79 | insert_loader = (pos=2) -> | ||
| 80 | if not package.moonpath | ||
| 81 | package.moonpath = create_moonpath package.path | ||
| 82 | |||
| 83 | loaders = package.loaders or package.searchers | ||
| 84 | for loader in *loaders | ||
| 85 | return false if loader == moon_loader | ||
| 86 | |||
| 87 | insert loaders, pos, moon_loader | ||
| 88 | true | ||
| 89 | |||
| 90 | remove_loader = -> | ||
| 91 | loaders = package.loaders or package.searchers | ||
| 92 | |||
| 93 | for i, loader in ipairs loaders | ||
| 94 | if loader == moon_loader | ||
| 95 | remove loaders, i | ||
| 96 | return true | ||
| 97 | |||
| 98 | false | ||
| 99 | |||
| 100 | moon_require = (name)-> | ||
| 101 | insert_loader! | ||
| 102 | xpcall (-> require name), (err)-> | ||
| 103 | msg = moonp.stp.stacktrace err, 2 | ||
| 104 | print msg | ||
| 105 | |||
| 106 | setmetatable moonp, { | ||
| 107 | __index: (key)=> | ||
| 108 | return nil unless key == "stp" | ||
| 109 | stp = rawget moonp,"stp" | ||
| 110 | unless stp | ||
| 111 | stp = with moonp.load_stacktraceplus! | ||
| 112 | .dump_locals = false | ||
| 113 | .simplified = true | ||
| 114 | rawset moonp,"stp",stp | ||
| 115 | rawset moonp,"load_stacktraceplus",nil | ||
| 116 | stp | ||
| 117 | __call: (name)=> @.require name | ||
| 118 | } | ||
| 119 | |||
| 120 | moonp[k] = v for k,v in pairs { | ||
| 121 | :insert_loader, :remove_loader, :moon_loader, :dirsep, | ||
| 122 | :dofile, :loadfile, :loadstring, :create_moonpath, | ||
| 123 | require:moon_require | ||
| 124 | } | ||
| 125 | )moonscript_codes"; | ||
