Skip to content

Commit

Permalink
Fix parsing for block/if expression within if/for/etc. statements
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerBill committed Jan 27, 2017
1 parent 9245336 commit 31aacd5
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 281 deletions.
2 changes: 1 addition & 1 deletion build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ cl %compiler_settings% "src\main.c" ^
/link %linker_settings% -OUT:%exe_name% ^
&& odin run code/demo.odin
rem && odin build_dll code/example.odin ^
rem && odin run code/demo.odin
rem odin run code/punity.odin

rem pushd src\asm
rem nasm hellope.asm -fwin64 -o hellope.obj ^
Expand Down
29 changes: 16 additions & 13 deletions code/demo.odin
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#import "fmt.odin";
#import "utf8.odin";
#import "atomic.odin";
#import "hash.odin";
#import "math.odin";
#import "mem.odin";
#import "opengl.odin";
#import "os.odin";
#import "sync.odin";
// #import "atomic.odin";
// #import "hash.odin";
// #import "math.odin";
// #import "mem.odin";
// #import "opengl.odin";
// #import "os.odin";
// #import "sync.odin";
// #import win32 "sys/windows.odin";

main :: proc() {
syntax();
Expand All @@ -15,8 +16,8 @@ main :: proc() {
syntax :: proc() {
// Cyclic type checking
// Uncomment to see the error
// A :: struct { b: B };
// B :: struct { a: A };
// A :: struct {b: B};
// B :: struct {a: A};

x: int;
y := cast(f32)x;
Expand All @@ -40,10 +41,10 @@ syntax :: proc() {
};
Thing2 :: struct {x: f32, y: int, z: ^[]int};

// Slice interals are not just a `ptr+count`
// Slice interals are now just a `ptr+count`
slice: []int; compile_assert(size_of_val(slice) == 2*size_of(int));

// Helper type - Help the reader understand that it is quicker
// Helper type - Help the reader understand what it is quicker
My_Int :: type int;
My_Proc :: type proc(int) -> f32;

Expand All @@ -62,6 +63,9 @@ syntax :: proc() {
// ++ and -- have been removed
// x++;
// x--;
// Question: Should they be added again?
// They were removed as they are redundant and statements, not expressions
// like in C/C++


// You can now build files as a `.dll`
Expand Down Expand Up @@ -167,7 +171,6 @@ special_expressions :: proc() {
}

loops :: proc() {

// The C-style for loop
for i := 0; i < 123; i += 1 {
break;
Expand Down Expand Up @@ -259,7 +262,7 @@ procedure_overloading :: proc() {
}


a: i32 = #line;
a: i32 = 123;
b: f32;
c: rawptr;
fmt.println(foo(^a));
Expand Down
172 changes: 86 additions & 86 deletions code/old_demos/demo001.odin
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#import "fmt.odin"
#import "os.odin"
#import "mem.odin"
// #import "http_test.odin" as ht
// #import "game.odin" as game
// #import "punity.odin" as pn
#import "fmt.odin";
#import "os.odin";
#import "mem.odin";
// #import "http_test.odin" as ht;
// #import "game.odin" as game;
// #import "punity.odin" as pn;

main :: proc() {
// struct_padding()
Expand All @@ -26,58 +26,58 @@ main :: proc() {
struct_padding :: proc() {
{
A :: struct {
a: u8
b: u32
c: u16
a: u8,
b: u32,
c: u16,
}

B :: struct {
a: [7]u8
b: [3]u16
c: u8
d: u16
a: [7]u8,
b: [3]u16,
c: u8,
d: u16,
}

fmt.println("size_of(A):", size_of(A))
fmt.println("size_of(B):", size_of(B))
fmt.println("size_of(A):", size_of(A));
fmt.println("size_of(B):", size_of(B));

// n.b. http://cbloomrants.blogspot.co.uk/2012/07/07-23-12-structs-are-not-what-you-want.html
}
{
A :: struct #ordered {
a: u8
b: u32
c: u16
a: u8,
b: u32,
c: u16,
}

B :: struct #ordered {
a: [7]u8
b: [3]u16
c: u8
d: u16
a: [7]u8,
b: [3]u16,
c: u8,
d: u16,
}

fmt.println("size_of(A):", size_of(A))
fmt.println("size_of(B):", size_of(B))
fmt.println("size_of(A):", size_of(A));
fmt.println("size_of(B):", size_of(B));

// C-style structure layout
}
{
A :: struct #packed {
a: u8
b: u32
c: u16
a: u8,
b: u32,
c: u16,
}

B :: struct #packed {
a: [7]u8
b: [3]u16
c: u8
d: u16
a: [7]u8,
b: [3]u16,
c: u8,
d: u16,
}

fmt.println("size_of(A):", size_of(A))
fmt.println("size_of(B):", size_of(B))
fmt.println("size_of(A):", size_of(A));
fmt.println("size_of(B):", size_of(B));

// Useful for explicit layout
}
Expand Down Expand Up @@ -119,7 +119,7 @@ struct_padding :: proc() {
}

bounds_checking :: proc() {
x: [4]int
x: [4]int;
// x[-1] = 0; // Compile Time
// x[4] = 0; // Compile Time

Expand All @@ -132,9 +132,9 @@ bounds_checking :: proc() {
// Works for arrays, strings, slices, and related procedures & operations

{
base: [10]int
s := base[2:6]
a, b := -1, 6
base: [10]int;
s := base[2:6];
a, b := -1, 6;

#no_bounds_check {
s[a] = 0;
Expand All @@ -154,69 +154,69 @@ bounds_checking :: proc() {

type_introspection :: proc() {
{
info: ^Type_Info
x: int
info: ^Type_Info;
x: int;

info = type_info(int) // by type
info = type_info_of_val(x) // by value
info = type_info(int); // by type
info = type_info_of_val(x); // by value
// See: runtime.odin

match type i : info {
match type i in info {
case Type_Info.Integer:
fmt.println("integer!")
fmt.println("integer!");
case Type_Info.Float:
fmt.println("float!")
fmt.println("float!");
default:
fmt.println("potato!")
fmt.println("potato!");
}

// Unsafe cast
integer_info := info as ^Type_Info.Integer
integer_info := cast(^Type_Info.Integer)info;
}

{
Vector2 :: struct { x, y: f32 }
Vector3 :: struct { x, y, z: f32 }

v1: Vector2
v2: Vector3
v3: Vector3
v1: Vector2;
v2: Vector3;
v3: Vector3;

t1 := type_info_of_val(v1)
t2 := type_info_of_val(v2)
t3 := type_info_of_val(v3)
t1 := type_info_of_val(v1);
t2 := type_info_of_val(v2);
t3 := type_info_of_val(v3);

fmt.println()
fmt.print("Type of v1 is:\n\t", t1)
fmt.println();
fmt.print("Type of v1 is:\n\t", t1);

fmt.println()
fmt.print("Type of v2 is:\n\t", t2)
fmt.println();
fmt.print("Type of v2 is:\n\t", t2);

fmt.println("\n")
fmt.println("t1 == t2:", t1 == t2)
fmt.println("t2 == t3:", t2 == t3)
fmt.println("\n");
fmt.println("t1 == t2:", t1 == t2);
fmt.println("t2 == t3:", t2 == t3);
}
}

any_type :: proc() {
a: any
a: any;

x: int = 123
y: f64 = 6.28
z: string = "Yo-Yo Ma"
x: int = 123;
y: f64 = 6.28;
z: string = "Yo-Yo Ma";
// All types can be implicit cast to `any`
a = x
a = y
a = z
a = a // This the "identity" type, it doesn't get converted
a = x;
a = y;
a = z;
a = a; // This the "identity" type, it doesn't get converted

a = 123 // Literals are copied onto the stack first
a = 123; // Literals are copied onto the stack first

// any has two members
// data - rawptr to the data
// type_info - pointer to the type info

fmt.println(x, y, z)
fmt.println(x, y, z);
// See: fmt.odin
// For variadic any procedures in action
}
Expand All @@ -232,15 +232,15 @@ crazy_introspection :: proc() {
TOMATO,
}

s: string
s = enum_to_string(Fruit.PEACH)
fmt.println(s)
s: string;
// s = enum_to_string(Fruit.PEACH);
fmt.println(s);

f := Fruit.GRAPE
s = enum_to_string(f)
fmt.println(s)
f := Fruit.GRAPE;
// s = enum_to_string(f);
fmt.println(s);

fmt.println(f)
fmt.println(f);
// See: runtime.odin
}

Expand All @@ -259,26 +259,26 @@ crazy_introspection :: proc() {
TOMATO,
}

fruit_ti := type_info(Fruit)
name := (fruit_ti as ^Type_Info.Named).name // Unsafe casts
info := type_info_base(fruit_ti) as ^Type_Info.Enum // Unsafe casts
fruit_ti := type_info(Fruit);
name := (cast(^Type_Info.Named)fruit_ti).name; // Unsafe casts
info := cast(^Type_Info.Enum)type_info_base(fruit_ti); // Unsafe casts

fmt.printf("% :: enum % {\n", name, info.base);
for i := 0; i < info.values.count; i++ {
fmt.printf("\t%\t= %,\n", info.names[i], info.values[i])
for i := 0; i < info.values.count; i += 1 {
fmt.printf("\t%\t= %,\n", info.names[i], info.values[i]);
}
fmt.printf("}\n")
fmt.printf("}\n");

// NOTE(bill): look at that type-safe printf!
}

{
Vector3 :: struct {x, y, z: f32}

a := Vector3{x = 1, y = 4, z = 9}
fmt.println(a)
b := Vector3{x = 9, y = 3, z = 1}
fmt.println(b)
a := Vector3{x = 1, y = 4, z = 9};
fmt.println(a);
b := Vector3{x = 9, y = 3, z = 1};
fmt.println(b);

// NOTE(bill): See fmt.odin
}
Expand Down
Loading

0 comments on commit 31aacd5

Please sign in to comment.