Skip to content

Commit

Permalink
feat: Type is now after identifiers
Browse files Browse the repository at this point in the history
Type variable -> var variable: Type

closes #310
  • Loading branch information
giann committed Aug 30, 2024
1 parent 4884fdb commit c172188
Show file tree
Hide file tree
Showing 14 changed files with 563 additions and 687 deletions.
736 changes: 308 additions & 428 deletions src/Parser.zig

Large diffs are not rendered by default.

98 changes: 49 additions & 49 deletions src/lib/buffer.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,74 @@ export object OutOfBoundError {}
|| @private
extern fun BufferNew(int capacity) > ud;
|| @private
extern fun BufferDeinit(ud userdata) > void;
extern fun BufferDeinit(userdata: ud) > void;
|| @private
extern fun BufferRead(ud userdata, int n) > str?;
extern fun BufferRead(userdata: ud, int n) > str?;
|| @private
extern fun BufferWrite(ud userdata, str bytes) > void !> WriteWhileReadingError;
extern fun BufferWrite(userdata: ud, bytes: str) > void !> WriteWhileReadingError;
|| @private
extern fun BufferReadBoolean(ud userdata) > bool?;
extern fun BufferReadBoolean(userdata: ud) > bool?;
|| @private
extern fun BufferWriteBoolean(ud userdata, bool b) > void !> WriteWhileReadingError;
extern fun BufferWriteBoolean(userdata: ud, b: bool) > void !> WriteWhileReadingError;
|| @private
extern fun BufferWriteInt(ud userdata, int n) > void !> WriteWhileReadingError;
extern fun BufferWriteInt(userdata: ud, n: int) > void !> WriteWhileReadingError;
|| @private
extern fun BufferReadInt(ud userdata) > int?;
extern fun BufferReadInt(userdata: ud) > int?;
|| @private
extern fun BufferWriteUserData(ud userdata, ud ptr) > void !> WriteWhileReadingError;
extern fun BufferWriteUserData(userdata: ud, ptr: ud) > void !> WriteWhileReadingError;
|| @private
extern fun BufferReadUserData(ud userdata) > ud?;
extern fun BufferReadUserData(userdata: ud) > ud?;
|| @private
extern fun BufferWriteFloat(ud userdata, float n) > void !> WriteWhileReadingError;
extern fun BufferWriteFloat(userdata: ud, n: float) > void !> WriteWhileReadingError;
|| @private
extern fun BufferReadFloat(ud userdata) > float?;
extern fun BufferReadFloat(userdata: ud) > float?;
|| @private
extern fun BufferLen(ud userdata, int align) > int;
extern fun BufferLen(userdata: ud, align: int) > int;
|| @private
extern fun BufferCursor(ud userdata) > int;
extern fun BufferCursor(userdata: ud) > int;
|| @private
extern fun BufferBuffer(ud userdata) > str;
extern fun BufferBuffer(userdata: ud) > str;
|| @private
extern fun BufferPtr(ud userdata, int at, int alignment) > ud;
extern fun BufferPtr(userdata: ud, at: int, alignment: int) > ud;
|| @private
extern fun BufferEmpty(ud userdata) > void;
extern fun BufferEmpty(userdata: ud) > void;
|| @private
extern fun BufferAt(ud userdata, int index) > int;
extern fun BufferAt(userdata: ud, index: int) > int;
|| @private
extern fun BufferSetAt(ud userdata, int index, int value) > void;
extern fun BufferSetAt(userdata: ud, index: int, value:int ) > void;
|| @private
extern fun BufferWriteZ(ud userdata, str zigType, [any] values) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError;
extern fun BufferWriteZ(userdata: ud, zigType: str, values: [any]) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError;
|| @private
extern fun BufferWriteZAt(ud userdata, int at, str zigType, [any] values) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError;
extern fun BufferWriteZAt(userdata: ud, at: int, zigType: str, values: [any]) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError;
|| @private
extern fun BufferReadZ::<T>(ud userdata, str zigType) > T !> ffi.FFITypeMismatchError;
extern fun BufferReadZ::<T>(userdata: ud, zigType: str) > T !> ffi.FFITypeMismatchError;
|| @private
extern fun BufferReadZAt::<T>(ud userdata, int at, str zigType) > T !> ffi.FFITypeMismatchError;
extern fun BufferReadZAt::<T>(userdata: ud, at: int, zigType: str) > T !> ffi.FFITypeMismatchError;
|| @private
extern fun BufferWriteStruct::<T>(ud userdata, type structType, [T] values) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError;
extern fun BufferWriteStruct::<T>(userdata: ud, structType: type, values: [T]) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError;
|| @private
extern fun BufferWriteStructAt::<T>(ud userdata, type structType, int at, [T] values) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError;
extern fun BufferWriteStructAt::<T>(userdata: ud, structType: type, at: int, values: [T]) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError;
|| @private
extern fun BufferReadStruct::<T>(ud userdata, type structType) > T !> ffi.FFITypeMismatchError;
extern fun BufferReadStruct::<T>(userdata: ud, structType: type) > T !> ffi.FFITypeMismatchError;
|| @private
extern fun BufferReadStructAt::<T>(ud userdata, type structType, int at) > T !> ffi.FFITypeMismatchError;
extern fun BufferReadStructAt::<T>(userdata: ud, structType: type, at: int) > T !> ffi.FFITypeMismatchError;

|| Read and write data to a string buffer
export object Buffer {
|| @private
ud buffer,
buffer: ud,
|| @private
bool released = false,
released: bool = false,

|| @return A new `Buffer`
static fun init(int capacity = 0) > Buffer {
static fun init(capacity: int = 0) > Buffer {
return Buffer{
buffer = BufferNew(capacity)
};
}

static fun fromStr(str string) > Buffer {
Buffer buffer = Buffer.init();
static fun fromStr(string: str) > Buffer {
const buffer = Buffer.init();

| We're sure we did not read this buffer before
buffer.write(string) catch void;
Expand All @@ -94,45 +94,45 @@ export object Buffer {

|| Reads `n` bytes
|| @return Read bytes or `null` if nothing to read
fun read(int n = 1) > str? {
fun read(n: int = 1) > str? {
return BufferRead(this.buffer, n: n);
}

|| Writes a string
|| @param bytes Bytes to write
fun write(str bytes) > void !> WriteWhileReadingError {
fun write(bytes: str) > void !> WriteWhileReadingError {
BufferWrite(this.buffer, bytes: bytes);
}

fun writeZ::<T>(str zigType, [T] values) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError {
fun writeZ::<T>(zigType: str, values: [T]) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError {
BufferWriteZ(this.buffer, zigType: zigType, values: values);
}

fun writeZAt::<T>(int at, str zigType, [T] values) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError {
fun writeZAt::<T>(at: int, zigType: str, values: [T]) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError {
BufferWriteZAt(this.buffer, at: at, zigType: zigType, values: values);
}

fun readZ::<T>(str zigType) > T !> ffi.FFITypeMismatchError {
fun readZ::<T>(zigType: str) > T !> ffi.FFITypeMismatchError {
return BufferReadZ::<T>(this.buffer, zigType: zigType);
}

fun readZAt::<T>(int at, str zigType) > T !> ffi.FFITypeMismatchError {
fun readZAt::<T>(at: int, zigType: str) > T !> ffi.FFITypeMismatchError {
return BufferReadZAt::<T>(this.buffer, at: at, zigType: zigType);
}

fun writeStruct::<T>(type structType, [T] values) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError {
fun writeStruct::<T>(structType: type, values: [T]) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError {
BufferWriteStruct::<T>(this.buffer, structType: structType, values: values);
}

fun writeStructAt::<T>(type structType, int at, [T] values) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError {
fun writeStructAt::<T>(structType: type, at: int, values: [T]) > void !> WriteWhileReadingError, ffi.FFITypeMismatchError {
BufferWriteStructAt::<T>(this.buffer, structType: structType, at: at, values: values);
}

fun readStruct::<T>(type structType) > T !> ffi.FFITypeMismatchError {
fun readStruct::<T>(structType: type) > T !> ffi.FFITypeMismatchError {
return BufferReadStruct::<T>(this.buffer, structType: structType);
}

fun readStructAt::<T>(type structType, int at) > T !> ffi.FFITypeMismatchError {
fun readStructAt::<T>(structType: type, at: int) > T !> ffi.FFITypeMismatchError {
return BufferReadStructAt::<T>(this.buffer, structType: structType, at: at);
}

Expand All @@ -144,7 +144,7 @@ export object Buffer {

|| Writes a boolean
|| @param boolean Boolean to write
fun writeBoolean(bool boolean) > void !> WriteWhileReadingError {
fun writeBoolean(boolean: bool) > void !> WriteWhileReadingError {
BufferWriteBoolean(this.buffer, b: boolean);
}

Expand All @@ -156,7 +156,7 @@ export object Buffer {

|| Writes an integer
|| @param number Integer to write
fun writeInt(int number) > void !> WriteWhileReadingError {
fun writeInt(number: int) > void !> WriteWhileReadingError {
BufferWriteInt(this.buffer, n: number);
}

Expand All @@ -168,7 +168,7 @@ export object Buffer {

|| Writes an ud
|| @param number UserDataeger to write
fun writeUserData(ud userdata) > void !> WriteWhileReadingError {
fun writeUserData(userdata: ud) > void !> WriteWhileReadingError {
BufferWriteUserData(this.buffer, ptr: userdata);
}

Expand All @@ -180,13 +180,13 @@ export object Buffer {

|| Writes a float
|| @param number Float to write
fun writeFloat(float number) > void !> WriteWhileReadingError {
fun writeFloat(number: float) > void !> WriteWhileReadingError {
BufferWriteFloat(this.buffer, n: number);
}


|| @return Length of the buffer
fun len(int align = 1) > int {
fun len(align: int = 1) > int {
return BufferLen(this.buffer, align: align);
}

Expand All @@ -206,12 +206,12 @@ export object Buffer {
}

|| Get buffer's ptr
fun ptr(int at = 0, int align = 1) > ud {
fun ptr(at: int = 0, int align = 1) > ud {
return BufferPtr(this.buffer, at: at, alignment: align);
}

|| Get byte at `index`
fun at(int index) > int !> OutOfBoundError {
fun at(index: int) > int !> OutOfBoundError {
if (index < this.len()) {
return BufferAt(this.buffer, index: index);
}
Expand All @@ -220,7 +220,7 @@ export object Buffer {
}

|| Set byte at `index`
fun setAt(int index, int value) > void !> WriteWhileReadingError, OutOfBoundError {
fun setAt(index: int, value: int) > void !> WriteWhileReadingError, OutOfBoundError {
if (index < this.len()) {
BufferSetAt(this.buffer, index: index, value: value);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/crypto.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ export enum HashAlgorithm {
|| @param algo Hash algorithm to use
|| @param data Data to hash
|| @return Hash of data has hex string
export extern fun hash(HashAlgorithm algo, str data) > str;
export extern fun hash(algo: HashAlgorithm, data: str) > str;
4 changes: 2 additions & 2 deletions src/lib/debug.buzz
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace debug;

|| Dump any value to stdout
export extern fun dump(any value) > void;
export extern fun dump(value: any) > void;

| Parse `source` and return the abstract syntax tree in JSON
| @param source the buzz source
| @param script name (used to fetch eventual extern functions)
| @return AST as JSON
| TODO: reactivate
| export extern fun ast(str source, str scriptName) > str !> CompileError;
| export extern fun ast(source: str, scriptName: str) > str !> CompileError;
14 changes: 7 additions & 7 deletions src/lib/errors.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,23 @@ export enum ReadWriteError {
}

export object CompileError {
str message = "CompileError",
message: str = "CompileError",
}
export object InterpretError {
str message = "InterpretError",
message: str = "InterpretError",
}
export object InvalidArgumentError {
str message = "InvalidArgumentError",
message: str = "InvalidArgumentError",
}
export object NotYetImplementedError {
str message = "NotYetImplementedError",
message: str = "NotYetImplementedError",
}
export object OverflowError {
str message = "OverflowError",
message: str = "OverflowError",
}
export object UnderflowError {
str message = "UnderflowError",
message: str = "UnderflowError",
}
export object UnexpectedError {
str message = "UnexpectedError",
message: str = "UnexpectedError",
}
14 changes: 7 additions & 7 deletions src/lib/ffi.buzz
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
namespace ffi;

export fun cstr(str string) => "{string}\0";
export fun cstr(string: str) => "{string}\0";

export object FFITypeMismatchError {
str message = "Provided buzz value type does not match expected FFI type",
message: str = "Provided buzz value type does not match expected FFI type",
}

export object FFIZigTypeParseError {
str message = "Could not parse zig type",
message: str = "Could not parse zig type",
}

export extern fun alignOf(str zigType) > int;
export extern fun alignOf(zigType: str) > int;

export extern fun sizeOf(str zigType) > int;
export extern fun sizeOf(zigType: str) > int;

export extern fun sizeOfStruct(type structType) > int;
export extern fun sizeOfStruct(structType: type) > int;

export extern fun alignOfStruct(type structType) > int;
export extern fun alignOfStruct(structType: type) > int;
10 changes: 5 additions & 5 deletions src/lib/fs.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ export fun currentDirectory() > str !> errors.FileSystemError, errors.InvalidArg

|| Creates directory path
|| @param path directory to create
export extern fun makeDirectory(str path) > void !> errors.FileSystemError, errors.UnexpectedError;
export extern fun makeDirectory(path: str) > void !> errors.FileSystemError, errors.UnexpectedError;

|| Deletes directory or file at path
|| @param path direcotry/file to delete
export extern fun delete(str path) > void !> errors.FileSystemError, errors.UnexpectedError;
export extern fun delete(path: str) > void !> errors.FileSystemError, errors.UnexpectedError;

|| Moves/renames file
|| @param source file to move
|| @param destination where to move it
export extern fun move(str source, str destination) > void !> errors.FileSystemError, errors.UnexpectedError;
export extern fun move(source: str, destination: str) > void !> errors.FileSystemError, errors.UnexpectedError;

|| List files under path
|| @param path directory to list
export extern fun list(str path) > [str] !> errors.FileSystemError, errors.UnexpectedError;
export extern fun list(path: str) > [str] !> errors.FileSystemError, errors.UnexpectedError;

|| Returns true if path exists
|| @param path directory/file to test
|| @return wether file exists
export extern fun exists(str path) > bool !> errors.FileSystemError;
export extern fun exists(path: str) > bool !> errors.FileSystemError;
Loading

0 comments on commit c172188

Please sign in to comment.