Skip to content

Commit

Permalink
fix lsp bug; add bencode
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaquraish committed Mar 28, 2024
1 parent f917a49 commit 04f84cc
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 87 deletions.
87 changes: 2 additions & 85 deletions bootstrap/stage0.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,6 @@ typedef struct std_vector_Iterator__14 std_vector_Iterator__14;
typedef struct std_vector_Iterator__15 std_vector_Iterator__15;
typedef struct std_vector_Iterator__16 std_vector_Iterator__16;
typedef struct std_vector_Iterator__17 std_vector_Iterator__17;
typedef struct std_vector_Iterator__18 std_vector_Iterator__18;

/* Struct definitions */
struct compiler_docgen_DocGenerator {
Expand Down Expand Up @@ -1549,11 +1548,6 @@ struct std_vector_Iterator__17 {
u32 index;
};

struct std_vector_Iterator__18 {
std_vector_Vector__18 *vec;
u32 index;
};


bool compiler_lsp_verbose = false;
char *exec_path = "./out";
Expand Down Expand Up @@ -2004,7 +1998,6 @@ std_compact_map_Iterator__0 std_compact_map_Map__0_iter(std_compact_map_Map__0 *
std_compact_map_Item__0 std_compact_map_Iterator__0_cur(std_compact_map_Iterator__0 *this);
void std_compact_map_Iterator__0_next(std_compact_map_Iterator__0 *this);
bool std_compact_map_Iterator__0_has_value(std_compact_map_Iterator__0 *this);
void std_compact_map_Map__0_free(std_compact_map_Map__0 *this);
void std_compact_map_Map__0_insert(std_compact_map_Map__0 *this, char *key, std_value_Value *value);
u32 std_compact_map_Map__0_get_index(std_compact_map_Map__0 *this, char *key, u32 hash);
std_value_Value *std_compact_map_Map__0_get(std_compact_map_Map__0 *this, char *key, std_value_Value *defolt);
Expand Down Expand Up @@ -2052,7 +2045,6 @@ std_value_Value *std_value_Value_get(std_value_Value *this, char *key, std_value
void std_value_Value_insert(std_value_Value *this, char *key, std_value_Value *value);
std_compact_map_Map__0 *std_value_Value_as_dict(std_value_Value *this);
char *std_value_Value_dbg(std_value_Value *this);
void std_value_Value_free(std_value_Value *this);
char *std_fs_realpath(char *path);
bool std_fs_file_exists(char *path);
void std_fs_write_file_bytes(char *path, void *data, u32 size);
Expand Down Expand Up @@ -2328,13 +2320,7 @@ compiler_ast_nodes_MatchCase *std_vector_Vector__17_at(std_vector_Vector__17 *th
void std_vector_Vector__17_resize(std_vector_Vector__17 *this, u32 new_capacity);
std_vector_Vector__17 *std_vector_Vector__17_new(u32 capacity);
void std_vector_Vector__17_push(std_vector_Vector__17 *this, compiler_ast_nodes_MatchCase *value);
std_vector_Iterator__18 std_vector_Vector__18_iter(std_vector_Vector__18 *this);
std_value_Value *std_vector_Iterator__18_cur(std_vector_Iterator__18 *this);
void std_vector_Iterator__18_next(std_vector_Iterator__18 *this);
std_vector_Iterator__18 std_vector_Iterator__18_make(std_vector_Vector__18 *vec);
bool std_vector_Iterator__18_has_value(std_vector_Iterator__18 *this);
std_value_Value *std_vector_Vector__18_at(std_vector_Vector__18 *this, u32 i);
void std_vector_Vector__18_free(std_vector_Vector__18 *this);
void std_vector_Vector__18_resize(std_vector_Vector__18 *this, u32 new_capacity);
std_vector_Vector__18 *std_vector_Vector__18_new(u32 capacity);
void std_vector_Vector__18_push(std_vector_Vector__18 *this, std_value_Value *value);
Expand All @@ -2344,7 +2330,6 @@ void std_vector_Iterator__19_next(std_vector_Iterator__19 *this);
std_vector_Iterator__19 std_vector_Iterator__19_make(std_vector_Vector__19 *vec);
bool std_vector_Iterator__19_has_value(std_vector_Iterator__19 *this);
std_compact_map_Item__0 std_vector_Vector__19_at(std_vector_Vector__19 *this, u32 i);
void std_vector_Vector__19_free(std_vector_Vector__19 *this);
void std_vector_Vector__19_resize(std_vector_Vector__19 *this, u32 new_capacity);
std_vector_Vector__19 *std_vector_Vector__19_new(u32 capacity);
void std_vector_Vector__19_push(std_vector_Vector__19 *this, std_compact_map_Item__0 value);
Expand Down Expand Up @@ -8945,6 +8930,7 @@ void compiler_parser_Parser_include_prelude_only(compiler_parser_Parser *this) {
if (!((bool)std_path)) {
compiler_parser_couldnt_find_stdlib();
}
compiler_parser_Parser_consume_end_of_statement(this);
char *prelude_path = format_string("%s/prelude.h", std_path);
if (!std_fs_file_exists(prelude_path)) {
compiler_parser_couldnt_find_stdlib();
Expand Down Expand Up @@ -10007,7 +9993,6 @@ void compiler_lsp_handle_validate(compiler_ast_program_Program *program, char *p

std_value_Value *err_value = compiler_lsp_utils_gen_error_json(err);
printf("%s\n", std_value_Value_dbg(err_value));
std_value_Value_free(err_value);
}
}
}
Expand Down Expand Up @@ -10112,7 +10097,7 @@ void compiler_lsp_lsp_usage(i32 code, bool full) {
printf(" -c <line> <col> Completions for the given location""\n");
printf(" -v Verbose output""\n");
printf(" --doc-symbols List all symbols in the file""\n");
printf(" --validate Does nothing yet""\n");
printf(" --validate List all errors in the file""\n");
exit(code);
}

Expand Down Expand Up @@ -12361,12 +12346,6 @@ bool std_compact_map_Iterator__0_has_value(std_compact_map_Iterator__0 *this) {
return std_vector_Iterator__19_has_value(&this->iter);
}

void std_compact_map_Map__0_free(std_compact_map_Map__0 *this) {
free(this->indices);
std_vector_Vector__19_free(this->items);
free(this);
}

void std_compact_map_Map__0_insert(std_compact_map_Map__0 *this, char *key, std_value_Value *value) {
u32 hash = str_hash(key);
u32 index = std_compact_map_Map__0_get_index(this, key, hash);
Expand Down Expand Up @@ -12695,36 +12674,6 @@ char *std_value_Value_dbg(std_value_Value *this) {
return std_buffer_Buffer_str(buf);
}

void std_value_Value_free(std_value_Value *this) {
switch (this->type) {
case std_value_ValueType_String: {
std_buffer_Buffer_free(&this->u.as_str);
} break;
case std_value_ValueType_List: {
for (std_vector_Iterator__18 __iter = std_vector_Vector__18_iter(this->u.as_list); std_vector_Iterator__18_has_value(&__iter); std_vector_Iterator__18_next(&__iter)) {
std_value_Value *val = std_vector_Iterator__18_cur(&__iter);
{
std_value_Value_free(val);
}
}
std_vector_Vector__18_free(this->u.as_list);
} break;
case std_value_ValueType_Dictionary: {
for (std_compact_map_Iterator__0 __iter = std_compact_map_Map__0_iter(this->u.as_dict); std_compact_map_Iterator__0_has_value(&__iter); std_compact_map_Iterator__0_next(&__iter)) {
std_compact_map_Item__0 iter = std_compact_map_Iterator__0_cur(&__iter);
{
free(iter.key);
std_value_Value_free(iter.value);
}
}
std_compact_map_Map__0_free(this->u.as_dict);
} break;
default: {
} break;
}
free(this);
}

char *std_fs_realpath(char *path) {
return realpath(path, NULL);
}
Expand Down Expand Up @@ -14772,38 +14721,11 @@ void std_vector_Vector__17_push(std_vector_Vector__17 *this, compiler_ast_nodes_
this->size+=1;
}

std_vector_Iterator__18 std_vector_Vector__18_iter(std_vector_Vector__18 *this) {
return std_vector_Iterator__18_make(this);
}

std_value_Value *std_vector_Iterator__18_cur(std_vector_Iterator__18 *this) {
ae_assert((this->index < this->vec->size), "std/vector.oc:128:12: Assertion failed: `.index < .vec.size`", "Out of bounds in Iterator::current");
return this->vec->data[this->index];
}

void std_vector_Iterator__18_next(std_vector_Iterator__18 *this) {
ae_assert((this->index < this->vec->size), "std/vector.oc:123:12: Assertion failed: `.index < .vec.size`", "Out of bounds in Iterator::next");
this->index+=1;
}

std_vector_Iterator__18 std_vector_Iterator__18_make(std_vector_Vector__18 *vec) {
return (std_vector_Iterator__18){.vec=vec, .index=0};
}

bool std_vector_Iterator__18_has_value(std_vector_Iterator__18 *this) {
return (this->index < this->vec->size);
}

std_value_Value *std_vector_Vector__18_at(std_vector_Vector__18 *this, u32 i) {
ae_assert((i < this->size), "std/vector.oc:90:12: Assertion failed: `i < .size`", "Out of bounds in Vector::at");
return this->data[i];
}

void std_vector_Vector__18_free(std_vector_Vector__18 *this) {
free(this->data);
free(this);
}

void std_vector_Vector__18_resize(std_vector_Vector__18 *this, u32 new_capacity) {
this->capacity=new_capacity;
this->data=((std_value_Value **)realloc(this->data, (this->capacity * ((u32)sizeof(std_value_Value *)))));
Expand Down Expand Up @@ -14852,11 +14774,6 @@ std_compact_map_Item__0 std_vector_Vector__19_at(std_vector_Vector__19 *this, u3
return this->data[i];
}

void std_vector_Vector__19_free(std_vector_Vector__19 *this) {
free(this->data);
free(this);
}

void std_vector_Vector__19_resize(std_vector_Vector__19 *this, u32 new_capacity) {
this->capacity=new_capacity;
this->data=((std_compact_map_Item__0 *)realloc(this->data, (this->capacity * ((u32)sizeof(std_compact_map_Item__0)))));
Expand Down
3 changes: 1 addition & 2 deletions compiler/lsp/mod.oc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def handle_validate(program: &Program, path: str) {
if not err.span1.start.filename? continue
let err_value = utils::gen_error_json(err)
println(`{err_value.dbg()}`)
err_value.free()
}
}

Expand Down Expand Up @@ -145,7 +144,7 @@ def lsp_usage(code: i32, full: bool) {
println(" -c <line> <col> Completions for the given location")
println(" -v Verbose output")
println(" --doc-symbols List all symbols in the file")
println(" --validate Does nothing yet")
println(" --validate List all errors in the file")
exit(code)
}

Expand Down
144 changes: 144 additions & 0 deletions std/bencode.oc
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// byte-encoding of objects used in BitTorrent protocol

import std::value::{ Value }
import std::buffer::{ Buffer }
import std::sv::{ SV }
import std::span::{ Location, Span }
import std::vector::{ Vector }
import std::compact_map::{ Map }

struct Parser {
input: SV
loc: Location
}

def Parser::new(input: Buffer): Parser {
return Parser(
input: input.sv(),
loc: Location(
filename: "<bencoded string>",
line: 1,
col: 1,
index: 0,
)
)
}

def Parser::cur(&this): char => .input.data[.loc.index] as char

def Parser::parse(&this): &Value {
let start = .loc
let val = match .cur() {
'i' => .parse_int()
'l' => .parse_list()
'd' => .parse_dict()
else => .parse_string()
}
val.span = Span(start, .loc)
return val
}

def Parser::inc(&this) => .loc.index += 1

def Parser::parse_int_internal(&this): i64 {
let scale = 1i64
if .cur() == '-' {
scale = -1
.inc()
}
let num = 0i64
while .cur().is_digit() {
num *= 10
num += .cur() as i64 - '0' as i64
.inc()
}
return num * scale
}

def Parser::parse_list(&this): &Value {
let val = Value::new(List)
val.u.as_list = Vector<&Value>::new()
.inc() // skip 'l'
while .cur() != 'e' {
val.u.as_list.push(.parse())
}
.inc() // skip 'e'
return val
}

def Parser::parse_dict(&this): &Value {
let val = Value::new(Dictionary)
val.u.as_dict = Map<str, &Value>::new()
.inc() // skip 'l'
while .cur() != 'e' {
let key = .parse_string_internal()
let value = .parse()
val.u.as_dict.insert(key.str(), value)
}
.inc() // skip 'e'
return val
}

def Parser::parse_string_internal(&this): Buffer {
let len = .parse_int_internal() as u32
.inc() // skip ':'
let s = .input.slice(.loc.index, .loc.index + len)

for let i = 0; i < len; i++ {
.inc()
}
return Buffer::from_sv(s)
}

def Parser::parse_string(&this): &Value {
let val = Value::new(String)
val.u.as_str = .parse_string_internal()
return val
}

def Parser::parse_int(&this): &Value {
let val = Value::new(Integer)
.inc() // skip 'i'
let num = .parse_int_internal()
.inc() // skip 'e'
val.u.as_int = num
return val
}

def parse(input: Buffer): &Value {
let parser = Parser::new(input)
return parser.parse()
}

def serialize_into(val: &Value, sb: &Buffer) {
match val.type {
String => {
let s = val.u.as_str
sb.putsf(`{s.size}:`)
sb.putb(&s)
}
Integer => sb.putsf(`i{val.u.as_int}e`)
List => {
sb.puts("l")
for val : val.u.as_list.iter() {
serialize_into(val, sb)
}
sb.puts("e")
}
Dictionary => {
sb.puts("d")
for it : val.u.as_dict.iter() {
sb.putsf(`{it.key.len()}:{it.key}`)
serialize_into(it.value, sb)
}
sb.puts("e")
}
else => panic(`Unsupported Value type {val.type} in Bencode::serialize_into()`)
}
}

def serialize(val: &Value): Buffer {
let sb = Buffer::make()
serialize_into(val, &sb)
return sb
}

0 comments on commit 04f84cc

Please sign in to comment.