print_(f32|f64)

This commit is contained in:
Ginger Bill
2016-08-18 15:58:36 +01:00
parent 6b2cd1b33f
commit f931b089d5
12 changed files with 714 additions and 813 deletions

View File

@@ -1,7 +1,5 @@
#load "runtime.odin"
TWO_HEARTS :: '💕';
print_string :: proc(s: string) {
for i := 0; i < len(s); i++ {
putchar(s[i] as i32);
@@ -15,10 +13,10 @@ byte_reverse :: proc(b: []byte) {
}
}
encode_rune :: proc(r : rune) -> ([4]byte, int) {
buf : [4]byte;
encode_rune :: proc(r: rune) -> ([4]byte, int) {
buf: [4]byte;
i := r as u32;
mask : byte : 0x3f;
mask: byte : 0x3f;
if i <= 1<<7-1 {
buf[0] = r as byte;
return buf, 1;
@@ -49,16 +47,16 @@ encode_rune :: proc(r : rune) -> ([4]byte, int) {
return buf, 4;
}
print_rune :: proc(r : rune) {
print_rune :: proc(r: rune) {
buf, n := encode_rune(r);
str := buf[:n] as string;
print_string(str);
}
print_int :: proc(i : int) {
print_int :: proc(i: int) {
print_int_base(i, 10);
}
print_int_base :: proc(i, base : int) {
print_int_base :: proc(i, base: int) {
NUM_TO_CHAR_TABLE :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$";
buf: [65]byte;
@@ -87,19 +85,14 @@ print_int_base :: proc(i, base : int) {
print_string(buf[:len] as string);
}
print_uint :: proc(i : uint) {
print_uint_base(i, 10);
print_uint :: proc(i: uint) {
print__uint(i, 10, 0, ' ');
}
print_uint_base :: proc(i, base : uint) {
print__uint :: proc(i, base: uint, min_width: int, pad_char: byte) {
NUM_TO_CHAR_TABLE :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$";
buf: [65]byte;
len := 0;
negative := false;
if i < 0 {
negative = true;
i = -i;
}
if i == 0 {
buf[len] = '0';
len++;
@@ -109,9 +102,8 @@ print_uint_base :: proc(i, base : uint) {
len++;
i /= base;
}
if negative {
buf[len] = '-';
for len < min_width {
buf[len] = pad_char;
len++;
}
@@ -119,9 +111,55 @@ print_uint_base :: proc(i, base : uint) {
print_string(buf[:len] as string);
}
print_bool :: proc(b : bool) {
if b { print_string("true"); }
else { print_string("false"); }
}
print_pointer :: proc(p: rawptr) #inline { print__uint(p as uint, 16, 0, ' '); }
print_f32 :: proc(f: f32) #inline { print__f64(f as f64, 7); }
print_f64 :: proc(f: f64) #inline { print__f64(f, 10); }
print__f64 :: proc(f: f64, decimal_places: int) {
if f == 0 {
print_rune('0');
return;
}
if f < 0 {
print_rune('-');
f = -f;
}
print_u64 :: proc(i: u64) {
NUM_TO_CHAR_TABLE :: "0123456789";
buf: [22]byte;
len := 0;
if i == 0 {
buf[len] = '0';
len++;
}
for i > 0 {
buf[len] = NUM_TO_CHAR_TABLE[i % 10];
len++;
i /= 10;
}
byte_reverse(buf[:len]);
print_string(buf[:len] as string);
}
i := f as u64;
print_u64(i);
f -= i as f64;
print_rune('.');
mult := 10.0;
for decimal_places := 6; decimal_places >= 0; decimal_places-- {
i = (f * mult) as u64;
print_u64(i as u64);
f -= i as f64 / mult;
mult *= 10;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,9 +1,10 @@
#load "basic.odin"
#load "win32.odin"
#load "opengl.odin"
#load "stb_image.odin"
#load "math.odin"
TWO_HEARTS :: '💕';
win32_perf_count_freq := GetQueryPerformanceFrequency();
time_now :: proc() -> f64 {
if win32_perf_count_freq == 0 {
@@ -26,7 +27,7 @@ win32_print_last_error :: proc() {
// Yuk!
to_c_string :: proc(s: string) -> ^u8 {
c_str := heap_alloc(len(s)+1) as ^u8;
c_str: ^u8 = heap_alloc(len(s)+1);
mem_copy(c_str, ^s[0], len(s));
c_str[len(s)] = 0;
return c_str;
@@ -38,20 +39,12 @@ type Window: struct {
wc: WNDCLASSEXA,
dc: HDC,
hwnd: HWND,
opengl_context: HGLRC,
opengl_context: rawptr,
rc: HGLRC,
c_title: ^u8,
}
win32_proc :: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT #no_inline {
if msg == WM_DESTROY || msg == WM_CLOSE || msg == WM_QUIT {
ExitProcess(0);
return 0;
}
return DefWindowProcA(hwnd, msg, wparam, lparam);
}
make_window :: proc(title: string, msg, height: int) -> (Window, bool) {
make_window :: proc(title: string, msg, height: int, window_proc: WNDPROC) -> (Window, bool) {
w: Window;
w.width, w.height = msg, height;
@@ -66,7 +59,7 @@ make_window :: proc(title: string, msg, height: int) -> (Window, bool) {
style = CS_VREDRAW | CS_HREDRAW,
hInstance = instance as HINSTANCE,
className = c_class_name,
wndProc = win32_proc,
wndProc = window_proc,
};
if RegisterClassExA(^w.wc) == 0 {
@@ -126,23 +119,6 @@ destroy_window :: proc(w: ^Window) {
heap_free(w.c_title);
}
update_window :: proc(w: ^Window) -> bool {
msg: MSG;
for {
ok := PeekMessageA(^msg, null, 0, 0, PM_REMOVE) != 0;
if !ok {
break;
}
if msg.message == WM_QUIT {
return true;
}
_ = TranslateMessage(^msg);
_ = DispatchMessageA(^msg);
}
return false;
}
display_window :: proc(w: ^Window) {
SwapBuffers(w.dc);
}
@@ -151,12 +127,23 @@ display_window :: proc(w: ^Window) {
main :: proc() {
window, window_success := make_window("Odin Language Demo", 854, 480);
win32_proc :: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT #no_inline {
if msg == WM_DESTROY || msg == WM_CLOSE || msg == WM_QUIT {
ExitProcess(0);
return 0;
}
return DefWindowProcA(hwnd, msg, wparam, lparam);
}
print_f64(13.37);
print_rune('\n');
/*
window, window_success := make_window("Odin Language Demo", 854, 480, win32_proc);
if !window_success {
return;
}
defer destroy_window(^window);
prev_time := time_now();
running := true;
for running {
@@ -164,8 +151,13 @@ main :: proc() {
dt := (curr_time - prev_time) as f32;
prev_time = curr_time;
if update_window(^window) {
running = false;
msg: MSG;
for PeekMessageA(^msg, null, 0, 0, PM_REMOVE) > 0 {
if msg.message == WM_QUIT {
running = false;
}
_ = TranslateMessage(^msg);
_ = DispatchMessageA(^msg);
}
glClearColor(0.5, 0.7, 1.0, 1.0);
@@ -176,6 +168,7 @@ main :: proc() {
0, window.height as f64, 0, 1);
draw_rect :: proc(x, y, w, h: f32) {
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0); glVertex3f(x, y, 0);
glColor3f(0, 1, 0); glVertex3f(x+w, y, 0);
glColor3f(0, 0, 1); glVertex3f(x+w, y+h, 0);
@@ -187,9 +180,14 @@ main :: proc() {
glEnd();
}
x, y : f32 = 100, 100;
x, y : f32 = 100+50*sinf(curr_time as f32), 100;
draw_rect(x, y, 50, 50);
display_window(^window);
ms_to_sleep := (16 - 1000*dt) as i32;
if ms_to_sleep > 0 {
sleep_ms(ms_to_sleep);
}
}
*/
}

View File

@@ -5,14 +5,15 @@ type Mat2: {4}f32
sqrt_f32 :: proc(x: f32) -> f32 #foreign "llvm.sqrt.f32"
sqrt_f64 :: proc(x: f64) -> f64 #foreign "llvm.sqrt.f64"
sqrtf :: proc(x: f32) -> f32 #foreign "llvm.sqrt.f32"
sinf :: proc(x: f32) -> f32 #foreign "llvm.sin.f32"
cosf :: proc(x: f32) -> f32 #foreign "llvm.cos.f32"
vec2_dot :: proc(a, b: Vec2) -> f32 { c := a*b; return c[0] + c[1]; }
vec3_dot :: proc(a, b: Vec3) -> f32 { c := a*b; return c[0] + c[1] + c[2]; }
lerp :: proc(a, b, t: f32) -> f32 { return a*(1-t) + b*t; }
vec2_mag :: proc(a: Vec2) -> f32 { return sqrt_f32(vec2_dot(a, a)); }
vec3_mag :: proc(a: Vec3) -> f32 { return sqrt_f32(vec3_dot(a, a)); }
vec2_mag :: proc(a: Vec2) -> f32 { return sqrtf(vec2_dot(a, a)); }
vec3_mag :: proc(a: Vec3) -> f32 { return sqrtf(vec3_dot(a, a)); }

View File

@@ -24,20 +24,20 @@ PM_REMOVE :: 1;
COLOR_BACKGROUND: rawptr : 1; // NOTE(bill): cast to HBRUSH when needed
type HANDLE: rawptr
type HWND: HANDLE
type HDC: HANDLE
type HANDLE: rawptr
type HWND: HANDLE
type HDC: HANDLE
type HINSTANCE: HANDLE
type HICON: HANDLE
type HCURSOR: HANDLE
type HMENU: HANDLE
type HBRUSH: HANDLE
type WPARAM: uint
type LPARAM: int
type LRESULT: int
type ATOM: i16
type HICON: HANDLE
type HCURSOR: HANDLE
type HMENU: HANDLE
type HBRUSH: HANDLE
type WPARAM: uint
type LPARAM: int
type LRESULT: int
type ATOM: i16
type BOOL: i32
type POINT: struct { x, y: i32 }
type BOOL: i32
type WNDPROC: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT

View File

@@ -210,7 +210,7 @@ void destroy_scope(Scope *scope) {
gb_for_array(i, scope->elements.entries) {
Entity *e =scope->elements.entries[i].value;
if (e->kind == Entity_Variable) {
if (!e->variable.used) {
if (!e->Variable.used) {
#if 0
warning(e->token, "Unused variable `%.*s`", LIT(e->token.string));
#endif
@@ -314,7 +314,7 @@ void add_global_constant(gbAllocator a, String name, Type *type, ExactValue valu
Token token = {Token_Identifier};
token.string = name;
Entity *entity = alloc_entity(a, Entity_Constant, NULL, token, type);
entity->constant.value = value;
entity->Constant.value = value;
add_global_entity(entity);
}
@@ -346,7 +346,7 @@ void init_universal_scope(void) {
Token token = {Token_Identifier};
token.string = builtin_procs[i].name;
Entity *entity = alloc_entity(a, Entity_Builtin, NULL, token, t_invalid);
entity->builtin.id = id;
entity->Builtin.id = id;
add_global_entity(entity);
}
}

View File

@@ -7,7 +7,6 @@ enum BuiltinProcId;
ENTITY_KIND(Constant), \
ENTITY_KIND(Variable), \
ENTITY_KIND(TypeName), \
ENTITY_KIND(AliasName), \
ENTITY_KIND(Procedure), \
ENTITY_KIND(Builtin), \
ENTITY_KIND(Count),
@@ -38,16 +37,14 @@ struct Entity {
isize order;
union {
struct { ExactValue value; } constant;
struct { ExactValue value; } Constant;
struct {
b8 visited;
b8 is_field;
b8 used;
} variable;
struct {} type_name;
struct {} alias_name;
struct {} procedure;
struct { BuiltinProcId id; } builtin;
} Variable;
struct { b8 used; } Procedure;
struct { BuiltinProcId id; } Builtin;
};
};
@@ -74,7 +71,7 @@ Entity *make_entity_variable(gbAllocator a, Scope *parent, Token token, Type *ty
Entity *make_entity_constant(gbAllocator a, Scope *parent, Token token, Type *type, ExactValue value) {
Entity *entity = alloc_entity(a, Entity_Constant, parent, token, type);
entity->constant.value = value;
entity->Constant.value = value;
return entity;
}
@@ -83,20 +80,15 @@ Entity *make_entity_type_name(gbAllocator a, Scope *parent, Token token, Type *t
return entity;
}
Entity *make_entity_alias_name(gbAllocator a, Scope *parent, Token token, Type *type) {
Entity *entity = alloc_entity(a, Entity_AliasName, parent, token, type);
return entity;
}
Entity *make_entity_param(gbAllocator a, Scope *parent, Token token, Type *type) {
Entity *entity = make_entity_variable(a, parent, token, type);
entity->variable.used = true;
entity->Variable.used = true;
return entity;
}
Entity *make_entity_field(gbAllocator a, Scope *parent, Token token, Type *type) {
Entity *entity = make_entity_variable(a, parent, token, type);
entity->variable.is_field = true;
entity->Variable.is_field = true;
return entity;
}
@@ -107,7 +99,7 @@ Entity *make_entity_procedure(gbAllocator a, Scope *parent, Token token, Type *s
Entity *make_entity_builtin(gbAllocator a, Scope *parent, Token token, Type *type, BuiltinProcId id) {
Entity *entity = alloc_entity(a, Entity_Builtin, parent, token, type);
entity->builtin.id = id;
entity->Builtin.id = id;
return entity;
}

View File

@@ -162,21 +162,20 @@ void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type) {
add_declaration_dependency(c, e);
if (e->type == t_invalid)
return;
o->value = e->constant.value;
o->value = e->Constant.value;
GB_ASSERT(o->value.kind != ExactValue_Invalid);
o->mode = Addressing_Constant;
break;
case Entity_Variable:
add_declaration_dependency(c, e);
e->variable.used = true;
e->Variable.used = true;
if (e->type == t_invalid)
return;
o->mode = Addressing_Variable;
break;
case Entity_TypeName:
case Entity_AliasName:
o->mode = Addressing_Type;
break;
@@ -186,7 +185,7 @@ void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type) {
break;
case Entity_Builtin:
o->builtin_id = e->builtin.id;
o->builtin_id = e->Builtin.id;
o->mode = Addressing_Builtin;
break;
@@ -665,7 +664,10 @@ void check_unary_expr(Checker *c, Operand *o, Token op, AstNode *node) {
void check_comparison(Checker *c, Operand *x, Operand *y, Token op) {
gbString err_str = NULL;
defer (gb_string_free(err_str));
defer ({
if (err_str != NULL)
gb_string_free(err_str);
});
if (check_is_assignable_to(c, x, y->type) ||
check_is_assignable_to(c, y, x->type)) {
@@ -673,13 +675,13 @@ void check_comparison(Checker *c, Operand *x, Operand *y, Token op) {
switch (op.kind) {
case Token_CmpEq:
case Token_NotEq:
defined = is_type_comparable(x->type);
defined = is_type_comparable(get_base_type(x->type));
break;
case Token_Lt:
case Token_Gt:
case Token_LtEq:
case Token_GtEq: {
defined = is_type_ordered(x->type);
defined = is_type_ordered(get_base_type(x->type));
} break;
}
@@ -698,7 +700,7 @@ void check_comparison(Checker *c, Operand *x, Operand *y, Token op) {
gb_bprintf("mismatched types `%s` and `%s`", xt, yt));
}
if (err_str) {
if (err_str != NULL) {
error(&c->error_collector, op, "Cannot compare expression, %s", err_str);
return;
}
@@ -893,8 +895,8 @@ void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
b32 is_const_expr = x->mode == Addressing_Constant;
b32 can_convert = false;
if (is_const_expr && is_type_constant_type(type)) {
Type *base_type = get_base_type(type);
Type *base_type = get_base_type(type);
if (is_const_expr && is_type_constant_type(base_type)) {
if (base_type->kind == Type_Basic) {
if (check_value_is_expressible(c, x->value, base_type, &x->value)) {
can_convert = true;
@@ -1274,7 +1276,7 @@ Entity *lookup_field(Type *type, AstNode *field_node, isize *index = NULL) {
case Type_Structure:
for (isize i = 0; i < type->structure.field_count; i++) {
Entity *f = type->structure.fields[i];
GB_ASSERT(f->kind == Entity_Variable && f->variable.is_field);
GB_ASSERT(f->kind == Entity_Variable && f->Variable.is_field);
String str = f->token.string;
if (are_strings_equal(field_str, str)) {
if (index) *index = i;

View File

@@ -190,14 +190,14 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
ast_node(i, Ident, node);
e = scope_lookup_entity(c->context.scope, i->token.string);
if (e != NULL && e->kind == Entity_Variable) {
used = e->variable.used; // TODO(bill): Make backup just in case
used = e->Variable.used; // TODO(bill): Make backup just in case
}
}
Operand op_b = {Addressing_Invalid};
check_expr(c, &op_b, lhs);
if (e) e->variable.used = used;
if (e) e->Variable.used = used;
if (op_b.mode == Addressing_Invalid ||
op_b.type == t_invalid) {
@@ -325,18 +325,18 @@ void check_init_constant(Checker *c, Entity *e, Operand *operand) {
if (operand->mode == Addressing_Invalid)
return;
e->constant.value = operand->value;
e->Constant.value = operand->value;
}
void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr) {
GB_ASSERT(e->type == NULL);
if (e->variable.visited) {
if (e->Variable.visited) {
e->type = t_invalid;
return;
}
e->variable.visited = true;
e->Variable.visited = true;
if (type_expr) {
Type *t = check_type(c, type_expr);
@@ -437,11 +437,11 @@ void check_var_decl(Checker *c, Entity *e, Entity **entities, isize entity_count
GB_ASSERT(e->type == NULL);
GB_ASSERT(e->kind == Entity_Variable);
if (e->variable.visited) {
if (e->Variable.visited) {
e->type = t_invalid;
return;
}
e->variable.visited = true;
e->Variable.visited = true;
if (type_expr != NULL)
e->type = check_type(c, type_expr, NULL);
@@ -791,11 +791,11 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
for (isize i = 0; i < entity_count; i++) {
Entity *e = entities[i];
GB_ASSERT(e != NULL);
if (e->variable.visited) {
if (e->Variable.visited) {
e->type = t_invalid;
continue;
}
e->variable.visited = true;
e->Variable.visited = true;
if (e->type == NULL)
e->type = init_type;

View File

@@ -1364,12 +1364,10 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) {
// Pointer <-> int
if (is_type_pointer(src) && is_type_int_or_uint(dst)) {
ssaValue *p = ssa_emit_load(proc, value);
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_ptrtoint, p, src, dst));
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_ptrtoint, value, src, dst));
}
if (is_type_int_or_uint(src) && is_type_pointer(dst)) {
ssaValue *i = ssa_emit_load(proc, value);
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_inttoptr, i, src, dst));
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_inttoptr, value, src, dst));
}
// Pointer <-> Pointer
@@ -1708,7 +1706,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
Entity **found = map_get(&proc->module->info->uses, hash_pointer(p));
if (found && (*found)->kind == Entity_Builtin) {
Entity *e = *found;
switch (e->builtin.id) {
switch (e->Builtin.id) {
case BuiltinProc_len: {
// len :: proc(Type) -> int
// NOTE(bill): len of an array is a constant expression

View File

@@ -35,9 +35,8 @@ i32 win32_exec_command_line_app(char *fmt, ...) {
return cast(i32)exit_code;
} else {
// NOTE(bill): failed to create process
return -1;
}
return 0;
}
int main(int argc, char **argv) {
@@ -79,19 +78,19 @@ int main(int argc, char **argv) {
isize base_name_len = gb_path_extension(output_name)-1 - output_name;
i32 exit_code = win32_exec_command_line_app(
"opt -mem2reg %s -o %.*s.bc",
"../misc/llvm-bin/opt -mem2reg %s -o %.*s.bc",
output_name, cast(int)base_name_len, output_name);
if (exit_code == 0) {
win32_exec_command_line_app(
"clang -o %.*s.exe %.*s.bc -Wno-override-module "
"-lKernel32.lib -lUser32.lib -lGdi32.lib -lOpengl32.lib "
"-l../c_libs/stb_image.lib"
,
cast(int)base_name_len, output_name,
cast(int)base_name_len, output_name);
if (run_output) {
win32_exec_command_line_app("%.*s.exe", cast(int)base_name_len, output_name);
}
} else {
}
return 0;

View File

@@ -172,7 +172,7 @@ gb_no_inline void warning(Token token, char *fmt, ...) {
// NOTE(bill): result == priority
i32 token_precedence(Token t) {
switch (t.kind) {
case Token_CmpOr: