Skip to content

Commit

Permalink
Misc additions
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaquraish committed Oct 7, 2024
1 parent 999f632 commit cc41e55
Show file tree
Hide file tree
Showing 14 changed files with 9,018 additions and 8,449 deletions.
16,970 changes: 8,524 additions & 8,446 deletions bootstrap/stage0.c

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion compiler/passes/code_generator.oc
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ def CodeGenerator::gen_in_yield_context(&this, node: &AST) {
.indent -= 1
}

.out += ";"
.out += "\n"
.gen_indent()
.out += yield_var
.out += "; })"

Expand Down
63 changes: 63 additions & 0 deletions examples/compress.oc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import std::shift_args
import std::fs
import std::huffman

enum Mode {
Compress
Decompress
None
}

def usage(code: i32, full: bool) {
println("Usage:")
println(" ./huffman <-c|-d> -i <path> -o <path>")
if not full then std::exit(code)

println("--------------------------------------------------------")
println("Compile Options:")
println(" -i path Input path")
println(" -o path Output path")
println(" -c Compress")
println(" -d Decompress")
println(" -h Display this information")
std::exit(code)
}

let mode: Mode = None
let input: str = "/dev/stdin"
let output: str = "/dev/stdout"


def parse_args(argc: &i32, argv: &&str) {
while *argc > 0 {
let arg = shift_args(argc, argv)
match arg {
"-h" | "--help" => usage(code: 0, true)
"-i" | "--input" => input = shift_args(argc, argv)
"-o" | "--output" => output = shift_args(argc, argv)
"-c" | "--compress" => mode = Compress
"-d" | "--decompress" => mode = Decompress
else => {
println("Unknown option/argument: '%s'", arg)
usage(code: 1, true)
}
}
}

if mode == None {
println("Mode was not selected. Please choose -c or -d")
usage(code: 1, true)
}
}

def main(argc: i32, argv: &str) {
shift_args(&argc, &argv)
parse_args(&argc, &argv)
let file = fs::read_file_inc(input)
let res = match mode {
Compress => huffman::compress(&file)
Decompress => huffman::decompress(&file)
None => std::panic("unreachable")
}
fs::write_file(output, res)
}
2 changes: 1 addition & 1 deletion meta/gen_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cat > compiler/doc.oc << EOF
//* Nothing to see here
import std::{ vector, map, compact_map, heap, deque, set, linkedlist, variadic, bencode }
import std::{ buffer, image, json, math, complex, fft, random, curl, og, setjmp }
import std::{ buffer, image, json, math, complex, fft, random, curl, og, setjmp, huffman }
import std::{ value, vec, glut, socket, sort, thread, video_renderer, sv, fs, zlib, time }
import std::traits::{ hash, eq, compare }
import std::{ image::draw, hash::sha1, hash::sha256 }
Expand Down
91 changes: 91 additions & 0 deletions std/bitio.oc
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//! Wrapper over {{Buffer}} and {{SV}} that let you read/write one bit at a time.
import std::buffer::{ Buffer }
import std::sv::{ SV }

struct BitWriter {
buffer: &Buffer
byte: u8
bit: u8
}

def BitWriter::make(buffer: &Buffer): BitWriter {
return BitWriter(
buffer: buffer,
byte: 0,
bit: 0
)
}

def BitWriter::write_bit(&this, bit: u8) {
if bit != 0 {
.byte = .byte | (1u8 << .bit)
} else {
.byte = .byte & ~(1u8 << .bit)
}
if ++.bit == 8 {
.buffer.write_u8(.byte)
.byte = 0
.bit = 0
}
}

def BitWriter::write_u8(&this, byte: u8) {
for let i = 0u8; i < 8; i++ {
.write_bit((byte >> i) & 1)
}
}

def BitWriter::finish(&this) {
if .bit != 0 {
.buffer.write_u8(.byte)
}
}


struct BitReader {
data: &u8
num_bits: u32 // Maximum number of bits to read
idx: u32
}

def BitReader::from_data(data: &u8, num_bits: u32): BitReader {
return BitReader(
data: data,
num_bits: num_bits,
idx: 0
)
}

def BitReader::from_sv(sv: SV, num_bits: u32 = 0): BitReader {
if num_bits == 0 {
num_bits = sv.len * 8
}
return BitReader::from_data(sv.data as &u8, num_bits)
}

def BitReader::read_bit(&this): u8 {
if .idx >= .num_bits {
return 0
}
let byte = .data[.idx / 8]
let bit_offset = (.idx % 8) as u8
let bit = (byte >> bit_offset) & 1
.idx += 1
return bit
}

def BitReader::read_u8(&this): u8 {
let byte: u8 = 0
for let i = 0u8; i < 8; i++ {
byte = byte | (.read_bit() << i)
}
return byte
}

def BitReader::has_bits(&this): bool => .idx < .num_bits

// Iterator methods
def BitReader::has_value(&this): bool => .idx < .num_bits
def BitReader::cur(&this): u8 => .data[.idx / 8] >> (.idx % 8) as u8 & 1
def BitReader::next(&this) => .idx += 1
12 changes: 12 additions & 0 deletions std/buffer.oc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,19 @@ def hex_dump(data: &u8, size: u32) {
println("")
}

def bit_dump(data: &u8, size: u32) {
print("(%d bytes): ", size)
for let i = 0; i < size; i += 1 {
if (i > 0) print("_")
for let j = 0u8; j < 8; j += 1 {
print("%d", (data[i] >> (7u8 - j)) & 1)
}
}
println("")
}

def Buffer::hex_dump(this) => hex_dump(.data, .size)
def Buffer::bit_dump(this) => bit_dump(.data, .size)

[operator "[]"]
def Buffer::get_byte_at(this, index: u32): u8 {
Expand Down
23 changes: 23 additions & 0 deletions std/fs.oc
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,29 @@ def read_file(path: str): Buffer {
return data
}

//! Incrementally read the whole file (ie; without fseek/ftell). This is useful
//! for reading special files like /dev/stdin and /proc/cpuinfo.
def read_file_inc(path: str): Buffer {
let file = bindings::fopen(path)
if not file? {
std::panic(`[-] Failed to open file: {path}: {std::libc::get_err()}`)
}

let buf = Buffer::make(capacity: 1025)
while true {
let read = bindings::fread(buf.data + buf.size, 1, 1024, file)
if read == 0 {
break
}
buf.size += read as u32
buf.resize_if_necessary(new_size: buf.size + 1025)
}
buf.data[buf.size] = 0
bindings::fclose(file)
return buf
}


def directory_exists(path: str): bool {
let dir = bindings::opendir(path)
if dir == null return false
Expand Down
Loading

0 comments on commit cc41e55

Please sign in to comment.