Time lib and http

This commit is contained in:
allexanderbergmns
2026-08-25 15:39:44 +02:00
parent 4ccc8a0f95
commit 5daadce7a9
11 changed files with 310 additions and 7 deletions
+21 -6
View File
@@ -232,12 +232,17 @@ fn (mut c Checker) check_stmt(st Stmt) ! {
c.loop_depth--
}
.for_in_stmt {
seq := c.check_expr(st.expr)!
// iterate enums and arrays; unknown is allowed (dynamic)
if seq.kind == .int_t || seq.kind == .float_t || seq.kind == .bool_t || seq.kind == .none_t {
return error('cannot iterate a ${type_name(seq.kind)} (line ${st.line})')
// for x in EnumName { ... } iterates the enum's variants
if st.expr.kind == .ident && st.expr.name in c.enums {
c.types[st.target] = TypeInfo{ kind: .enum_t, name: st.expr.name }
} else {
seq := c.check_expr(st.expr)!
// iterate enums and arrays; unknown is allowed (dynamic)
if seq.kind == .int_t || seq.kind == .float_t || seq.kind == .bool_t || seq.kind == .none_t {
return error('cannot iterate a ${type_name(seq.kind)} (line ${st.line})')
}
c.types[st.target] = TypeInfo{ kind: .unknown }
}
c.types[st.target] = TypeInfo{ kind: .unknown }
if st.idx_target.len > 0 {
c.types[st.idx_target] = TypeInfo{ kind: .int_t }
}
@@ -328,9 +333,14 @@ fn (mut c Checker) check_expr(e Expr) !TypeInfo {
TypeInfo{ kind: .unknown }
}
.field {
// enum variant: Color.red — the base is the enum type name, not a
// variable, so it must be resolved before check_expr on the base
if e.left.kind == .ident && e.left.name in c.enums {
return TypeInfo{ kind: .enum_t, name: e.left.name }
}
base := c.check_expr(*e.left)!
c.expect_struct_like(base, 'field access', e.line)!
// enum variant: Color.red → enum_t
// enum variant on an enum-typed receiver: Color.red → enum_t
if base.kind == .enum_t {
return TypeInfo{ kind: .enum_t, name: base.name }
}
@@ -599,6 +609,11 @@ fn builtin_result_type(name string) TypeInfo {
'build_glob', 'build_ls' { TypeInfo{ kind: .array_t } }
'build_run', 'build_test', 'build_bench', 'build_clean', 'build_exec_status',
'build_exists', 'build_mkdir', 'build_rm', 'build_copy' { TypeInfo{ kind: .int_t } }
// HTTP client: returns a {status, body} struct
'http_get', 'http_post' { TypeInfo{ kind: .struct_t } }
// date/time
'now', 'time_ms', 'parse_time' { TypeInfo{ kind: .int_t } }
'format_time' { TypeInfo{ kind: .string_t } }
else { TypeInfo{ kind: .unknown } }
}
}
+8
View File
@@ -974,6 +974,14 @@ fn builtin_spec(name string) (int, int) {
'build_is_dir' { native_build_is_dir, 1 }
'cwd' { native_cwd, 0 }
'json_pretty' { native_json_pretty, 1 }
// HTTP client
'http_get' { native_http_get, 1 }
'http_post' { native_http_post, 2 }
// date/time
'now' { native_now, 0 }
'time_ms' { native_time_ms, 0 }
'format_time' { native_format_time, 2 }
'parse_time' { native_parse_time, 1 }
else { -1, 0 }
}
}
+10
View File
@@ -141,3 +141,13 @@ const native_repeat = 164
const native_build_is_dir = 165
const native_cwd = 166
const native_json_pretty = 167
// HTTP client builtins
const native_http_get = 168
const native_http_post = 169
// date/time builtins
const native_now = 170
const native_time_ms = 171
const native_format_time = 172
const native_parse_time = 173