Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ repository = "https://github.com/Inferara/inference"
# Core Inference crates
inference = { path = "./core/inference", version = "0.0.1" }
inference-ast = { path = "./core/ast", version = "0.0.1" }
inference-hir = { path = "./core/hir", version = "0.0.1" }
inference-cli = { path = "./core/cli", version = "0.0.1" }
inference-wasm-to-v-translator = { path = "./core/wasm-to-v", version = "0.0.1" }
inference-wasm-codegen = { path = "./core/wasm-codegen", version = "0.0.1" }
Expand All @@ -34,7 +35,7 @@ wat-fmt = { path = "./tools/wat-fmt", version = "0.0.9" }
inf-wast = { path = "./tools/inf-wast", version = "0.0.9" }
inf-wasmparser = { path = "./tools/inf-wasmparser", version = "0.0.9" }

tree-sitter = "0.26.2"
tree-sitter = "0.26.3"
tree-sitter-inference = "0.0.37"
anyhow = "1.0.100"
serde = { version = "1.0.228", features = ["derive", "rc"] }
Expand Down
File renamed without changes.
11 changes: 10 additions & 1 deletion core/ast/src/arena.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use std::{collections::HashMap, rc::Rc};

use crate::nodes::{AstNode, Definition, TypeDefinition};
use crate::nodes::{AstNode, Definition, SourceFile, TypeDefinition};

#[derive(Default, Clone)]
pub struct Arena {
pub(crate) nodes: HashMap<u32, AstNode>,
pub(crate) node_routes: Vec<NodeRoute>,
pub sources: Vec<SourceFile>,
}

impl Arena {
/// Adds a node to the arena with the specified parent.
///
/// # Panics
///
/// Panics if the node ID is zero or if a node with the same ID already exists in the arena.
pub fn add_node(&mut self, node: AstNode, parent_id: u32) {
// println!("Adding node with ID: {node:?}");
assert!(node.id() != 0, "Node ID must be non-zero");
Expand Down Expand Up @@ -36,10 +42,12 @@ impl Arena {
self.node_routes.push(node);
}

#[must_use]
pub fn find_node(&self, id: u32) -> Option<AstNode> {
self.nodes.get(&id).cloned()
}

#[must_use]
pub fn find_parent_node(&self, id: u32) -> Option<u32> {
self.node_routes
.iter()
Expand Down Expand Up @@ -73,6 +81,7 @@ impl Arena {
result
}

#[must_use]
pub fn list_type_definitions(&self) -> Vec<Rc<TypeDefinition>> {
self.list_nodes_cmp(|node| {
if let AstNode::Definition(Definition::Type(type_def)) = node {
Expand Down
34 changes: 3 additions & 31 deletions core/ast/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use crate::nodes::{
ArgumentType, Directive, IgnoreArgument, Misc, SelfReference, StructExpression,
TypeMemberAccessExpression,
};
use crate::type_infer::TypeChecker;
use crate::type_info::TypeInfo;
use crate::{
arena::Arena,
nodes::{
Expand All @@ -20,7 +18,6 @@ use crate::{
TypeQualifiedName, UnaryOperatorKind, UnitLiteral, UseDirective, UzumakiExpression,
VariableDefinitionStatement,
},
t_ast::TypedAst,
};
use tree_sitter::Node;

Expand All @@ -40,7 +37,6 @@ pub type CompletedBuilder<'a> = Builder<'a, CompleteState>;
pub struct Builder<'a, S> {
arena: Arena,
source_code: Vec<(Node<'a>, &'a [u8])>,
t_ast: Option<TypedAst>,
_state: PhantomData<S>,
}

Expand All @@ -56,7 +52,6 @@ impl<'a> Builder<'a, InitState> {
Self {
arena: Arena::default(),
source_code: Vec::new(),
t_ast: None,
_state: PhantomData,
}
}
Expand Down Expand Up @@ -109,20 +104,10 @@ impl<'a> Builder<'a, InitState> {
}
res.push(ast);
}
let mut type_checker = TypeChecker::new();
let _ = type_checker.infer_types(&mut res);

// let mut type_checker = TypeChecker::new();
let t_ast = TypedAst::new(res, self.arena.clone());
t_ast.infer_expression_types();
// run type inference over all expressions
// type_checker
// .infer_types(&t_ast.source_files)
// .map_err(|e| anyhow::Error::msg(format!("Type error: {e:?}")))?;
self.arena.sources = res;
Ok(Builder {
arena: Arena::default(),
arena: std::mem::take(&mut self.arena),
source_code: Vec::new(),
t_ast: Some(t_ast),
_state: PhantomData,
})
}
Expand Down Expand Up @@ -1260,9 +1245,6 @@ impl<'a> Builder<'a, InitState> {
node.utf8_text(code).unwrap().to_string()
};
let node = Rc::new(SimpleType::new(id, location, name));
node.type_info
.borrow_mut()
.replace(TypeInfo::new(&Type::Simple(node.clone())));
self.arena.add_node(
AstNode::Expression(Expression::Type(Type::Simple(node.clone()))),
parent_id,
Expand Down Expand Up @@ -1418,14 +1400,4 @@ impl<'a> Builder<'a, InitState> {
}
}

impl Builder<'_, CompleteState> {
/// Returns typed AST
///
/// # Panics
///
/// This function will panic if resulted `TypedAst` is `None` which means an error occured during the parsing process.
#[must_use]
pub fn t_ast(self) -> TypedAst {
self.t_ast.unwrap()
}
}
impl Builder<'_, CompleteState> {}
94 changes: 93 additions & 1 deletion core/ast/src/enums_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,96 @@
use crate::nodes::Type;
use crate::nodes::{BlockType, Definition, Expression, Statement, Type};

impl Definition {
#[must_use]
pub fn name(&self) -> String {
match self {
Definition::Spec(spec) => spec.name.name(),
Definition::Struct(struct_def) => struct_def.name.name(),
Definition::Enum(enum_def) => enum_def.name.name(),
Definition::Constant(const_def) => const_def.name.name(),
Definition::Function(func_def) => func_def.name.name(),
Definition::ExternalFunction(ext_func_def) => ext_func_def.name.name(),
Definition::Type(type_def) => type_def.name.name(),
}
}
}

impl BlockType {
#[must_use]
pub fn statements(&self) -> Vec<Statement> {
match self {
BlockType::Block(block)
| BlockType::Forall(block)
| BlockType::Assume(block)
| BlockType::Exists(block)
| BlockType::Unique(block) => block.statements.clone(),
}
}
#[must_use]
pub fn is_non_det(&self) -> bool {
match self {
BlockType::Block(block) => block
.statements
.iter()
.any(super::nodes::Statement::is_non_det),
_ => true,
}
}
#[must_use]
pub fn is_void(&self) -> bool {
let fn_find_ret_stmt = |statements: &Vec<Statement>| -> bool {
for stmt in statements {
match stmt {
Statement::Return(_) => return true,
Statement::Block(block_type) => {
if block_type.is_void() {
return true;
}
}
_ => {}
}
}
false
};
!fn_find_ret_stmt(&self.statements())
}
}

impl Statement {
#[must_use]
pub fn is_non_det(&self) -> bool {
match self {
Statement::Block(block_type) => !matches!(block_type, BlockType::Block(_)),
Statement::Expression(expr_stmt) => expr_stmt.is_non_det(),
Statement::Return(ret_stmt) => ret_stmt.expression.borrow().is_non_det(),
Statement::Loop(loop_stmt) => loop_stmt
.condition
.borrow()
.as_ref()
.is_some_and(super::nodes::Expression::is_non_det),
Statement::If(if_stmt) => {
if_stmt.condition.borrow().is_non_det()
|| if_stmt.if_arm.is_non_det()
|| if_stmt
.else_arm
.as_ref()
.is_some_and(super::nodes::BlockType::is_non_det)
}
Statement::VariableDefinition(var_def) => var_def
.value
.as_ref()
.is_some_and(|value| value.borrow().is_non_det()),
_ => false,
}
}
}

impl Expression {
#[must_use]
pub fn is_non_det(&self) -> bool {
matches!(self, Expression::Uzumaki(_))
}
}

impl Type {
pub(crate) fn is_unit_type(&self) -> bool {
Expand Down
5 changes: 1 addition & 4 deletions core/ast/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#![warn(clippy::pedantic)]
pub(crate) mod arena;
pub mod arena;
pub mod builder;
pub(crate) mod enums_impl;
pub mod nodes;
pub(crate) mod nodes_impl;
pub mod t_ast;
pub mod type_infer;
pub mod type_info;
51 changes: 0 additions & 51 deletions core/ast/src/node_kind.rs

This file was deleted.

Loading
Loading