polycomp 0.0.0-stage → 0.2.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 +24 -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 +246 -0
- package/src/bundle.rs +3180 -0
- package/src/diagnostics.rs +276 -0
- package/src/interpreter.rs +1214 -0
- package/src/ir.rs +239 -0
- package/src/jit.rs +1099 -0
- package/src/lib.rs +11 -0
- package/src/lowering.rs +826 -0
- package/src/main.rs +634 -0
- package/src/optimizer.rs +602 -0
- package/src/parser.rs +2605 -0
- package/src/resolver.rs +613 -0
- package/src/runtime.rs +829 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "polycomp",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
1
|
+
{
|
|
2
|
+
"name": "polycomp",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "PolyComp Universal Polyglot Compiler, Lazy VM, Cranelift JIT, and Armored UTF-8 IR Runtime",
|
|
5
|
+
"main": "./js/index.cjs",
|
|
6
|
+
"module": "./js/index.mjs",
|
|
7
|
+
"types": "./js/index.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"polycomp": "./bin/polycomp.js"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./js/index.d.ts",
|
|
14
|
+
"browser": "./js/frontend.js",
|
|
15
|
+
"import": "./js/index.mjs",
|
|
16
|
+
"require": "./js/index.cjs"
|
|
17
|
+
},
|
|
18
|
+
"./browser": {
|
|
19
|
+
"types": "./js/index.d.ts",
|
|
20
|
+
"import": "./js/frontend.js",
|
|
21
|
+
"default": "./js/frontend.js"
|
|
22
|
+
},
|
|
23
|
+
"./vm": {
|
|
24
|
+
"types": "./js/index.d.ts",
|
|
25
|
+
"browser": "./js/polycomp-vm.mjs",
|
|
26
|
+
"import": "./js/polycomp-vm.mjs",
|
|
27
|
+
"require": "./js/index.cjs"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"bin/",
|
|
32
|
+
"js/",
|
|
33
|
+
"src/",
|
|
34
|
+
"Cargo.toml"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build:native": "cargo build --release"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=16.0.0"
|
|
41
|
+
}
|
|
6
42
|
}
|
package/src/ast.rs
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
use crate::diagnostics::Span;
|
|
2
|
+
use std::path::Path;
|
|
3
|
+
|
|
4
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
5
|
+
pub enum Language {
|
|
6
|
+
Rust,
|
|
7
|
+
Python,
|
|
8
|
+
JavaScript,
|
|
9
|
+
C,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
impl Language {
|
|
13
|
+
pub fn from_path(path: &Path) -> Option<Self> {
|
|
14
|
+
let ext = path.extension()?.to_str()?.to_ascii_lowercase();
|
|
15
|
+
match ext.as_str() {
|
|
16
|
+
"rs" => Some(Language::Rust),
|
|
17
|
+
"py" => Some(Language::Python),
|
|
18
|
+
"js" | "mjs" | "cjs" | "ts" => Some(Language::JavaScript),
|
|
19
|
+
"c" | "h" => Some(Language::C),
|
|
20
|
+
_ => None,
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
pub fn name(self) -> &'static str {
|
|
25
|
+
match self {
|
|
26
|
+
Language::Rust => "Rust",
|
|
27
|
+
Language::Python => "Python",
|
|
28
|
+
Language::JavaScript => "JavaScript",
|
|
29
|
+
Language::C => "C",
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
|
35
|
+
pub enum TypeHint {
|
|
36
|
+
#[default]
|
|
37
|
+
Unknown,
|
|
38
|
+
Void,
|
|
39
|
+
Bool,
|
|
40
|
+
Int,
|
|
41
|
+
Float,
|
|
42
|
+
Str,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
46
|
+
pub enum BinOp {
|
|
47
|
+
Add,
|
|
48
|
+
Sub,
|
|
49
|
+
Mul,
|
|
50
|
+
Div,
|
|
51
|
+
FloorDiv,
|
|
52
|
+
Mod,
|
|
53
|
+
Eq,
|
|
54
|
+
Ne,
|
|
55
|
+
Lt,
|
|
56
|
+
Le,
|
|
57
|
+
Gt,
|
|
58
|
+
Ge,
|
|
59
|
+
And,
|
|
60
|
+
Or,
|
|
61
|
+
BitAnd,
|
|
62
|
+
BitOr,
|
|
63
|
+
BitXor,
|
|
64
|
+
Shl,
|
|
65
|
+
Shr,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
69
|
+
pub enum UnaryOp {
|
|
70
|
+
Neg,
|
|
71
|
+
Not,
|
|
72
|
+
BitNot,
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
#[derive(Debug, Clone)]
|
|
76
|
+
pub struct ImportItem {
|
|
77
|
+
pub name: String,
|
|
78
|
+
pub alias: String,
|
|
79
|
+
pub span: Span,
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
#[derive(Debug, Clone)]
|
|
83
|
+
pub enum ImportKind {
|
|
84
|
+
Namespace { alias: String },
|
|
85
|
+
Named { items: Vec<ImportItem> },
|
|
86
|
+
Wildcard,
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
#[derive(Debug, Clone)]
|
|
90
|
+
pub struct ImportDecl {
|
|
91
|
+
pub module_specifier: String,
|
|
92
|
+
pub kind: ImportKind,
|
|
93
|
+
pub span: Span,
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
#[derive(Debug, Clone)]
|
|
97
|
+
pub struct Param {
|
|
98
|
+
pub name: String,
|
|
99
|
+
pub ty: TypeHint,
|
|
100
|
+
pub span: Span,
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
#[derive(Debug, Clone)]
|
|
104
|
+
pub struct FunctionDecl {
|
|
105
|
+
pub name: String,
|
|
106
|
+
pub params: Vec<Param>,
|
|
107
|
+
pub return_type: TypeHint,
|
|
108
|
+
pub body: Vec<Stmt>,
|
|
109
|
+
pub is_exported: bool,
|
|
110
|
+
pub span: Span,
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
#[derive(Debug, Clone)]
|
|
114
|
+
pub struct StructDecl {
|
|
115
|
+
pub name: String,
|
|
116
|
+
pub fields: Vec<(String, TypeHint)>,
|
|
117
|
+
pub methods: Vec<FunctionDecl>,
|
|
118
|
+
pub span: Span,
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
#[derive(Debug, Clone)]
|
|
122
|
+
pub enum AssignTarget {
|
|
123
|
+
Var(String),
|
|
124
|
+
Field(Box<Expr>, String),
|
|
125
|
+
Index(Box<Expr>, Box<Expr>),
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
#[derive(Debug, Clone)]
|
|
129
|
+
pub enum Stmt {
|
|
130
|
+
VarDecl {
|
|
131
|
+
name: String,
|
|
132
|
+
ty: TypeHint,
|
|
133
|
+
init: Option<Expr>,
|
|
134
|
+
is_function_scoped: bool,
|
|
135
|
+
span: Span,
|
|
136
|
+
},
|
|
137
|
+
Assign {
|
|
138
|
+
target: AssignTarget,
|
|
139
|
+
op: Option<BinOp>,
|
|
140
|
+
value: Expr,
|
|
141
|
+
span: Span,
|
|
142
|
+
},
|
|
143
|
+
If {
|
|
144
|
+
cond: Expr,
|
|
145
|
+
then_body: Vec<Stmt>,
|
|
146
|
+
else_body: Vec<Stmt>,
|
|
147
|
+
span: Span,
|
|
148
|
+
},
|
|
149
|
+
While {
|
|
150
|
+
cond: Expr,
|
|
151
|
+
body: Vec<Stmt>,
|
|
152
|
+
span: Span,
|
|
153
|
+
},
|
|
154
|
+
ForRange {
|
|
155
|
+
var: String,
|
|
156
|
+
start: Expr,
|
|
157
|
+
end: Expr,
|
|
158
|
+
inclusive: bool,
|
|
159
|
+
step: Option<Expr>,
|
|
160
|
+
body: Vec<Stmt>,
|
|
161
|
+
span: Span,
|
|
162
|
+
},
|
|
163
|
+
ForIn {
|
|
164
|
+
var: String,
|
|
165
|
+
iter: Expr,
|
|
166
|
+
body: Vec<Stmt>,
|
|
167
|
+
span: Span,
|
|
168
|
+
},
|
|
169
|
+
Return(Option<Expr>, Span),
|
|
170
|
+
Break(Span),
|
|
171
|
+
Continue(Span),
|
|
172
|
+
ExprStmt(Expr, Span),
|
|
173
|
+
Block(Vec<Stmt>, Span),
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
#[derive(Debug, Clone)]
|
|
177
|
+
pub enum Expr {
|
|
178
|
+
Nil(Span),
|
|
179
|
+
Bool(bool, Span),
|
|
180
|
+
Int(i64, Span),
|
|
181
|
+
Float(f64, Span),
|
|
182
|
+
Str(String, Span),
|
|
183
|
+
Var(String, Span),
|
|
184
|
+
Array(Vec<Expr>, Span),
|
|
185
|
+
ObjectLit(Option<String>, Vec<(String, Expr)>, Span),
|
|
186
|
+
Unary(UnaryOp, Box<Expr>, Span),
|
|
187
|
+
Binary(BinOp, Box<Expr>, Box<Expr>, Span),
|
|
188
|
+
Update {
|
|
189
|
+
name: String,
|
|
190
|
+
inc: bool,
|
|
191
|
+
prefix: bool,
|
|
192
|
+
span: Span,
|
|
193
|
+
},
|
|
194
|
+
Call {
|
|
195
|
+
callee: Box<Expr>,
|
|
196
|
+
args: Vec<Expr>,
|
|
197
|
+
span: Span,
|
|
198
|
+
},
|
|
199
|
+
Member {
|
|
200
|
+
object: Box<Expr>,
|
|
201
|
+
field: String,
|
|
202
|
+
span: Span,
|
|
203
|
+
},
|
|
204
|
+
Index {
|
|
205
|
+
object: Box<Expr>,
|
|
206
|
+
index: Box<Expr>,
|
|
207
|
+
span: Span,
|
|
208
|
+
},
|
|
209
|
+
Ternary {
|
|
210
|
+
cond: Box<Expr>,
|
|
211
|
+
then_expr: Box<Expr>,
|
|
212
|
+
else_expr: Box<Expr>,
|
|
213
|
+
span: Span,
|
|
214
|
+
},
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
impl Expr {
|
|
218
|
+
pub fn span(&self) -> Span {
|
|
219
|
+
match self {
|
|
220
|
+
Expr::Nil(s)
|
|
221
|
+
| Expr::Bool(_, s)
|
|
222
|
+
| Expr::Int(_, s)
|
|
223
|
+
| Expr::Float(_, s)
|
|
224
|
+
| Expr::Str(_, s)
|
|
225
|
+
| Expr::Var(_, s)
|
|
226
|
+
| Expr::Array(_, s)
|
|
227
|
+
| Expr::ObjectLit(_, _, s)
|
|
228
|
+
| Expr::Unary(_, _, s)
|
|
229
|
+
| Expr::Binary(_, _, _, s)
|
|
230
|
+
| Expr::Update { span: s, .. }
|
|
231
|
+
| Expr::Call { span: s, .. }
|
|
232
|
+
| Expr::Member { span: s, .. }
|
|
233
|
+
| Expr::Index { span: s, .. }
|
|
234
|
+
| Expr::Ternary { span: s, .. } => *s,
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
#[derive(Debug, Clone)]
|
|
240
|
+
pub struct ModuleAst {
|
|
241
|
+
pub language: Language,
|
|
242
|
+
pub imports: Vec<ImportDecl>,
|
|
243
|
+
pub structs: Vec<StructDecl>,
|
|
244
|
+
pub functions: Vec<FunctionDecl>,
|
|
245
|
+
pub top_level: Vec<Stmt>,
|
|
246
|
+
}
|