Skip to content

Commit 1de9d48

Browse files
committed
Some fixes
1 parent 933aa0e commit 1de9d48

File tree

3 files changed

+46
-38
lines changed

3 files changed

+46
-38
lines changed

src/debug.pr

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,8 +767,8 @@ def float_t_to_string(tpe: &typechecking::Type) -> Str {
767767
return "float" + tpe.size * 8
768768
}
769769

770-
def type_def_t_to_string(tpe: &typechecking::Type, full_name: bool) -> Str {
771-
return "Type<" + type_to_str(tpe.tpe, full_name) + '>'
770+
def type_t_to_string(tpe: &typechecking::Type, full_name: bool) -> Str {
771+
return "type<" + type_to_str(tpe.tpe, full_name) + '>'
772772
}
773773

774774
def variant_t_to_string(tpe: &typechecking::Type, full_name: bool) -> Str {
@@ -863,9 +863,7 @@ export def type_to_str(tpe: &typechecking::Type, full_name: bool = false) -> Str
863863
if tpe.is_anon { return "<anonymous>" }
864864
return tpe.type_name if full_name else tpe.name
865865
case typechecking::TypeKind::TYPE
866-
return "type"
867-
case typechecking::TypeKind::TYPE_DEF
868-
return type_def_t_to_string(tpe, full_name)
866+
return type_t_to_string(tpe, full_name)
869867
case typechecking::TypeKind::FUNCTION, typechecking::TypeKind::CLOSURE
870868
return function_t_to_string(tpe, full_name)
871869
case typechecking::TypeKind::TUPLE

src/scope.pr

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,10 +1547,12 @@ export def get(
15471547
}
15481548
}
15491549

1550-
export def get_const_value(scope: &Scope, id: &parser::Node) -> &Value {
1550+
export def get_const_value(scope: &Scope, id: &parser::Node, error_on_fail: bool = true) -> &Value {
15511551
let value = get(scope, id)
15521552
if not value {
1553-
errors::errorn(id, "Unknown identifier `", parser::identifier_to_str(id), "`")
1553+
if error_on_fail {
1554+
errors::errorn(id, "Unknown identifier `", parser::identifier_to_str(id), "`")
1555+
}
15541556
return null
15551557
}
15561558
if value.modifier != parser::VarDecl::CONST and value.modifier != parser::VarDecl::TYPE {
@@ -1559,16 +1561,16 @@ export def get_const_value(scope: &Scope, id: &parser::Node) -> &Value {
15591561
return value
15601562
}
15611563

1562-
export def get_type(scope: &Scope, id: &parser::Node) -> &typechecking::TypeRef {
1563-
let value = get_const_value(scope, id)
1564+
export def get_type(scope: &Scope, id: &parser::Node, error_on_fail: bool = true) -> &typechecking::TypeRef {
1565+
let value = get_const_value(scope, id, error_on_fail)
15641566
if not value { return null }
15651567
if value.modifier != parser::VarDecl::TYPE {
15661568
return null
15671569
}
15681570
return value.tpe
15691571
}
1570-
export def get_type_id(scope: &Scope, id: &parser::Node) -> typechecking::TypeId {
1571-
let tpe = get_type(scope, id)
1572+
export def get_type_id(scope: &Scope, id: &parser::Node, error_on_fail: bool = true) -> typechecking::TypeId {
1573+
let tpe = get_type(scope, id, error_on_fail)
15721574
if not tpe { return 0 }
15731575
return tpe.resolve(scope)
15741576
}
@@ -1705,8 +1707,7 @@ export def create_function(scope: &Scope, name_node: &parser::Node, v: &Value, i
17051707
/*if phase == Phase::COMPILED and next.node and next.state {
17061708
next.tpe = typechecking::lookup_parameters(next.node, next.state)
17071709
}*/
1708-
let s = typechecking::overload_score(next.fdef, fdef.parameter_t, scope, true, impl = false)
1709-
if s == 0 {
1710+
if typechecking::parameter_equals(next.fdef, fdef.parameter_t, scope) {
17101711
//if phase == Phase::DECLARED { return next }
17111712
if phase == Phase::DEFINED and next.phase != Phase::DECLARED {
17121713
errors::errorn(name_node, "Function `", name, "` was already declared previously (same arguments)")

src/typechecking.pr

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,13 @@ export def kind(typeref: &TypeRef) -> TypeKind {
287287
}
288288
}
289289

290-
export def resolve(typeref: &TypeRef, scpe: &Scope) -> TypeId {
290+
export def resolve(typeref: &TypeRef, scpe: &Scope, error_on_fail: bool = true) -> TypeId {
291291
if not typeref { return 0 }
292292
if typeref.tpe {
293293
return typeref.tpe
294294
}
295295
if typeref.name {
296-
let id = scope::get_type_id(scpe, parser::make_identifier(typeref.name))
296+
let id = scope::get_type_id(scpe, parser::make_identifier(typeref.name), error_on_fail = error_on_fail)
297297
typeref.tpe = id
298298
return id
299299
}
@@ -2819,6 +2819,18 @@ export def replace_type_defs(
28192819
rtpe = left.value.tpe
28202820
}
28212821

2822+
if left.kw == parser::VarDecl::TYPE and rtpe and rtpe.kind == TypeKind::TYPE {
2823+
// We have a type parameter, create it in scope and continue
2824+
left.tpe = make_type_ref(rtpe)
2825+
scope::create_type(
2826+
scpe,
2827+
parser::make_identifier(left.name),
2828+
parser::ShareMarker::NONE,
2829+
make_type_ref(rtpe.tpe)
2830+
)
2831+
continue
2832+
}
2833+
28222834
if not rtpe { return }
28232835
if convert_type_score(left.tpe, rtpe, scpe, impl) < 0 { return }
28242836

@@ -2851,28 +2863,20 @@ export def replace_type_defs(
28512863
}
28522864
}
28532865

2854-
export def overload_score(
2855-
a: FunctionDef, param_b: &Vector(NamedParameterPre), scpe: &scope::Scope,
2856-
positional: bool, partial: bool = false, impl: bool = true) -> int {
2866+
export def parameter_equals(
2867+
a: FunctionDef, param_b: &Vector(NamedParameterPre), scpe: &scope::Scope) -> bool {
28572868

2858-
// TODO Factor this out into a function and make sure that its used everywhere
2859-
let param_b_converted = vector::make(NamedParameter)
2869+
if a.parameter_t.length != vector::length(param_b) {
2870+
return false
2871+
}
28602872
for var i in 0..vector::length(param_b) {
2861-
let npb = param_b(i)
2862-
param_b_converted.push([
2863-
name = npb.name,
2864-
tpe = npb.tpe.resolve(scpe),
2865-
value = npb.value,
2866-
kw = npb.kw,
2867-
varargs = npb.varargs,
2868-
node = npb.node
2869-
] !NamedParameter)
2873+
let left = param_b(i)
2874+
let right = a.parameter_t(i)
2875+
if left != right {
2876+
return false
2877+
}
28702878
}
2871-
2872-
return overload_score(
2873-
a.parameter_t, a.return_t, param_b_converted, scpe,
2874-
positional, partial, impl
2875-
)
2879+
return true
28762880
}
28772881

28782882
export def overload_score(
@@ -2948,11 +2952,11 @@ export def overload_score(
29482952
lvalue = array(left.tpe, parser::VarDecl::VAR)
29492953
}
29502954
var score = -1
2951-
if lvalue and lvalue.kind() == TypeKind::TYPE {
2955+
if left.kw == parser::VarDecl::TYPE {
29522956
if kind(right.tpe) == TypeKind::TYPE {
29532957
if left.value and right.value and equals(left.value.value_tpe, right.value.value_tpe) {
29542958
score = 0
2955-
} else if not left.value.value_tpe and right.value {
2959+
} else if not left.value and right.value {
29562960
// TODO test this
29572961
score = 4
29582962
}
@@ -3975,7 +3979,7 @@ def walk_Identifier(node: &parser::Node, state: &State) {
39753979
scope::add_reference(value, node)
39763980

39773981
if value.kind == scope::ValueKind::TYPE {
3978-
node.tpe = _type(value.value.value_tpe)
3982+
node.tpe = _type(value.tpe.resolve(state.scope))
39793983
} else {
39803984
node.tpe = value.get_type()
39813985
}
@@ -4280,7 +4284,12 @@ export def walk_VarDecl(node: &parser::Node, state: &State, set_constant: bool =
42804284
}
42814285

42824286
if tpe_node {
4283-
ltypes.push(type_lookup(tpe_node, state).tpe)
4287+
let tpe = type_lookup(tpe_node, state)
4288+
if not tpe {
4289+
ltypes.push(0)
4290+
} else {
4291+
ltypes.push(tpe.tpe)
4292+
}
42844293
} else {
42854294
ltypes.push(0)
42864295
}

0 commit comments

Comments
 (0)