polycomp 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +25 -0
- package/bin/native/polycomp.exe +0 -0
- package/bin/polycomp.js +25 -0
- package/js/frontend.js +1061 -0
- package/js/index.cjs +245 -0
- package/js/index.d.ts +2 -0
- package/js/index.mjs +19 -0
- package/js/polycomp-vm.mjs +1047 -0
- package/package.json +41 -5
- package/src/ast.rs +329 -0
- package/src/bundle.rs +3307 -0
- package/src/diagnostics.rs +276 -0
- package/src/interpreter.rs +1565 -0
- package/src/ir.rs +272 -0
- package/src/jit.rs +1151 -0
- package/src/lib.rs +11 -0
- package/src/lowering.rs +1157 -0
- package/src/main.rs +655 -0
- package/src/optimizer.rs +660 -0
- package/src/parser.rs +4755 -0
- package/src/resolver.rs +722 -0
- package/src/runtime.rs +1104 -0
- package/README.md +0 -3
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
use std::fmt;
|
|
2
|
+
use std::path::PathBuf;
|
|
3
|
+
|
|
4
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
5
|
+
pub struct Span {
|
|
6
|
+
pub file_id: usize,
|
|
7
|
+
pub start: usize,
|
|
8
|
+
pub end: usize,
|
|
9
|
+
pub line: u32,
|
|
10
|
+
pub col: u32,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
impl Span {
|
|
14
|
+
pub fn dummy() -> Self {
|
|
15
|
+
Self {
|
|
16
|
+
file_id: 0,
|
|
17
|
+
start: 0,
|
|
18
|
+
end: 0,
|
|
19
|
+
line: 0,
|
|
20
|
+
col: 0,
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
pub fn merge(self, other: Span) -> Span {
|
|
25
|
+
if self.file_id != other.file_id || self.line == 0 {
|
|
26
|
+
return other;
|
|
27
|
+
}
|
|
28
|
+
if other.line == 0 {
|
|
29
|
+
return self;
|
|
30
|
+
}
|
|
31
|
+
Span {
|
|
32
|
+
file_id: self.file_id,
|
|
33
|
+
start: self.start.min(other.start),
|
|
34
|
+
end: self.end.max(other.end),
|
|
35
|
+
line: self.line.min(other.line),
|
|
36
|
+
col: if self.line <= other.line {
|
|
37
|
+
self.col
|
|
38
|
+
} else {
|
|
39
|
+
other.col
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
46
|
+
pub enum Severity {
|
|
47
|
+
Error,
|
|
48
|
+
Warning,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
#[derive(Debug, Clone)]
|
|
52
|
+
pub struct Diagnostic {
|
|
53
|
+
pub severity: Severity,
|
|
54
|
+
pub code: &'static str,
|
|
55
|
+
pub message: String,
|
|
56
|
+
pub span: Option<Span>,
|
|
57
|
+
pub notes: Vec<String>,
|
|
58
|
+
pub help: Option<String>,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
impl Diagnostic {
|
|
62
|
+
pub fn error(code: &'static str, message: impl Into<String>) -> Self {
|
|
63
|
+
Self {
|
|
64
|
+
severity: Severity::Error,
|
|
65
|
+
code,
|
|
66
|
+
message: message.into(),
|
|
67
|
+
span: None,
|
|
68
|
+
notes: Vec::new(),
|
|
69
|
+
help: None,
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
pub fn with_span(mut self, span: Span) -> Self {
|
|
74
|
+
if span.line > 0 {
|
|
75
|
+
self.span = Some(span);
|
|
76
|
+
}
|
|
77
|
+
self
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
pub fn with_note(mut self, note: impl Into<String>) -> Self {
|
|
81
|
+
self.notes.push(note.into());
|
|
82
|
+
self
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
pub fn with_help(mut self, help: impl Into<String>) -> Self {
|
|
86
|
+
self.help = Some(help.into());
|
|
87
|
+
self
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
pub fn render(&self, sources: Option<&SourceMap>, color: bool) -> String {
|
|
91
|
+
let mut out = String::new();
|
|
92
|
+
let (red, cyan, yellow, bold, reset) = if color {
|
|
93
|
+
("\x1b[31;1m", "\x1b[36;1m", "\x1b[33;1m", "\x1b[1m", "\x1b[0m")
|
|
94
|
+
} else {
|
|
95
|
+
("", "", "", "", "")
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
let label = match self.severity {
|
|
99
|
+
Severity::Error => format!("{red}error[{}]{reset}", self.code),
|
|
100
|
+
Severity::Warning => format!("{yellow}warning[{}]{reset}", self.code),
|
|
101
|
+
};
|
|
102
|
+
out.push_str(&format!("{label}: {bold}{}{reset}\n", self.message));
|
|
103
|
+
|
|
104
|
+
if let (Some(span), Some(map)) = (self.span, sources)
|
|
105
|
+
&& let Some(file) = map.get(span.file_id)
|
|
106
|
+
{
|
|
107
|
+
out.push_str(&format!(
|
|
108
|
+
" {cyan}-->{reset} {}:{}:{}\n",
|
|
109
|
+
file.path.display(),
|
|
110
|
+
span.line,
|
|
111
|
+
span.col
|
|
112
|
+
));
|
|
113
|
+
if let Some(line_str) = file.line_text(span.line) {
|
|
114
|
+
let line_num = span.line.to_string();
|
|
115
|
+
let pad = " ".repeat(line_num.len());
|
|
116
|
+
out.push_str(&format!(" {cyan}{pad} |{reset}\n"));
|
|
117
|
+
out.push_str(&format!(" {cyan}{line_num} |{reset} {line_str}\n"));
|
|
118
|
+
let col_idx = span.col.saturating_sub(1) as usize;
|
|
119
|
+
let caret_len = span
|
|
120
|
+
.end
|
|
121
|
+
.saturating_sub(span.start)
|
|
122
|
+
.max(1)
|
|
123
|
+
.min(line_str.len().saturating_sub(col_idx).max(1));
|
|
124
|
+
let spaces = " ".repeat(col_idx);
|
|
125
|
+
let carets = "^".repeat(caret_len);
|
|
126
|
+
out.push_str(&format!(" {cyan}{pad} |{reset} {spaces}{red}{carets}{reset}\n"));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for note in &self.notes {
|
|
131
|
+
out.push_str(&format!(" {cyan}= note:{reset} {note}\n"));
|
|
132
|
+
}
|
|
133
|
+
if let Some(help) = &self.help {
|
|
134
|
+
out.push_str(&format!(" {cyan}= help:{reset} {help}\n"));
|
|
135
|
+
}
|
|
136
|
+
out
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
#[derive(Debug, Clone)]
|
|
141
|
+
pub struct SourceFile {
|
|
142
|
+
pub path: PathBuf,
|
|
143
|
+
pub source: String,
|
|
144
|
+
line_starts: Vec<usize>,
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
impl SourceFile {
|
|
148
|
+
pub fn new(path: PathBuf, source: String) -> Self {
|
|
149
|
+
let mut line_starts = vec![0];
|
|
150
|
+
for (idx, b) in source.bytes().enumerate() {
|
|
151
|
+
if b == b'\n' {
|
|
152
|
+
line_starts.push(idx + 1);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
Self {
|
|
156
|
+
path,
|
|
157
|
+
source,
|
|
158
|
+
line_starts,
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
pub fn line_text(&self, line_1based: u32) -> Option<&str> {
|
|
163
|
+
let idx = line_1based.checked_sub(1)? as usize;
|
|
164
|
+
let start = *self.line_starts.get(idx)?;
|
|
165
|
+
let end = self
|
|
166
|
+
.line_starts
|
|
167
|
+
.get(idx + 1)
|
|
168
|
+
.copied()
|
|
169
|
+
.unwrap_or(self.source.len());
|
|
170
|
+
let slice = &self.source[start..end];
|
|
171
|
+
Some(slice.trim_end_matches(&['\r', '\n'][..]))
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
#[derive(Debug, Default)]
|
|
176
|
+
pub struct SourceMap {
|
|
177
|
+
files: Vec<SourceFile>,
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
impl SourceMap {
|
|
181
|
+
pub fn add_file(&mut self, path: PathBuf, source: String) -> usize {
|
|
182
|
+
let id = self.files.len();
|
|
183
|
+
self.files.push(SourceFile::new(path, source));
|
|
184
|
+
id
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
pub fn get(&self, id: usize) -> Option<&SourceFile> {
|
|
188
|
+
self.files.get(id)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
pub fn format_error(&self, err: &PolyError, color: bool) -> String {
|
|
192
|
+
err.diagnostics
|
|
193
|
+
.iter()
|
|
194
|
+
.map(|d| d.render(Some(self), color))
|
|
195
|
+
.collect::<Vec<_>>()
|
|
196
|
+
.join("\n")
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
#[derive(Debug, Clone)]
|
|
201
|
+
pub struct PolyError {
|
|
202
|
+
pub diagnostics: Vec<Diagnostic>,
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
impl PolyError {
|
|
206
|
+
pub fn single(diag: Diagnostic) -> Self {
|
|
207
|
+
Self {
|
|
208
|
+
diagnostics: vec![diag],
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
pub fn msg(code: &'static str, message: impl Into<String>) -> Self {
|
|
213
|
+
Self::single(Diagnostic::error(code, message))
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
pub fn spanned(code: &'static str, message: impl Into<String>, span: Span) -> Self {
|
|
217
|
+
Self::single(Diagnostic::error(code, message).with_span(span))
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
impl fmt::Display for PolyError {
|
|
222
|
+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
223
|
+
for (i, d) in self.diagnostics.iter().enumerate() {
|
|
224
|
+
if i > 0 {
|
|
225
|
+
writeln!(f)?;
|
|
226
|
+
}
|
|
227
|
+
write!(f, "{}", d.render(None, false))?;
|
|
228
|
+
}
|
|
229
|
+
Ok(())
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
impl std::error::Error for PolyError {}
|
|
234
|
+
|
|
235
|
+
impl From<std::io::Error> for PolyError {
|
|
236
|
+
fn from(value: std::io::Error) -> Self {
|
|
237
|
+
PolyError::msg("E0001", format!("I/O error: {value}"))
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
pub fn suggest_did_you_mean<'a>(
|
|
242
|
+
target: &str,
|
|
243
|
+
candidates: impl IntoIterator<Item = &'a str>,
|
|
244
|
+
) -> Option<String> {
|
|
245
|
+
let mut best: Option<(&str, usize)> = None;
|
|
246
|
+
for cand in candidates {
|
|
247
|
+
let dist = levenshtein(target, cand);
|
|
248
|
+
let max_dist = (target.len() / 2).clamp(1, 3);
|
|
249
|
+
if dist <= max_dist && best.is_none_or(|(_, d)| dist < d) {
|
|
250
|
+
best = Some((cand, dist));
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
best.map(|(s, _)| s.to_string())
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
fn levenshtein(a: &str, b: &str) -> usize {
|
|
257
|
+
let a: Vec<char> = a.chars().collect();
|
|
258
|
+
let b: Vec<char> = b.chars().collect();
|
|
259
|
+
if a.is_empty() {
|
|
260
|
+
return b.len();
|
|
261
|
+
}
|
|
262
|
+
if b.is_empty() {
|
|
263
|
+
return a.len();
|
|
264
|
+
}
|
|
265
|
+
let mut prev: Vec<usize> = (0..=b.len()).collect();
|
|
266
|
+
let mut curr = vec![0; b.len() + 1];
|
|
267
|
+
for (i, &ca) in a.iter().enumerate() {
|
|
268
|
+
curr[0] = i + 1;
|
|
269
|
+
for (j, &cb) in b.iter().enumerate() {
|
|
270
|
+
let cost = usize::from(ca != cb);
|
|
271
|
+
curr[j + 1] = (prev[j + 1] + 1).min(curr[j] + 1).min(prev[j] + cost);
|
|
272
|
+
}
|
|
273
|
+
std::mem::swap(&mut prev, &mut curr);
|
|
274
|
+
}
|
|
275
|
+
prev[b.len()]
|
|
276
|
+
}
|