Skip to content

Commit ca41a45

Browse files
committed
improvement: experiment with semantic analysis ideas.
1 parent 4a469fa commit ca41a45

File tree

11 files changed

+330
-60
lines changed

11 files changed

+330
-60
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ fuse_ast = { version = "0.0.0", path = "crates/fuse-ast" }
1616
fuse_codegen = { version = "0.0.0", path = "crates/fuse-codegen" }
1717
fuse_common = { version = "0.0.0", path = "crates/fuse-common" }
1818
fuse_common_proc = { version = "0.0.0", path = "crates/fuse-common-proc" }
19+
fuse_ir = { version = "0.0.0", path = "crates/fuse-ir" }
1920
fuse_parser = { version = "0.0.0", path = "crates/fuse-parser" }
2021
fuse_resolve = { version = "0.0.0", path = "crates/fuse-resolve" }
22+
fuse_semantic = { version = "0.0.0", path = "crates/fuse-semantic" }
2123
fuse_visitor = { version = "0.0.0", path = "crates/fuse-visitor" }
2224
fusec = { version = "0.0.0", path = "crates/fusec" }
2325

crates/fuse-ast/src/ast.rs

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ pub struct TypeAnnotation {
9797
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
9898
pub struct Atom(pub Rc<str>);
9999

100+
impl Atom {
101+
pub fn as_str<'a>(&'a self) -> &'a str {
102+
&self.0
103+
}
104+
}
105+
100106
#[serializable]
101107
#[derive(Debug, PartialEq)]
102108
pub enum Expression {

crates/fuse-ir/Cargo.toml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "fuse_ir"
3+
version = "0.0.0"
4+
description.workspace = true
5+
authors.workspace = true
6+
license.workspace = true
7+
repository.workspace = true
8+
edition.workspace = true
9+
10+
[dependencies]
11+
fuse_ast = { workspace = true }
12+
fuse_common = { workspace = true }
13+
fuse_visitor = { workspace = true }
14+

crates/fuse-ir/src/ir.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use fuse_ast::Identifier;
2+
3+
pub enum PrimitiveType {
4+
Number,
5+
String,
6+
Boolean,
7+
}
8+
9+
impl PrimitiveType {
10+
/// All of the primitive types
11+
pub const ALL: [Self; 3] = [Self::Number, Self::Boolean, Self::String];
12+
13+
pub fn from_identifier(ident: &Identifier) -> Option<Self> {
14+
match ident.name.as_str() {
15+
"number" => Some(Self::Number),
16+
"string" => Some(Self::String),
17+
"boolean" => Some(Self::Boolean),
18+
_ => None,
19+
}
20+
}
21+
}

crates/fuse-ir/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod ir;
2+
3+
pub use ir::*;

crates/fuse-resolve/src/import_resolver.rs

Whitespace-only changes.

crates/fuse-resolve/src/lib.rs

+16-58
Original file line numberDiff line numberDiff line change
@@ -126,28 +126,19 @@ impl ScopeTree {
126126
}
127127
}
128128

129-
pub struct Resolver<'ast> {
130-
source: &'ast str,
129+
pub struct Module {
131130
scope: ScopeTree,
132131
last_reference: ReferenceType,
133132
}
134133

135-
impl<'ast> Resolver<'ast> {
136-
pub fn new(source: &'ast str) -> Self {
134+
impl Module {
135+
fn new() -> Self {
137136
Self {
138-
source,
139137
scope: ScopeTree::root_scope(),
140138
last_reference: 0,
141139
}
142140
}
143141

144-
pub fn resolve(&mut self, chunk: &'ast mut Chunk) -> ResolverResult {
145-
self.visit_chunk_mut(chunk);
146-
ResolverResult {
147-
errors: Vec::default(),
148-
}
149-
}
150-
151142
fn declare_identifier(&mut self, ident: &Identifier) {
152143
self.last_reference += 1;
153144
self.scope
@@ -161,56 +152,23 @@ impl<'ast> Resolver<'ast> {
161152
}
162153
}
163154

164-
impl<'ast> VisitorMut<'ast> for Resolver<'ast> {
165-
fn visit_identifier_mut(&mut self, ident: &'ast mut Identifier) {
166-
if ident.reference.get_mut().is_none() {
167-
self.reference_identifier(ident);
168-
}
169-
}
170-
171-
fn visit_variable_declaration_mut(&mut self, decl: &'ast mut VariableDeclaration) {
172-
match &decl.binding.kind {
173-
BindingPatternKind::Identifier(bind) => self.declare_identifier(&bind.identifier),
174-
_ => todo!(),
175-
}
176-
177-
walk_variable_declaration_mut(self, decl)
178-
}
179-
180-
fn visit_function_declaration_mut(&mut self, decl: &'ast mut Function) {
181-
let identifier = decl
182-
.signature
183-
.identifier
184-
.as_ref()
185-
.expect("All function declarations need an identifier.");
186-
self.declare_identifier(identifier);
187-
walk_function_mut(self, decl)
188-
}
189-
190-
fn visit_binary_operator_mut(&mut self, op: &'ast mut BinaryOperator) {
191-
match &op.kind {
192-
BinaryOperatorKind::Member(_) => {
193-
println!("{:?}", op);
194-
// let rhs = match &op.rhs {
195-
// Expression::Identifier(rhs) => rhs,
196-
// Expression::BinaryOperator(op) => match op {
197-
// },
198-
// _ => panic!("Right hand side of a member(.) operator should be an identifier"),
199-
// };
200-
}
201-
_ => {}
202-
}
203-
walk_binary_operator_mut(self, op)
204-
}
155+
pub struct Resolver<'ast> {
156+
source: &'ast str,
157+
modules: HashMap<&'ast str, Module>,
205158
}
206159

207-
impl<'ast> ScopeVisitor for Resolver<'ast> {
208-
fn enter_scope(&mut self) {
209-
self.scope.push_stack();
160+
impl<'ast> Resolver<'ast> {
161+
pub fn new(source: &'ast str) -> Self {
162+
Self {
163+
source,
164+
modules: HashMap::new(),
165+
}
210166
}
211167

212-
fn leave_scope(&mut self) {
213-
self.scope.pop_stack();
168+
pub fn resolve(&mut self, chunk: &'ast mut Chunk) -> ResolverResult {
169+
ResolverResult {
170+
errors: Vec::default(),
171+
}
214172
}
215173
}
216174

crates/fuse-semantic/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "fuse_semantic"
3+
version = "0.0.0"
4+
description.workspace = true
5+
authors.workspace = true
6+
license.workspace = true
7+
repository.workspace = true
8+
edition.workspace = true
9+
10+
[dependencies]
11+
fuse_ast = { workspace = true }
12+
fuse_common = { workspace = true }
13+
fuse_visitor = { workspace = true }

0 commit comments

Comments
 (0)