Add struct literals, generics, log/print stdlib registration, activate DB wiring

- Register print/println/log/print_err in TypeEnv::with_builtins() with polymorphic (Unknown) param type so any value type is accepted without spurious warnings
- Add StructLit expr to AST + parser (uppercase-IDENT { ... } syntax), BuildStruct bytecode instruction + Struct value to runtime
- Add type_params to FnDef AST node and TypeParam variant to TypeExpr; parser parses <T, E> generics; type checker treats TypeParam as Unknown (universal type)
- Rewrite interpreter to support user-defined function calls via call stack (Frame + return_ip); dispatch_builtin handles print/println/log/print_err/__build_list__
- Fix engram_activate_search to unwrap { results: [...] } response envelope from /search; use std::net for sync HTTP to avoid reqwest dependency
- Add run-file command to CLI for single-file execution without el.toml
- Fix worktree engram-crypto path dep
This commit is contained in:
Will Anderson
2026-04-28 11:36:25 -05:00
parent 0a36a454f9
commit 977a2cd654
11 changed files with 532 additions and 34 deletions
+36
View File
@@ -370,6 +370,42 @@ impl TypeChecker {
}
}
}
Expr::StructLit { type_name, fields, .. } => {
// Look up the type definition
match self.env.get_type(type_name).cloned() {
Some(TypeDef::Struct { fields: declared_fields, .. }) => {
// Check that all provided fields are valid and have compatible types
for (field_name, field_expr) in fields {
let got_ty = self.infer_expr(field_expr);
if let Some((_, expected_ty)) = declared_fields.iter().find(|(n, _)| n == field_name) {
if !self.env.check_compatible(&got_ty, expected_ty) {
self.emit_error(TypeErrorKind::TypeMismatch {
expected: expected_ty.to_string(),
got: got_ty.to_string(),
});
}
} else {
self.emit_error(TypeErrorKind::UnknownField {
type_name: type_name.clone(),
field: field_name.clone(),
});
}
}
Type::Named(type_name.clone())
}
Some(_) => {
// Not a struct — can't construct with struct literal syntax
self.emit_error(TypeErrorKind::UndefinedType(
format!("{type_name} is not a struct type"),
));
Type::Unknown
}
None => {
self.emit_error(TypeErrorKind::UndefinedType(type_name.clone()));
Type::Unknown
}
}
}
}
}
+17 -1
View File
@@ -103,7 +103,7 @@ pub struct TypeEnv {
}
impl TypeEnv {
/// Create a fresh environment pre-populated with built-in types.
/// Create a fresh environment pre-populated with built-in types and functions.
pub fn with_builtins() -> Self {
let mut env = Self::default();
// Register primitive types so Named("Int") resolves
@@ -113,6 +113,17 @@ impl TypeEnv {
env.types.insert("Bool".into(), TypeDef::Primitive(Type::Bool));
env.types.insert("Uuid".into(), TypeDef::Primitive(Type::Uuid));
env.types.insert("Void".into(), TypeDef::Primitive(Type::Void));
// Register built-in output functions — accept any value (Unknown = polymorphic)
let void_fn_any = Type::Fn {
params: vec![Type::Unknown],
return_type: Box::new(Type::Void),
};
env.functions.insert("print".into(), void_fn_any.clone());
env.functions.insert("println".into(), void_fn_any.clone());
env.functions.insert("log".into(), void_fn_any.clone());
env.functions.insert("print_err".into(), void_fn_any);
env
}
@@ -244,6 +255,11 @@ impl TypeEnv {
let ret = self.resolve_type_expr(return_type)?;
Ok(Type::Fn { params: ps, return_type: Box::new(ret) })
}
el_parser::TypeExpr::TypeParam(_) => {
// Generic type parameters resolve to Unknown at the call site —
// the actual type is inferred from arguments during call checking.
Ok(Type::Unknown)
}
}
}
}