odin-libui

Odin bindings to libui
Log | Files | Refs | Submodules | README | LICENSE

main.odin (1474B)


      1 // Package generator implements logic for generating bindings for libui.
      2 // 
      3 // This package has rough parsing logic that is tuned for the C syntax
      4 // found in ui.h, and does not come even close to being a generally-useful
      5 // C parser.
      6 // 
      7 // Specfically we do no type checking and we minimal expression parsing.
      8 package main
      9 
     10 import "core:bufio"
     11 import "core:fmt"
     12 import "core:log"
     13 import "core:mem/virtual"
     14 import "core:os"
     15 
     16 main :: proc() {
     17 	context.logger = log.create_console_logger()
     18 
     19 	dir := os.get_current_directory()
     20 
     21 	f, f_err := os.open("libui/ui.h")
     22 	if f_err != os.ERROR_NONE {
     23 		log.errorf("failed opening header file: %w", f_err)
     24 		return
     25 	}
     26 
     27 	defer os.close(f)
     28 
     29 	by, by_err := os.read_entire_file_or_err(f)
     30 	if by_err != os.ERROR_NONE {
     31 		log.errorf("failed reading header file: %w", by_err)
     32 		return
     33 	}
     34 
     35 	defer delete(by)
     36 
     37 	arena: virtual.Arena
     38 	if err := virtual.arena_init_growing(&arena); err != nil {
     39 		panic(fmt.tprintf("creating growing virtual memory arena: %w", err))
     40 	}
     41 
     42 	context.allocator = virtual.arena_allocator(&arena)
     43 	defer free_all()
     44 
     45 	p: Parser
     46 	parser_parse(&p, by)
     47 
     48 	os.remove("libui.odin")
     49 
     50 	out_f, out_f_err := os.open("libui.odin", os.O_CREATE | os.O_RDWR, 0o644)
     51 	if out_f_err != os.ERROR_NONE {
     52 		log.errorf("failed opening output file: %w", out_f_err)
     53 		return
     54 	}
     55 
     56 	defer os.close(out_f)
     57 
     58 	buf_writer: bufio.Writer
     59 	bufio.writer_init(&buf_writer, os.stream_from_handle(out_f))
     60 
     61 	generate(bufio.writer_to_writer(&buf_writer), p)
     62 }
     63