Skip to content

Commit

Permalink
improvement: experiment with semantic analysis ideas.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Mar 14, 2024
1 parent 4a469fa commit ca41a45
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 60 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ fuse_ast = { version = "0.0.0", path = "crates/fuse-ast" }
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_ir = { version = "0.0.0", path = "crates/fuse-ir" }
fuse_parser = { version = "0.0.0", path = "crates/fuse-parser" }
fuse_resolve = { version = "0.0.0", path = "crates/fuse-resolve" }
fuse_semantic = { version = "0.0.0", path = "crates/fuse-semantic" }
fuse_visitor = { version = "0.0.0", path = "crates/fuse-visitor" }
fusec = { version = "0.0.0", path = "crates/fusec" }

Expand Down
6 changes: 6 additions & 0 deletions crates/fuse-ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ pub struct TypeAnnotation {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Atom(pub Rc<str>);

impl Atom {
pub fn as_str<'a>(&'a self) -> &'a str {
&self.0
}
}

#[serializable]
#[derive(Debug, PartialEq)]
pub enum Expression {
Expand Down
14 changes: 14 additions & 0 deletions crates/fuse-ir/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "fuse_ir"
version = "0.0.0"
description.workspace = true
authors.workspace = true
license.workspace = true
repository.workspace = true
edition.workspace = true

[dependencies]
fuse_ast = { workspace = true }
fuse_common = { workspace = true }
fuse_visitor = { workspace = true }

21 changes: 21 additions & 0 deletions crates/fuse-ir/src/ir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use fuse_ast::Identifier;

pub enum PrimitiveType {
Number,
String,
Boolean,
}

impl PrimitiveType {
/// All of the primitive types
pub const ALL: [Self; 3] = [Self::Number, Self::Boolean, Self::String];

pub fn from_identifier(ident: &Identifier) -> Option<Self> {
match ident.name.as_str() {
"number" => Some(Self::Number),
"string" => Some(Self::String),
"boolean" => Some(Self::Boolean),
_ => None,
}
}
}
3 changes: 3 additions & 0 deletions crates/fuse-ir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod ir;

pub use ir::*;
Empty file.
74 changes: 16 additions & 58 deletions crates/fuse-resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,28 +126,19 @@ impl ScopeTree {
}
}

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

impl<'ast> Resolver<'ast> {
pub fn new(source: &'ast str) -> Self {
impl Module {
fn new() -> Self {
Self {
source,
scope: ScopeTree::root_scope(),
last_reference: 0,
}
}

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

fn declare_identifier(&mut self, ident: &Identifier) {
self.last_reference += 1;
self.scope
Expand All @@ -161,56 +152,23 @@ impl<'ast> Resolver<'ast> {
}
}

impl<'ast> VisitorMut<'ast> for Resolver<'ast> {
fn visit_identifier_mut(&mut self, ident: &'ast mut Identifier) {
if ident.reference.get_mut().is_none() {
self.reference_identifier(ident);
}
}

fn visit_variable_declaration_mut(&mut self, decl: &'ast mut VariableDeclaration) {
match &decl.binding.kind {
BindingPatternKind::Identifier(bind) => self.declare_identifier(&bind.identifier),
_ => todo!(),
}

walk_variable_declaration_mut(self, decl)
}

fn visit_function_declaration_mut(&mut self, decl: &'ast mut Function) {
let identifier = decl
.signature
.identifier
.as_ref()
.expect("All function declarations need an identifier.");
self.declare_identifier(identifier);
walk_function_mut(self, decl)
}

fn visit_binary_operator_mut(&mut self, op: &'ast mut BinaryOperator) {
match &op.kind {
BinaryOperatorKind::Member(_) => {
println!("{:?}", op);
// let rhs = match &op.rhs {
// Expression::Identifier(rhs) => rhs,
// Expression::BinaryOperator(op) => match op {
// },
// _ => panic!("Right hand side of a member(.) operator should be an identifier"),
// };
}
_ => {}
}
walk_binary_operator_mut(self, op)
}
pub struct Resolver<'ast> {
source: &'ast str,
modules: HashMap<&'ast str, Module>,
}

impl<'ast> ScopeVisitor for Resolver<'ast> {
fn enter_scope(&mut self) {
self.scope.push_stack();
impl<'ast> Resolver<'ast> {
pub fn new(source: &'ast str) -> Self {
Self {
source,
modules: HashMap::new(),
}
}

fn leave_scope(&mut self) {
self.scope.pop_stack();
pub fn resolve(&mut self, chunk: &'ast mut Chunk) -> ResolverResult {
ResolverResult {
errors: Vec::default(),
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions crates/fuse-semantic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "fuse_semantic"
version = "0.0.0"
description.workspace = true
authors.workspace = true
license.workspace = true
repository.workspace = true
edition.workspace = true

[dependencies]
fuse_ast = { workspace = true }
fuse_common = { workspace = true }
fuse_visitor = { workspace = true }
Loading

0 comments on commit ca41a45

Please sign in to comment.