Skip to content

Commit cd8f794

Browse files
committed
Fix more test cases
1 parent 850a30e commit cd8f794

4 files changed

Lines changed: 240 additions & 98 deletions

File tree

src/compiler.pr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5428,7 +5428,7 @@ def walk_YieldFrom(node: &parser::Node, state: &State) {
54285428
if not node.svalue { return }
54295429

54305430
let ident = node.svalue.identifier
5431-
let iddecl = [ kind = parser::NodeKind::FOR_ID_DECL, loc = node.loc ] !&parser::Node
5431+
let iddecl = [ kind = parser::NodeKind::FOR_ID_DECL, loc = node.loc, scope = node.scope ] !&parser::Node
54325432
iddecl.value.for_id_decl = [
54335433
kw = parser::VarDecl::VAR,
54345434
ident = ident
@@ -5437,13 +5437,17 @@ def walk_YieldFrom(node: &parser::Node, state: &State) {
54375437

54385438
let yield_body = vector::make(type &parser::Node)
54395439
yield_body.push(ident)
5440-
let yield_ = [ kind = parser::NodeKind::YIELD, loc = node.loc ] !&parser::Node
5440+
let yield_ = [ kind = parser::NodeKind::YIELD, loc = node.loc, scope = node.scope ] !&parser::Node
54415441
yield_.value.body = yield_body
54425442

54435443
let body = vector::make(type &parser::Node)
54445444
body.push(yield_)
54455445

5446-
let for_ = [ kind = parser::NodeKind::FOR, loc = node.loc, inner_scope = node.inner_scope ] !&parser::Node
5446+
let for_ = [
5447+
kind = parser::NodeKind::FOR, loc = node.loc,
5448+
inner_scope = node.inner_scope, scope = node.scope
5449+
] !&parser::Node
5450+
54475451
for_.value.for_loop = [
54485452
iddecl = iddecl,
54495453
expr = node.value.expr,
@@ -9436,14 +9440,10 @@ def generate_reflection_data_2(id: typechecking::TypeId, tpe: &typechecking::Typ
94369440
data.write(0 !int)
94379441
}
94389442
}
9439-
case typechecking::TypeKind::TUPLE
9443+
case typechecking::TypeKind::TUPLE, typechecking::TypeKind::TUNION
94409444
for var t in tpe.return_t {
94419445
data.write(t.hash)
94429446
}
9443-
//case typechecking::TypeKind::VARIANT
9444-
// for var t in @tpe.variants.keys() {
9445-
// data.write(t.hash)
9446-
// }
94479447
case typechecking::TypeKind::CLOSURE, typechecking::TypeKind::FUNCTION
94489448
for var np in tpe.parameter_t {
94499449
data.write(np.tpe.hash)

src/typechecking.pr

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,21 +1762,6 @@ export def function(
17621762

17631763

17641764
export def tuple(generics: &Vector(&TypeRef)) -> &TypeRef {
1765-
let types = vector::make(TypeId)
1766-
var is_generic = false
1767-
for var generic in generics {
1768-
if is_type(generic) {
1769-
types.push(generic.tpe)
1770-
} else {
1771-
is_generic = true
1772-
break
1773-
}
1774-
}
1775-
1776-
if not is_generic {
1777-
return make_type_ref(tuple(types))
1778-
}
1779-
17801765
return make_type_ref([ kind = TypeKind::TUPLE, return_t = generics ] !&Pattern)
17811766
}
17821767

@@ -1805,6 +1790,8 @@ export def tuple(types: &Vector(TypeId)) -> TypeId {
18051790
}
18061791

18071792
export def variant(types: &Vector(TypeId)) -> TypeId {
1793+
assert types.length > 0
1794+
18081795
let signature = make_signature(null, types)
18091796
if type_registry_variant.contains(signature) {
18101797
return type_registry_variant(signature).id
@@ -1834,6 +1821,7 @@ export def variant(types: &Vector(TypeId)) -> TypeId {
18341821

18351822
let union_tpe = make_union_type(fields)
18361823
union_tpe.id = next_type_id()
1824+
union_tpe.is_literal = true
18371825
register_internal(union_tpe.id, union_tpe)
18381826
variant._tpe = union_tpe.id
18391827

@@ -1845,22 +1833,6 @@ export def variant(types: &Vector(TypeId)) -> TypeId {
18451833

18461834
export def variant(generics: &Vector(&TypeRef)) -> &TypeRef {
18471835
sort_by_typeid(generics)
1848-
1849-
let types = vector::make(TypeId)
1850-
var is_generic = false
1851-
for var tpe in types {
1852-
if is_type(tpe) {
1853-
types.push(tpe.tpe)
1854-
} else {
1855-
is_generic = true
1856-
break
1857-
}
1858-
}
1859-
1860-
if not is_generic {
1861-
return make_type_ref(variant(types))
1862-
}
1863-
18641836
return make_type_ref([ kind = TypeKind::TUNION, return_t = generics ] !&Pattern)
18651837
}
18661838

@@ -6112,7 +6084,7 @@ def walk_YieldFrom(node: &parser::Node, state: &State) {
61126084
}
61136085
if not node.value.expr { return }
61146086

6115-
let ftpe = current_fun.value.get_type().resolve()
6087+
let ftpe = current_fun.value.fdef.to_type().resolve()
61166088
let generator = ftpe.return_t(0)
61176089

61186090
walk(node, node.value.expr, state)

std/stdio.pr

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import std
2+
import cstd
3+
import reflection
4+
import runtime
5+
import strings
6+
7+
def print_val(file: File, value: *, tpe: reflection::Type) -> int {
8+
//cstd::printf("%.*s\n".value, tpe.name.count, tpe.name.data ++ tpe.name.offset)
9+
if not tpe or not value {
10+
return cstd::fprintf(file, "%p".value, value)
11+
} else if tpe == string {
12+
let str = value !*string
13+
cstd::fputs(str.value, file)
14+
return (str.size - 1) !int
15+
} else if tpe == StringSlice {
16+
let slc = value !*StringSlice
17+
if slc.data {
18+
cstd::fwrite(slc.data ++ slc.offset, 1, slc.count, file)
19+
} else {
20+
let val = @slc
21+
for var i in 0..slc.count {
22+
cstd::putc(val(i), file)
23+
}
24+
}
25+
return slc.count !int
26+
} else if tpe == Str {
27+
let str = value !*Str
28+
cstd::fwrite(get_internal_buffer(str), 1, length(@str), file)
29+
return length(@str) !int
30+
} else if tpe == StringBuffer {
31+
fprint(file, to_string(@(value !*StringBuffer)))
32+
} else if tpe == type *char {
33+
return cstd::fprintf(file, "%s".value, @(value !**char))
34+
} else if tpe == size_t {
35+
return cstd::fprintf(file, "%zu".value, @(value !*size_t))
36+
} else if tpe == char {
37+
return cstd::fprintf(file, "%c".value, @(value !*char))
38+
} else if tpe == int8 {
39+
return cstd::fprintf(file, "%hhd".value, @(value !*int8))
40+
} else if tpe == uint8 {
41+
return cstd::fprintf(file, "%hhu".value, @(value !*uint8))
42+
} else if tpe == int16 {
43+
return cstd::fprintf(file, "%hd".value, @(value !*int16))
44+
} else if tpe == uint16 {
45+
return cstd::fprintf(file, "%hu".value, @(value !*uint16))
46+
} else if tpe == int32 {
47+
return cstd::fprintf(file, "%d".value, @(value !*int32))
48+
} else if tpe == uint32 {
49+
return cstd::fprintf(file, "%u".value, @(value !*uint32))
50+
} else if tpe == int64 {
51+
return cstd::fprintf(file, "%ld".value, @(value !*int64))
52+
} else if tpe == uint64 {
53+
return cstd::fprintf(file, "%lu".value, @(value !*uint64))
54+
} else if tpe == float64 {
55+
return cstd::fprintf(file, "%lf".value, @(value !*float64))
56+
} else if tpe == float32 {
57+
return cstd::fprintf(file, "%f".value, @(value !*float32))
58+
} else if tpe == bool {
59+
if @(value !*bool) {
60+
return cstd::fprintf(file, "true".value)
61+
} else {
62+
return cstd::fprintf(file, "false".value)
63+
}
64+
} else if tpe.type == reflection::PointerT {
65+
return cstd::fprintf(file, "%p".value, @(value !**))
66+
} else if tpe.type == reflection::ArrayT {
67+
let artpe = tpe !reflection::ArrayT
68+
let arr = @(value !*[int8])
69+
let size = arr.size
70+
let elements = arr.value
71+
var sum = 0
72+
73+
sum += cstd::fprintf(file, "[".value)
74+
for var i in 0..size {
75+
sum += print_val(file, elements ++ i * artpe.tpe.size, artpe.tpe)
76+
if i < size - 1 {
77+
sum += cstd::fprintf(file, ", ".value)
78+
}
79+
}
80+
sum += cstd::fprintf(file, "]".value)
81+
return sum
82+
} else if tpe.type == reflection::StaticArrayT {
83+
let artpe = tpe !reflection::StaticArrayT
84+
if artpe == char {
85+
return cstd::fprintf(file, "%s".value, value !*char)
86+
}
87+
88+
let size = artpe.length
89+
var sum = 0
90+
91+
sum += cstd::fprintf(file, "[".value)
92+
for var i in 0..size {
93+
sum += print_val(file, value ++ i * artpe.tpe.size, artpe.tpe)
94+
if i < size - 1 {
95+
sum += cstd::fprintf(file, ", ".value)
96+
}
97+
}
98+
sum += cstd::fprintf(file, "]".value)
99+
return sum
100+
} else if tpe.type == reflection::EnumT {
101+
let etpe = tpe !reflection::EnumT
102+
var v: int64 = 0
103+
for var i in 0..etpe.size {
104+
v |= (@((value ++ i) !*byte)) !int64 << (i * 8)
105+
}
106+
107+
var str: Str = "INVALID!!"
108+
for var i in 0..etpe.values.size {
109+
let ev = etpe.values(i)
110+
if ev.value == v {
111+
str = to_str(ev.name)
112+
}
113+
}
114+
return fprint(file, str)
115+
} else if reflection::implements(tpe, type &ToString) {
116+
let r = value !*runtime::Ref
117+
let str = (@r !& !&ToString).to_string()
118+
return fprint(file, str)
119+
} else if reflection::implements(tpe, String) {
120+
let r = value !*runtime::Ref
121+
let str = (@r !& !String).to_str()
122+
return fprint(file, str)
123+
} else if tpe.type == reflection::StructT or tpe.type == reflection::UnionT {
124+
let rect = *(tpe !RecordT)
125+
let members = rect.members
126+
var sum = 0
127+
sum += cstd::fprintf(file, "[".value)
128+
for var i in 0..members.size {
129+
let field = members(i)
130+
sum += fprint(file, field.name.to_str())
131+
sum += cstd::fprintf(file, " = ".value)
132+
133+
sum += print_val(file, value ++ field.offset, field.tpe)
134+
if i < members.size - 1 {
135+
sum += cstd::fprintf(file, ", ".value)
136+
}
137+
}
138+
sum += cstd::fprintf(file, "] !".value)
139+
sum += fprint(file, tpe.name.to_str())
140+
return sum
141+
} else if tpe.type == reflection::ReferenceT {
142+
let reftpe = tpe !reflection::ReferenceT
143+
let ref_tpe = reftpe.tpe
144+
let r = value !*runtime::Ref
145+
return print_val(file, (@r).value, ref_tpe)
146+
}
147+
}
148+
149+
export def fprint(file: File, args: &...) -> int {
150+
var sum = 0
151+
for var i in 0..args.size {
152+
let arg = args(i)
153+
let ref_tpe = arg.type
154+
155+
if ref_tpe {
156+
sum += print_val(file, (*arg) !*, ref_tpe)
157+
} else {
158+
sum += cstd::fprintf(file, "null".value)
159+
}
160+
}
161+
return sum
162+
}
163+
164+
// Optimized version
165+
export def fprint(file: File, str: Str) -> int {
166+
cstd::fwrite(get_internal_buffer(*str), 1, length(str), file)
167+
return length(str) !int
168+
}
169+
170+
export def print(args: &...) -> int {
171+
return fprint(stdout(), args)
172+
}
173+
174+
export def error(args: &...) -> int {
175+
return fprint(stderr(), args)
176+
}

0 commit comments

Comments
 (0)