Skip to content

Commit 3c873ff

Browse files
committed
refactor: remove type tracking from ZynPEG parser
Remove opaque_types HashMap and type resolution logic from parser runtime. Parser now creates Type::Named with placeholder TypeId(0) for all unknown types, deferring resolution to the compiler's type system. Changes: - Remove opaque_types field from TypedAstBuilder - Simplify create_named_type to always create placeholder - Simplify create_primitive_type to create placeholder for unknown types - Remove opaque type lookups from create_identifier This properly separates concerns: ZynPEG builds TypedAST structure, compiler resolves types using TypeRegistry.
1 parent fc9205f commit 3c873ff

2 files changed

Lines changed: 22 additions & 47 deletions

File tree

crates/typed_ast/src/type_checker.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::type_inference::{InferenceContext, InferenceError};
1313
use crate::type_registry::{CallingConvention, MethodSig, Mutability, ParamInfo, PrimitiveType, Type, TypeBound, Visibility};
1414
use crate::{typed_ast::*, AsyncKind};
1515
use std::collections::HashMap;
16-
use string_interner::Symbol as SymbolTrait;
1716

1817
/// Type checking context
1918
pub struct TypeChecker {

crates/zyn_peg/src/runtime.rs

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,8 +1113,6 @@ pub struct TypedAstBuilder {
11131113
variable_types: HashMap<String, Type>,
11141114
/// Enum type name to variant names (in order, for discriminant calculation)
11151115
enum_types: HashMap<String, Vec<String>>,
1116-
/// Opaque/extern type name to external name mapping (e.g., "Tensor" -> "$Tensor")
1117-
opaque_types: HashMap<String, InternedString>,
11181116
/// Program declaration handles (in order)
11191117
program_decls: Vec<NodeHandle>,
11201118
/// Current span being processed (start, end)
@@ -1147,7 +1145,6 @@ impl TypedAstBuilder {
11471145
types: HashMap::new(),
11481146
variable_types: HashMap::new(),
11491147
enum_types: HashMap::new(),
1150-
opaque_types: HashMap::new(),
11511148
program_decls: Vec::new(),
11521149
current_span: (0, 0),
11531150
}
@@ -1610,10 +1607,6 @@ impl AstHostFunctions for TypedAstBuilder {
16101607
let name_interned = self.inner.intern(name);
16111608
let runtime_prefix = self.inner.intern(external_name);
16121609

1613-
// Register the opaque type mapping for later type resolution
1614-
self.opaque_types.insert(name.to_string(), runtime_prefix);
1615-
eprintln!("[DEBUG create_opaque_type] Registered opaque type: '{}' -> '{}'", name, external_name);
1616-
16171610
let extern_struct = TypedExternStruct {
16181611
name: name_interned,
16191612
runtime_prefix,
@@ -1720,17 +1713,10 @@ impl AstHostFunctions for TypedAstBuilder {
17201713
let span = self.default_span();
17211714

17221715
// Look up the variable's actual type from our tracking map
1723-
// If not found, check opaque types, then default to I32
1724-
let var_type = if let Some(ty) = self.variable_types.get(name) {
1725-
ty.clone()
1726-
} else if let Some(runtime_prefix) = self.opaque_types.get(name) {
1727-
Type::Extern {
1728-
name: *runtime_prefix,
1729-
layout: None,
1730-
}
1731-
} else {
1732-
Type::Primitive(PrimitiveType::I32)
1733-
};
1716+
// If not found, default to I32
1717+
let var_type = self.variable_types.get(name)
1718+
.cloned()
1719+
.unwrap_or(Type::Primitive(PrimitiveType::I32));
17341720

17351721
eprintln!("[DEBUG create_identifier] Variable '{}' has type {:?}", name, var_type);
17361722
let expr = self.inner.variable(name, var_type, span);
@@ -2104,16 +2090,14 @@ impl AstHostFunctions for TypedAstBuilder {
21042090
"bool" => Type::Primitive(PrimitiveType::Bool),
21052091
"void" | "unit" => Type::Primitive(PrimitiveType::Unit),
21062092
_ => {
2107-
// Check if this is a registered opaque/extern type
2108-
if let Some(runtime_prefix) = self.opaque_types.get(name) {
2109-
eprintln!("[DEBUG create_primitive_type] Found opaque type '{}' -> '{}'", name, runtime_prefix.resolve_global().unwrap_or_default());
2110-
Type::Extern {
2111-
name: *runtime_prefix,
2112-
layout: None,
2113-
}
2114-
} else {
2115-
eprintln!("[DEBUG create_primitive_type] Type '{}' not found in opaque types, defaulting to I32", name);
2116-
Type::Primitive(PrimitiveType::I32) // Default to i32
2093+
// Unknown type - use Named with placeholder for compiler resolution
2094+
eprintln!("[DEBUG create_primitive_type] Unknown type '{}', creating Named with placeholder TypeId(0)", name);
2095+
Type::Named {
2096+
id: zyntax_typed_ast::TypeId::new(0),
2097+
type_args: vec![],
2098+
const_args: vec![],
2099+
variance: vec![],
2100+
nullability: zyntax_typed_ast::type_registry::NullabilityKind::NonNull,
21172101
}
21182102
}
21192103
};
@@ -2140,24 +2124,16 @@ impl AstHostFunctions for TypedAstBuilder {
21402124
fn create_named_type(&mut self, name: &str) -> NodeHandle {
21412125
let handle = self.alloc_handle();
21422126

2143-
// Check if this is a registered opaque/extern type
2144-
let ty = if let Some(runtime_prefix) = self.opaque_types.get(name) {
2145-
eprintln!("[DEBUG create_named_type] Found opaque type '{}' -> '{}'", name, runtime_prefix.resolve_global().unwrap_or_default());
2146-
Type::Extern {
2147-
name: *runtime_prefix,
2148-
layout: None,
2149-
}
2150-
} else {
2151-
eprintln!("[DEBUG create_named_type] Type '{}' not found in opaque types, using Named with placeholder ID", name);
2152-
// Create a named type with placeholder ID (0)
2153-
// Type inference will resolve this to the actual TypeId
2154-
Type::Named {
2155-
id: zyntax_typed_ast::TypeId::new(0),
2156-
type_args: vec![],
2157-
const_args: vec![],
2158-
variance: vec![],
2159-
nullability: zyntax_typed_ast::type_registry::NullabilityKind::NonNull,
2160-
}
2127+
// Create a named type with placeholder ID (0)
2128+
// The compiler's type resolution phase will resolve this to the actual TypeId
2129+
// using the TypeRegistry
2130+
eprintln!("[DEBUG create_named_type] Creating named type '{}' with placeholder TypeId(0)", name);
2131+
let ty = Type::Named {
2132+
id: zyntax_typed_ast::TypeId::new(0),
2133+
type_args: vec![],
2134+
const_args: vec![],
2135+
variance: vec![],
2136+
nullability: zyntax_typed_ast::type_registry::NullabilityKind::NonNull,
21612137
};
21622138

21632139
self.types.insert(handle, ty);

0 commit comments

Comments
 (0)