diff options
Diffstat (limited to 'src/Moonscript.h')
-rw-r--r-- | src/Moonscript.h | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/Moonscript.h b/src/Moonscript.h new file mode 100644 index 0000000..6e84552 --- /dev/null +++ b/src/Moonscript.h | |||
@@ -0,0 +1,146 @@ | |||
1 | R"moonscript_codes( | ||
2 | --[[ | ||
3 | Copyright (C) 2020 by Leaf Corcoran, modified by Li Jin | ||
4 | |||
5 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
6 | of this software and associated documentation files (the "Software"), to deal | ||
7 | in the Software without restriction, including without limitation the rights | ||
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
9 | copies of the Software, and to permit persons to whom the Software is | ||
10 | furnished to do so, subject to the following conditions: | ||
11 | |||
12 | The above copyright notice and this permission notice shall be included in | ||
13 | all copies or substantial portions of the Software. | ||
14 | |||
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
21 | THE SOFTWARE.]] | ||
22 | |||
23 | import "moonp" | ||
24 | import concat, insert, remove from table | ||
25 | unpack = unpack or table.unpack | ||
26 | lua = :loadstring, :load | ||
27 | |||
28 | local * | ||
29 | |||
30 | dirsep = "/" | ||
31 | |||
32 | moonp.moon_compiled = {} | ||
33 | |||
34 | split = (str, delim) -> | ||
35 | return {} if str == "" | ||
36 | str ..= delim | ||
37 | [m for m in str\gmatch("(.-)"..delim)] | ||
38 | |||
39 | get_options = (...) -> | ||
40 | count = select "#", ... | ||
41 | opts = select count, ... | ||
42 | if type(opts) == "table" | ||
43 | opts, unpack {...}, nil, count - 1 | ||
44 | else | ||
45 | {}, ... | ||
46 | |||
47 | -- create moon path package from lua package path | ||
48 | create_moonpath = (package_path) -> | ||
49 | moonpaths = for path in *split package_path, ";" | ||
50 | prefix = path\match "^(.-)%.lua$" | ||
51 | continue unless prefix | ||
52 | prefix .. ".moon" | ||
53 | concat moonpaths, ";" | ||
54 | |||
55 | moon_loader = (name) -> | ||
56 | name_path = name\gsub "%.", dirsep | ||
57 | |||
58 | local file, file_path | ||
59 | for path in package.moonpath\gmatch "[^;]+" | ||
60 | file_path = path\gsub "?", name_path | ||
61 | file = io.open file_path | ||
62 | break if file | ||
63 | |||
64 | if file | ||
65 | text = file\read "*a" | ||
66 | file\close! | ||
67 | res, err = loadstring text, "@#{file_path}" | ||
68 | if not res | ||
69 | error file_path .. ": " .. err | ||
70 | |||
71 | return res | ||
72 | |||
73 | return nil, "Could not find moon file" | ||
74 | |||
75 | |||
76 | loadstring = (...) -> | ||
77 | options, str, chunk_name, mode, env = get_options ... | ||
78 | chunk_name or= "=(moonscript.loadstring)" | ||
79 | |||
80 | code, err = moonp.to_lua str, options | ||
81 | unless code | ||
82 | return nil, err | ||
83 | |||
84 | moonp.moon_compiled[chunk_name] = code if chunk_name | ||
85 | -- the unpack prevents us from passing nil | ||
86 | (lua.loadstring or lua.load) code, chunk_name, unpack { mode, env } | ||
87 | |||
88 | loadfile = (fname, ...) -> | ||
89 | file, err = io.open fname | ||
90 | return nil, err unless file | ||
91 | text = assert file\read "*a" | ||
92 | file\close! | ||
93 | loadstring text, "@#{fname}", ... | ||
94 | |||
95 | -- throws errros | ||
96 | dofile = (...) -> | ||
97 | f = assert loadfile ... | ||
98 | f! | ||
99 | |||
100 | insert_loader = (pos=2) -> | ||
101 | if not package.moonpath | ||
102 | package.moonpath = create_moonpath package.path | ||
103 | |||
104 | loaders = package.loaders or package.searchers | ||
105 | for loader in *loaders | ||
106 | return false if loader == moon_loader | ||
107 | |||
108 | insert loaders, pos, moon_loader | ||
109 | true | ||
110 | |||
111 | remove_loader = -> | ||
112 | loaders = package.loaders or package.searchers | ||
113 | |||
114 | for i, loader in ipairs loaders | ||
115 | if loader == moon_loader | ||
116 | remove loaders, i | ||
117 | return true | ||
118 | |||
119 | false | ||
120 | |||
121 | moon_require = (name)-> | ||
122 | insert_loader! | ||
123 | xpcall (-> require name), (err)-> | ||
124 | msg = moonp.stp.stacktrace err, 2 | ||
125 | print msg | ||
126 | |||
127 | setmetatable moonp, { | ||
128 | __index: (key)=> | ||
129 | return nil unless key == "stp" | ||
130 | stp = rawget moonp,"stp" | ||
131 | unless stp | ||
132 | stp = with moonp.load_stacktraceplus! | ||
133 | .dump_locals = false | ||
134 | .simplified = true | ||
135 | rawset moonp,"stp",stp | ||
136 | rawset moonp,"load_stacktraceplus",nil | ||
137 | stp | ||
138 | __call: (name)=> @.require name | ||
139 | } | ||
140 | |||
141 | moonp[k] = v for k,v in pairs { | ||
142 | :insert_loader, :remove_loader, :moon_loader, :dirsep, | ||
143 | :dofile, :loadfile, :loadstring, :create_moonpath, | ||
144 | require:moon_require | ||
145 | } | ||
146 | )moonscript_codes"; | ||