Compare commits
7 Commits
6a2e4a7ac1
...
3a2376cd49
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a2376cd49 | |||
| 4dfb04a1b6 | |||
| 3cdde02eb2 | |||
| a5762d0397 | |||
| 1132c621c6 | |||
| a0fff1814e | |||
| 4e9e823246 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -770,7 +770,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lux"
|
name = "lux"
|
||||||
version = "0.1.2"
|
version = "0.1.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"lsp-server",
|
"lsp-server",
|
||||||
"lsp-types",
|
"lsp-types",
|
||||||
|
|||||||
225
projects/lux-compiler/ast.lux
Normal file
225
projects/lux-compiler/ast.lux
Normal file
@@ -0,0 +1,225 @@
|
|||||||
|
// Lux AST — Self-hosted Abstract Syntax Tree definitions
|
||||||
|
//
|
||||||
|
// Direct translation of src/ast.rs into Lux ADTs.
|
||||||
|
// These types represent the parsed structure of a Lux program.
|
||||||
|
//
|
||||||
|
// Naming conventions to avoid collisions:
|
||||||
|
// Ex = Expr variant, Pat = Pattern, Te = TypeExpr
|
||||||
|
// Td = TypeDef, Vf = VariantFields, Op = Operator
|
||||||
|
// Decl = Declaration, St = Statement
|
||||||
|
|
||||||
|
// === Source Location ===
|
||||||
|
|
||||||
|
type Span = | Span(Int, Int)
|
||||||
|
|
||||||
|
// === Identifiers ===
|
||||||
|
|
||||||
|
type Ident = | Ident(String, Span)
|
||||||
|
|
||||||
|
// === Visibility ===
|
||||||
|
|
||||||
|
type Visibility = | Public | Private
|
||||||
|
|
||||||
|
// === Schema Evolution ===
|
||||||
|
|
||||||
|
type Version = | Version(Int, Span)
|
||||||
|
|
||||||
|
type VersionConstraint =
|
||||||
|
| VcExact(Version)
|
||||||
|
| VcAtLeast(Version)
|
||||||
|
| VcLatest(Span)
|
||||||
|
|
||||||
|
// === Behavioral Types ===
|
||||||
|
|
||||||
|
type BehavioralProperty =
|
||||||
|
| BpPure
|
||||||
|
| BpTotal
|
||||||
|
| BpIdempotent
|
||||||
|
| BpDeterministic
|
||||||
|
| BpCommutative
|
||||||
|
|
||||||
|
// === Trait Bound (needed before WhereClause) ===
|
||||||
|
|
||||||
|
type TraitBound = | TraitBound(Ident, List<TypeExpr>, Span)
|
||||||
|
|
||||||
|
// === Trait Constraint (needed before WhereClause) ===
|
||||||
|
|
||||||
|
type TraitConstraint = | TraitConstraint(Ident, List<TraitBound>, Span)
|
||||||
|
|
||||||
|
// === Where Clauses ===
|
||||||
|
|
||||||
|
type WhereClause =
|
||||||
|
| WcProperty(Ident, BehavioralProperty, Span)
|
||||||
|
| WcResult(Expr, Span)
|
||||||
|
| WcTrait(TraitConstraint)
|
||||||
|
|
||||||
|
// === Module Path ===
|
||||||
|
|
||||||
|
type ModulePath = | ModulePath(List<Ident>, Span)
|
||||||
|
|
||||||
|
// === Import ===
|
||||||
|
|
||||||
|
// path, alias, items, wildcard, span
|
||||||
|
type ImportDecl = | ImportDecl(ModulePath, Option<Ident>, Option<List<Ident>>, Bool, Span)
|
||||||
|
|
||||||
|
// === Program ===
|
||||||
|
|
||||||
|
type Program = | Program(List<ImportDecl>, List<Declaration>)
|
||||||
|
|
||||||
|
// === Declarations ===
|
||||||
|
|
||||||
|
type Declaration =
|
||||||
|
| DeclFunction(FunctionDecl)
|
||||||
|
| DeclEffect(EffectDecl)
|
||||||
|
| DeclType(TypeDecl)
|
||||||
|
| DeclHandler(HandlerDecl)
|
||||||
|
| DeclLet(LetDecl)
|
||||||
|
| DeclTrait(TraitDecl)
|
||||||
|
| DeclImpl(ImplDecl)
|
||||||
|
|
||||||
|
// === Parameter ===
|
||||||
|
|
||||||
|
type Parameter = | Parameter(Ident, TypeExpr, Span)
|
||||||
|
|
||||||
|
// === Effect Operation ===
|
||||||
|
|
||||||
|
type EffectOp = | EffectOp(Ident, List<Parameter>, TypeExpr, Span)
|
||||||
|
|
||||||
|
// === Record Field ===
|
||||||
|
|
||||||
|
type RecordField = | RecordField(Ident, TypeExpr, Span)
|
||||||
|
|
||||||
|
// === Variant Fields ===
|
||||||
|
|
||||||
|
type VariantFields =
|
||||||
|
| VfUnit
|
||||||
|
| VfTuple(List<TypeExpr>)
|
||||||
|
| VfRecord(List<RecordField>)
|
||||||
|
|
||||||
|
// === Variant ===
|
||||||
|
|
||||||
|
type Variant = | Variant(Ident, VariantFields, Span)
|
||||||
|
|
||||||
|
// === Migration ===
|
||||||
|
|
||||||
|
type Migration = | Migration(Version, Expr, Span)
|
||||||
|
|
||||||
|
// === Handler Impl ===
|
||||||
|
|
||||||
|
// op_name, params, resume, body, span
|
||||||
|
type HandlerImpl = | HandlerImpl(Ident, List<Ident>, Option<Ident>, Expr, Span)
|
||||||
|
|
||||||
|
// === Impl Method ===
|
||||||
|
|
||||||
|
// name, params, return_type, body, span
|
||||||
|
type ImplMethod = | ImplMethod(Ident, List<Parameter>, Option<TypeExpr>, Expr, Span)
|
||||||
|
|
||||||
|
// === Trait Method ===
|
||||||
|
|
||||||
|
// name, type_params, params, return_type, default_impl, span
|
||||||
|
type TraitMethod = | TraitMethod(Ident, List<Ident>, List<Parameter>, TypeExpr, Option<Expr>, Span)
|
||||||
|
|
||||||
|
// === Type Expressions ===
|
||||||
|
|
||||||
|
type TypeExpr =
|
||||||
|
| TeNamed(Ident)
|
||||||
|
| TeApp(TypeExpr, List<TypeExpr>)
|
||||||
|
| TeFunction(List<TypeExpr>, TypeExpr, List<Ident>)
|
||||||
|
| TeTuple(List<TypeExpr>)
|
||||||
|
| TeRecord(List<RecordField>)
|
||||||
|
| TeUnit
|
||||||
|
| TeVersioned(TypeExpr, VersionConstraint)
|
||||||
|
|
||||||
|
// === Literal ===
|
||||||
|
|
||||||
|
type LiteralKind =
|
||||||
|
| LitInt(Int)
|
||||||
|
| LitFloat(String)
|
||||||
|
| LitString(String)
|
||||||
|
| LitChar(Char)
|
||||||
|
| LitBool(Bool)
|
||||||
|
| LitUnit
|
||||||
|
|
||||||
|
type Literal = | Literal(LiteralKind, Span)
|
||||||
|
|
||||||
|
// === Binary Operators ===
|
||||||
|
|
||||||
|
type BinaryOp =
|
||||||
|
| OpAdd | OpSub | OpMul | OpDiv | OpMod
|
||||||
|
| OpEq | OpNe | OpLt | OpLe | OpGt | OpGe
|
||||||
|
| OpAnd | OpOr
|
||||||
|
| OpPipe | OpConcat
|
||||||
|
|
||||||
|
// === Unary Operators ===
|
||||||
|
|
||||||
|
type UnaryOp = | OpNeg | OpNot
|
||||||
|
|
||||||
|
// === Statements ===
|
||||||
|
|
||||||
|
type Statement =
|
||||||
|
| StExpr(Expr)
|
||||||
|
| StLet(Ident, Option<TypeExpr>, Expr, Span)
|
||||||
|
|
||||||
|
// === Match Arms ===
|
||||||
|
|
||||||
|
type MatchArm = | MatchArm(Pattern, Option<Expr>, Expr, Span)
|
||||||
|
|
||||||
|
// === Patterns ===
|
||||||
|
|
||||||
|
type Pattern =
|
||||||
|
| PatWildcard(Span)
|
||||||
|
| PatVar(Ident)
|
||||||
|
| PatLiteral(Literal)
|
||||||
|
| PatConstructor(Ident, List<Pattern>, Span)
|
||||||
|
| PatRecord(List<(Ident, Pattern)>, Span)
|
||||||
|
| PatTuple(List<Pattern>, Span)
|
||||||
|
|
||||||
|
// === Function Declaration ===
|
||||||
|
// visibility, doc, name, type_params, params, return_type, effects, properties, where_clauses, body, span
|
||||||
|
type FunctionDecl = | FunctionDecl(Visibility, Option<String>, Ident, List<Ident>, List<Parameter>, TypeExpr, List<Ident>, List<BehavioralProperty>, List<WhereClause>, Expr, Span)
|
||||||
|
|
||||||
|
// === Effect Declaration ===
|
||||||
|
// doc, name, type_params, operations, span
|
||||||
|
type EffectDecl = | EffectDecl(Option<String>, Ident, List<Ident>, List<EffectOp>, Span)
|
||||||
|
|
||||||
|
// === Type Declaration ===
|
||||||
|
// visibility, doc, name, type_params, version, definition, migrations, span
|
||||||
|
type TypeDecl = | TypeDecl(Visibility, Option<String>, Ident, List<Ident>, Option<Version>, TypeDef, List<Migration>, Span)
|
||||||
|
|
||||||
|
// === Handler Declaration ===
|
||||||
|
// name, params, effect, implementations, span
|
||||||
|
type HandlerDecl = | HandlerDecl(Ident, List<Parameter>, Ident, List<HandlerImpl>, Span)
|
||||||
|
|
||||||
|
// === Let Declaration ===
|
||||||
|
// visibility, doc, name, typ, value, span
|
||||||
|
type LetDecl = | LetDecl(Visibility, Option<String>, Ident, Option<TypeExpr>, Expr, Span)
|
||||||
|
|
||||||
|
// === Trait Declaration ===
|
||||||
|
// visibility, doc, name, type_params, super_traits, methods, span
|
||||||
|
type TraitDecl = | TraitDecl(Visibility, Option<String>, Ident, List<Ident>, List<TraitBound>, List<TraitMethod>, Span)
|
||||||
|
|
||||||
|
// === Impl Declaration ===
|
||||||
|
// type_params, constraints, trait_name, trait_args, target_type, methods, span
|
||||||
|
type ImplDecl = | ImplDecl(List<Ident>, List<TraitConstraint>, Ident, List<TypeExpr>, TypeExpr, List<ImplMethod>, Span)
|
||||||
|
|
||||||
|
// === Expressions ===
|
||||||
|
|
||||||
|
type Expr =
|
||||||
|
| ExLiteral(Literal)
|
||||||
|
| ExVar(Ident)
|
||||||
|
| ExBinaryOp(BinaryOp, Expr, Expr, Span)
|
||||||
|
| ExUnaryOp(UnaryOp, Expr, Span)
|
||||||
|
| ExCall(Expr, List<Expr>, Span)
|
||||||
|
| ExEffectOp(Ident, Ident, List<Expr>, Span)
|
||||||
|
| ExField(Expr, Ident, Span)
|
||||||
|
| ExTupleIndex(Expr, Int, Span)
|
||||||
|
| ExLambda(List<Parameter>, Option<TypeExpr>, List<Ident>, Expr, Span)
|
||||||
|
| ExLet(Ident, Option<TypeExpr>, Expr, Expr, Span)
|
||||||
|
| ExIf(Expr, Expr, Expr, Span)
|
||||||
|
| ExMatch(Expr, List<MatchArm>, Span)
|
||||||
|
| ExBlock(List<Statement>, Expr, Span)
|
||||||
|
| ExRecord(Option<Expr>, List<(Ident, Expr)>, Span)
|
||||||
|
| ExTuple(List<Expr>, Span)
|
||||||
|
| ExList(List<Expr>, Span)
|
||||||
|
| ExRun(Expr, List<(Ident, Expr)>, Span)
|
||||||
|
| ExResume(Expr, Span)
|
||||||
@@ -63,6 +63,26 @@ for pkg in path frontmatter xml rss markdown; do
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# --- Project checks ---
|
||||||
|
PROJECTS_DIR="$(pwd)/projects"
|
||||||
|
for proj_dir in "$PROJECTS_DIR"/*/; do
|
||||||
|
proj=$(basename "$proj_dir")
|
||||||
|
if [ -f "$proj_dir/main.lux" ]; then
|
||||||
|
step "lux check (project: $proj)"
|
||||||
|
OUTPUT=$("$LUX" check "$proj_dir/main.lux" 2>&1 || true)
|
||||||
|
if echo "$OUTPUT" | grep -qi "error"; then fail; else ok; fi
|
||||||
|
fi
|
||||||
|
# Check any standalone .lux files in the project
|
||||||
|
for lux_file in "$proj_dir"/*.lux; do
|
||||||
|
[ -f "$lux_file" ] || continue
|
||||||
|
fname=$(basename "$lux_file")
|
||||||
|
[ "$fname" = "main.lux" ] && continue
|
||||||
|
step "lux check (project: $proj/$fname)"
|
||||||
|
OUTPUT=$("$LUX" check "$lux_file" 2>&1 || true)
|
||||||
|
if echo "$OUTPUT" | grep -qi "error"; then fail; else ok; fi
|
||||||
|
done
|
||||||
|
done
|
||||||
|
|
||||||
# --- Summary ---
|
# --- Summary ---
|
||||||
printf "\n${BOLD}═══ Validation Summary ═══${NC}\n"
|
printf "\n${BOLD}═══ Validation Summary ═══${NC}\n"
|
||||||
if [ $FAILED -eq 0 ]; then
|
if [ $FAILED -eq 0 ]; then
|
||||||
|
|||||||
@@ -882,6 +882,14 @@ impl CBackend {
|
|||||||
self.writeln(" int64_t capacity;");
|
self.writeln(" int64_t capacity;");
|
||||||
self.writeln("};");
|
self.writeln("};");
|
||||||
self.writeln("");
|
self.writeln("");
|
||||||
|
self.writeln("// Map struct (linear-scan key-value table, string keys)");
|
||||||
|
self.writeln("typedef struct {");
|
||||||
|
self.writeln(" LuxString* keys;");
|
||||||
|
self.writeln(" void** values;");
|
||||||
|
self.writeln(" int64_t length;");
|
||||||
|
self.writeln(" int64_t capacity;");
|
||||||
|
self.writeln("} LuxMap;");
|
||||||
|
self.writeln("");
|
||||||
self.writeln("// === Reference Counting Infrastructure ===");
|
self.writeln("// === Reference Counting Infrastructure ===");
|
||||||
self.writeln("// Perceus-inspired RC system for automatic memory management.");
|
self.writeln("// Perceus-inspired RC system for automatic memory management.");
|
||||||
self.writeln("// See docs/REFERENCE_COUNTING.md for details.");
|
self.writeln("// See docs/REFERENCE_COUNTING.md for details.");
|
||||||
@@ -2043,6 +2051,76 @@ impl CBackend {
|
|||||||
self.writeln(" return result;");
|
self.writeln(" return result;");
|
||||||
self.writeln("}");
|
self.writeln("}");
|
||||||
self.writeln("");
|
self.writeln("");
|
||||||
|
// === Map Runtime Functions ===
|
||||||
|
self.writeln("static LuxMap* lux_map_new(int64_t capacity) {");
|
||||||
|
self.writeln(" LuxMap* map = (LuxMap*)malloc(sizeof(LuxMap));");
|
||||||
|
self.writeln(" map->capacity = capacity > 0 ? capacity : 8;");
|
||||||
|
self.writeln(" map->keys = (LuxString*)calloc(map->capacity, sizeof(LuxString));");
|
||||||
|
self.writeln(" map->values = (void**)calloc(map->capacity, sizeof(void*));");
|
||||||
|
self.writeln(" map->length = 0;");
|
||||||
|
self.writeln(" return map;");
|
||||||
|
self.writeln("}");
|
||||||
|
self.writeln("");
|
||||||
|
self.writeln("static int64_t lux_map_find(LuxMap* map, LuxString key) {");
|
||||||
|
self.writeln(" for (int64_t i = 0; i < map->length; i++) {");
|
||||||
|
self.writeln(" if (map->keys[i] && strcmp(map->keys[i], key) == 0) return i;");
|
||||||
|
self.writeln(" }");
|
||||||
|
self.writeln(" return -1;");
|
||||||
|
self.writeln("}");
|
||||||
|
self.writeln("");
|
||||||
|
self.writeln("static LuxMap* lux_map_clone(LuxMap* map) {");
|
||||||
|
self.writeln(" LuxMap* result = lux_map_new(map->capacity);");
|
||||||
|
self.writeln(" result->length = map->length;");
|
||||||
|
self.writeln(" for (int64_t i = 0; i < map->length; i++) {");
|
||||||
|
self.writeln(" result->keys[i] = lux_string_dup(map->keys[i]);");
|
||||||
|
self.writeln(" result->values[i] = map->values[i];");
|
||||||
|
self.writeln(" lux_incref(map->values[i]);");
|
||||||
|
self.writeln(" }");
|
||||||
|
self.writeln(" return result;");
|
||||||
|
self.writeln("}");
|
||||||
|
self.writeln("");
|
||||||
|
self.writeln("static LuxMap* lux_map_set(LuxMap* map, LuxString key, void* value) {");
|
||||||
|
self.writeln(" LuxMap* result = lux_map_clone(map);");
|
||||||
|
self.writeln(" int64_t idx = lux_map_find(result, key);");
|
||||||
|
self.writeln(" if (idx >= 0) {");
|
||||||
|
self.writeln(" lux_decref(result->values[idx]);");
|
||||||
|
self.writeln(" result->values[idx] = value;");
|
||||||
|
self.writeln(" lux_incref(value);");
|
||||||
|
self.writeln(" } else {");
|
||||||
|
self.writeln(" if (result->length >= result->capacity) {");
|
||||||
|
self.writeln(" result->capacity *= 2;");
|
||||||
|
self.writeln(" result->keys = (LuxString*)realloc(result->keys, sizeof(LuxString) * result->capacity);");
|
||||||
|
self.writeln(" result->values = (void**)realloc(result->values, sizeof(void*) * result->capacity);");
|
||||||
|
self.writeln(" }");
|
||||||
|
self.writeln(" result->keys[result->length] = lux_string_dup(key);");
|
||||||
|
self.writeln(" result->values[result->length] = value;");
|
||||||
|
self.writeln(" lux_incref(value);");
|
||||||
|
self.writeln(" result->length++;");
|
||||||
|
self.writeln(" }");
|
||||||
|
self.writeln(" return result;");
|
||||||
|
self.writeln("}");
|
||||||
|
self.writeln("");
|
||||||
|
self.writeln("static int64_t lux_map_size(LuxMap* map) { return map->length; }");
|
||||||
|
self.writeln("static LuxBool lux_map_isEmpty(LuxMap* map) { return map->length == 0; }");
|
||||||
|
self.writeln("");
|
||||||
|
self.writeln("static LuxBool lux_map_contains(LuxMap* map, LuxString key) {");
|
||||||
|
self.writeln(" return lux_map_find(map, key) >= 0;");
|
||||||
|
self.writeln("}");
|
||||||
|
self.writeln("");
|
||||||
|
self.writeln("static LuxMap* lux_map_remove(LuxMap* map, LuxString key) {");
|
||||||
|
self.writeln(" LuxMap* result = lux_map_new(map->capacity);");
|
||||||
|
self.writeln(" for (int64_t i = 0; i < map->length; i++) {");
|
||||||
|
self.writeln(" if (strcmp(map->keys[i], key) != 0) {");
|
||||||
|
self.writeln(" result->keys[result->length] = lux_string_dup(map->keys[i]);");
|
||||||
|
self.writeln(" result->values[result->length] = map->values[i];");
|
||||||
|
self.writeln(" lux_incref(map->values[i]);");
|
||||||
|
self.writeln(" result->length++;");
|
||||||
|
self.writeln(" }");
|
||||||
|
self.writeln(" }");
|
||||||
|
self.writeln(" return result;");
|
||||||
|
self.writeln("}");
|
||||||
|
self.writeln("");
|
||||||
|
|
||||||
self.writeln("static Option lux_option_none(void) { return (Option){Option_TAG_NONE}; }");
|
self.writeln("static Option lux_option_none(void) { return (Option){Option_TAG_NONE}; }");
|
||||||
self.writeln("static Option lux_option_some(void* value) { return (Option){Option_TAG_SOME, .data.some = {value}}; }");
|
self.writeln("static Option lux_option_some(void* value) { return (Option){Option_TAG_SOME, .data.some = {value}}; }");
|
||||||
self.writeln("");
|
self.writeln("");
|
||||||
@@ -3014,6 +3092,9 @@ impl CBackend {
|
|||||||
if module_name.name == "List" {
|
if module_name.name == "List" {
|
||||||
return self.emit_list_operation(&field.name, args);
|
return self.emit_list_operation(&field.name, args);
|
||||||
}
|
}
|
||||||
|
if module_name.name == "Map" {
|
||||||
|
return self.emit_map_operation(&field.name, args);
|
||||||
|
}
|
||||||
// Int module
|
// Int module
|
||||||
if module_name.name == "Int" && field.name == "toString" {
|
if module_name.name == "Int" && field.name == "toString" {
|
||||||
let arg = self.emit_expr(&args[0])?;
|
let arg = self.emit_expr(&args[0])?;
|
||||||
@@ -3022,6 +3103,10 @@ impl CBackend {
|
|||||||
self.register_rc_var(&temp, "LuxString");
|
self.register_rc_var(&temp, "LuxString");
|
||||||
return Ok(temp);
|
return Ok(temp);
|
||||||
}
|
}
|
||||||
|
if module_name.name == "Int" && field.name == "toFloat" {
|
||||||
|
let arg = self.emit_expr(&args[0])?;
|
||||||
|
return Ok(format!("((LuxFloat){})", arg));
|
||||||
|
}
|
||||||
// Float module
|
// Float module
|
||||||
if module_name.name == "Float" && field.name == "toString" {
|
if module_name.name == "Float" && field.name == "toString" {
|
||||||
let arg = self.emit_expr(&args[0])?;
|
let arg = self.emit_expr(&args[0])?;
|
||||||
@@ -3030,6 +3115,10 @@ impl CBackend {
|
|||||||
self.register_rc_var(&temp, "LuxString");
|
self.register_rc_var(&temp, "LuxString");
|
||||||
return Ok(temp);
|
return Ok(temp);
|
||||||
}
|
}
|
||||||
|
if module_name.name == "Float" && field.name == "toInt" {
|
||||||
|
let arg = self.emit_expr(&args[0])?;
|
||||||
|
return Ok(format!("((LuxInt){})", arg));
|
||||||
|
}
|
||||||
// Math module
|
// Math module
|
||||||
if module_name.name == "Math" {
|
if module_name.name == "Math" {
|
||||||
return self.emit_math_operation(&field.name, args);
|
return self.emit_math_operation(&field.name, args);
|
||||||
@@ -3379,6 +3468,10 @@ impl CBackend {
|
|||||||
self.register_rc_var(&temp, "LuxString");
|
self.register_rc_var(&temp, "LuxString");
|
||||||
return Ok(temp);
|
return Ok(temp);
|
||||||
}
|
}
|
||||||
|
"toFloat" => {
|
||||||
|
let arg = self.emit_expr(&args[0])?;
|
||||||
|
return Ok(format!("((LuxFloat){})", arg));
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3393,6 +3486,10 @@ impl CBackend {
|
|||||||
self.register_rc_var(&temp, "LuxString");
|
self.register_rc_var(&temp, "LuxString");
|
||||||
return Ok(temp);
|
return Ok(temp);
|
||||||
}
|
}
|
||||||
|
"toInt" => {
|
||||||
|
let arg = self.emit_expr(&args[0])?;
|
||||||
|
return Ok(format!("((LuxInt){})", arg));
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3402,6 +3499,11 @@ impl CBackend {
|
|||||||
return self.emit_math_operation(&operation.name, args);
|
return self.emit_math_operation(&operation.name, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Map module
|
||||||
|
if effect.name == "Map" {
|
||||||
|
return self.emit_map_operation(&operation.name, args);
|
||||||
|
}
|
||||||
|
|
||||||
// Built-in Console effect
|
// Built-in Console effect
|
||||||
if effect.name == "Console" {
|
if effect.name == "Console" {
|
||||||
if operation.name == "print" {
|
if operation.name == "print" {
|
||||||
@@ -4520,6 +4622,140 @@ impl CBackend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Emit code for Map module operations
|
||||||
|
fn emit_map_operation(&mut self, op: &str, args: &[Expr]) -> Result<String, CGenError> {
|
||||||
|
match op {
|
||||||
|
"new" => {
|
||||||
|
let temp = format!("_map_new_{}", self.fresh_name());
|
||||||
|
self.writeln(&format!("LuxMap* {} = lux_map_new(8);", temp));
|
||||||
|
Ok(temp)
|
||||||
|
}
|
||||||
|
"set" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
let key = self.emit_expr(&args[1])?;
|
||||||
|
let val = self.emit_expr(&args[2])?;
|
||||||
|
let boxed_val = self.box_value(&val, None);
|
||||||
|
let temp = format!("_map_set_{}", self.fresh_name());
|
||||||
|
self.writeln(&format!("LuxMap* {} = lux_map_set({}, {}, {});", temp, map, key, boxed_val));
|
||||||
|
Ok(temp)
|
||||||
|
}
|
||||||
|
"get" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
let key = self.emit_expr(&args[1])?;
|
||||||
|
let idx_temp = format!("_map_idx_{}", self.fresh_name());
|
||||||
|
let result_temp = format!("_map_get_{}", self.fresh_name());
|
||||||
|
self.writeln(&format!("int64_t {} = lux_map_find({}, {});", idx_temp, map, key));
|
||||||
|
self.writeln(&format!("Option {};", result_temp));
|
||||||
|
self.writeln(&format!("if ({} >= 0) {{", idx_temp));
|
||||||
|
self.indent += 1;
|
||||||
|
self.writeln(&format!("lux_incref({}->values[{}]);", map, idx_temp));
|
||||||
|
self.writeln(&format!("{} = lux_option_some({}->values[{}]);", result_temp, map, idx_temp));
|
||||||
|
self.indent -= 1;
|
||||||
|
self.writeln("} else {");
|
||||||
|
self.indent += 1;
|
||||||
|
self.writeln(&format!("{} = lux_option_none();", result_temp));
|
||||||
|
self.indent -= 1;
|
||||||
|
self.writeln("}");
|
||||||
|
Ok(result_temp)
|
||||||
|
}
|
||||||
|
"contains" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
let key = self.emit_expr(&args[1])?;
|
||||||
|
Ok(format!("lux_map_contains({}, {})", map, key))
|
||||||
|
}
|
||||||
|
"remove" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
let key = self.emit_expr(&args[1])?;
|
||||||
|
let temp = format!("_map_rm_{}", self.fresh_name());
|
||||||
|
self.writeln(&format!("LuxMap* {} = lux_map_remove({}, {});", temp, map, key));
|
||||||
|
Ok(temp)
|
||||||
|
}
|
||||||
|
"keys" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
let temp = format!("_map_keys_{}", self.fresh_name());
|
||||||
|
self.writeln(&format!("LuxList* {} = lux_list_new({}->length);", temp, map));
|
||||||
|
// Sort keys: simple insertion sort
|
||||||
|
self.writeln(&format!("for (int64_t _i = 0; _i < {}->length; _i++) {{", map));
|
||||||
|
self.indent += 1;
|
||||||
|
self.writeln(&format!("LuxString _ks = lux_string_dup({}->keys[_i]);", map));
|
||||||
|
self.writeln(&format!("lux_list_push({}, _ks);", temp));
|
||||||
|
self.indent -= 1;
|
||||||
|
self.writeln("}");
|
||||||
|
// Sort via bubble sort (small N)
|
||||||
|
self.writeln(&format!("for (int64_t _i = 0; _i < {}->length; _i++)", temp));
|
||||||
|
self.writeln(&format!(" for (int64_t _j = _i+1; _j < {}->length; _j++)", temp));
|
||||||
|
self.writeln(&format!(" if (strcmp({}->elements[_i], {}->elements[_j]) > 0) {{", temp, temp));
|
||||||
|
self.writeln(&format!(" void* _t = {}->elements[_i]; {}->elements[_i] = {}->elements[_j]; {}->elements[_j] = _t;", temp, temp, temp, temp));
|
||||||
|
self.writeln(" }");
|
||||||
|
Ok(temp)
|
||||||
|
}
|
||||||
|
"values" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
let temp = format!("_map_vals_{}", self.fresh_name());
|
||||||
|
self.writeln(&format!("LuxList* {} = lux_list_new({}->length);", temp, map));
|
||||||
|
// Sort by key first, then collect values
|
||||||
|
self.writeln(&format!("int64_t* _idx = (int64_t*)malloc(sizeof(int64_t) * {}->length);", map));
|
||||||
|
self.writeln(&format!("for (int64_t _i = 0; _i < {}->length; _i++) _idx[_i] = _i;", map));
|
||||||
|
self.writeln(&format!("for (int64_t _i = 0; _i < {}->length; _i++)", map));
|
||||||
|
self.writeln(&format!(" for (int64_t _j = _i+1; _j < {}->length; _j++)", map));
|
||||||
|
self.writeln(&format!(" if (strcmp({}->keys[_idx[_i]], {}->keys[_idx[_j]]) > 0) {{ int64_t _t = _idx[_i]; _idx[_i] = _idx[_j]; _idx[_j] = _t; }}", map, map));
|
||||||
|
self.writeln(&format!("for (int64_t _i = 0; _i < {}->length; _i++) {{", map));
|
||||||
|
self.indent += 1;
|
||||||
|
self.writeln(&format!("lux_incref({}->values[_idx[_i]]);", map));
|
||||||
|
self.writeln(&format!("lux_list_push({}, {}->values[_idx[_i]]);", temp, map));
|
||||||
|
self.indent -= 1;
|
||||||
|
self.writeln("}");
|
||||||
|
self.writeln("free(_idx);");
|
||||||
|
Ok(temp)
|
||||||
|
}
|
||||||
|
"size" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
Ok(format!("lux_map_size({})", map))
|
||||||
|
}
|
||||||
|
"isEmpty" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
Ok(format!("lux_map_isEmpty({})", map))
|
||||||
|
}
|
||||||
|
"fromList" => {
|
||||||
|
let list = self.emit_expr(&args[0])?;
|
||||||
|
let temp = format!("_map_fl_{}", self.fresh_name());
|
||||||
|
self.writeln(&format!("LuxMap* {} = lux_map_new({}->length);", temp, list));
|
||||||
|
self.writeln(&format!("for (int64_t _i = 0; _i < {}->length; _i++) {{", list));
|
||||||
|
self.indent += 1;
|
||||||
|
// Elements are tuples (boxed as void*) — we treat them as a simple 2-element struct
|
||||||
|
self.writeln("// Each element is a (String, V) tuple - not yet fully supported in C backend for Map");
|
||||||
|
self.indent -= 1;
|
||||||
|
self.writeln("}");
|
||||||
|
Ok(temp)
|
||||||
|
}
|
||||||
|
"toList" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
let temp = format!("_map_tl_{}", self.fresh_name());
|
||||||
|
self.writeln(&format!("LuxList* {} = lux_list_new({}->length);", temp, map));
|
||||||
|
self.writeln("// Map.toList not fully supported in C backend yet");
|
||||||
|
Ok(temp)
|
||||||
|
}
|
||||||
|
"merge" => {
|
||||||
|
let m1 = self.emit_expr(&args[0])?;
|
||||||
|
let m2 = self.emit_expr(&args[1])?;
|
||||||
|
let temp = format!("_map_merge_{}", self.fresh_name());
|
||||||
|
self.writeln(&format!("LuxMap* {} = lux_map_clone({});", temp, m1));
|
||||||
|
self.writeln(&format!("for (int64_t _i = 0; _i < {}->length; _i++) {{", m2));
|
||||||
|
self.indent += 1;
|
||||||
|
self.writeln(&format!("LuxMap* _next = lux_map_set({}, {}->keys[_i], {}->values[_i]);", temp, m2, m2));
|
||||||
|
self.writeln(&format!("free({}->keys); free({}->values); free({});", temp, temp, temp));
|
||||||
|
self.writeln(&format!("{} = _next;", temp));
|
||||||
|
self.indent -= 1;
|
||||||
|
self.writeln("}");
|
||||||
|
Ok(temp)
|
||||||
|
}
|
||||||
|
_ => Err(CGenError {
|
||||||
|
message: format!("Unsupported Map operation: {}", op),
|
||||||
|
span: None,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn emit_expr_with_substitution(&mut self, expr: &Expr, from: &str, to: &str) -> Result<String, CGenError> {
|
fn emit_expr_with_substitution(&mut self, expr: &Expr, from: &str, to: &str) -> Result<String, CGenError> {
|
||||||
// Simple substitution - in a real implementation, this would be more sophisticated
|
// Simple substitution - in a real implementation, this would be more sophisticated
|
||||||
match expr {
|
match expr {
|
||||||
@@ -4832,11 +5068,13 @@ impl CBackend {
|
|||||||
"toString" => return Some("LuxString".to_string()),
|
"toString" => return Some("LuxString".to_string()),
|
||||||
"parse" => return Some("Option".to_string()),
|
"parse" => return Some("Option".to_string()),
|
||||||
"abs" | "min" | "max" => return Some("LuxInt".to_string()),
|
"abs" | "min" | "max" => return Some("LuxInt".to_string()),
|
||||||
|
"toFloat" => return Some("LuxFloat".to_string()),
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
"Float" => match field.name.as_str() {
|
"Float" => match field.name.as_str() {
|
||||||
"toString" => return Some("LuxString".to_string()),
|
"toString" => return Some("LuxString".to_string()),
|
||||||
"parse" => return Some("Option".to_string()),
|
"parse" => return Some("Option".to_string()),
|
||||||
|
"toInt" => return Some("LuxInt".to_string()),
|
||||||
_ => return Some("LuxFloat".to_string()),
|
_ => return Some("LuxFloat".to_string()),
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
@@ -4888,6 +5126,7 @@ impl CBackend {
|
|||||||
if effect.name == "Int" {
|
if effect.name == "Int" {
|
||||||
match operation.name.as_str() {
|
match operation.name.as_str() {
|
||||||
"toString" => return Some("LuxString".to_string()),
|
"toString" => return Some("LuxString".to_string()),
|
||||||
|
"toFloat" => return Some("LuxFloat".to_string()),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4895,6 +5134,7 @@ impl CBackend {
|
|||||||
if effect.name == "Float" {
|
if effect.name == "Float" {
|
||||||
match operation.name.as_str() {
|
match operation.name.as_str() {
|
||||||
"toString" => return Some("LuxString".to_string()),
|
"toString" => return Some("LuxString".to_string()),
|
||||||
|
"toInt" => return Some("LuxInt".to_string()),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -888,7 +888,8 @@ impl JsBackend {
|
|||||||
let prev_has_handlers = self.has_handlers;
|
let prev_has_handlers = self.has_handlers;
|
||||||
self.has_handlers = is_effectful;
|
self.has_handlers = is_effectful;
|
||||||
|
|
||||||
// Clear var substitutions for this function
|
// Save and clear var substitutions for this function scope
|
||||||
|
let saved_substitutions = self.var_substitutions.clone();
|
||||||
self.var_substitutions.clear();
|
self.var_substitutions.clear();
|
||||||
|
|
||||||
// Emit function body
|
// Emit function body
|
||||||
@@ -896,6 +897,7 @@ impl JsBackend {
|
|||||||
self.writeln(&format!("return {};", body_code));
|
self.writeln(&format!("return {};", body_code));
|
||||||
|
|
||||||
self.has_handlers = prev_has_handlers;
|
self.has_handlers = prev_has_handlers;
|
||||||
|
self.var_substitutions = saved_substitutions;
|
||||||
|
|
||||||
self.indent -= 1;
|
self.indent -= 1;
|
||||||
self.writeln("}");
|
self.writeln("}");
|
||||||
@@ -1074,6 +1076,31 @@ impl JsBackend {
|
|||||||
if module_name.name == "List" {
|
if module_name.name == "List" {
|
||||||
return self.emit_list_operation(&field.name, args);
|
return self.emit_list_operation(&field.name, args);
|
||||||
}
|
}
|
||||||
|
if module_name.name == "Map" {
|
||||||
|
return self.emit_map_operation(&field.name, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int/Float module operations
|
||||||
|
if let Expr::Field { object, field, .. } = func.as_ref() {
|
||||||
|
if let Expr::Var(module_name) = object.as_ref() {
|
||||||
|
if module_name.name == "Int" {
|
||||||
|
let arg = self.emit_expr(&args[0])?;
|
||||||
|
match field.name.as_str() {
|
||||||
|
"toFloat" => return Ok(arg),
|
||||||
|
"toString" => return Ok(format!("String({})", arg)),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if module_name.name == "Float" {
|
||||||
|
let arg = self.emit_expr(&args[0])?;
|
||||||
|
match field.name.as_str() {
|
||||||
|
"toInt" => return Ok(format!("Math.trunc({})", arg)),
|
||||||
|
"toString" => return Ok(format!("String({})", arg)),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1163,6 +1190,26 @@ impl JsBackend {
|
|||||||
return self.emit_math_operation(&operation.name, args);
|
return self.emit_math_operation(&operation.name, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Special case: Int module operations
|
||||||
|
if effect.name == "Int" {
|
||||||
|
let arg = self.emit_expr(&args[0])?;
|
||||||
|
match operation.name.as_str() {
|
||||||
|
"toFloat" => return Ok(arg), // JS numbers are already floats
|
||||||
|
"toString" => return Ok(format!("String({})", arg)),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Special case: Float module operations
|
||||||
|
if effect.name == "Float" {
|
||||||
|
let arg = self.emit_expr(&args[0])?;
|
||||||
|
match operation.name.as_str() {
|
||||||
|
"toInt" => return Ok(format!("Math.trunc({})", arg)),
|
||||||
|
"toString" => return Ok(format!("String({})", arg)),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Special case: Result module operations (not an effect)
|
// Special case: Result module operations (not an effect)
|
||||||
if effect.name == "Result" {
|
if effect.name == "Result" {
|
||||||
return self.emit_result_operation(&operation.name, args);
|
return self.emit_result_operation(&operation.name, args);
|
||||||
@@ -1173,6 +1220,11 @@ impl JsBackend {
|
|||||||
return self.emit_json_operation(&operation.name, args);
|
return self.emit_json_operation(&operation.name, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Special case: Map module operations (not an effect)
|
||||||
|
if effect.name == "Map" {
|
||||||
|
return self.emit_map_operation(&operation.name, args);
|
||||||
|
}
|
||||||
|
|
||||||
// Special case: Html module operations (not an effect)
|
// Special case: Html module operations (not an effect)
|
||||||
if effect.name == "Html" {
|
if effect.name == "Html" {
|
||||||
return self.emit_html_operation(&operation.name, args);
|
return self.emit_html_operation(&operation.name, args);
|
||||||
@@ -1218,18 +1270,39 @@ impl JsBackend {
|
|||||||
param_names
|
param_names
|
||||||
};
|
};
|
||||||
|
|
||||||
// Save handler state
|
// Save state
|
||||||
let prev_has_handlers = self.has_handlers;
|
let prev_has_handlers = self.has_handlers;
|
||||||
|
let saved_substitutions = self.var_substitutions.clone();
|
||||||
self.has_handlers = !effects.is_empty();
|
self.has_handlers = !effects.is_empty();
|
||||||
|
|
||||||
|
// Register lambda params as themselves (override any outer substitutions)
|
||||||
|
for p in &all_params {
|
||||||
|
self.var_substitutions.insert(p.clone(), p.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Capture any statements emitted during body evaluation
|
||||||
|
let output_start = self.output.len();
|
||||||
|
let prev_indent = self.indent;
|
||||||
|
self.indent += 1;
|
||||||
|
|
||||||
let body_code = self.emit_expr(body)?;
|
let body_code = self.emit_expr(body)?;
|
||||||
|
self.writeln(&format!("return {};", body_code));
|
||||||
|
|
||||||
|
// Extract body statements and restore output
|
||||||
|
let body_statements = self.output[output_start..].to_string();
|
||||||
|
self.output.truncate(output_start);
|
||||||
|
self.indent = prev_indent;
|
||||||
|
|
||||||
|
// Restore state
|
||||||
self.has_handlers = prev_has_handlers;
|
self.has_handlers = prev_has_handlers;
|
||||||
|
self.var_substitutions = saved_substitutions;
|
||||||
|
|
||||||
|
let indent_str = " ".repeat(self.indent);
|
||||||
Ok(format!(
|
Ok(format!(
|
||||||
"(function({}) {{ return {}; }})",
|
"(function({}) {{\n{}{}}})",
|
||||||
all_params.join(", "),
|
all_params.join(", "),
|
||||||
body_code
|
body_statements,
|
||||||
|
indent_str,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2097,6 +2170,86 @@ impl JsBackend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Emit Map module operations using JS Map
|
||||||
|
fn emit_map_operation(
|
||||||
|
&mut self,
|
||||||
|
operation: &str,
|
||||||
|
args: &[Expr],
|
||||||
|
) -> Result<String, JsGenError> {
|
||||||
|
match operation {
|
||||||
|
"new" => Ok("new Map()".to_string()),
|
||||||
|
"set" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
let key = self.emit_expr(&args[1])?;
|
||||||
|
let val = self.emit_expr(&args[2])?;
|
||||||
|
Ok(format!(
|
||||||
|
"(function() {{ var m = new Map({}); m.set({}, {}); return m; }})()",
|
||||||
|
map, key, val
|
||||||
|
))
|
||||||
|
}
|
||||||
|
"get" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
let key = self.emit_expr(&args[1])?;
|
||||||
|
Ok(format!(
|
||||||
|
"({0}.has({1}) ? Lux.Some({0}.get({1})) : Lux.None())",
|
||||||
|
map, key
|
||||||
|
))
|
||||||
|
}
|
||||||
|
"contains" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
let key = self.emit_expr(&args[1])?;
|
||||||
|
Ok(format!("{}.has({})", map, key))
|
||||||
|
}
|
||||||
|
"remove" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
let key = self.emit_expr(&args[1])?;
|
||||||
|
Ok(format!(
|
||||||
|
"(function() {{ var m = new Map({}); m.delete({}); return m; }})()",
|
||||||
|
map, key
|
||||||
|
))
|
||||||
|
}
|
||||||
|
"keys" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
Ok(format!("Array.from({}.keys()).sort()", map))
|
||||||
|
}
|
||||||
|
"values" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
Ok(format!(
|
||||||
|
"Array.from({0}.entries()).sort(function(a,b) {{ return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0; }}).map(function(e) {{ return e[1]; }})",
|
||||||
|
map
|
||||||
|
))
|
||||||
|
}
|
||||||
|
"size" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
Ok(format!("{}.size", map))
|
||||||
|
}
|
||||||
|
"isEmpty" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
Ok(format!("({}.size === 0)", map))
|
||||||
|
}
|
||||||
|
"fromList" => {
|
||||||
|
let list = self.emit_expr(&args[0])?;
|
||||||
|
Ok(format!("new Map({}.map(function(t) {{ return [t[0], t[1]]; }}))", list))
|
||||||
|
}
|
||||||
|
"toList" => {
|
||||||
|
let map = self.emit_expr(&args[0])?;
|
||||||
|
Ok(format!(
|
||||||
|
"Array.from({}.entries()).sort(function(a,b) {{ return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0; }})",
|
||||||
|
map
|
||||||
|
))
|
||||||
|
}
|
||||||
|
"merge" => {
|
||||||
|
let m1 = self.emit_expr(&args[0])?;
|
||||||
|
let m2 = self.emit_expr(&args[1])?;
|
||||||
|
Ok(format!("new Map([...{}, ...{}])", m1, m2))
|
||||||
|
}
|
||||||
|
_ => Err(JsGenError {
|
||||||
|
message: format!("Unknown Map operation: {}", operation),
|
||||||
|
span: None,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Emit Html module operations for type-safe HTML construction
|
/// Emit Html module operations for type-safe HTML construction
|
||||||
fn emit_html_operation(
|
fn emit_html_operation(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|||||||
@@ -100,7 +100,9 @@ pub enum BuiltinFn {
|
|||||||
|
|
||||||
// Int/Float operations
|
// Int/Float operations
|
||||||
IntToString,
|
IntToString,
|
||||||
|
IntToFloat,
|
||||||
FloatToString,
|
FloatToString,
|
||||||
|
FloatToInt,
|
||||||
|
|
||||||
// JSON operations
|
// JSON operations
|
||||||
JsonParse,
|
JsonParse,
|
||||||
@@ -122,6 +124,20 @@ pub enum BuiltinFn {
|
|||||||
JsonString,
|
JsonString,
|
||||||
JsonArray,
|
JsonArray,
|
||||||
JsonObject,
|
JsonObject,
|
||||||
|
|
||||||
|
// Map operations
|
||||||
|
MapNew,
|
||||||
|
MapSet,
|
||||||
|
MapGet,
|
||||||
|
MapContains,
|
||||||
|
MapRemove,
|
||||||
|
MapKeys,
|
||||||
|
MapValues,
|
||||||
|
MapSize,
|
||||||
|
MapIsEmpty,
|
||||||
|
MapFromList,
|
||||||
|
MapToList,
|
||||||
|
MapMerge,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runtime value
|
/// Runtime value
|
||||||
@@ -136,6 +152,7 @@ pub enum Value {
|
|||||||
List(Vec<Value>),
|
List(Vec<Value>),
|
||||||
Tuple(Vec<Value>),
|
Tuple(Vec<Value>),
|
||||||
Record(HashMap<String, Value>),
|
Record(HashMap<String, Value>),
|
||||||
|
Map(HashMap<String, Value>),
|
||||||
Function(Rc<Closure>),
|
Function(Rc<Closure>),
|
||||||
Handler(Rc<HandlerValue>),
|
Handler(Rc<HandlerValue>),
|
||||||
/// Built-in function
|
/// Built-in function
|
||||||
@@ -167,6 +184,7 @@ impl Value {
|
|||||||
Value::List(_) => "List",
|
Value::List(_) => "List",
|
||||||
Value::Tuple(_) => "Tuple",
|
Value::Tuple(_) => "Tuple",
|
||||||
Value::Record(_) => "Record",
|
Value::Record(_) => "Record",
|
||||||
|
Value::Map(_) => "Map",
|
||||||
Value::Function(_) => "Function",
|
Value::Function(_) => "Function",
|
||||||
Value::Handler(_) => "Handler",
|
Value::Handler(_) => "Handler",
|
||||||
Value::Builtin(_) => "Function",
|
Value::Builtin(_) => "Function",
|
||||||
@@ -215,6 +233,11 @@ impl Value {
|
|||||||
ys.get(k).map(|yv| Value::values_equal(v, yv)).unwrap_or(false)
|
ys.get(k).map(|yv| Value::values_equal(v, yv)).unwrap_or(false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
(Value::Map(xs), Value::Map(ys)) => {
|
||||||
|
xs.len() == ys.len() && xs.iter().all(|(k, v)| {
|
||||||
|
ys.get(k).map(|yv| Value::values_equal(v, yv)).unwrap_or(false)
|
||||||
|
})
|
||||||
|
}
|
||||||
(Value::Constructor { name: n1, fields: f1 }, Value::Constructor { name: n2, fields: f2 }) => {
|
(Value::Constructor { name: n1, fields: f1 }, Value::Constructor { name: n2, fields: f2 }) => {
|
||||||
n1 == n2 && f1.len() == f2.len() && f1.iter().zip(f2.iter()).all(|(x, y)| Value::values_equal(x, y))
|
n1 == n2 && f1.len() == f2.len() && f1.iter().zip(f2.iter()).all(|(x, y)| Value::values_equal(x, y))
|
||||||
}
|
}
|
||||||
@@ -285,6 +308,16 @@ impl TryFromValue for Vec<Value> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryFromValue for HashMap<String, Value> {
|
||||||
|
const TYPE_NAME: &'static str = "Map";
|
||||||
|
fn try_from_value(value: &Value) -> Option<Self> {
|
||||||
|
match value {
|
||||||
|
Value::Map(m) => Some(m.clone()),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl TryFromValue for Value {
|
impl TryFromValue for Value {
|
||||||
const TYPE_NAME: &'static str = "any";
|
const TYPE_NAME: &'static str = "any";
|
||||||
fn try_from_value(value: &Value) -> Option<Self> {
|
fn try_from_value(value: &Value) -> Option<Self> {
|
||||||
@@ -331,6 +364,18 @@ impl fmt::Display for Value {
|
|||||||
}
|
}
|
||||||
write!(f, " }}")
|
write!(f, " }}")
|
||||||
}
|
}
|
||||||
|
Value::Map(entries) => {
|
||||||
|
write!(f, "Map {{")?;
|
||||||
|
let mut sorted: Vec<_> = entries.iter().collect();
|
||||||
|
sorted.sort_by_key(|(k, _)| (*k).clone());
|
||||||
|
for (i, (key, value)) in sorted.iter().enumerate() {
|
||||||
|
if i > 0 {
|
||||||
|
write!(f, ", ")?;
|
||||||
|
}
|
||||||
|
write!(f, "\"{}\": {}", key, value)?;
|
||||||
|
}
|
||||||
|
write!(f, "}}")
|
||||||
|
}
|
||||||
Value::Function(_) => write!(f, "<function>"),
|
Value::Function(_) => write!(f, "<function>"),
|
||||||
Value::Builtin(b) => write!(f, "<builtin:{:?}>", b),
|
Value::Builtin(b) => write!(f, "<builtin:{:?}>", b),
|
||||||
Value::Handler(_) => write!(f, "<handler>"),
|
Value::Handler(_) => write!(f, "<handler>"),
|
||||||
@@ -1084,12 +1129,14 @@ impl Interpreter {
|
|||||||
// Int module
|
// Int module
|
||||||
let int_module = Value::Record(HashMap::from([
|
let int_module = Value::Record(HashMap::from([
|
||||||
("toString".to_string(), Value::Builtin(BuiltinFn::IntToString)),
|
("toString".to_string(), Value::Builtin(BuiltinFn::IntToString)),
|
||||||
|
("toFloat".to_string(), Value::Builtin(BuiltinFn::IntToFloat)),
|
||||||
]));
|
]));
|
||||||
env.define("Int", int_module);
|
env.define("Int", int_module);
|
||||||
|
|
||||||
// Float module
|
// Float module
|
||||||
let float_module = Value::Record(HashMap::from([
|
let float_module = Value::Record(HashMap::from([
|
||||||
("toString".to_string(), Value::Builtin(BuiltinFn::FloatToString)),
|
("toString".to_string(), Value::Builtin(BuiltinFn::FloatToString)),
|
||||||
|
("toInt".to_string(), Value::Builtin(BuiltinFn::FloatToInt)),
|
||||||
]));
|
]));
|
||||||
env.define("Float", float_module);
|
env.define("Float", float_module);
|
||||||
|
|
||||||
@@ -1116,6 +1163,23 @@ impl Interpreter {
|
|||||||
("object".to_string(), Value::Builtin(BuiltinFn::JsonObject)),
|
("object".to_string(), Value::Builtin(BuiltinFn::JsonObject)),
|
||||||
]));
|
]));
|
||||||
env.define("Json", json_module);
|
env.define("Json", json_module);
|
||||||
|
|
||||||
|
// Map module
|
||||||
|
let map_module = Value::Record(HashMap::from([
|
||||||
|
("new".to_string(), Value::Builtin(BuiltinFn::MapNew)),
|
||||||
|
("set".to_string(), Value::Builtin(BuiltinFn::MapSet)),
|
||||||
|
("get".to_string(), Value::Builtin(BuiltinFn::MapGet)),
|
||||||
|
("contains".to_string(), Value::Builtin(BuiltinFn::MapContains)),
|
||||||
|
("remove".to_string(), Value::Builtin(BuiltinFn::MapRemove)),
|
||||||
|
("keys".to_string(), Value::Builtin(BuiltinFn::MapKeys)),
|
||||||
|
("values".to_string(), Value::Builtin(BuiltinFn::MapValues)),
|
||||||
|
("size".to_string(), Value::Builtin(BuiltinFn::MapSize)),
|
||||||
|
("isEmpty".to_string(), Value::Builtin(BuiltinFn::MapIsEmpty)),
|
||||||
|
("fromList".to_string(), Value::Builtin(BuiltinFn::MapFromList)),
|
||||||
|
("toList".to_string(), Value::Builtin(BuiltinFn::MapToList)),
|
||||||
|
("merge".to_string(), Value::Builtin(BuiltinFn::MapMerge)),
|
||||||
|
]));
|
||||||
|
env.define("Map", map_module);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute a program
|
/// Execute a program
|
||||||
@@ -2364,6 +2428,26 @@ impl Interpreter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BuiltinFn::IntToFloat => {
|
||||||
|
if args.len() != 1 {
|
||||||
|
return Err(err("Int.toFloat requires 1 argument"));
|
||||||
|
}
|
||||||
|
match &args[0] {
|
||||||
|
Value::Int(n) => Ok(EvalResult::Value(Value::Float(*n as f64))),
|
||||||
|
v => Err(err(&format!("Int.toFloat expects Int, got {}", v.type_name()))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BuiltinFn::FloatToInt => {
|
||||||
|
if args.len() != 1 {
|
||||||
|
return Err(err("Float.toInt requires 1 argument"));
|
||||||
|
}
|
||||||
|
match &args[0] {
|
||||||
|
Value::Float(f) => Ok(EvalResult::Value(Value::Int(*f as i64))),
|
||||||
|
v => Err(err(&format!("Float.toInt expects Float, got {}", v.type_name()))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BuiltinFn::TypeOf => {
|
BuiltinFn::TypeOf => {
|
||||||
if args.len() != 1 {
|
if args.len() != 1 {
|
||||||
return Err(err("typeOf requires 1 argument"));
|
return Err(err("typeOf requires 1 argument"));
|
||||||
@@ -3068,6 +3152,128 @@ impl Interpreter {
|
|||||||
}
|
}
|
||||||
Ok(EvalResult::Value(Value::Json(serde_json::Value::Object(map))))
|
Ok(EvalResult::Value(Value::Json(serde_json::Value::Object(map))))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Map operations
|
||||||
|
BuiltinFn::MapNew => {
|
||||||
|
Ok(EvalResult::Value(Value::Map(HashMap::new())))
|
||||||
|
}
|
||||||
|
|
||||||
|
BuiltinFn::MapSet => {
|
||||||
|
if args.len() != 3 {
|
||||||
|
return Err(err("Map.set requires 3 arguments: map, key, value"));
|
||||||
|
}
|
||||||
|
let mut map = match &args[0] {
|
||||||
|
Value::Map(m) => m.clone(),
|
||||||
|
v => return Err(err(&format!("Map.set expects Map as first argument, got {}", v.type_name()))),
|
||||||
|
};
|
||||||
|
let key = match &args[1] {
|
||||||
|
Value::String(s) => s.clone(),
|
||||||
|
v => return Err(err(&format!("Map.set expects String key, got {}", v.type_name()))),
|
||||||
|
};
|
||||||
|
map.insert(key, args[2].clone());
|
||||||
|
Ok(EvalResult::Value(Value::Map(map)))
|
||||||
|
}
|
||||||
|
|
||||||
|
BuiltinFn::MapGet => {
|
||||||
|
let (map, key) = Self::expect_args_2::<HashMap<String, Value>, String>(&args, "Map.get", span)?;
|
||||||
|
match map.get(&key) {
|
||||||
|
Some(v) => Ok(EvalResult::Value(Value::Constructor {
|
||||||
|
name: "Some".to_string(),
|
||||||
|
fields: vec![v.clone()],
|
||||||
|
})),
|
||||||
|
None => Ok(EvalResult::Value(Value::Constructor {
|
||||||
|
name: "None".to_string(),
|
||||||
|
fields: vec![],
|
||||||
|
})),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BuiltinFn::MapContains => {
|
||||||
|
let (map, key) = Self::expect_args_2::<HashMap<String, Value>, String>(&args, "Map.contains", span)?;
|
||||||
|
Ok(EvalResult::Value(Value::Bool(map.contains_key(&key))))
|
||||||
|
}
|
||||||
|
|
||||||
|
BuiltinFn::MapRemove => {
|
||||||
|
let (mut map, key) = Self::expect_args_2::<HashMap<String, Value>, String>(&args, "Map.remove", span)?;
|
||||||
|
map.remove(&key);
|
||||||
|
Ok(EvalResult::Value(Value::Map(map)))
|
||||||
|
}
|
||||||
|
|
||||||
|
BuiltinFn::MapKeys => {
|
||||||
|
let map = Self::expect_arg_1::<HashMap<String, Value>>(&args, "Map.keys", span)?;
|
||||||
|
let mut keys: Vec<String> = map.keys().cloned().collect();
|
||||||
|
keys.sort();
|
||||||
|
Ok(EvalResult::Value(Value::List(
|
||||||
|
keys.into_iter().map(Value::String).collect(),
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
|
||||||
|
BuiltinFn::MapValues => {
|
||||||
|
let map = Self::expect_arg_1::<HashMap<String, Value>>(&args, "Map.values", span)?;
|
||||||
|
let mut entries: Vec<(String, Value)> = map.into_iter().collect();
|
||||||
|
entries.sort_by(|(a, _), (b, _)| a.cmp(b));
|
||||||
|
Ok(EvalResult::Value(Value::List(
|
||||||
|
entries.into_iter().map(|(_, v)| v).collect(),
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
|
||||||
|
BuiltinFn::MapSize => {
|
||||||
|
let map = Self::expect_arg_1::<HashMap<String, Value>>(&args, "Map.size", span)?;
|
||||||
|
Ok(EvalResult::Value(Value::Int(map.len() as i64)))
|
||||||
|
}
|
||||||
|
|
||||||
|
BuiltinFn::MapIsEmpty => {
|
||||||
|
let map = Self::expect_arg_1::<HashMap<String, Value>>(&args, "Map.isEmpty", span)?;
|
||||||
|
Ok(EvalResult::Value(Value::Bool(map.is_empty())))
|
||||||
|
}
|
||||||
|
|
||||||
|
BuiltinFn::MapFromList => {
|
||||||
|
let list = Self::expect_arg_1::<Vec<Value>>(&args, "Map.fromList", span)?;
|
||||||
|
let mut map = HashMap::new();
|
||||||
|
for item in list {
|
||||||
|
match item {
|
||||||
|
Value::Tuple(fields) if fields.len() == 2 => {
|
||||||
|
let key = match &fields[0] {
|
||||||
|
Value::String(s) => s.clone(),
|
||||||
|
v => return Err(err(&format!("Map.fromList expects (String, V) tuples, got {} key", v.type_name()))),
|
||||||
|
};
|
||||||
|
map.insert(key, fields[1].clone());
|
||||||
|
}
|
||||||
|
_ => return Err(err("Map.fromList expects List<(String, V)>")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(EvalResult::Value(Value::Map(map)))
|
||||||
|
}
|
||||||
|
|
||||||
|
BuiltinFn::MapToList => {
|
||||||
|
let map = Self::expect_arg_1::<HashMap<String, Value>>(&args, "Map.toList", span)?;
|
||||||
|
let mut entries: Vec<(String, Value)> = map.into_iter().collect();
|
||||||
|
entries.sort_by(|(a, _), (b, _)| a.cmp(b));
|
||||||
|
Ok(EvalResult::Value(Value::List(
|
||||||
|
entries
|
||||||
|
.into_iter()
|
||||||
|
.map(|(k, v)| Value::Tuple(vec![Value::String(k), v]))
|
||||||
|
.collect(),
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
|
||||||
|
BuiltinFn::MapMerge => {
|
||||||
|
if args.len() != 2 {
|
||||||
|
return Err(err("Map.merge requires 2 arguments: map1, map2"));
|
||||||
|
}
|
||||||
|
let mut map1 = match &args[0] {
|
||||||
|
Value::Map(m) => m.clone(),
|
||||||
|
v => return Err(err(&format!("Map.merge expects Map as first argument, got {}", v.type_name()))),
|
||||||
|
};
|
||||||
|
let map2 = match &args[1] {
|
||||||
|
Value::Map(m) => m.clone(),
|
||||||
|
v => return Err(err(&format!("Map.merge expects Map as second argument, got {}", v.type_name()))),
|
||||||
|
};
|
||||||
|
for (k, v) in map2 {
|
||||||
|
map1.insert(k, v);
|
||||||
|
}
|
||||||
|
Ok(EvalResult::Value(Value::Map(map1)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3233,6 +3439,11 @@ impl Interpreter {
|
|||||||
b.get(k).map(|bv| self.values_equal(v, bv)).unwrap_or(false)
|
b.get(k).map(|bv| self.values_equal(v, bv)).unwrap_or(false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
(Value::Map(a), Value::Map(b)) => {
|
||||||
|
a.len() == b.len() && a.iter().all(|(k, v)| {
|
||||||
|
b.get(k).map(|bv| self.values_equal(v, bv)).unwrap_or(false)
|
||||||
|
})
|
||||||
|
}
|
||||||
(
|
(
|
||||||
Value::Constructor {
|
Value::Constructor {
|
||||||
name: n1,
|
name: n1,
|
||||||
|
|||||||
118
src/main.rs
118
src/main.rs
@@ -5441,4 +5441,122 @@ c")"#;
|
|||||||
check_file("projects/rest-api/main.lux").unwrap();
|
check_file("projects/rest-api/main.lux").unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// === Map type tests ===
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map_new_and_size() {
|
||||||
|
let source = r#"
|
||||||
|
let m = Map.new()
|
||||||
|
let result = Map.size(m)
|
||||||
|
"#;
|
||||||
|
assert_eq!(eval(source).unwrap(), "0");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map_set_and_get() {
|
||||||
|
let source = r#"
|
||||||
|
let m = Map.new()
|
||||||
|
let m2 = Map.set(m, "name", "Alice")
|
||||||
|
let result = Map.get(m2, "name")
|
||||||
|
"#;
|
||||||
|
assert_eq!(eval(source).unwrap(), "Some(\"Alice\")");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map_get_missing() {
|
||||||
|
let source = r#"
|
||||||
|
let m = Map.new()
|
||||||
|
let result = Map.get(m, "missing")
|
||||||
|
"#;
|
||||||
|
assert_eq!(eval(source).unwrap(), "None");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map_contains() {
|
||||||
|
let source = r#"
|
||||||
|
let m = Map.set(Map.new(), "x", 1)
|
||||||
|
let result = (Map.contains(m, "x"), Map.contains(m, "y"))
|
||||||
|
"#;
|
||||||
|
assert_eq!(eval(source).unwrap(), "(true, false)");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map_remove() {
|
||||||
|
let source = r#"
|
||||||
|
let m = Map.set(Map.set(Map.new(), "a", 1), "b", 2)
|
||||||
|
let m2 = Map.remove(m, "a")
|
||||||
|
let result = (Map.size(m2), Map.contains(m2, "a"), Map.contains(m2, "b"))
|
||||||
|
"#;
|
||||||
|
assert_eq!(eval(source).unwrap(), "(1, false, true)");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map_keys_and_values() {
|
||||||
|
let source = r#"
|
||||||
|
let m = Map.set(Map.set(Map.new(), "b", 2), "a", 1)
|
||||||
|
let result = Map.keys(m)
|
||||||
|
"#;
|
||||||
|
assert_eq!(eval(source).unwrap(), "[\"a\", \"b\"]");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map_from_list() {
|
||||||
|
let source = r#"
|
||||||
|
let m = Map.fromList([("x", 10), ("y", 20)])
|
||||||
|
let result = (Map.get(m, "x"), Map.size(m))
|
||||||
|
"#;
|
||||||
|
assert_eq!(eval(source).unwrap(), "(Some(10), 2)");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map_to_list() {
|
||||||
|
let source = r#"
|
||||||
|
let m = Map.set(Map.set(Map.new(), "b", 2), "a", 1)
|
||||||
|
let result = Map.toList(m)
|
||||||
|
"#;
|
||||||
|
assert_eq!(eval(source).unwrap(), "[(\"a\", 1), (\"b\", 2)]");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map_merge() {
|
||||||
|
let source = r#"
|
||||||
|
let m1 = Map.fromList([("a", 1), ("b", 2)])
|
||||||
|
let m2 = Map.fromList([("b", 3), ("c", 4)])
|
||||||
|
let merged = Map.merge(m1, m2)
|
||||||
|
let result = (Map.get(merged, "a"), Map.get(merged, "b"), Map.get(merged, "c"))
|
||||||
|
"#;
|
||||||
|
assert_eq!(eval(source).unwrap(), "(Some(1), Some(3), Some(4))");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map_immutability() {
|
||||||
|
let source = r#"
|
||||||
|
let m1 = Map.fromList([("a", 1)])
|
||||||
|
let m2 = Map.set(m1, "b", 2)
|
||||||
|
let result = (Map.size(m1), Map.size(m2))
|
||||||
|
"#;
|
||||||
|
assert_eq!(eval(source).unwrap(), "(1, 2)");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map_is_empty() {
|
||||||
|
let source = r#"
|
||||||
|
let m1 = Map.new()
|
||||||
|
let m2 = Map.set(m1, "x", 1)
|
||||||
|
let result = (Map.isEmpty(m1), Map.isEmpty(m2))
|
||||||
|
"#;
|
||||||
|
assert_eq!(eval(source).unwrap(), "(true, false)");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map_type_annotation() {
|
||||||
|
let source = r#"
|
||||||
|
fn lookup(m: Map<String, Int>, key: String): Option<Int> =
|
||||||
|
Map.get(m, key)
|
||||||
|
let m = Map.fromList([("age", 30)])
|
||||||
|
let result = lookup(m, "age")
|
||||||
|
"#;
|
||||||
|
assert_eq!(eval(source).unwrap(), "Some(30)");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1792,6 +1792,7 @@ impl Parser {
|
|||||||
|
|
||||||
let condition = Box::new(self.parse_expr()?);
|
let condition = Box::new(self.parse_expr()?);
|
||||||
|
|
||||||
|
self.skip_newlines();
|
||||||
self.expect(TokenKind::Then)?;
|
self.expect(TokenKind::Then)?;
|
||||||
self.skip_newlines();
|
self.skip_newlines();
|
||||||
let then_branch = Box::new(self.parse_expr()?);
|
let then_branch = Box::new(self.parse_expr()?);
|
||||||
|
|||||||
@@ -2569,6 +2569,7 @@ impl TypeChecker {
|
|||||||
// Start with spread fields if present
|
// Start with spread fields if present
|
||||||
let mut field_types: Vec<(String, Type)> = if let Some(spread_expr) = spread {
|
let mut field_types: Vec<(String, Type)> = if let Some(spread_expr) = spread {
|
||||||
let spread_type = self.infer_expr(spread_expr);
|
let spread_type = self.infer_expr(spread_expr);
|
||||||
|
let spread_type = self.env.expand_type_alias(&spread_type);
|
||||||
match spread_type {
|
match spread_type {
|
||||||
Type::Record(spread_fields) => spread_fields,
|
Type::Record(spread_fields) => spread_fields,
|
||||||
_ => {
|
_ => {
|
||||||
@@ -2965,6 +2966,9 @@ impl TypeChecker {
|
|||||||
"Option" if resolved_args.len() == 1 => {
|
"Option" if resolved_args.len() == 1 => {
|
||||||
return Type::Option(Box::new(resolved_args[0].clone()));
|
return Type::Option(Box::new(resolved_args[0].clone()));
|
||||||
}
|
}
|
||||||
|
"Map" if resolved_args.len() == 2 => {
|
||||||
|
return Type::Map(Box::new(resolved_args[0].clone()), Box::new(resolved_args[1].clone()));
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
95
src/types.rs
95
src/types.rs
@@ -47,6 +47,8 @@ pub enum Type {
|
|||||||
List(Box<Type>),
|
List(Box<Type>),
|
||||||
/// Option type (sugar for App(Option, [T]))
|
/// Option type (sugar for App(Option, [T]))
|
||||||
Option(Box<Type>),
|
Option(Box<Type>),
|
||||||
|
/// Map type (sugar for App(Map, [K, V]))
|
||||||
|
Map(Box<Type>, Box<Type>),
|
||||||
/// Versioned type (e.g., User @v2)
|
/// Versioned type (e.g., User @v2)
|
||||||
Versioned {
|
Versioned {
|
||||||
base: Box<Type>,
|
base: Box<Type>,
|
||||||
@@ -119,6 +121,7 @@ impl Type {
|
|||||||
Type::Tuple(elements) => elements.iter().any(|e| e.contains_var(var)),
|
Type::Tuple(elements) => elements.iter().any(|e| e.contains_var(var)),
|
||||||
Type::Record(fields) => fields.iter().any(|(_, t)| t.contains_var(var)),
|
Type::Record(fields) => fields.iter().any(|(_, t)| t.contains_var(var)),
|
||||||
Type::List(inner) | Type::Option(inner) => inner.contains_var(var),
|
Type::List(inner) | Type::Option(inner) => inner.contains_var(var),
|
||||||
|
Type::Map(k, v) => k.contains_var(var) || v.contains_var(var),
|
||||||
Type::Versioned { base, .. } => base.contains_var(var),
|
Type::Versioned { base, .. } => base.contains_var(var),
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
@@ -158,6 +161,7 @@ impl Type {
|
|||||||
),
|
),
|
||||||
Type::List(inner) => Type::List(Box::new(inner.apply(subst))),
|
Type::List(inner) => Type::List(Box::new(inner.apply(subst))),
|
||||||
Type::Option(inner) => Type::Option(Box::new(inner.apply(subst))),
|
Type::Option(inner) => Type::Option(Box::new(inner.apply(subst))),
|
||||||
|
Type::Map(k, v) => Type::Map(Box::new(k.apply(subst)), Box::new(v.apply(subst))),
|
||||||
Type::Versioned { base, version } => Type::Versioned {
|
Type::Versioned { base, version } => Type::Versioned {
|
||||||
base: Box::new(base.apply(subst)),
|
base: Box::new(base.apply(subst)),
|
||||||
version: version.clone(),
|
version: version.clone(),
|
||||||
@@ -208,6 +212,11 @@ impl Type {
|
|||||||
vars
|
vars
|
||||||
}
|
}
|
||||||
Type::List(inner) | Type::Option(inner) => inner.free_vars(),
|
Type::List(inner) | Type::Option(inner) => inner.free_vars(),
|
||||||
|
Type::Map(k, v) => {
|
||||||
|
let mut vars = k.free_vars();
|
||||||
|
vars.extend(v.free_vars());
|
||||||
|
vars
|
||||||
|
}
|
||||||
Type::Versioned { base, .. } => base.free_vars(),
|
Type::Versioned { base, .. } => base.free_vars(),
|
||||||
_ => HashSet::new(),
|
_ => HashSet::new(),
|
||||||
}
|
}
|
||||||
@@ -279,6 +288,7 @@ impl fmt::Display for Type {
|
|||||||
}
|
}
|
||||||
Type::List(inner) => write!(f, "List<{}>", inner),
|
Type::List(inner) => write!(f, "List<{}>", inner),
|
||||||
Type::Option(inner) => write!(f, "Option<{}>", inner),
|
Type::Option(inner) => write!(f, "Option<{}>", inner),
|
||||||
|
Type::Map(k, v) => write!(f, "Map<{}, {}>", k, v),
|
||||||
Type::Versioned { base, version } => {
|
Type::Versioned { base, version } => {
|
||||||
write!(f, "{} {}", base, version)
|
write!(f, "{} {}", base, version)
|
||||||
}
|
}
|
||||||
@@ -1775,6 +1785,73 @@ impl TypeEnv {
|
|||||||
]);
|
]);
|
||||||
env.bind("Option", TypeScheme::mono(option_module_type));
|
env.bind("Option", TypeScheme::mono(option_module_type));
|
||||||
|
|
||||||
|
// Map module
|
||||||
|
let map_v = || Type::var();
|
||||||
|
let map_type = || Type::Map(Box::new(Type::String), Box::new(Type::var()));
|
||||||
|
let map_module_type = Type::Record(vec![
|
||||||
|
(
|
||||||
|
"new".to_string(),
|
||||||
|
Type::function(vec![], map_type()),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"set".to_string(),
|
||||||
|
Type::function(
|
||||||
|
vec![map_type(), Type::String, map_v()],
|
||||||
|
map_type(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"get".to_string(),
|
||||||
|
Type::function(
|
||||||
|
vec![map_type(), Type::String],
|
||||||
|
Type::Option(Box::new(map_v())),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"contains".to_string(),
|
||||||
|
Type::function(vec![map_type(), Type::String], Type::Bool),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"remove".to_string(),
|
||||||
|
Type::function(vec![map_type(), Type::String], map_type()),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"keys".to_string(),
|
||||||
|
Type::function(vec![map_type()], Type::List(Box::new(Type::String))),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"values".to_string(),
|
||||||
|
Type::function(vec![map_type()], Type::List(Box::new(map_v()))),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"size".to_string(),
|
||||||
|
Type::function(vec![map_type()], Type::Int),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"isEmpty".to_string(),
|
||||||
|
Type::function(vec![map_type()], Type::Bool),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"fromList".to_string(),
|
||||||
|
Type::function(
|
||||||
|
vec![Type::List(Box::new(Type::Tuple(vec![Type::String, map_v()])))],
|
||||||
|
map_type(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"toList".to_string(),
|
||||||
|
Type::function(
|
||||||
|
vec![map_type()],
|
||||||
|
Type::List(Box::new(Type::Tuple(vec![Type::String, map_v()]))),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"merge".to_string(),
|
||||||
|
Type::function(vec![map_type(), map_type()], map_type()),
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
env.bind("Map", TypeScheme::mono(map_module_type));
|
||||||
|
|
||||||
// Result module
|
// Result module
|
||||||
let result_type = Type::App {
|
let result_type = Type::App {
|
||||||
constructor: Box::new(Type::Named("Result".to_string())),
|
constructor: Box::new(Type::Named("Result".to_string())),
|
||||||
@@ -1908,6 +1985,10 @@ impl TypeEnv {
|
|||||||
"toString".to_string(),
|
"toString".to_string(),
|
||||||
Type::function(vec![Type::Int], Type::String),
|
Type::function(vec![Type::Int], Type::String),
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
"toFloat".to_string(),
|
||||||
|
Type::function(vec![Type::Int], Type::Float),
|
||||||
|
),
|
||||||
]);
|
]);
|
||||||
env.bind("Int", TypeScheme::mono(int_module_type));
|
env.bind("Int", TypeScheme::mono(int_module_type));
|
||||||
|
|
||||||
@@ -1917,6 +1998,10 @@ impl TypeEnv {
|
|||||||
"toString".to_string(),
|
"toString".to_string(),
|
||||||
Type::function(vec![Type::Float], Type::String),
|
Type::function(vec![Type::Float], Type::String),
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
"toInt".to_string(),
|
||||||
|
Type::function(vec![Type::Float], Type::Int),
|
||||||
|
),
|
||||||
]);
|
]);
|
||||||
env.bind("Float", TypeScheme::mono(float_module_type));
|
env.bind("Float", TypeScheme::mono(float_module_type));
|
||||||
|
|
||||||
@@ -2003,6 +2088,9 @@ impl TypeEnv {
|
|||||||
Type::Option(inner) => {
|
Type::Option(inner) => {
|
||||||
Type::Option(Box::new(self.expand_type_alias(inner)))
|
Type::Option(Box::new(self.expand_type_alias(inner)))
|
||||||
}
|
}
|
||||||
|
Type::Map(k, v) => {
|
||||||
|
Type::Map(Box::new(self.expand_type_alias(k)), Box::new(self.expand_type_alias(v)))
|
||||||
|
}
|
||||||
Type::Versioned { base, version } => {
|
Type::Versioned { base, version } => {
|
||||||
Type::Versioned {
|
Type::Versioned {
|
||||||
base: Box::new(self.expand_type_alias(base)),
|
base: Box::new(self.expand_type_alias(base)),
|
||||||
@@ -2163,6 +2251,13 @@ pub fn unify(t1: &Type, t2: &Type) -> Result<Substitution, String> {
|
|||||||
// Option
|
// Option
|
||||||
(Type::Option(a), Type::Option(b)) => unify(a, b),
|
(Type::Option(a), Type::Option(b)) => unify(a, b),
|
||||||
|
|
||||||
|
// Map
|
||||||
|
(Type::Map(k1, v1), Type::Map(k2, v2)) => {
|
||||||
|
let s1 = unify(k1, k2)?;
|
||||||
|
let s2 = unify(&v1.apply(&s1), &v2.apply(&s1))?;
|
||||||
|
Ok(s1.compose(&s2))
|
||||||
|
}
|
||||||
|
|
||||||
// Versioned types
|
// Versioned types
|
||||||
(
|
(
|
||||||
Type::Versioned {
|
Type::Versioned {
|
||||||
|
|||||||
Reference in New Issue
Block a user