mirror of
https://github.com/odin-lang/Odin.git
synced 2026-04-19 04:50:29 +00:00
A decent union type with common fields and variants
This commit is contained in:
100
code/demo.odin
100
code/demo.odin
@@ -7,12 +7,102 @@
|
||||
#import "os.odin";
|
||||
// #import "halloc.odin";
|
||||
|
||||
Token_Kind :: enum {
|
||||
}
|
||||
Token_Pos :: struct {
|
||||
file: string,
|
||||
line: int,
|
||||
column: int,
|
||||
}
|
||||
Token :: struct {
|
||||
kind: Token_Kind,
|
||||
name: string,
|
||||
using pos: Token_Pos,
|
||||
}
|
||||
|
||||
Exact_Value :: union {
|
||||
Boolean {b: bool},
|
||||
String {s: string},
|
||||
Integer {i: i64},
|
||||
Float {f: f64},
|
||||
Pointer {p: i64},
|
||||
Compound{c: rawptr},
|
||||
}
|
||||
Overload_Kind :: enum {
|
||||
UNKNOWN,
|
||||
NO,
|
||||
YES,
|
||||
}
|
||||
Scope :: struct {
|
||||
parent: ^Scope,
|
||||
prev, next: ^Scope,
|
||||
first_child, last_child: ^Scope,
|
||||
elements: map[string]^Entity,
|
||||
implicit: map[^Entity]bool,
|
||||
|
||||
shared: [dynamic]^Scope,
|
||||
imported: [dynamic]^Scope,
|
||||
is_proc: bool,
|
||||
is_global: bool,
|
||||
is_file: bool,
|
||||
is_init: bool,
|
||||
has_been_imported: bool, // This is only applicable to file scopes
|
||||
file: rawptr,
|
||||
}
|
||||
|
||||
Type :: struct {
|
||||
}
|
||||
|
||||
Entity :: union {
|
||||
// Common Fields
|
||||
flags: u32,
|
||||
using token: Token,
|
||||
scope: ^Scope, // Parent's scope
|
||||
type: ^Type,
|
||||
// identifier: ^ast.Node,
|
||||
|
||||
using_parent: ^Entity,
|
||||
// using_expr: ^ast.Node,
|
||||
|
||||
// Variants
|
||||
Constant{value: Exact_Value},
|
||||
Variable{
|
||||
field_index, field_src_index: int,
|
||||
is_immutable, is_thread_local: bool,
|
||||
},
|
||||
Type_Name{},
|
||||
Procedure{
|
||||
is_foreign: bool,
|
||||
foreign_name: string,
|
||||
foreign_library: ^Entity,
|
||||
link_name: string,
|
||||
tags: u64,
|
||||
overload_kind: Overload_Kind,
|
||||
},
|
||||
Builtin{id: int},
|
||||
Import_Name{
|
||||
import_path: string,
|
||||
import_name: string,
|
||||
import_scope: ^Scope,
|
||||
used: bool,
|
||||
},
|
||||
Library_Name{
|
||||
library_path: string,
|
||||
library_name: string,
|
||||
used: bool,
|
||||
},
|
||||
Nil{},
|
||||
}
|
||||
|
||||
main :: proc() {
|
||||
m: map[int]int;
|
||||
m[123] = 312;
|
||||
fmt.println(m[123]);
|
||||
delete(m, 123);
|
||||
fmt.println(m[123]);
|
||||
e: Entity;
|
||||
u := union_cast(^Type_Info.Union)type_info_base(type_info_of_val(e));
|
||||
|
||||
|
||||
fmt.println(type_info_base(type_info(Entity)));
|
||||
|
||||
|
||||
// e.flags = 123;
|
||||
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user