mirror of
https://github.com/bearlanguageorg/bear.git
synced 2026-08-26 14:57:18 +00:00
Enhacnementsand toolchain
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
// toolchain alloc
|
||||
// A tiny byte arena for toolchain scratch memory: allocate fixed-size blocks
|
||||
// from a pre-sized arena and release them all at once.
|
||||
// Run standalone with: v run bin/tl_alloc.v
|
||||
|
||||
module main
|
||||
|
||||
struct Arena {
|
||||
mut:
|
||||
data []u8
|
||||
used int
|
||||
}
|
||||
|
||||
// new_arena reserves `cap` bytes of scratch memory.
|
||||
fn new_arena(cap int) Arena {
|
||||
return Arena{
|
||||
data: []u8{len: cap}
|
||||
used: 0
|
||||
}
|
||||
}
|
||||
|
||||
// alloc returns an offset into the arena for a block of `n` bytes.
|
||||
fn (mut a Arena) alloc(n int) !int {
|
||||
if a.used + n > a.data.len {
|
||||
return error('arena out of memory: need ${n} bytes, ${a.data.len - a.used} left')
|
||||
}
|
||||
off := a.used
|
||||
a.used += n
|
||||
return off
|
||||
}
|
||||
|
||||
// free_all releases everything allocated so far.
|
||||
fn (mut a Arena) free_all() {
|
||||
a.used = 0
|
||||
}
|
||||
|
||||
fn (a Arena) stats() string {
|
||||
return 'arena: ${a.used} / ${a.data.len} bytes used'
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut arena := new_arena(1024 * 1024) // 1 MiB
|
||||
println('toolchain alloc: arena ready')
|
||||
println(arena.stats())
|
||||
off1 := arena.alloc(64) or { eprintln(err); exit(1) }
|
||||
off2 := arena.alloc(4096) or { eprintln(err); exit(1) }
|
||||
println('allocated 64 bytes at offset ${off1}')
|
||||
println('allocated 4096 bytes at offset ${off2}')
|
||||
println(arena.stats())
|
||||
arena.free_all()
|
||||
println('after free_all: ${arena.stats()}')
|
||||
}
|
||||
+48
-23
@@ -1,28 +1,53 @@
|
||||
// toolchain loader
|
||||
// Discovers and describes the toolchain components baked into the binary.
|
||||
// Run standalone with: v run bin/tl_loader.v
|
||||
|
||||
module tl_loader
|
||||
module main
|
||||
|
||||
import os
|
||||
import v.vmod
|
||||
|
||||
pub fn load_toolchain() {
|
||||
// Load the toolchain modules and initialize them
|
||||
// We call the package functions to load the toolchain modules and initialize them
|
||||
// each package has their own load argument, which is passed to the package function.
|
||||
toolchain_loader(load)
|
||||
toolchain_alloc(load)
|
||||
toolchain_compile(load)
|
||||
toolchain_assemble(load)
|
||||
toolchain_link(load)
|
||||
toolchain_run(load)
|
||||
toolchain_debug(load)
|
||||
toolchain_help(load)
|
||||
toolchain_version(load)
|
||||
toolchain_info(load)
|
||||
toolchain_config(load)
|
||||
toolchain_test(load)
|
||||
toolchain_bench(load)
|
||||
toolchain_clean(load)
|
||||
toolchain_up(load)
|
||||
toolchain_symlink(load)
|
||||
}
|
||||
struct Component {
|
||||
mut:
|
||||
name string
|
||||
role string
|
||||
path string
|
||||
enabled bool
|
||||
}
|
||||
|
||||
// load_toolchain returns the components that make up this toolchain.
|
||||
fn load_toolchain() []Component {
|
||||
root := os.dir(os.executable())
|
||||
return [
|
||||
Component{
|
||||
name: 'compiler'
|
||||
role: 'lexer, parser, bytecode codegen'
|
||||
path: os.join_path(root, 'compiler')
|
||||
enabled: true
|
||||
},
|
||||
Component{
|
||||
name: 'assembler'
|
||||
role: 'vasm -> vobj'
|
||||
path: os.join_path(root, 'assembler')
|
||||
enabled: true
|
||||
},
|
||||
Component{
|
||||
name: 'linker'
|
||||
role: 'vobj set -> vbin'
|
||||
path: os.join_path(root, 'linker')
|
||||
enabled: true
|
||||
},
|
||||
Component{
|
||||
name: 'vm'
|
||||
role: 'stack-based runtime'
|
||||
path: os.join_path(root, 'vm')
|
||||
enabled: true
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
fn main() {
|
||||
for c in load_toolchain() {
|
||||
status := if c.enabled { 'loaded' } else { 'disabled' }
|
||||
println('[${status}] ${c.name:-12} ${c.role} (${c.path})')
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user