Skip to content

Commit

Permalink
refactor(inference): rename semantic crate to inference.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Mar 12, 2024
1 parent 5fb255d commit ee8cf6a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fuse_codegen = { version = "0.0.0", path = "crates/fuse-codegen" }
fuse_common = { version = "0.0.0", path = "crates/fuse-common" }
fuse_common_proc = { version = "0.0.0", path = "crates/fuse-common-proc" }
fuse_parser = { version = "0.0.0", path = "crates/fuse-parser" }
fuse_semantic = { version = "0.0.0", path = "crates/fuse-semantic" }
fuse_inference = { version = "0.0.0", path = "crates/fuse-inference" }
fuse_visitor = { version = "0.0.0", path = "crates/fuse-visitor" }
fusec = { version = "0.0.0", path = "crates/fusec" }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "fuse_semantic"
name = "fuse_inference"
version = "0.0.0"
description.workspace = true
authors.workspace = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use std::collections::HashMap;

use fuse_ast::{Atom, BindingPatternKind, Chunk, Function, Identifier, VariableDeclaration};
use fuse_ast::{
Atom, BindingPatternKind, CallExpression, Chunk, Function, Identifier, VariableDeclaration,
};
use fuse_common::ReferenceType;
use fuse_visitor::{walk_function_mut, walk_variable_declaration_mut, ScopeVisitor, VisitorMut};
use fuse_visitor::{
walk_call_expression_mut, walk_function_mut, walk_variable_declaration_mut, ScopeVisitor,
VisitorMut,
};

#[derive(Debug, PartialEq, Clone, Copy)]
struct ScopeId(ReferenceType);
Expand Down Expand Up @@ -120,13 +125,13 @@ impl ScopeTree {
}
}

pub struct Semantic<'ast> {
pub struct Inference<'ast> {
source: &'ast str,
scope: ScopeTree,
last_reference: ReferenceType,
}

impl<'ast> Semantic<'ast> {
impl<'ast> Inference<'ast> {
pub fn new(source: &'ast str) -> Self {
Self {
source,
Expand All @@ -135,8 +140,11 @@ impl<'ast> Semantic<'ast> {
}
}

pub fn build(&mut self, chunk: &'ast mut Chunk) {
self.visit_chunk_mut(chunk)
pub fn resolve(&mut self, chunk: &'ast mut Chunk) -> InferenceResult {
self.visit_chunk_mut(chunk);
InferenceResult {
errors: Vec::default(),
}
}

fn declare_identifier(&mut self, ident: &Identifier) {
Expand All @@ -147,15 +155,12 @@ impl<'ast> Semantic<'ast> {
}

fn reference_identifier(&mut self, ident: &Identifier) {
let reference = self
.scope
.identifier_reference(&ident.name)
.expect("Reference to undefined identifier.");
ident.reference.set(Some(reference))
let reference = self.scope.identifier_reference(&ident.name);
ident.reference.set(reference)
}
}

impl<'ast> VisitorMut<'ast> for Semantic<'ast> {
impl<'ast> VisitorMut<'ast> for Inference<'ast> {
fn visit_identifier_mut(&mut self, ident: &'ast mut Identifier) {
if ident.reference.get_mut().is_none() {
self.reference_identifier(ident);
Expand All @@ -182,7 +187,7 @@ impl<'ast> VisitorMut<'ast> for Semantic<'ast> {
}
}

impl<'ast> ScopeVisitor for Semantic<'ast> {
impl<'ast> ScopeVisitor for Inference<'ast> {
fn enter_scope(&mut self) {
self.scope.push_stack();
}
Expand All @@ -191,3 +196,9 @@ impl<'ast> ScopeVisitor for Semantic<'ast> {
self.scope.pop_stack();
}
}

pub struct InferenceResult {
pub errors: Vec<InferenceError>,
}

pub struct InferenceError {}
2 changes: 1 addition & 1 deletion crates/fusec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ edition.workspace = true

[dependencies]
fuse_parser = { workspace = true }
fuse_semantic = { workspace = true }
fuse_inference = { workspace = true }
16 changes: 6 additions & 10 deletions crates/fusec/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
use fuse_parser::Parser;
use fuse_semantic::Semantic;
use fuse_inference::Inference;

fn compile_chunk(source: &str) {
let parsed = Parser::new(source).parse();
assert!(!parsed.paniced);
assert!(parsed.errors.len() == 0);
let mut chunk = parsed.chunk.unwrap();
let semantic = Semantic::new(source).build(&mut chunk);
let inference = Inference::new(source).resolve(&mut chunk);
// panic!("{:#?}", chunk)
}

#[test]
fn manual_test() {
compile_chunk(
r#"
let z = 123
let x = 123
let y = x
fn x()
let x = y
end
x()
let a = 0
let d = a.b.c
"#,
)
}

0 comments on commit ee8cf6a

Please sign in to comment.