Skip to content

Commit

Permalink
feat: Namespace
Browse files Browse the repository at this point in the history
- A script exporting at least one symbol, must specify its namespace at the start of the script
- When importing, the namespace can be changed with `as`
- A `_` import namespace will erase the imported symbols namespace

closes #271
  • Loading branch information
giann committed Mar 11, 2024
1 parent cccdf0f commit 9f4356d
Show file tree
Hide file tree
Showing 114 changed files with 772 additions and 720 deletions.
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ pub fn build(b: *Build) !void {
.{ .name = "http", .path = "src/lib/buzz_http.zig", .wasm_compatible = false },
.{ .name = "ffi", .path = "src/lib/buzz_ffi.zig", .wasm_compatible = false },
.{ .name = "serialize", .path = "src/lib/buzz_serialize.zig" },
.{ .name = "test", .path = null },
.{ .name = "testing", .path = null },
.{ .name = "errors", .path = null },
};

Expand Down
10 changes: 5 additions & 5 deletions examples/game-of-life.buzz
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import "std";
import "io" as io;
import "io";
import "os";
import "debug";
import "examples/sdl-wrapped";
import "examples/sdl-wrapped" as _;

| buzz -L/path/to/SDL2.dylib/so/dll examples/game-of-life.buzz
object World {
Expand All @@ -18,7 +18,7 @@ object World {
};

for (int i = 0; i < width * height; i = i + 1) {
world.cells.append(random(max: 5) == 1);
world.cells.append(std.random(max: 5) == 1);
}

return world;
Expand Down Expand Up @@ -156,8 +156,8 @@ fun main([str] args) > void !> SDLError {
height: 600,
);

int? width = if (args.len() > 0) parseInt(args[0]) else null;
int? height = if (args.len() > 1) parseInt(args[1]) else null;
int? width = if (args.len() > 0) std.parseInt(args[0]) else null;
int? height = if (args.len() > 1) std.parseInt(args[1]) else null;

var world = World.init(
width: width ?? 10,
Expand Down
12 changes: 7 additions & 5 deletions examples/sdl-wrapped.buzz
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
namespace sdl;

import "std";
import "ffi";

Expand Down Expand Up @@ -212,7 +214,7 @@ export object SDL {
flags = flags \ subsystem.value;
}

if (SDL_Init(toFloat(flags)) != 0) {
if (SDL_Init(std.toFloat(flags)) != 0) {
throw SDLError{
message = "SDL_Init error: {SDL_GetError()}",
};
Expand Down Expand Up @@ -293,7 +295,7 @@ export object Renderer {
) > Texture !> SDLError {
const SDL_Texture? texture = SDL_CreateTexture(
this.renderer,
format: toFloat(format.value),
format: std.toFloat(format.value),
access: access.value,
w: width,
h: height,
Expand Down Expand Up @@ -415,12 +417,12 @@ export object Window {
}

const SDL_Window? window = SDL_CreateWindow(
cstr(name),
ffi.cstr(name),
x,
y,
w: width,
h: height,
flags: toFloat(windowFlags),
flags: std.toFloat(windowFlags),
);

if (window -> unwrapped) {
Expand All @@ -446,7 +448,7 @@ export object Window {
const SDL_Renderer? renderer = SDL_CreateRenderer(
this.window,
index: index,
flags: toFloat(rendererFlags),
flags: std.toFloat(rendererFlags),
);

if (renderer -> unwrapped) {
Expand Down
2 changes: 1 addition & 1 deletion examples/sdl.buzz
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "std";
import "examples/sdl-wrapped";
import "examples/sdl-wrapped" as _;

| buzz -L/path/to/SDL2.dylib/so/dll examples/sdl.buzz
fun main([str] args) > int !> SDLError {
Expand Down
41 changes: 18 additions & 23 deletions examples/sqlite.buzz
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace sqlite;

import "ffi";
import "std";
import "buffer";
import "serialize";
import "log";
import "buffer" as _;
import "serialize" as _;

zdef("sqlite3", `
fn sqlite3_initialize() c_int;
Expand Down Expand Up @@ -152,7 +153,7 @@ export object Statement {
ud stmt,

fun execute() > [[Boxed]] *> [Boxed]? !> SQLiteError {
Logger.instance?.debug("Executing query `{this.query}`", namespace: "SQL");
std.print("Executing query `{this.query}`");
ResultCode code = ResultCode.Ok;

[[Boxed]] rows = [<[Boxed]>];
Expand Down Expand Up @@ -198,28 +199,22 @@ export object Statement {
const [Boxed] boxedRow = (Boxed.init(row) catch void).listValue();
rows.append(boxedRow);

yield boxedRow;
_ = yield boxedRow;
}

return rows;
}

fun collect() > void {
sqlite3_finalize(this.stmt);
_ = sqlite3_finalize(this.stmt);
}
}

export object Database {
ud db,

fun collect() > void {
const ResultCode code = ResultCode(sqlite3_close_v2(this.db)) ?? ResultCode.Error;
if (code != ResultCode.Ok) {
throw SQLiteError{
code = code,
message = "Could not close database: {code} {sqlite3_errmsg(this.db)}"
};
}
_ = ResultCode(sqlite3_close_v2(this.db)) ?? ResultCode.Error;
}

fun prepare(Query query) > Statement !> SQLiteError {
Expand All @@ -228,12 +223,12 @@ export object Database {

fun prepareRaw(str query) > Statement !> SQLiteError {
Buffer buffer = Buffer.init();
buffer.writeUserData(toUd(0)) catch void;
buffer.writeUserData(std.toUd(0)) catch void;

const ResultCode code = ResultCode(
sqlite3_prepare_v2(
db: this.db,
zSql: cstr(query),
zSql: ffi.cstr(query),
nBytes: -1,
ppStmt: buffer.ptr(),
pzTail: null,
Expand Down Expand Up @@ -268,11 +263,11 @@ export object SQLite {
}

static fun escape(str string) > str {
return sqlite3_mprintf(cstr(string)) ?? "";
return sqlite3_mprintf(ffi.cstr(string)) ?? "";
}

fun collect() > void {
sqlite3_shutdown();
_ = sqlite3_shutdown();
}

fun open(str filename, [OpenFlag] flags) > Database !> SQLiteError {
Expand All @@ -282,11 +277,11 @@ export object SQLite {
}

Buffer buffer = Buffer.init();
buffer.writeUserData(toUd(0)) catch void;
buffer.writeUserData(std.toUd(0)) catch void;

const ResultCode code = ResultCode(
sqlite3_open_v2(
filename: cstr(filename),
filename: ffi.cstr(filename),
ppDb: buffer.ptr(),
flags: cFlags,
zVfs: null
Expand All @@ -300,7 +295,7 @@ export object SQLite {
};
}

Logger.instance?.info("Database `{filename}` opened", namespace: "SQL");
std.print("Database `{filename}` opened");

return Database {
db = buffer.readUserData()!,
Expand All @@ -319,10 +314,10 @@ test "Testing sqlite" {
)
`);

statement.execute();
_ = statement.execute();

foreach (str name in ["john", "joe", "janet", "joline"]) {
Query{}
_ = Query{}
.insertInto("test", columns: [ "name" ])
.values([ "'{name}'" ])
.execute(database);
Expand All @@ -334,6 +329,6 @@ test "Testing sqlite" {
.prepare(database);

foreach ([Boxed] row in &select.execute()) {
print("{row[0].integerValue()}: {row[1].stringValue()}");
std.print("{row[0].integerValue()}: {row[1].stringValue()}");
}
}
3 changes: 3 additions & 0 deletions src/Ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub const Node = struct {
ListType,
Map,
MapType,
Namespace,
NamedVariable,
Null,
ObjectDeclaration,
Expand Down Expand Up @@ -174,6 +175,7 @@ pub const Node = struct {
ListType: ListType,
Map: Map,
MapType: MapType,
Namespace: TokenIndex,
NamedVariable: NamedVariable,
Null: void,
ObjectDeclaration: ObjectDeclaration,
Expand Down Expand Up @@ -562,6 +564,7 @@ pub fn isConstant(self: Self, node: Node.Index) bool {
.StringLiteral,
.TypeExpression,
.Void,
.Namespace,
=> true,
.AsyncCall,
.Block,
Expand Down
3 changes: 2 additions & 1 deletion src/Codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jit: ?*JIT,
reporter: Reporter,

const generators = [_]NodeGen{
&noGen, // AnonymousObjectType,
noGen, // AnonymousObjectType,
generateAs, // As,
generateAsyncCall, // AsyncCall,
generateBinary, // Binary,
Expand Down Expand Up @@ -93,6 +93,7 @@ const generators = [_]NodeGen{
noGen, // ListType,
generateMap, // Map,
noGen, // MapType,
noGen, // Namespace,
generateNamedVariable, // NamedVariable,
generateNull, // Null,
generateObjectDeclaration, // ObjectDeclaration,
Expand Down
Loading

0 comments on commit 9f4356d

Please sign in to comment.